#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 */
typedef scalar_t__ time_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
float CLOCKS_PER_SEC;
scalar_t__ clock();
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ free(int *);
int *primes(int);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  int max = 2e6;
  time_t t_start, t_end;
  printf("Calculating primes up to %d\n", max);
  t_start = clock();
  int *p = primes(max);
  t_end = clock();
  if (p == NULL) {
    printf("Error calculating primes.\n");
    return -1;
  }
  float t = (float)(t_end - t_start) / CLOCKS_PER_SEC;
  printf("Done. Run in %fs\n", t);
  if (0) {
    int i = 0;
    while (p[i] != -999) {
      printf("%d ", p[i]);
      i++;
    }
  }
  if (1) {
    printf("Printing results to file.\n");
    FILE *pFile = fopen("meusPrimos.txt", "w");
    if (pFile == NULL) {
      printf("Error in writting to file.\n");
      return -1;
    }
    int i = 0;
    while (p[i] != -999) {
      fprintf(pFile, "\t%d", p[i]);
      if ((i + 1) % 10 == 0)
        fprintf(pFile, "\n");
      i++;
    }
  }
  free(p);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1e8480,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1040 <clock@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1080 <primes@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <clock@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ce <main+0x6e>
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cc <main+0x16c>
mov    -0x18(%rbp),%rax
sub    -0x10(%rbp),%rax
cvtsi2ss %rax,%xmm0
mov    0x2e06(%rip),%rax
divss  (%rax),%xmm0
movss  %xmm0,-0x24(%rbp)
movss  -0x24(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xe3d(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe4e(%rip),%rdi
lea    0xe56(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    124c <main+0xec>
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cc <main+0x16c>
movl   $0x0,-0x34(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
cmpl   $0xfffffc19,(%rax,%rcx,4)
je     12bc <main+0x15c>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe14(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    12ae <main+0x14e>
mov    -0x30(%rbp),%rdi
lea    0xde9(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1253 <main+0xf3>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe6e(%rip),%rdi
mov    $0x1e8480,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1050 <clock@plt>
mov    %rax,%rbx
mov    $0x1e8480,%edi
call   10a0 <primes@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   1050 <clock@plt>
test   %rbp,%rbp
je     1226 <main+0xa6>
sub    %rbx,%rax
cvtsi2ss %rax,%xmm0
mov    0x2e18(%rip),%rax
divss  (%rax),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xe3e(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
lea    0xe57(%rip),%rdi
call   1040 <puts@plt>
lea    0xe36(%rip),%rdi
lea    0xe3e(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     122f <main+0xaf>
mov    %rbp,(%rsp)
mov    0x0(%rbp),%edx
cmp    $0xfffffc19,%edx
jne    124f <main+0xcf>
mov    (%rsp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    1240 <main+0xc0>
lea    0xe4c(%rip),%rdi
jmp    1236 <main+0xb6>
lea    0xe28(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rax,%r12
mov    (%rsp),%r13
add    $0x4,%r13
mov    $0xffffffff,%ebp
mov    $0x1,%ebx
lea    0xdd5(%rip),%r15
jmp    1286 <main+0x106>
nopl   (%rax)
mov    0x0(%r13),%edx
add    $0x4,%r13
add    $0xffffffff,%ebp
add    $0x1,%ebx
cmp    $0xfffffc19,%edx
je     1219 <main+0x99>
mov    %ebx,%eax
mov    $0xcccccccd,%ecx
imul   %rcx,%rax
shr    $0x23,%rax
lea    (%rax,%rax,4),%r14d
add    %r14d,%r14d
mov    %r12,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
add    %ebp,%r14d
jne    1270 <main+0xf0>
mov    $0xa,%edi
mov    %r12,%rsi
call   1070 <fputc@plt>
jmp    1270 <main+0xf0>

LLVM IR (-O0):
@.str = [29 x i8] c"Calculating primes up to %d\0A\00"
@.str.1 = [27 x i8] c"Error calculating primes.\0A\00"
@CLOCKS_PER_SEC = global float 0.000000e+00
@.str.2 = [18 x i8] c"Done. Run in %fs\0A\00"
@.str.3 = [27 x i8] c"Printing results to file.\0A\00"
@.str.4 = [15 x i8] c"meusPrimos.txt\00"
@.str.5 = [2 x i8] c"w\00"
@.str.6 = [28 x i8] c"Error in writting to file.\0A\00"
@.str.7 = [4 x i8] c"\09%d\00"
@.str.8 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32*
%6 = alloca float
%7 = alloca i32*
%8 = alloca i32
store i32 0, i32* %1
store i32 2000000, i32* %2
%9 = load i32, i32* %2
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = call i64 (...) @clock()
store i64 %11, i64* %3
%12 = load i32, i32* %2
%13 = call i32* @primes(i32 %12)
store i32* %13, i32** %5
%14 = call i64 (...) @clock()
store i64 %14, i64* %4
%15 = load i32*, i32** %5
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %64
19: ; preds = %0
%20 = load i64, i64* %4
%21 = load i64, i64* %3
%22 = sub nsw i64 %20, %21
%23 = sitofp i64 %22 to float
%24 = load float, float* @CLOCKS_PER_SEC
%25 = fdiv float %23, %24
store float %25, float* %6
%26 = load float, float* %6
%27 = fpext float %26 to double
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), double %27)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%30 = call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %30, i32** %7
%31 = load i32*, i32** %7
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %35
33: ; preds = %19
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %1
br label %64
35: ; preds = %19
store i32 0, i32* %8
br label %36
36: ; preds = %58, %35
%37 = load i32*, i32** %5
%38 = load i32, i32* %8
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i32, i32* %37, i64 %39
%41 = load i32, i32* %40
%42 = icmp ne i32 %41, -999
br i1 %42, label %43, label %61
43: ; preds = %36
%44 = load i32*, i32** %7
%45 = load i32*, i32** %5
%46 = load i32, i32* %8
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %45, i64 %47
%49 = load i32, i32* %48
%50 = call i32 (i32*, i8*, ...) @fprintf(i32* %44, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32 %49)
%51 = load i32, i32* %8
%52 = add nsw i32 %51, 1
%53 = srem i32 %52, 10
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %43
%56 = load i32*, i32** %7
%57 = call i32 (i32*, i8*, ...) @fprintf(i32* %56, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
br label %58
58: ; preds = %55, %43
%59 = load i32, i32* %8
%60 = add nsw i32 %59, 1
store i32 %60, i32* %8
br label %36
61: ; preds = %36
%62 = load i32*, i32** %5
%63 = call i32 @free(i32* %62)
store i32 0, i32* %1
br label %64
64: ; preds = %61, %33, %17
%65 = load i32, i32* %1
ret i32 %65
}

LLVM IR (-O2):
@.str = [29 x i8] c"Calculating primes up to %d\0A\00"
@CLOCKS_PER_SEC = global float 0.000000e+00
@.str.2 = [18 x i8] c"Done. Run in %fs\0A\00"
@.str.4 = [15 x i8] c"meusPrimos.txt\00"
@.str.5 = [2 x i8] c"w\00"
@.str.7 = [4 x i8] c"\09%d\00"
@str = [26 x i8] c"Printing results to file.\00"
@str.9 = [27 x i8] c"Error in writting to file.\00"
@str.10 = [26 x i8] c"Error calculating primes.\00"
define i32 @main() {
%1 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 2000000)
%2 = tail call i64 (...) @clock()
%3 = tail call i32* @primes(i32 2000000)
%4 = tail call i64 (...) @clock()
%5 = icmp eq i32* %3, null
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.10, i64 0, i64 0))
br label %39
8: ; preds = %0
%9 = sub nsw i64 %4, %2
%10 = sitofp i64 %9 to float
%11 = load float, float* @CLOCKS_PER_SEC
%12 = fdiv float %10, %11
%13 = fpext float %12 to double
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), double %13)
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%16 = tail call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %21, label %18
18: ; preds = %8
%19 = load i32, i32* %3
%20 = icmp eq i32 %19, -999
br i1 %20, label %37, label %23
21: ; preds = %8
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.9, i64 0, i64 0))
br label %39
23: ; preds = %18, %33
%24 = phi i64 [ %27, %33 ], [ 0, %18 ]
%25 = phi i32 [ %35, %33 ], [ %19, %18 ]
%26 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32 %25)
%27 = add nuw i64 %24, 1
%28 = trunc i64 %27 to i32
%29 = urem i32 %28, 10
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %23
%32 = tail call i32 @fputc(i32 10, i32* %16)
br label %33
33: ; preds = %31, %23
%34 = getelementptr inbounds i32, i32* %3, i64 %27
%35 = load i32, i32* %34
%36 = icmp eq i32 %35, -999
br i1 %36, label %37, label %23
37: ; preds = %33, %18
%38 = tail call i32 @free(i32* %3)
br label %39
39: ; preds = %21, %37, %6
%40 = phi i32 [ -1, %6 ], [ 0, %37 ], [ -1, %21 ]
ret i32 %40
}