#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 */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(unsigned int *);
scalar_t__ is_prime(unsigned int, unsigned int, unsigned int *);
int /*<<< orphan*/ printf(char *, unsigned long long);
unsigned int *realloc(unsigned int *, int);
int main() {
  unsigned int last = 2000000;
  unsigned int *primes = (unsigned int *)calloc(1, sizeof(unsigned int));
  *primes = 2;
  unsigned int numprimes = 1;
  unsigned long long sum = 2;
  unsigned int test;
  for (test = 3; test < last; test += 2) {
    if (is_prime(test, numprimes, primes)) {
      primes = realloc(primes, sizeof(int) * (numprimes + 1));
      primes[numprimes++] = test;
      sum += test;
    }
  }
  printf("%llu\n", sum);
  free(primes);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1e8480,-0x8(%rbp)
mov    $0x1,%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x2,(%rax)
movl   $0x1,-0x14(%rbp)
movq   $0x2,-0x20(%rbp)
movl   $0x3,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x8(%rbp),%eax
jae    121e <main+0xbe>
mov    -0x24(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1060 <is_prime@plt>
cmp    $0x0,%rax
je     120b <main+0xab>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,%eax
shl    $0x2,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x24(%rbp),%edx
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x14(%rbp)
mov    %ecx,%ecx
mov    %edx,(%rax,%rcx,4)
mov    -0x24(%rbp),%eax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1210 <main+0xb0>
mov    -0x24(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x24(%rbp)
jmp    11a9 <main+0x49>
mov    -0x20(%rbp),%rsi
lea    0xdd7(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0x1,%r12d
mov    $0x1,%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,%r15
movl   $0x2,(%rax)
mov    $0x2,%r14d
mov    $0x3,%ebx
jmp    11b0 <main+0x50>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0x2(%rbx),%rax
cmp    $0x1e847e,%rbx
mov    %rax,%rbx
jae    11e9 <main+0x89>
mov    %ebx,%edi
mov    %r12d,%esi
mov    %r15,%rdx
call   1060 <is_prime@plt>
test   %rax,%rax
je     11a0 <main+0x40>
lea    0x1(%r12),%ebp
lea    0x4(,%r12,4),%esi
mov    %r15,%rdi
call   1070 <realloc@plt>
mov    %rax,%r15
mov    %r12d,%eax
mov    %ebx,(%r15,%rax,4)
add    %rbx,%r14
mov    %ebp,%r12d
jmp    11a0 <main+0x40>
lea    0xe10(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"%llu\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
store i32 0, i32* %1
store i32 2000000, i32* %2
%7 = call i64 @calloc(i32 1, i32 4)
%8 = inttoptr i64 %7 to i32*
store i32* %8, i32** %3
%9 = load i32*, i32** %3
store i32 2, i32* %9
store i32 1, i32* %4
store i64 2, i64* %5
store i32 3, i32* %6
br label %10
10: ; preds = %39, %0
%11 = load i32, i32* %6
%12 = load i32, i32* %2
%13 = icmp ult i32 %11, %12
br i1 %13, label %14, label %42
14: ; preds = %10
%15 = load i32, i32* %6
%16 = load i32, i32* %4
%17 = load i32*, i32** %3
%18 = call i64 @is_prime(i32 %15, i32 %16, i32* %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %38
20: ; preds = %14
%21 = load i32*, i32** %3
%22 = load i32, i32* %4
%23 = add i32 %22, 1
%24 = zext i32 %23 to i64
%25 = mul i64 4, %24
%26 = trunc i64 %25 to i32
%27 = call i32* @realloc(i32* %21, i32 %26)
store i32* %27, i32** %3
%28 = load i32, i32* %6
%29 = load i32*, i32** %3
%30 = load i32, i32* %4
%31 = add i32 %30, 1
store i32 %31, i32* %4
%32 = zext i32 %30 to i64
%33 = getelementptr inbounds i32, i32* %29, i64 %32
store i32 %28, i32* %33
%34 = load i32, i32* %6
%35 = zext i32 %34 to i64
%36 = load i64, i64* %5
%37 = add i64 %36, %35
store i64 %37, i64* %5
br label %38
38: ; preds = %20, %14
br label %39
39: ; preds = %38
%40 = load i32, i32* %6
%41 = add i32 %40, 2
store i32 %41, i32* %6
br label %10
42: ; preds = %10
%43 = load i64, i64* %5
%44 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 %43)
%45 = load i32*, i32** %3
%46 = call i32 @free(i32* %45)
%47 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [6 x i8] c"%llu\0A\00"
define i32 @main() {
%1 = tail call i64 @calloc(i32 1, i32 4)
%2 = inttoptr i64 %1 to i32*
store i32 2, i32* %2
br label %3
3: ; preds = %0, %18
%4 = phi i64 [ 3, %0 ], [ %22, %18 ]
%5 = phi i64 [ 2, %0 ], [ %21, %18 ]
%6 = phi i32 [ 1, %0 ], [ %20, %18 ]
%7 = phi i32* [ %2, %0 ], [ %19, %18 ]
%8 = trunc i64 %4 to i32
%9 = tail call i64 @is_prime(i32 %8, i32 %6, i32* %7)
%10 = icmp eq i64 %9, 0
br i1 %10, label %18, label %11
11: ; preds = %3
%12 = add i32 %6, 1
%13 = shl i32 %12, 2
%14 = tail call i32* @realloc(i32* %7, i32 %13)
%15 = zext i32 %6 to i64
%16 = getelementptr inbounds i32, i32* %14, i64 %15
store i32 %8, i32* %16
%17 = add i64 %5, %4
br label %18
18: ; preds = %3, %11
%19 = phi i32* [ %14, %11 ], [ %7, %3 ]
%20 = phi i32 [ %12, %11 ], [ %6, %3 ]
%21 = phi i64 [ %17, %11 ], [ %5, %3 ]
%22 = add nuw nsw i64 %4, 2
%23 = icmp ult i64 %4, 1999998
br i1 %23, label %3, label %24
24: ; preds = %18
%25 = tail call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 %21)
%26 = tail call i32 @free(i32* %19)
%27 = tail call i32 @exit(i32 0)
unreachable
}