#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 /*<<< orphan*/ free(int *);
int *generate_primes(int const);
int /*<<< orphan*/ printf(char *, unsigned long);
int main(int argc, char **argv) {
  const int limit = 2000000;
  unsigned long sum = 0;
  const int *const primes = generate_primes(limit);
  for (int i = 0; primes[i]; i++) {
    sum += (unsigned long)primes[i];
  }
  printf("%lu\n", sum);
  free((int *)primes);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1e8480,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x1e8480,%edi
call   1040 <generate_primes@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
je     119e <main+0x6e>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movslq (%rax,%rcx,4),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    116a <main+0x3a>
mov    -0x20(%rbp),%rsi
lea    0xe57(%rip),%rdi
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0x1e8480,%edi
call   1040 <generate_primes@plt>
mov    %rax,%rbx
mov    (%rax),%eax
test   %eax,%eax
je     1161 <main+0x31>
mov    %rbx,%rcx
add    $0x4,%rcx
xor    %esi,%esi
nopl   (%rax)
cltq
add    %rax,%rsi
mov    (%rcx),%eax
add    $0x4,%rcx
test   %eax,%eax
jne    1150 <main+0x20>
jmp    1163 <main+0x33>
xor    %esi,%esi
lea    0xe96(%rip),%rdi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%lu\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 2000000, i32* %6
store i64 0, i64* %7
%10 = call i32* @generate_primes(i32 2000000)
store i32* %10, i32** %8
store i32 0, i32* %9
br label %11
11: ; preds = %27, %2
%12 = load i32*, i32** %8
%13 = load i32, i32* %9
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i32, i32* %12, i64 %14
%16 = load i32, i32* %15
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %30
18: ; preds = %11
%19 = load i32*, i32** %8
%20 = load i32, i32* %9
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i32, i32* %19, i64 %21
%23 = load i32, i32* %22
%24 = sext i32 %23 to i64
%25 = load i64, i64* %7
%26 = add i64 %25, %24
store i64 %26, i64* %7
br label %27
27: ; preds = %18
%28 = load i32, i32* %9
%29 = add nsw i32 %28, 1
store i32 %29, i32* %9
br label %11
30: ; preds = %11
%31 = load i64, i64* %7
%32 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %31)
%33 = load i32*, i32** %8
%34 = call i32 @free(i32* %33)
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@.str = [5 x i8] c"%lu\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32* @generate_primes(i32 2000000)
%4 = load i32, i32* %3
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %10, %2
%7 = phi i64 [ 0, %2 ], [ %15, %10 ]
%8 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %7)
%9 = tail call i32 @free(i32* %3)
ret i32 0
10: ; preds = %2, %10
%11 = phi i64 [ %16, %10 ], [ 0, %2 ]
%12 = phi i32 [ %18, %10 ], [ %4, %2 ]
%13 = phi i64 [ %15, %10 ], [ 0, %2 ]
%14 = sext i32 %12 to i64
%15 = add i64 %13, %14
%16 = add nuw nsw i64 %11, 1
%17 = getelementptr inbounds i32, i32* %3, i64 %16
%18 = load i32, i32* %17
%19 = icmp eq i32 %18, 0
br i1 %19, label %6, label %10
}