[Tex/LaTex] How to write the beautiful code as follows

highlighting

I want to know how to generate the following beautiful code, including fonts, colors, line, and the figure description. I think it is not written by \lstset. This picture is extracted from a paper titled Towards Optimization-Safe Systems: Analyzing the Impact of Undefined Behavior.

enter image description here

Best Answer

The code seems to be highlighted using the package minted.

Typeset either with pdflatex --shell-escape or with arara.

% arara: pdflatex: { shell: yes }
\documentclass{article}
\pagestyle{empty}% for cropping
\usepackage{minted,newtxtext,newtxmath,caption}
\newcommand\code{\texttt}
\newcommand\param{\textit}
\DeclareCaptionFont{bfmath}{\boldmath\bfseries}
\DeclareCaptionFormat{ruled}{\hrulefill\par#1#2#3}
\captionsetup{format=ruled,font=bfmath}
\begin{document}
\begin{figure}
  \centering
\begin{minted}{c}
const uint8_t *data     = /* buffer head */;
const uint8_t *data_end = /* buffer tail */;
int size = bytestream_get_be16(&data);
if (data + size >= data_end || data + size < data)
    return -1;
data += size;
...
int len = ff_amf_tag_size(data, data_end);
if (len < 0 || data + len >= data_end
            || data + len < data)
    return -1;
data += len;
/* continue to read data */
\end{minted}
  \caption{Unstable bounds checks in the form $\code{data} + x < \code{data}$ from FFmpeg/Libav, which gcc optimizes into $x < 0$.}
\end{figure}
\begin{figure}
  \centering
\begin{minted}{c}
void pdec(io *f, int k) {
    if (k < 0) {          /* print negative k */
        if (-k >= 0) {    /* not INT_MIN? */
            pchr(f, '-'); /* print minus */
            pdec(f, -k);  /* print -k */
            return;
        }
            ...           /* print INT_MIN */
            return;
        }
    ...                   /* print positive k */
}
\end{minted}
  \caption{An unstable integer check in plan9port. The function \code{pdec} prints a signed integer \code{k}; gcc optimizes the check \code{-k >= 0} into \param{true} when it learns that \code{k} is negative, leading to an infinite loop if the input \code{k} is \code{INT\_MIN}.}
\end{figure}
\end{document}

enter image description here