#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 double clock_t;

/* Variables and functions */
scalar_t__ CLOCKS_PER_SEC;
int atoi(char *);
double clock();
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  int failure = 0;
  if (argc != 2) {
    printf("Usage: primes <max up to 2^31>\n");
    failure = 1;
  } else {
    clock_t start, end;
    double runTime;
    start = clock();
    int i, num = 1, primes = 0;
    int max = atoi(argv[1]);
    while (num <= max) {
      i = 2;
      while (i <= num) {
        if (num % i == 0)
          break;
        i++;
      }
      if (i == num)
        primes++;
      num++;
    }
    end = clock();
    runTime = (end - start) / (double)CLOCKS_PER_SEC;
    printf(
        "This machine calculated all %d prime numbers under %d in %g seconds\n",
        primes, max, runTime);
  }
  return failure;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1171 <main+0x41>
lea    0xea2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x14(%rbp)
jmp    1244 <main+0x114>
mov    $0x0,%al
call   1030 <clock@plt>
movsd  %xmm0,-0x20(%rbp)
movl   $0x1,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x40(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x40(%rbp),%eax
jg     1200 <main+0xd0>
movl   $0x2,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x38(%rbp),%eax
jg     11dd <main+0xad>
mov    -0x38(%rbp),%eax
cltd
idivl  -0x34(%rbp)
cmp    $0x0,%edx
jne    11cf <main+0x9f>
jmp    11dd <main+0xad>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11ae <main+0x7e>
mov    -0x34(%rbp),%eax
cmp    -0x38(%rbp),%eax
jne    11f2 <main+0xc2>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    119b <main+0x6b>
mov    $0x0,%al
call   1030 <clock@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
subsd  -0x20(%rbp),%xmm0
mov    0x2dcb(%rip),%rax
cvtsi2sdq (%rax),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x3c(%rbp),%esi
mov    -0x40(%rbp),%edx
movsd  -0x30(%rbp),%xmm0
lea    0xde3(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    11f0 <main+0xb0>
mov    %rsi,%rbx
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <clock@plt>
movsd  %xmm0,(%rsp)
mov    0x8(%rbx),%rdi
call   1060 <atoi@plt>
mov    %eax,%r15d
mov    $0x0,%ebx
test   %eax,%eax
jle    1204 <main+0xc4>
mov    %r15d,%r8d
add    $0x1,%r8d
mov    $0x1,%ecx
mov    $0x2,%esi
xor    %edi,%edi
xor    %ebx,%ebx
jmp    11b9 <main+0x79>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
cmp    %ecx,%ebp
sete   %al
add    %eax,%ebx
add    $0x1,%ecx
lea    0x1(%rsi),%eax
add    $0xffffffff,%edi
cmp    %r8d,%esi
mov    %eax,%esi
je     1204 <main+0xc4>
mov    $0x2,%ebp
cmp    $0x2,%ecx
jb     11a0 <main+0x60>
mov    $0x2,%ebp
nopl   0x0(%rax,%rax,1)
mov    %ecx,%eax
xor    %edx,%edx
div    %ebp
test   %edx,%edx
je     11a0 <main+0x60>
lea    (%rdi,%rbp,1),%eax
add    $0x1,%eax
mov    %ebp,%edx
add    $0x1,%edx
mov    %edx,%ebp
cmp    $0x2,%eax
jne    11d0 <main+0x90>
mov    %esi,%ebp
jmp    11a0 <main+0x60>
lea    0xe4e(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%r14d
jmp    1233 <main+0xf3>
xor    %eax,%eax
call   1040 <clock@plt>
subsd  (%rsp),%xmm0
mov    0x2dd1(%rip),%rax
cvtsi2sdq (%rax),%xmm1
divsd  %xmm1,%xmm0
lea    0xdd9(%rip),%rdi
mov    %ebx,%esi
mov    %r15d,%edx
mov    $0x1,%al
call   1050 <printf@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [32 x i8] c"Usage: primes <max up to 2^31>\0A\00"
@CLOCKS_PER_SEC = global i64 0
@.str.1 = [69 x i8] c"This machine calculated all %d prime numbers under %d in %g seconds\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 2
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %6
br label %64
18: ; preds = %2
%19 = call double (...) @clock()
store double %19, double* %7
store i32 1, i32* %11
store i32 0, i32* %12
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %13
br label %24
24: ; preds = %49, %18
%25 = load i32, i32* %11
%26 = load i32, i32* %13
%27 = icmp sle i32 %25, %26
br i1 %27, label %28, label %52
28: ; preds = %24
store i32 2, i32* %10
br label %29
29: ; preds = %39, %28
%30 = load i32, i32* %10
%31 = load i32, i32* %11
%32 = icmp sle i32 %30, %31
br i1 %32, label %33, label %42
33: ; preds = %29
%34 = load i32, i32* %11
%35 = load i32, i32* %10
%36 = srem i32 %34, %35
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %33
br label %42
39: ; preds = %33
%40 = load i32, i32* %10
%41 = add nsw i32 %40, 1
store i32 %41, i32* %10
br label %29
42: ; preds = %38, %29
%43 = load i32, i32* %10
%44 = load i32, i32* %11
%45 = icmp eq i32 %43, %44
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = load i32, i32* %12
%48 = add nsw i32 %47, 1
store i32 %48, i32* %12
br label %49
49: ; preds = %46, %42
%50 = load i32, i32* %11
%51 = add nsw i32 %50, 1
store i32 %51, i32* %11
br label %24
52: ; preds = %24
%53 = call double (...) @clock()
store double %53, double* %8
%54 = load double, double* %8
%55 = load double, double* %7
%56 = fsub double %54, %55
%57 = load i64, i64* @CLOCKS_PER_SEC
%58 = sitofp i64 %57 to double
%59 = fdiv double %56, %58
store double %59, double* %9
%60 = load i32, i32* %12
%61 = load i32, i32* %13
%62 = load double, double* %9
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.1, i64 0, i64 0), i32 %60, i32 %61, double %62)
br label %64
64: ; preds = %52, %16
%65 = load i32, i32* %6
ret i32 %65
}

