#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, unsigned long);
unsigned long nextprime(unsigned long);
int main() {
  unsigned long int last = 0;
  unsigned long int i = 1;
  FILE *out;
  out = fopen("primes.txt", "w");
  while (1) {
    last = i;
    i = nextprime(i);
    if (last > i)
      break;
    fprintf(out, "%lu\n", i);
  }
  fclose(out);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x1,-0x18(%rbp)
lea    0xe9a(%rip),%rdi
lea    0xe9e(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <nextprime@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    119e <main+0x5e>
jmp    11b7 <main+0x77>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe60(%rip),%rsi
call   1040 <fprintf@plt>
jmp    1176 <main+0x36>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
lea    0xeb4(%rip),%rdi
lea    0xeb8(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r14
mov    $0x1,%edi
call   1050 <nextprime@plt>
test   %rax,%rax
je     119e <main+0x5e>
mov    %rax,%rbx
lea    0xe99(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <nextprime@plt>
cmp    %rax,%rbx
mov    %rax,%rbx
jbe    1180 <main+0x40>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"primes.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [5 x i8] c"%lu\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i32*
store i32 0, i32* %1
store i64 0, i64* %2
store i64 1, i64* %3
%5 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %4
br label %6
6: ; preds = %0, %14
%7 = load i64, i64* %3
store i64 %7, i64* %2
%8 = load i64, i64* %3
%9 = call i64 @nextprime(i64 %8)
store i64 %9, i64* %3
%10 = load i64, i64* %2
%11 = load i64, i64* %3
%12 = icmp ugt i64 %10, %11
br i1 %12, label %13, label %14
13: ; preds = %6
br label %18
14: ; preds = %6
%15 = load i32*, i32** %4
%16 = load i64, i64* %3
%17 = call i32 @fprintf(i32* %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %16)
br label %6
18: ; preds = %13
%19 = load i32*, i32** %4
%20 = call i32 @fclose(i32* %19)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"primes.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [5 x i8] c"%lu\0A\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = tail call i64 @nextprime(i64 1)
%3 = icmp eq i64 %2, 0
br i1 %3, label %9, label %4
4: ; preds = %0, %4
%5 = phi i64 [ %7, %4 ], [ %2, %0 ]
%6 = tail call i32 @fprintf(i32* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %5)
%7 = tail call i64 @nextprime(i64 %5)
%8 = icmp ugt i64 %5, %7
br i1 %8, label %9, label %4
9: ; preds = %4, %0
%10 = tail call i32 @fclose(i32* %1)
ret i32 0
}