#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*/ mpz_t;
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 long);
int /*<<< orphan*/ gmp_fprintf(int /*<<< orphan*/ *, char *,
                               int /*<<< orphan*/);
int /*<<< orphan*/ mpz_clear(int /*<<< orphan*/);
scalar_t__ mpz_cmp_ui(int /*<<< orphan*/, unsigned long long);
int /*<<< orphan*/ mpz_init(int /*<<< orphan*/);
int /*<<< orphan*/ mpz_nextprime(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_set(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_set_ui(int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
int generate_primes(unsigned long long int upper_bound) {
  mpz_t p, q;
  FILE *prime_f;
  prime_f = fopen("primes.txt", "r");
  if (prime_f != (FILE *)NULL) {
    fclose(prime_f);
    return 1;
  }
  prime_f = fopen("primes.txt", "w");
  if (prime_f == (FILE *)NULL) {
    return 2;
  }
  fprintf(stderr, "Generating prime numbers with an upper bound of %llu\n",
          upper_bound);
  mpz_init(p);
  mpz_init(q);
  mpz_set_ui(p, 1);
  do {
    mpz_nextprime(q, p);
    gmp_fprintf(prime_f, "%Zd\n", q);
    mpz_set(p, q);
  } while (mpz_cmp_ui(p, upper_bound) < 0);
  fclose(prime_f);
  mpz_clear(p);
  mpz_clear(q);
  return 0;
}


x86 assembly (clang, -O0):
<generate_primes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe4d(%rip),%rdi
lea    0xe51(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11e3 <generate_primes+0x43>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a6 <generate_primes+0x106>
lea    0xe16(%rip),%rdi
lea    0xe1c(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1211 <generate_primes+0x71>
movl   $0x2,-0x4(%rbp)
jmp    12a6 <generate_primes+0x106>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdea(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x18(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1090 <mpz_set_ui@plt>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1070 <mpz_nextprime@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%edx
lea    0xde5(%rip),%rsi
call   10c0 <gmp_fprintf@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1030 <mpz_set@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1080 <mpz_cmp_ui@plt>
cmp    $0x0,%rax
jl     1247 <generate_primes+0xa7>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x14(%rbp),%edi
call   10a0 <mpz_clear@plt>
mov    -0x18(%rbp),%edi
call   10a0 <mpz_clear@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_primes>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xe51(%rip),%rdi
lea    0xe55(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     11d2 <generate_primes+0x32>
mov    %rax,%rdi
call   1040 <fclose@plt>
mov    $0x1,%eax
jmp    125d <generate_primes+0xbd>
lea    0xe27(%rip),%rdi
lea    0xe2d(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1258 <generate_primes+0xb8>
mov    %rax,%rbx
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xe12(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
call   1050 <mpz_init@plt>
call   1050 <mpz_init@plt>
mov    $0x1,%esi
call   1090 <mpz_set_ui@plt>
lea    0xe25(%rip),%r15
call   1070 <mpz_nextprime@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   10c0 <gmp_fprintf@plt>
call   1030 <mpz_set@plt>
mov    %r14,%rsi
call   1080 <mpz_cmp_ui@plt>
test   %rax,%rax
js     1220 <generate_primes+0x80>
mov    %rbx,%rdi
call   1040 <fclose@plt>
call   10a0 <mpz_clear@plt>
call   10a0 <mpz_clear@plt>
xor    %eax,%eax
jmp    125d <generate_primes+0xbd>
mov    $0x2,%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"r\00"
@.str.2 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.3 = [54 x i8] c"Generating prime numbers with an upper bound of %llu\0A\00"
@.str.4 = [5 x i8] c"%Zd\0A\00"
define i32 @generate_primes(i64 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
store i64 %0, i64* %3
%7 = 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* %7, i32** %6
%8 = load i32*, i32** %6
%9 = icmp ne i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32*, i32** %6
%12 = call i32 @fclose(i32* %11)
store i32 1, i32* %2
br label %50
13: ; preds = %1
%14 = 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.2, i64 0, i64 0))
store i32* %14, i32** %6
%15 = load i32*, i32** %6
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %18
17: ; preds = %13
store i32 2, i32* %2
br label %50
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = load i64, i64* %3
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i64 %20)
%22 = load i32, i32* %4
%23 = call i32 @mpz_init(i32 %22)
%24 = load i32, i32* %5
%25 = call i32 @mpz_init(i32 %24)
%26 = load i32, i32* %4
%27 = call i32 @mpz_set_ui(i32 %26, i32 1)
br label %28
28: ; preds = %38, %18
%29 = load i32, i32* %5
%30 = load i32, i32* %4
%31 = call i32 @mpz_nextprime(i32 %29, i32 %30)
%32 = load i32*, i32** %6
%33 = load i32, i32* %5
%34 = call i32 @gmp_fprintf(i32* %32, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %33)
%35 = load i32, i32* %4
%36 = load i32, i32* %5
%37 = call i32 @mpz_set(i32 %35, i32 %36)
br label %38
38: ; preds = %28
%39 = load i32, i32* %4
%40 = load i64, i64* %3
%41 = call i64 @mpz_cmp_ui(i32 %39, i64 %40)
%42 = icmp slt i64 %41, 0
br i1 %42, label %28, label %43
43: ; preds = %38
%44 = load i32*, i32** %6
%45 = call i32 @fclose(i32* %44)
%46 = load i32, i32* %4
%47 = call i32 @mpz_clear(i32 %46)
%48 = load i32, i32* %5
%49 = call i32 @mpz_clear(i32 %48)
store i32 0, i32* %2
br label %50
50: ; preds = %43, %17, %10
%51 = load i32, i32* %2
ret i32 %51
}

LLVM IR (-O2):
@.str = [11 x i8] c"primes.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.3 = [54 x i8] c"Generating prime numbers with an upper bound of %llu\0A\00"
@.str.4 = [5 x i8] c"%Zd\0A\00"
define i32 @generate_primes(i64 %0) {
%2 = 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))
%3 = icmp eq i32* %2, null
br i1 %3, label %6, label %4
4: ; preds = %1
%5 = tail call i32 @fclose(i32* %2)
br label %25
6: ; preds = %1
%7 = 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.2, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %25, label %9
9: ; preds = %6
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i64 %0)
%12 = tail call i32 @mpz_init(i32 undef)
%13 = tail call i32 @mpz_init(i32 undef)
%14 = tail call i32 @mpz_set_ui(i32 undef, i32 1)
br label %15
15: ; preds = %15, %9
%16 = tail call i32 @mpz_nextprime(i32 undef, i32 undef)
%17 = tail call i32 @gmp_fprintf(i32* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 undef)
%18 = tail call i32 @mpz_set(i32 undef, i32 undef)
%19 = tail call i64 @mpz_cmp_ui(i32 undef, i64 %0)
%20 = icmp slt i64 %19, 0
br i1 %20, label %15, label %21
21: ; preds = %15
%22 = tail call i32 @fclose(i32* %7)
%23 = tail call i32 @mpz_clear(i32 undef)
%24 = tail call i32 @mpz_clear(i32 undef)
br label %25
25: ; preds = %6, %21, %4
%26 = phi i32 [ 1, %4 ], [ 0, %21 ], [ 2, %6 ]
ret i32 %26
}