#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*/ FILE;

/* Variables and functions */
int atoi(char *);
scalar_t__ calloc(int, long);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ findPrimes(char *, char *, char *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printPrimes(int /*<<< orphan*/ *, char *, char *);
scalar_t__ sqrt(long);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  long max;
  int power;
  char *start;
  char *end;
  char *endsqrt;
  FILE *primes;
  if (argc != 2) {
    power = 20;
  } else {
    power = atoi(argv[1]);
    if (power < 3) {
      fprintf(stderr, "Invalid value for power\n");
      power = 20;
    }
  }
  fprintf(stderr, "power = %d\n", power);
  max = 1 << (power - 3);
  start = (char *)calloc(sizeof(char), max);
  end = start + max;
  endsqrt = start + (int)sqrt(max) + 1;
  if (start == NULL) {
    fprintf(stderr, "Failed to allocate memory\n");
    return 1;
  }
  findPrimes(start, endsqrt, end);
  primes = fopen("./primes.txt", "w+");
  if (primes == NULL) {
    fprintf(stderr, "Failed to create ./primes.txt\n");
    return 1;
  }
  printPrimes(primes, start, end);
  free(start);
  fclose(primes);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11bc <main+0x2c>
movl   $0x14,-0x1c(%rbp)
jmp    11f9 <main+0x69>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x3,-0x1c(%rbp)
jge    11f4 <main+0x64>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x14,-0x1c(%rbp)
jmp    11f9 <main+0x69>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xe0d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x1c(%rbp),%ecx
sub    $0x3,%ecx
mov    $0x1,%eax
shl    %cl,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
mov    $0x1,%edi
call   1060 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   10a0 <sqrt@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1298 <main+0x108>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
lea    0xda0(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1318 <main+0x188>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   10b0 <findPrimes@plt>
lea    0xd90(%rip),%rdi
lea    0xd96(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12ee <main+0x15e>
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
lea    0xd75(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1318 <main+0x188>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1050 <printPrimes@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0x14,%ebx
cmp    $0x2,%edi
jne    11cd <main+0x3d>
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebx
cmp    $0x2,%eax
jg     11cd <main+0x3d>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    0xe3f(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x14,%ebx
mov    0x2e0c(%rip),%r15
mov    (%r15),%edi
lea    0xe3b(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
add    $0xfd,%bl
mov    $0x1,%ebp
mov    $0x1,%eax
mov    %ebx,%ecx
shl    %cl,%eax
movslq %eax,%rbx
mov    $0x1,%edi
mov    %rbx,%rsi
call   1060 <calloc@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   10a0 <sqrt@plt>
test   %r14,%r14
je     126d <main+0xdd>
add    %r14,%rbx
cltq
lea    (%r14,%rax,1),%rsi
add    $0x1,%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   10b0 <findPrimes@plt>
lea    0xe09(%rip),%rdi
lea    0xe0f(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1280 <main+0xf0>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <printPrimes@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
xor    %ebp,%ebp
jmp    1296 <main+0x106>
mov    (%r15),%edi
lea    0xdae(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1296 <main+0x106>
mov    (%r15),%edi
lea    0xdc6(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [25 x i8] c"Invalid value for power\0A\00"
@.str.1 = [12 x i8] c"power = %d\0A\00"
@.str.2 = [27 x i8] c"Failed to allocate memory\0A\00"
@.str.3 = [13 x i8] c"./primes.txt\00"
@.str.4 = [3 x i8] c"w+\00"
@.str.5 = [31 x i8] c"Failed to create ./primes.txt\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 20, i32* %7
br label %26
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp slt i32 %20, 3
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 20, i32* %7
br label %25
25: ; preds = %22, %15
br label %26
26: ; preds = %25, %14
%27 = load i32, i32* @stderr
%28 = load i32, i32* %7
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %7
%31 = sub nsw i32 %30, 3
%32 = shl i32 1, %31
%33 = sext i32 %32 to i64
store i64 %33, i64* %6
%34 = load i64, i64* %6
%35 = call i64 @calloc(i32 1, i64 %34)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %8
%37 = load i8*, i8** %8
%38 = load i64, i64* %6
%39 = getelementptr inbounds i8, i8* %37, i64 %38
store i8* %39, i8** %9
%40 = load i8*, i8** %8
%41 = load i64, i64* %6
%42 = call i64 @sqrt(i64 %41)
%43 = trunc i64 %42 to i32
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %40, i64 %44
%46 = getelementptr inbounds i8, i8* %45, i64 1
store i8* %46, i8** %10
%47 = load i8*, i8** %8
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %52
49: ; preds = %26
%50 = load i32, i32* @stderr
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %72
52: ; preds = %26
%53 = load i8*, i8** %8
%54 = load i8*, i8** %10
%55 = load i8*, i8** %9
%56 = call i32 @findPrimes(i8* %53, i8* %54, i8* %55)
%57 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %57, i32** %11
%58 = load i32*, i32** %11
%59 = icmp eq i32* %58, null
br i1 %59, label %60, label %63
60: ; preds = %52
%61 = load i32, i32* @stderr
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %72
63: ; preds = %52
%64 = load i32*, i32** %11
%65 = load i8*, i8** %8
%66 = load i8*, i8** %9
%67 = call i32 @printPrimes(i32* %64, i8* %65, i8* %66)
%68 = load i8*, i8** %8
%69 = call i32 @free(i8* %68)
%70 = load i32*, i32** %11
%71 = call i32 @fclose(i32* %70)
store i32 0, i32* %3
br label %72
72: ; preds = %63, %60, %49
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [25 x i8] c"Invalid value for power\0A\00"
@.str.1 = [12 x i8] c"power = %d\0A\00"
@.str.2 = [27 x i8] c"Failed to allocate memory\0A\00"
@.str.3 = [13 x i8] c"./primes.txt\00"
@.str.4 = [3 x i8] c"w+\00"
@.str.5 = [31 x i8] c"Failed to create ./primes.txt\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %4, label %12
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 slt i32 %7, 3
br i1 %8, label %9, label %12
9: ; preds = %4
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %2, %4, %9
%13 = phi i32 [ 20, %9 ], [ %7, %4 ], [ 20, %2 ]
%14 = load i32, i32* @stderr
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%16 = add nsw i32 %13, -3
%17 = shl nuw i32 1, %16
%18 = sext i32 %17 to i64
%19 = tail call i64 @calloc(i32 1, i64 %18)
%20 = inttoptr i64 %19 to i8*
%21 = getelementptr inbounds i8, i8* %20, i64 %18
%22 = tail call i64 @sqrt(i64 %18)
%23 = icmp eq i64 %19, 0
br i1 %23, label %24, label %27
24: ; preds = %12
%25 = load i32, i32* @stderr
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %42
27: ; preds = %12
%28 = shl i64 %22, 32
%29 = ashr exact i64 %28, 32
%30 = getelementptr inbounds i8, i8* %20, i64 %29
%31 = getelementptr inbounds i8, i8* %30, i64 1
%32 = tail call i32 @findPrimes(i8* %20, i8* %31, i8* %21)
%33 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %38
35: ; preds = %27
%36 = load i32, i32* @stderr
%37 = tail call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
br label %42
38: ; preds = %27
%39 = tail call i32 @printPrimes(i32* %33, i8* %20, i8* %21)
%40 = tail call i32 @free(i8* %20)
%41 = tail call i32 @fclose(i32* %33)
br label %42
42: ; preds = %38, %35, %24
%43 = phi i32 [ 1, %24 ], [ 1, %35 ], [ 0, %38 ]
ret i32 %43
}