\documentclass{article}
\usepackage{tikz}
\usepackage{listings}
\usetikzlibrary{calc}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\begin{document}

text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text 

\begin{lstlisting}[escapechar= !]
!\tikzmark{x1}!int gilligan(int j){
codecodecode!\tikzmark{x4}!
int duck(int k){
        !\tikzmark{x2}!cout<< i; !\tikzmark{x5}!
}
!\tikzmark{x3}!}
!\tikzmark{x6}!}
\end{lstlisting}

\newlength{\somelength}
\settowidth{\somelength}{The Label}

\tikz[overlay,remember picture]{%
% \draw[fill=gray]([xshift=2pt]x6.south)--([xshift=8pt]x2.south)--(x5|-x2.south)--(x4.south)--(x4|-x6.south)--cycle; 
% \node[fill=purple!20,opacity=0.35] (all) at ([xshift=\textwidth]x1.base west) {This is like super long };
% \node[rectangle] (all) at ([xshift=\textwidth]x1.base west) {This is like super long };
\node [label={[xshift=-0.5*\somelength, yshift=0cm]The Label}] (all) at ([xshift=\textwidth]x1.base west) {};

% \draw[fill=\c] ([xshift=2pt]x6) rectangle { width=\x1, height=\y1 };
}
\end{document}