#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 int /*<<< orphan*/ primes;

/* Variables and functions */
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ memset(int *, int, int);
int /*<<< orphan*/ printPrime(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ stdout;
int main(void) {
  printf("\nUp to what number do you want to find prime numbers? ");
  fflush(stdout);
  int max;
  scanf("%i", &max);
  printf("Finding up to: %i\n", max);
  printPrime(2);
  int primes[max / 3];
  memset(primes, -1, sizeof(primes));
  int number = 3, numberOfPrimes = 0;
  while (number <= max) {
    int i;
    int isPrime = 1;
    for (i = 0; primes[i] != -1; i++) {
      if (number % primes[i] == 0) {
        isPrime = 0;
        break;
      }
    }
    if (isPrime) {
      printPrime(number);
      primes[i] = number;
      numberOfPrimes = i;
    }
    number += 2;
  }
  printf("Prime Count: %i\n", numberOfPrimes + 2);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
xor    %eax,%eax
mov    %al,-0x31(%rbp)
call   1030 <printf@plt>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
lea    0xeb2(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1060 <scanf@plt>
mov    -0x31(%rbp),%al
mov    -0x8(%rbp),%esi
lea    0xe9f(%rip),%rdi
call   1030 <printf@plt>
mov    $0x2,%edi
call   1070 <printPrime@plt>
movslq -0x8(%rbp),%rax
imul   $0x55555556,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
shr    $0x20,%rax
add    %ecx,%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
lea    0xf(,%rax,4),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x18(%rbp)
shl    $0x2,%rax
mov    %eax,%edx
mov    $0xffffffff,%esi
call   1040 <memset@plt>
movl   $0x3,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jg     129b <main+0x14b>
movl   $0x1,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmpl   $0xffffffff,(%rax,%rcx,4)
je     1267 <main+0x117>
mov    -0x30(%rbp),%rcx
mov    -0x1c(%rbp),%eax
movslq -0x24(%rbp),%rsi
cltd
idivl  (%rcx,%rsi,4)
cmp    $0x0,%edx
jne    1254 <main+0x104>
movl   $0x0,-0x28(%rbp)
jmp    1267 <main+0x117>
jmp    1259 <main+0x109>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    121e <main+0xce>
cmpl   $0x0,-0x28(%rbp)
je     128d <main+0x13d>
mov    -0x1c(%rbp),%edi
call   1070 <printPrime@plt>
mov    -0x30(%rbp),%rax
mov    -0x1c(%rbp),%edx
movslq -0x24(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x1c(%rbp)
jmp    1204 <main+0xb4>
mov    -0x20(%rbp),%esi
add    $0x2,%esi
lea    0xda5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe9b(%rip),%rdi
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
lea    0xeb3(%rip),%rdi
lea    -0x2c(%rbp),%rsi
call   1060 <scanf@plt>
mov    -0x2c(%rbp),%esi
lea    0xea3(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x2,%edi
call   1070 <printPrime@plt>
mov    %rsp,%r12
movslq -0x2c(%rbp),%rax
imul   $0x55555556,%rax,%rdx
mov    %rdx,%rax
shr    $0x3f,%rax
shr    $0x20,%rdx
add    %eax,%edx
lea    0xf(,%rdx,4),%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%r15
sub    %rax,%r15
mov    %r15,%rsp
shl    $0x2,%edx
mov    %r15,%rdi
mov    $0xffffffff,%esi
call   1040 <memset@plt>
mov    -0x2c(%rbp),%ecx
cmp    $0x3,%ecx
jge    1219 <main+0xc9>
add    $0x2,%r14d
lea    0xe52(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r12,%rsp
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x3,%r13d
xor    %r14d,%r14d
jmp    124e <main+0xfe>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%r14d
mov    %r13d,%edi
call   1070 <printPrime@plt>
mov    %r13d,(%r15,%r14,4)
mov    -0x2c(%rbp),%ecx
mov    %ebx,%r14d
add    $0x2,%r13d
cmp    %ecx,%r13d
jg     11f0 <main+0xa0>
mov    (%r15),%esi
mov    $0x0,%ebx
cmp    $0xffffffff,%esi
je     1230 <main+0xe0>
xor    %ebx,%ebx
nopl   (%rax)
mov    %r13d,%eax
cltd
idiv   %esi
test   %edx,%edx
je     1245 <main+0xf5>
mov    0x4(%r15,%rbx,4),%esi
add    $0x1,%rbx
cmp    $0xffffffff,%esi
jne    1260 <main+0x110>
jmp    1230 <main+0xe0>

LLVM IR (-O0):
@.str = [55 x i8] c"\0AUp to what number do you want to find prime numbers? \00"
@stdout = global i32 0
@.str.1 = [3 x i8] c"%i\00"
@.str.2 = [19 x i8] c"Finding up to: %i\0A\00"
@.str.3 = [17 x i8] c"Prime Count: %i\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @stdout
%11 = call i32 @fflush(i32 %10)
%12 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%13 = load i32, i32* %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = call i32 @printPrime(i32 2)
%16 = load i32, i32* %2
%17 = sdiv i32 %16, 3
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %3
%20 = alloca i32, i64 %18
store i64 %18, i64* %4
%21 = mul nuw i64 4, %18
%22 = trunc i64 %21 to i32
%23 = call i32 @memset(i32* %20, i32 -1, i32 %22)
store i32 3, i32* %5
store i32 0, i32* %6
br label %24
24: ; preds = %59, %0
%25 = load i32, i32* %5
%26 = load i32, i32* %2
%27 = icmp sle i32 %25, %26
br i1 %27, label %28, label %62
28: ; preds = %24
store i32 1, i32* %8
store i32 0, i32* %7
br label %29
29: ; preds = %45, %28
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %20, i64 %31
%33 = load i32, i32* %32
%34 = icmp ne i32 %33, -1
br i1 %34, label %35, label %48
35: ; preds = %29
%36 = load i32, i32* %5
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %20, i64 %38
%40 = load i32, i32* %39
%41 = srem i32 %36, %40
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %35
store i32 0, i32* %8
br label %48
44: ; preds = %35
br label %45
45: ; preds = %44
%46 = load i32, i32* %7
%47 = add nsw i32 %46, 1
store i32 %47, i32* %7
br label %29
48: ; preds = %43, %29
%49 = load i32, i32* %8
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %59
51: ; preds = %48
%52 = load i32, i32* %5
%53 = call i32 @printPrime(i32 %52)
%54 = load i32, i32* %5
%55 = load i32, i32* %7
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i32, i32* %20, i64 %56
store i32 %54, i32* %57
%58 = load i32, i32* %7
store i32 %58, i32* %6
br label %59
59: ; preds = %51, %48
%60 = load i32, i32* %5
%61 = add nsw i32 %60, 2
store i32 %61, i32* %5
br label %24
62: ; preds = %24
%63 = load i32, i32* %6
%64 = add nsw i32 %63, 2
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %64)
store i32 0, i32* %1
%66 = load i8*, i8** %3
%67 = load i32, i32* %1
ret i32 %67
}

LLVM IR (-O2):
@.str = [55 x i8] c"\0AUp to what number do you want to find prime numbers? \00"
@stdout = global i32 0
@.str.1 = [3 x i8] c"%i\00"
@.str.2 = [19 x i8] c"Finding up to: %i\0A\00"
@.str.3 = [17 x i8] c"Prime Count: %i\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0))
%3 = load i32, i32* @stdout
%4 = tail call i32 @fflush(i32 %3)
%5 = bitcast i32* %1 to i8*
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%7 = load i32, i32* %1
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %7)
%9 = call i32 @printPrime(i32 2)
%10 = load i32, i32* %1
%11 = sdiv i32 %10, 3
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
%14 = alloca i32, i64 %12
%15 = shl i32 %11, 2
%16 = call i32 @memset(i32* %14, i32 -1, i32 %15)
%17 = load i32, i32* %1
%18 = icmp slt i32 %17, 3
br i1 %18, label %48, label %19
19: ; preds = %0, %43
%20 = phi i32 [ %44, %43 ], [ %17, %0 ]
%21 = phi i32 [ %45, %43 ], [ 0, %0 ]
%22 = phi i32 [ %46, %43 ], [ 3, %0 ]
%23 = load i32, i32* %14
%24 = icmp eq i32 %23, -1
br i1 %24, label %37, label %25
25: ; preds = %19, %30
%26 = phi i64 [ %31, %30 ], [ 0, %19 ]
%27 = phi i32 [ %33, %30 ], [ %23, %19 ]
%28 = srem i32 %22, %27
%29 = icmp eq i32 %28, 0
br i1 %29, label %43, label %30
30: ; preds = %25
%31 = add nuw i64 %26, 1
%32 = getelementptr inbounds i32, i32* %14, i64 %31
%33 = load i32, i32* %32
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %25
35: ; preds = %30
%36 = trunc i64 %31 to i32
br label %37
37: ; preds = %35, %19
%38 = phi i32 [ 0, %19 ], [ %36, %35 ]
%39 = zext i32 %38 to i64
%40 = getelementptr inbounds i32, i32* %14, i64 %39
%41 = call i32 @printPrime(i32 %22)
store i32 %22, i32* %40
%42 = load i32, i32* %1
br label %43
43: ; preds = %25, %37
%44 = phi i32 [ %42, %37 ], [ %20, %25 ]
%45 = phi i32 [ %38, %37 ], [ %21, %25 ]
%46 = add nuw nsw i32 %22, 2
%47 = icmp sgt i32 %46, %44
br i1 %47, label %48, label %19
48: ; preds = %43, %0
%49 = phi i32 [ 0, %0 ], [ %45, %43 ]
%50 = add nsw i32 %49, 2
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %50)
ret i32 0
}