#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 atoi(char *);
int /*<<< orphan*/ printf(char *, int, int);
double sqrt(double);
int main(int argc, char *argv[]) {
  int n = 100000;
  if (argc >= 2) {
    n = atoi(argv[1]);
    if (n < 2) {
      n = 2;
    }
  }
  int num_primes = 0;
  int i;
  double limit;
  int prime, j;
  for (i = 2; i <= n; i++) {
    limit = sqrt((double)i);
    prime = 1;
    j = 2;
    while (prime && j <= limit) {
      if ((i % j) == 0) {
        prime = 0;
      }
      j++;
    }
    if (prime) {
      num_primes++;
    }
  }
  printf("Prime(%d)=%d\n", n, num_primes);
  return 0;
}


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   $0x186a0,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     117d <main+0x4d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x2,-0x14(%rbp)
jge    1178 <main+0x48>
movl   $0x2,-0x14(%rbp)
jmp    117d <main+0x4d>
movl   $0x0,-0x18(%rbp)
movl   $0x2,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     1232 <main+0x102>
cvtsi2sdl -0x1c(%rbp),%xmm0
call   1050 <sqrt@plt>
movsd  %xmm0,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
movl   $0x2,-0x30(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x2c(%rbp)
mov    %al,-0x31(%rbp)
je     11d7 <main+0xa7>
cvtsi2sdl -0x30(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm0
ucomisd %xmm1,%xmm0
setae  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    11e7 <main+0xb7>
jmp    120c <main+0xdc>
mov    -0x1c(%rbp),%eax
cltd
idivl  -0x30(%rbp)
cmp    $0x0,%edx
jne    11fe <main+0xce>
movl   $0x0,-0x2c(%rbp)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11b4 <main+0x84>
cmpl   $0x0,-0x2c(%rbp)
je     121f <main+0xef>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1224 <main+0xf4>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    118b <main+0x5b>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xdc1(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    $0x186a0,%r14d
cmp    $0x2,%edi
jl     1155 <main+0x25>
mov    0x8(%rsi),%rdi
call   1040 <atoi@plt>
cmp    $0x3,%eax
mov    $0x2,%r14d
cmovge %eax,%r14d
xor    %ebp,%ebp
mov    $0x2,%ebx
xorpd  %xmm2,%xmm2
movsd  0xe98(%rip),%xmm3
jmp    1183 <main+0x53>
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
test   %edx,%edx
setne  %al
add    %eax,%ebp
lea    0x1(%rbx),%eax
cmp    %r14d,%ebx
mov    %eax,%ebx
je     11ef <main+0xbf>
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
ucomisd %xmm2,%xmm0
jb     11a0 <main+0x70>
sqrtsd %xmm0,%xmm0
jmp    11b1 <main+0x81>
cs nopw 0x0(%rax,%rax,1)
call   1050 <sqrt@plt>
movsd  0xe53(%rip),%xmm3
xorpd  %xmm2,%xmm2
mov    $0x1,%eax
ucomisd %xmm3,%xmm0
jb     1177 <main+0x47>
mov    $0x3,%ecx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    -0x1(%rcx),%esi
mov    %ebx,%eax
xor    %edx,%edx
div    %esi
test   %edx,%edx
je     1170 <main+0x40>
xorps  %xmm1,%xmm1
cvtsi2sd %ecx,%xmm1
add    $0x1,%ecx
ucomisd %xmm1,%xmm0
jae    11d0 <main+0xa0>
jmp    1170 <main+0x40>
lea    0xe12(%rip),%rdi
mov    %r14d,%esi
mov    %ebp,%edx
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"Prime(%d)=%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca double
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 100000, i32* %6
%12 = load i32, i32* %4
%13 = icmp sge i32 %12, 2
br i1 %13, label %14, label %23
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @atoi(i8* %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp slt i32 %19, 2
br i1 %20, label %21, label %22
21: ; preds = %14
store i32 2, i32* %6
br label %22
22: ; preds = %21, %14
br label %23
23: ; preds = %22, %2
store i32 0, i32* %7
store i32 2, i32* %8
br label %24
24: ; preds = %58, %23
%25 = load i32, i32* %8
%26 = load i32, i32* %6
%27 = icmp sle i32 %25, %26
br i1 %27, label %28, label %61
28: ; preds = %24
%29 = load i32, i32* %8
%30 = sitofp i32 %29 to double
%31 = call double @sqrt(double %30)
store double %31, double* %9
store i32 1, i32* %10
store i32 2, i32* %11
br label %32
32: ; preds = %48, %28
%33 = load i32, i32* %10
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = load i32, i32* %11
%37 = sitofp i32 %36 to double
%38 = load double, double* %9
%39 = fcmp ole double %37, %38
br label %40
40: ; preds = %35, %32
%41 = phi i1 [ false, %32 ], [ %39, %35 ]
br i1 %41, label %42, label %51
42: ; preds = %40
%43 = load i32, i32* %8
%44 = load i32, i32* %11
%45 = srem i32 %43, %44
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %42
store i32 0, i32* %10
br label %48
48: ; preds = %47, %42
%49 = load i32, i32* %11
%50 = add nsw i32 %49, 1
store i32 %50, i32* %11
br label %32
51: ; preds = %40
%52 = load i32, i32* %10
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = load i32, i32* %7
%56 = add nsw i32 %55, 1
store i32 %56, i32* %7
br label %57
57: ; preds = %54, %51
br label %58
58: ; preds = %57
%59 = load i32, i32* %8
%60 = add nsw i32 %59, 1
store i32 %60, i32* %8
br label %24
61: ; preds = %24
%62 = load i32, i32* %6
%63 = load i32, i32* %7
%64 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %62, i32 %63)
ret i32 0
}

LLVM IR (-O2):
@.str = [14 x i8] c"Prime(%d)=%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %10
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @atoi(i8* %6)
%8 = icmp sgt i32 %7, 2
%9 = select i1 %8, i32 %7, i32 2
br label %10
10: ; preds = %4, %2
%11 = phi i32 [ %9, %4 ], [ 100000, %2 ]
br label %12
12: ; preds = %10, %28
%13 = phi i32 [ 2, %10 ], [ %31, %28 ]
%14 = phi i32 [ 0, %10 ], [ %30, %28 ]
%15 = sitofp i32 %13 to double
%16 = tail call double @sqrt(double %15)
%17 = fcmp ult double %16, 2.000000e+00
br i1 %17, label %28, label %18
18: ; preds = %12, %18
%19 = phi i32 [ %22, %18 ], [ 2, %12 ]
%20 = urem i32 %13, %19
%21 = icmp ne i32 %20, 0
%22 = add nuw nsw i32 %19, 1
%23 = sitofp i32 %22 to double
%24 = fcmp oge double %16, %23
%25 = select i1 %21, i1 %24, i1 false
br i1 %25, label %18, label %26
26: ; preds = %18
%27 = zext i1 %21 to i32
br label %28
28: ; preds = %26, %12
%29 = phi i32 [ 1, %12 ], [ %27, %26 ]
%30 = add nuw nsw i32 %29, %14
%31 = add nuw i32 %13, 1
%32 = icmp eq i32 %13, %11
br i1 %32, label %33, label %12
33: ; preds = %28
%34 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %30)
ret i32 0
}