#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int *prime_sieve(int const);
int /*<<< orphan*/ printf(char *, long);
int main() {
  const int pmax = 2000000;
  long int sum = 0;
  int *primes = prime_sieve(pmax);
  for (int i = 0; i < pmax; i++) {
    if (primes[i])
      sum += i;
  }
  printf("The sum of all primes less than 2,000,000 is %ld.\n", sum);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1e8480,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    $0x1e8480,%edi
call   1040 <prime_sieve@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x1e8480,-0x1c(%rbp)
jge    1191 <main+0x71>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
je     117e <main+0x5e>
movslq -0x1c(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1183 <main+0x63>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1153 <main+0x33>
mov    -0x10(%rbp),%rsi
lea    0xe64(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    $0x1e8480,%edi
call   1040 <prime_sieve@plt>
pxor   %xmm12,%xmm12
movdqa 0xec8(%rip),%xmm1
mov    $0x6,%ecx
movdqa 0xeca(%rip),%xmm8
movdqa 0xed1(%rip),%xmm9
movdqa 0xed8(%rip),%xmm10
movdqa 0xedf(%rip),%xmm11
pxor   %xmm6,%xmm6
pxor   %xmm7,%xmm7
nopl   0x0(%rax)
movdqa %xmm1,%xmm4
paddq  %xmm8,%xmm4
movq   -0x18(%rax,%rcx,4),%xmm3
movq   -0x10(%rax,%rcx,4),%xmm2
pcmpeqd %xmm12,%xmm3
pxor   %xmm5,%xmm5
pcmpgtd %xmm3,%xmm5
punpckldq %xmm5,%xmm3
pcmpeqd %xmm12,%xmm2
pxor   %xmm5,%xmm5
pcmpgtd %xmm2,%xmm5
punpckldq %xmm5,%xmm2
pandn  %xmm4,%xmm2
paddq  %xmm7,%xmm2
pandn  %xmm1,%xmm3
paddq  %xmm6,%xmm3
movdqa %xmm1,%xmm4
paddq  %xmm9,%xmm4
movdqa %xmm1,%xmm5
paddq  %xmm10,%xmm5
movq   -0x8(%rax,%rcx,4),%xmm6
movq   (%rax,%rcx,4),%xmm7
pcmpeqd %xmm12,%xmm6
pxor   %xmm0,%xmm0
pcmpgtd %xmm6,%xmm0
punpckldq %xmm0,%xmm6
pandn  %xmm4,%xmm6
paddq  %xmm3,%xmm6
pcmpeqd %xmm12,%xmm7
pxor   %xmm0,%xmm0
pcmpgtd %xmm7,%xmm0
punpckldq %xmm0,%xmm7
pandn  %xmm5,%xmm7
paddq  %xmm2,%xmm7
paddq  %xmm11,%xmm1
add    $0x8,%rcx
cmp    $0x1e8486,%rcx
jne    1170 <main+0x50>
paddq  %xmm6,%xmm7
pshufd $0xee,%xmm7,%xmm0
paddq  %xmm7,%xmm0
movq   %xmm0,%rsi
lea    0xe1b(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [51 x i8] c"The sum of all primes less than 2,000,000 is %ld.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i32
store i32 0, i32* %1
store i32 2000000, i32* %2
store i64 0, i64* %3
%6 = call i32* @prime_sieve(i32 2000000)
store i32* %6, i32** %4
store i32 0, i32* %5
br label %7
7: ; preds = %23, %0
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 2000000
br i1 %9, label %10, label %26
10: ; preds = %7
%11 = load i32*, i32** %4
%12 = load i32, i32* %5
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i32, i32* %11, i64 %13
%15 = load i32, i32* %14
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %10
%18 = load i32, i32* %5
%19 = sext i32 %18 to i64
%20 = load i64, i64* %3
%21 = add nsw i64 %20, %19
store i64 %21, i64* %3
br label %22
22: ; preds = %17, %10
br label %23
23: ; preds = %22
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
store i32 %25, i32* %5
br label %7
26: ; preds = %7
%27 = load i64, i64* %3
%28 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i64 %27)
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
@.str = [51 x i8] c"The sum of all primes less than 2,000,000 is %ld.\0A\00"
define i32 @main() {
%1 = tail call i32* @prime_sieve(i32 2000000)
br label %2
2: ; preds = %2, %0
%3 = phi i64 [ 0, %0 ], [ %35, %2 ]
%4 = phi <2 x i64> [ <i64 0, i64 1>, %0 ], [ %36, %2 ]
%5 = phi <2 x i64> [ zeroinitializer, %0 ], [ %33, %2 ]
%6 = phi <2 x i64> [ zeroinitializer, %0 ], [ %34, %2 ]
%7 = add <2 x i64> %4, <i64 2, i64 2>
%8 = getelementptr inbounds i32, i32* %1, i64 %3
%9 = bitcast i32* %8 to <2 x i32>*
%10 = load <2 x i32>, <2 x i32>* %9
%11 = getelementptr inbounds i32, i32* %8, i64 2
%12 = bitcast i32* %11 to <2 x i32>*
%13 = load <2 x i32>, <2 x i32>* %12
%14 = icmp eq <2 x i32> %10, zeroinitializer
%15 = icmp eq <2 x i32> %13, zeroinitializer
%16 = select <2 x i1> %14, <2 x i64> zeroinitializer, <2 x i64> %4
%17 = select <2 x i1> %15, <2 x i64> zeroinitializer, <2 x i64> %7
%18 = add <2 x i64> %16, %5
%19 = add <2 x i64> %17, %6
%20 = or i64 %3, 4
%21 = add <2 x i64> %4, <i64 4, i64 4>
%22 = add <2 x i64> %4, <i64 6, i64 6>
%23 = getelementptr inbounds i32, i32* %1, i64 %20
%24 = bitcast i32* %23 to <2 x i32>*
%25 = load <2 x i32>, <2 x i32>* %24
%26 = getelementptr inbounds i32, i32* %23, i64 2
%27 = bitcast i32* %26 to <2 x i32>*
%28 = load <2 x i32>, <2 x i32>* %27
%29 = icmp eq <2 x i32> %25, zeroinitializer
%30 = icmp eq <2 x i32> %28, zeroinitializer
%31 = select <2 x i1> %29, <2 x i64> zeroinitializer, <2 x i64> %21
%32 = select <2 x i1> %30, <2 x i64> zeroinitializer, <2 x i64> %22
%33 = add <2 x i64> %31, %18
%34 = add <2 x i64> %32, %19
%35 = add nuw nsw i64 %3, 8
%36 = add <2 x i64> %4, <i64 8, i64 8>
%37 = icmp eq i64 %35, 2000000
br i1 %37, label %38, label %2
38: ; preds = %2
%39 = add <2 x i64> %34, %33
%40 = call i64 @llvm.vector.reduce.add.v2i64(<2 x i64> %39)
%41 = tail call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i64 %40)
ret i32 0
}