[proofplan]
We count twin-prime candidates by a two-dimensional upper-bound sieve. A twin prime pair $p,p+2$ with $p$ not too small survives every congruence obstruction $n \equiv 0,-2 \pmod q$ for primes $q$ below a sieve level $z$. Selberg's upper-bound sieve gives an upper bound for the number of survivors in terms of a denominator whose size is at least a constant multiple of $(\log z)^2/(\log\log z)^2$. Taking $z = x^{1/4}$ and absorbing the finitely many small values of $x$ gives the asserted Brun upper bound.
[/proofplan]
[step:Reduce twin primes to integers avoiding two residue classes modulo each small prime]
Let $x \geq 16$ and define the sieve level
\begin{align*}
z := x^{1/4}.
\end{align*}
Define the prime product function $P:[2,\infty)\to\mathbb{N}$ by letting $P(y)$ be the product of all primes $q \leq y$. Define the sifted-counting function $S:[3,\infty)\times[2,\infty)\to\mathbb{N}\cup\{0\}$ as follows: $S(x,z)$ is the number of integers $n \in \{1,\dots,\lfloor x \rfloor\}$ such that $n$ is odd and, for every odd prime $q \leq z$,
\begin{align*}
n \not\equiv 0 \pmod q
\end{align*}
and
\begin{align*}
n \not\equiv -2 \pmod q.
\end{align*}
If $p \leq x$ and both $p$ and $p+2$ are prime, then either $p \leq z$ or $p$ is counted by $S(x,z)$. Indeed, when $p > z$, the prime $p$ is odd. For every odd prime $q \leq z$, the congruence $p \equiv 0 \pmod q$ would force $q=p$, impossible because $q \leq z < p$, and the congruence $p \equiv -2 \pmod q$ would force $q \mid p+2$, hence $q=p+2$, also impossible because $q \leq z < p+2$. Therefore
\begin{align*}
\pi_2(x) \leq z + S(x,z).
\end{align*}
[guided]
The sieve does not try to prove that the surviving integers are twin primes. It only uses the necessary congruence conditions that a twin-prime candidate must satisfy.
Fix $x \geq 16$ and set
\begin{align*}
z := x^{1/4}.
\end{align*}
Let $P:[2,\infty)\to\mathbb{N}$ be the prime product function for which $P(y)$ is the product of all primes at most $y$. We define $S:[3,\infty)\times[2,\infty)\to\mathbb{N}\cup\{0\}$ so that $S(x,z)$ is the number of integers $n \in \{1,\dots,\lfloor x\rfloor\}$ satisfying the following local conditions: first $n$ is odd, and second, for every odd prime $q \leq z$, neither $n$ nor $n+2$ is divisible by $q$. In congruence notation this means
\begin{align*}
n \not\equiv 0 \pmod q
\end{align*}
and
\begin{align*}
n \not\equiv -2 \pmod q.
\end{align*}
Now suppose $p \leq x$ and $p,p+2$ are both prime. If $p \leq z$, we count it by the error term $z$. If $p > z$, then $p$ is odd. Let $q \leq z$ be an odd prime. If $p \equiv 0 \pmod q$, then $q$ divides the prime $p$, so $q=p$, contradicting $q \leq z < p$. If $p \equiv -2 \pmod q$, then $q$ divides the prime $p+2$, so $q=p+2$, contradicting $q \leq z < p+2$. Thus every twin-prime lower member $p>z$ is counted by $S(x,z)$, and hence
\begin{align*}
\pi_2(x) \leq z + S(x,z).
\end{align*}
[/guided]
[/step]
[step:Apply Selberg's upper-bound sieve to the twin-prime residue system]
Define the multiplicative function $\omega:\mathbb{N}\to\mathbb{N}\cup\{0\}$ on prime powers by $\omega(q^k)=\omega(q)$ for every prime $q$ and every $k\geq 1$, with
\begin{align*}
\omega(2) := 1
\end{align*}
and, for each odd prime $q$,
\begin{align*}
\omega(q) := 2.
\end{align*}
For every squarefree divisor $d$ of $P(z)$, the value $\omega(d)$ is the number of residue classes modulo $d$ on which $d \mid n(n+2)$.
We use the following Selberg upper-bound sieve estimate.
[claim:Selberg sieve estimate for the twin-prime residue system]
There are absolute constants $C_0,C_1 > 0$ such that, for all $x \geq 16$ and all $2 \leq z \leq x^{1/2}$,
\begin{align*}
S(x,z) \leq C_0 x \frac{(\log\log z)^2}{(\log z)^2} + C_1 z^2(\log z)^6.
\end{align*}
[/claim]
[proof]
For squarefree $d \mid P(z)$, define $A_d:[16,\infty)\to\mathbb{N}\cup\{0\}$ by letting $A_d(x)$ be the number of integers $n \in \{1,\dots,\lfloor x \rfloor\}$ such that $d \mid n(n+2)$. By the [Chinese remainder theorem](/theorems/734), the congruence $d \mid n(n+2)$ is the union of exactly $\omega(d)$ residue classes modulo $d$. Hence there is a remainder function $R_d:[16,\infty)\to\mathbb{R}$ such that
\begin{align*}
A_d(x) = \frac{\omega(d)}{d}x + R_d(x), \qquad |R_d(x)| \leq \omega(d).
\end{align*}
We invoke Selberg's upper-bound sieve in dimension two for the residue system $n(n+2)$. In the form used here, it states that if $2\leq z\leq x^{1/2}$, $\omega(2)=1$, $\omega(q)=2$ for odd primes $q$, and the local counting functions satisfy the remainder estimate above, then there are Selberg weights $\lambda_d\in\mathbb{R}$ supported on squarefree $d\mid P(z)$ with $d\leq z$ and $\lambda_1=1$ such that
\begin{align*}
S(x,z) \leq \frac{x}{G(z)} + C_2 z^2(\log z)^6,
\end{align*}
where $C_2>0$ is an absolute constant and $G:[2,\infty)\to(0,\infty)$ is the Selberg denominator
\begin{align*}
G(z) := \sum_{\substack{r \mid P(z), r \leq z}} \mu(r)^2 \prod_{q \mid r}\frac{\omega(q)}{q-\omega(q)}.
\end{align*}
Here $\mu:\mathbb{N}\to\{-1,0,1\}$ is the Mobius function, so $\mu(r)^2$ is the indicator that $r$ is squarefree. The same sieve theorem includes the dimension-two denominator estimate
\begin{align*}
G(z) \geq c_2 \frac{(\log z)^2}{(\log\log z)^2}
\end{align*}
for every $z\geq3$, with an absolute constant $c_2>0$. This denominator estimate is the standard truncated Euler-product estimate for the weights $2/(q-2)$; its hypotheses are exactly the displayed values of $\omega(q)$ and the restriction to squarefree divisors of $P(z)$.
Combining the two displayed estimates gives
\begin{align*}
S(x,z) \leq c_2^{-1}x\frac{(\log\log z)^2}{(\log z)^2} + C_2 z^2(\log z)^6.
\end{align*}
Renaming the absolute constants proves the asserted estimate.
[/proof]
[guided]
The preceding claim is where the sieve theory enters. The local data have already been verified: for each squarefree $d\mid P(z)$, the forbidden congruences modulo the prime factors of $d$ combine by the Chinese [remainder theorem](/theorems/1707) into $\omega(d)$ residue classes modulo $d$, and the counting error in each residue class is bounded by one. Thus the counting function $A_d:[16,\infty)\to\mathbb{N}\cup\{0\}$ satisfies
\begin{align*}
A_d(x) = \frac{\omega(d)}{d}x + R_d(x), \qquad |R_d(x)| \leq \omega(d),
\end{align*}
for a remainder function $R_d:[16,\infty)\to\mathbb{R}$.
Selberg's upper-bound sieve is designed exactly for this situation: a finite set of integers is being sifted by excluding $\omega(q)$ residue classes modulo each prime $q\leq z$. The theorem produces weights $\lambda_d$ supported on squarefree divisors $d\mid P(z)$ with $d\leq z$ and converts the survivor count into a main term controlled by the reciprocal of the Selberg denominator plus a remainder controlled by the local errors. In the present dimension-two system it yields
\begin{align*}
S(x,z) \leq \frac{x}{G(z)} + C_2 z^2(\log z)^6,
\end{align*}
where $C_2>0$ is absolute and
\begin{align*}
G(z) := \sum_{\substack{r \mid P(z), r \leq z}} \mu(r)^2 \prod_{q \mid r}\frac{\omega(q)}{q-\omega(q)}.
\end{align*}
The function $\mu:\mathbb{N}\to\{-1,0,1\}$ is the Mobius function, so $\mu(r)^2$ restricts the sum to squarefree $r$. The condition $2\leq z\leq x^{1/2}$ is part of the quantitative sieve range and is verified after the claim is applied.
The size of $G(z)$ is the reason this is a two-dimensional sieve. Since $\omega(q)=2$ for odd primes, the Euler factors in $G(z)$ are comparable to $1+2/q$, so their cumulative growth is quadratic in $\log z$ up to the usual truncation loss. The standard denominator estimate supplied with the Selberg sieve gives
\begin{align*}
G(z) \geq c_2 \frac{(\log z)^2}{(\log\log z)^2}
\end{align*}
for every $z\geq3$, where $c_2>0$ is absolute. Substituting this lower bound into the Selberg inequality gives
\begin{align*}
S(x,z) \leq c_2^{-1}x\frac{(\log\log z)^2}{(\log z)^2} + C_2 z^2(\log z)^6.
\end{align*}
Renaming constants gives the claimed bound.
[/guided]
Since $z=x^{1/4}$ and $x \geq 16$, the hypotheses $2 \leq z \leq x^{1/2}$ hold. Therefore
\begin{align*}
S(x,z) \leq C_0 x \frac{(\log\log z)^2}{(\log z)^2} + C_1 z^2(\log z)^6.
\end{align*}
[/step]
[step:Convert the sieve estimate to the stated logarithmic bound]
Because $z=x^{1/4}$, we have
\begin{align*}
\log z = \frac{1}{4}\log x.
\end{align*}
Also, for $x$ sufficiently large,
\begin{align*}
\log\log z \leq \log\log x.
\end{align*}
Thus the main sieve term satisfies
\begin{align*}
C_0 x \frac{(\log\log z)^2}{(\log z)^2} \leq 16C_0 x \frac{(\log\log x)^2}{(\log x)^2}.
\end{align*}
The error term is
\begin{align*}
C_1 z^2(\log z)^6 = C_1 x^{1/2}\left(\frac{1}{4}\log x\right)^6.
\end{align*}
Since
\begin{align*}
x^{-1/2}(\log x)^8(\log\log x)^{-2} \to 0
\end{align*}
as $x \to \infty$, there is an absolute constant $C_2>0$ such that, for all sufficiently large $x$,
\begin{align*}
C_1 x^{1/2}\left(\frac{1}{4}\log x\right)^6 \leq C_2 x \frac{(\log\log x)^2}{(\log x)^2}.
\end{align*}
Combining this estimate with the previous step gives
\begin{align*}
S(x,z) \leq C_3 x \frac{(\log\log x)^2}{(\log x)^2}
\end{align*}
for all sufficiently large $x$, where $C_3>0$ is absolute.
[/step]
[step:Absorb the small primes and the bounded range of small $x$]
From the reduction step,
\begin{align*}
\pi_2(x) \leq z + S(x,z).
\end{align*}
Since $z=x^{1/4}$, and
\begin{align*}
x^{1/4} \leq C_4 x \frac{(\log\log x)^2}{(\log x)^2}
\end{align*}
for all sufficiently large $x$, we obtain
\begin{align*}
\pi_2(x) \leq C_5 x \frac{(\log\log x)^2}{(\log x)^2}
\end{align*}
for all sufficiently large $x$.
Finally, on the bounded interval $3 \leq x \leq X_0$, where $X_0$ is chosen large enough for the previous estimates to hold for $x \geq X_0$, the quantity
\begin{align*}
\frac{(\log x)^2}{x(\log\log x)^2}\pi_2(x)
\end{align*}
is bounded above by a finite absolute constant after increasing the constant to handle the finitely many points where $\log\log x$ is small. Enlarging $C_5$ once more gives an absolute constant $C>0$ such that, for every real $x \geq 3$,
\begin{align*}
\pi_2(x) \leq C \frac{x(\log\log x)^2}{(\log x)^2}.
\end{align*}
This is the desired Brun upper bound for twin primes.
[/step]