LLVM IR (-O2):
@CLOCKS_PER_SEC = global i64 0
@.str.1 = [69 x i8] c"This machine calculated all %d prime numbers under %d in %g seconds\0A\00"
@str = [31 x i8] c"Usage: primes <max up to 2^31>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %42
6: ; preds = %2
%7 = tail call double (...) @clock()
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @atoi(i8* %9)
%11 = icmp slt i32 %10, 1
br i1 %11, label %34, label %12
12: ; preds = %6
%13 = add i32 %10, 1
br label %14
14: ; preds = %12, %26
%15 = phi i32 [ 2, %12 ], [ %32, %26 ]
%16 = phi i32 [ 0, %12 ], [ %30, %26 ]
%17 = phi i32 [ 1, %12 ], [ %31, %26 ]
%18 = icmp ult i32 %17, 2
br i1 %18, label %26, label %19
19: ; preds = %14, %23
%20 = phi i32 [ %24, %23 ], [ 2, %14 ]
%21 = urem i32 %17, %20
%22 = icmp eq i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %19
%24 = add nuw i32 %20, 1
%25 = icmp eq i32 %24, %15
br i1 %25, label %26, label %19
26: ; preds = %23, %19, %14
%27 = phi i32 [ 2, %14 ], [ %20, %19 ], [ %15, %23 ]
%28 = icmp eq i32 %27, %17
%29 = zext i1 %28 to i32
%30 = add nuw nsw i32 %16, %29
%31 = add nuw nsw i32 %17, 1
%32 = add nuw i32 %15, 1
%33 = icmp eq i32 %15, %13
br i1 %33, label %34, label %14
34: ; preds = %26, %6
%35 = phi i32 [ 0, %6 ], [ %30, %26 ]
%36 = tail call double (...) @clock()
%37 = fsub double %36, %7
%38 = load i64, i64* @CLOCKS_PER_SEC
%39 = sitofp i64 %38 to double
%40 = fdiv double %37, %39
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([69 x i8], [69 x i8]* @.str.1, i64 0, i64 0), i32 %35, i32 %10, double %40)
br label %42
42: ; preds = %34, %4
%43 = phi i32 [ 1, %4 ], [ 0, %34 ]
ret i32 %43
}