#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 */
struct timeval {
  int tv_usec;
  int tv_sec;
};
typedef int /*<<< orphan*/ mpz_t;
typedef int /*<<< orphan*/ gmp_randstate_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ gmp_randclear(int /*<<< orphan*/);
int /*<<< orphan*/ gmp_randinit_mt(int /*<<< orphan*/);
int /*<<< orphan*/ gmp_randseed_ui(int /*<<< orphan*/, int);
int /*<<< orphan*/ mpz_add(int /*<<< orphan*/, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int /*<<< orphan*/ mpz_clear(int /*<<< orphan*/);
int mpz_cmp_ui(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_init(int /*<<< orphan*/);
int /*<<< orphan*/ mpz_invert(int /*<<< orphan*/, int /*<<< orphan*/,
                              int /*<<< orphan*/);
int /*<<< orphan*/ mpz_mul(int /*<<< orphan*/, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int /*<<< orphan*/ mpz_nextprime(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_out_raw(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_set_ui(int /*<<< orphan*/, int);
int /*<<< orphan*/ mpz_sub_ui(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ mpz_urandomb(int /*<<< orphan*/, int /*<<< orphan*/,
                                unsigned int);
void generate_keys(unsigned bits) {
  mpz_t p, q, n, pm1, qm1, t, e, d;
  gmp_randstate_t state;
  FILE *out;
  struct timeval tv;
  int i;
  mpz_init(p);
  mpz_init(q);
  mpz_init(n);
  mpz_init(pm1);
  mpz_init(qm1);
  mpz_init(t);
  mpz_init(e);
  mpz_init(d);
  gmp_randinit_mt(state);
  gettimeofday(&tv, NULL);
  gmp_randseed_ui(state, (tv.tv_usec ^ (tv.tv_sec << 20)) & 0xffffffff);
  do {
    mpz_urandomb(p, state, bits);
    mpz_nextprime(p, p);
    mpz_urandomb(q, state, bits);
    mpz_nextprime(q, q);
    mpz_mul(n, p, q);
    mpz_sub_ui(pm1, p, 1);
    mpz_sub_ui(qm1, q, 1);
    mpz_mul(t, pm1, qm1);
    mpz_set_ui(e, 65537);
    mpz_invert(d, e, t);
    if ((i = mpz_cmp_ui(d, 0)) < 0) {
      mpz_add(d, d, t);
    }
  } while (!i);
  out = fopen("gmp.pub", "w");
  mpz_out_raw(out, n);
  mpz_out_raw(out, e);
  fclose(out);
  out = fopen("gmp.prv", "w");
  mpz_out_raw(out, n);
  mpz_out_raw(out, d);
  fclose(out);
  gmp_randclear(state);
  mpz_clear(p);
  mpz_clear(q);
  mpz_clear(n);
  mpz_clear(pm1);
  mpz_clear(qm1);
  mpz_clear(t);
  mpz_clear(e);
  mpz_clear(d);
}


x86 assembly (clang, -O0):
<generate_keys>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x8(%rbp),%edi
call   1070 <mpz_init@plt>
mov    -0xc(%rbp),%edi
call   1070 <mpz_init@plt>
mov    -0x10(%rbp),%edi
call   1070 <mpz_init@plt>
mov    -0x14(%rbp),%edi
call   1070 <mpz_init@plt>
mov    -0x18(%rbp),%edi
call   1070 <mpz_init@plt>
mov    -0x1c(%rbp),%edi
call   1070 <mpz_init@plt>
mov    -0x20(%rbp),%edi
call   1070 <mpz_init@plt>
mov    -0x24(%rbp),%edi
call   1070 <mpz_init@plt>
mov    -0x28(%rbp),%edi
call   1130 <gmp_randinit_mt@plt>
lea    -0x38(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
mov    -0x28(%rbp),%edi
mov    -0x38(%rbp),%esi
mov    -0x34(%rbp),%eax
shl    $0x14,%eax
xor    %eax,%esi
and    $0xffffffff,%esi
call   1110 <gmp_randseed_ui@plt>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%esi
mov    -0x4(%rbp),%edx
call   1080 <mpz_urandomb@plt>
mov    -0x8(%rbp),%edi
mov    -0x8(%rbp),%esi
call   10c0 <mpz_nextprime@plt>
mov    -0xc(%rbp),%edi
mov    -0x28(%rbp),%esi
mov    -0x4(%rbp),%edx
call   1080 <mpz_urandomb@plt>
mov    -0xc(%rbp),%edi
mov    -0xc(%rbp),%esi
call   10c0 <mpz_nextprime@plt>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
call   10a0 <mpz_mul@plt>
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    $0x1,%edx
call   10b0 <mpz_sub_ui@plt>
mov    -0x18(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    $0x1,%edx
call   10b0 <mpz_sub_ui@plt>
mov    -0x1c(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   10a0 <mpz_mul@plt>
mov    -0x20(%rbp),%edi
mov    $0x10001,%esi
call   10e0 <mpz_set_ui@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1060 <mpz_invert@plt>
mov    -0x24(%rbp),%edi
xor    %esi,%esi
call   10d0 <mpz_cmp_ui@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jge    1333 <generate_keys+0x123>
mov    -0x24(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1090 <mpz_add@plt>
jmp    1338 <generate_keys+0x128>
cmpl   $0x0,-0x3c(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1286 <generate_keys+0x76>
lea    0xcb0(%rip),%rdi
lea    0xcb1(%rip),%rsi
call   1100 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1120 <mpz_out_raw@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1120 <mpz_out_raw@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xc82(%rip),%rdi
lea    0xc79(%rip),%rsi
call   1100 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1120 <mpz_out_raw@plt>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1120 <mpz_out_raw@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%edi
call   1030 <gmp_randclear@plt>
mov    -0x8(%rbp),%edi
call   10f0 <mpz_clear@plt>
mov    -0xc(%rbp),%edi
call   10f0 <mpz_clear@plt>
mov    -0x10(%rbp),%edi
call   10f0 <mpz_clear@plt>
mov    -0x14(%rbp),%edi
call   10f0 <mpz_clear@plt>
mov    -0x18(%rbp),%edi
call   10f0 <mpz_clear@plt>
mov    -0x1c(%rbp),%edi
call   10f0 <mpz_clear@plt>
mov    -0x20(%rbp),%edi
call   10f0 <mpz_clear@plt>
mov    -0x24(%rbp),%edi
call   10f0 <mpz_clear@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_keys>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
call   1070 <mpz_init@plt>
call   1070 <mpz_init@plt>
call   1070 <mpz_init@plt>
call   1070 <mpz_init@plt>
call   1070 <mpz_init@plt>
call   1070 <mpz_init@plt>
call   1070 <mpz_init@plt>
call   1070 <mpz_init@plt>
call   1130 <gmp_randinit_mt@plt>
mov    %rsp,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
mov    0x4(%rsp),%esi
shl    $0x14,%esi
xor    (%rsp),%esi
call   1110 <gmp_randseed_ui@plt>
nopl   0x0(%rax)
mov    %ebx,%edx
call   1080 <mpz_urandomb@plt>
call   10c0 <mpz_nextprime@plt>
mov    %ebx,%edx
call   1080 <mpz_urandomb@plt>
call   10c0 <mpz_nextprime@plt>
call   10a0 <mpz_mul@plt>
mov    $0x1,%edx
call   10b0 <mpz_sub_ui@plt>
mov    $0x1,%edx
call   10b0 <mpz_sub_ui@plt>
call   10a0 <mpz_mul@plt>
mov    $0x10001,%esi
call   10e0 <mpz_set_ui@plt>
call   1060 <mpz_invert@plt>
xor    %esi,%esi
call   10d0 <mpz_cmp_ui@plt>
test   %eax,%eax
js     12b4 <generate_keys+0xa4>
je     1260 <generate_keys+0x50>
jmp    12b9 <generate_keys+0xa9>
call   1090 <mpz_add@plt>
lea    0xd40(%rip),%rdi
lea    0xd41(%rip),%r14
mov    %r14,%rsi
call   1100 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1120 <mpz_out_raw@plt>
mov    %rbx,%rdi
call   1120 <mpz_out_raw@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xd19(%rip),%rdi
mov    %r14,%rsi
call   1100 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1120 <mpz_out_raw@plt>
mov    %rbx,%rdi
call   1120 <mpz_out_raw@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
call   1030 <gmp_randclear@plt>
call   10f0 <mpz_clear@plt>
call   10f0 <mpz_clear@plt>
call   10f0 <mpz_clear@plt>
call   10f0 <mpz_clear@plt>
call   10f0 <mpz_clear@plt>
call   10f0 <mpz_clear@plt>
call   10f0 <mpz_clear@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    10f0 <mpz_clear@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [8 x i8] c"gmp.pub\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [8 x i8] c"gmp.prv\00"
define void @generate_keys(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
%13 = alloca %struct.timeval
%14 = alloca i32
store i32 %0, i32* %2
%15 = load i32, i32* %3
%16 = call i32 @mpz_init(i32 %15)
%17 = load i32, i32* %4
%18 = call i32 @mpz_init(i32 %17)
%19 = load i32, i32* %5
%20 = call i32 @mpz_init(i32 %19)
%21 = load i32, i32* %6
%22 = call i32 @mpz_init(i32 %21)
%23 = load i32, i32* %7
%24 = call i32 @mpz_init(i32 %23)
%25 = load i32, i32* %8
%26 = call i32 @mpz_init(i32 %25)
%27 = load i32, i32* %9
%28 = call i32 @mpz_init(i32 %27)
%29 = load i32, i32* %10
%30 = call i32 @mpz_init(i32 %29)
%31 = load i32, i32* %11
%32 = call i32 @gmp_randinit_mt(i32 %31)
%33 = call i32 @gettimeofday(%struct.timeval* %13, i32* null)
%34 = load i32, i32* %11
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
%36 = load i32, i32* %35
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
%38 = load i32, i32* %37
%39 = shl i32 %38, 20
%40 = xor i32 %36, %39
%41 = and i32 %40, -1
%42 = call i32 @gmp_randseed_ui(i32 %34, i32 %41)
br label %43
43: ; preds = %87, %1
%44 = load i32, i32* %3
%45 = load i32, i32* %11
%46 = load i32, i32* %2
%47 = call i32 @mpz_urandomb(i32 %44, i32 %45, i32 %46)
%48 = load i32, i32* %3
%49 = load i32, i32* %3
%50 = call i32 @mpz_nextprime(i32 %48, i32 %49)
%51 = load i32, i32* %4
%52 = load i32, i32* %11
%53 = load i32, i32* %2
%54 = call i32 @mpz_urandomb(i32 %51, i32 %52, i32 %53)
%55 = load i32, i32* %4
%56 = load i32, i32* %4
%57 = call i32 @mpz_nextprime(i32 %55, i32 %56)
%58 = load i32, i32* %5
%59 = load i32, i32* %3
%60 = load i32, i32* %4
%61 = call i32 @mpz_mul(i32 %58, i32 %59, i32 %60)
%62 = load i32, i32* %6
%63 = load i32, i32* %3
%64 = call i32 @mpz_sub_ui(i32 %62, i32 %63, i32 1)
%65 = load i32, i32* %7
%66 = load i32, i32* %4
%67 = call i32 @mpz_sub_ui(i32 %65, i32 %66, i32 1)
%68 = load i32, i32* %8
%69 = load i32, i32* %6
%70 = load i32, i32* %7
%71 = call i32 @mpz_mul(i32 %68, i32 %69, i32 %70)
%72 = load i32, i32* %9
%73 = call i32 @mpz_set_ui(i32 %72, i32 65537)
%74 = load i32, i32* %10
%75 = load i32, i32* %9
%76 = load i32, i32* %8
%77 = call i32 @mpz_invert(i32 %74, i32 %75, i32 %76)
%78 = load i32, i32* %10
%79 = call i32 @mpz_cmp_ui(i32 %78, i32 0)
store i32 %79, i32* %14
%80 = icmp slt i32 %79, 0
br i1 %80, label %81, label %86
81: ; preds = %43
%82 = load i32, i32* %10
%83 = load i32, i32* %10
%84 = load i32, i32* %8
%85 = call i32 @mpz_add(i32 %82, i32 %83, i32 %84)
br label %86
86: ; preds = %81, %43
br label %87
87: ; preds = %86
%88 = load i32, i32* %14
%89 = icmp ne i32 %88, 0
%90 = xor i1 %89, true
br i1 %90, label %43, label %91
91: ; preds = %87
%92 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %92, i32** %12
%93 = load i32*, i32** %12
%94 = load i32, i32* %5
%95 = call i32 @mpz_out_raw(i32* %93, i32 %94)
%96 = load i32*, i32** %12
%97 = load i32, i32* %9
%98 = call i32 @mpz_out_raw(i32* %96, i32 %97)
%99 = load i32*, i32** %12
%100 = call i32 @fclose(i32* %99)
%101 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %101, i32** %12
%102 = load i32*, i32** %12
%103 = load i32, i32* %5
%104 = call i32 @mpz_out_raw(i32* %102, i32 %103)
%105 = load i32*, i32** %12
%106 = load i32, i32* %10
%107 = call i32 @mpz_out_raw(i32* %105, i32 %106)
%108 = load i32*, i32** %12
%109 = call i32 @fclose(i32* %108)
%110 = load i32, i32* %11
%111 = call i32 @gmp_randclear(i32 %110)
%112 = load i32, i32* %3
%113 = call i32 @mpz_clear(i32 %112)
%114 = load i32, i32* %4
%115 = call i32 @mpz_clear(i32 %114)
%116 = load i32, i32* %5
%117 = call i32 @mpz_clear(i32 %116)
%118 = load i32, i32* %6
%119 = call i32 @mpz_clear(i32 %118)
%120 = load i32, i32* %7
%121 = call i32 @mpz_clear(i32 %120)
%122 = load i32, i32* %8
%123 = call i32 @mpz_clear(i32 %122)
%124 = load i32, i32* %9
%125 = call i32 @mpz_clear(i32 %124)
%126 = load i32, i32* %10
%127 = call i32 @mpz_clear(i32 %126)
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str = [8 x i8] c"gmp.pub\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [8 x i8] c"gmp.prv\00"
define void @generate_keys(i32 %0) {
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %2 to i8*
%4 = tail call i32 @mpz_init(i32 undef)
%5 = tail call i32 @mpz_init(i32 undef)
%6 = tail call i32 @mpz_init(i32 undef)
%7 = tail call i32 @mpz_init(i32 undef)
%8 = tail call i32 @mpz_init(i32 undef)
%9 = tail call i32 @mpz_init(i32 undef)
%10 = tail call i32 @mpz_init(i32 undef)
%11 = tail call i32 @mpz_init(i32 undef)
%12 = tail call i32 @gmp_randinit_mt(i32 undef)
%13 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%17 = load i32, i32* %16
%18 = shl i32 %17, 20
%19 = xor i32 %18, %15
%20 = tail call i32 @gmp_randseed_ui(i32 undef, i32 %19)
br label %21
21: ; preds = %36, %1
%22 = tail call i32 @mpz_urandomb(i32 undef, i32 undef, i32 %0)
%23 = tail call i32 @mpz_nextprime(i32 undef, i32 undef)
%24 = tail call i32 @mpz_urandomb(i32 undef, i32 undef, i32 %0)
%25 = tail call i32 @mpz_nextprime(i32 undef, i32 undef)
%26 = tail call i32 @mpz_mul(i32 undef, i32 undef, i32 undef)
%27 = tail call i32 @mpz_sub_ui(i32 undef, i32 undef, i32 1)
%28 = tail call i32 @mpz_sub_ui(i32 undef, i32 undef, i32 1)
%29 = tail call i32 @mpz_mul(i32 undef, i32 undef, i32 undef)
%30 = tail call i32 @mpz_set_ui(i32 undef, i32 65537)
%31 = tail call i32 @mpz_invert(i32 undef, i32 undef, i32 undef)
%32 = tail call i32 @mpz_cmp_ui(i32 undef, i32 0)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %21
%35 = tail call i32 @mpz_add(i32 undef, i32 undef, i32 undef)
br label %38
36: ; preds = %21
%37 = icmp eq i32 %32, 0
br i1 %37, label %21, label %38
38: ; preds = %36, %34
%39 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%40 = tail call i32 @mpz_out_raw(i32* %39, i32 undef)
%41 = tail call i32 @mpz_out_raw(i32* %39, i32 undef)
%42 = tail call i32 @fclose(i32* %39)
%43 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%44 = tail call i32 @mpz_out_raw(i32* %43, i32 undef)
%45 = tail call i32 @mpz_out_raw(i32* %43, i32 undef)
%46 = tail call i32 @fclose(i32* %43)
%47 = tail call i32 @gmp_randclear(i32 undef)
%48 = tail call i32 @mpz_clear(i32 undef)
%49 = tail call i32 @mpz_clear(i32 undef)
%50 = tail call i32 @mpz_clear(i32 undef)
%51 = tail call i32 @mpz_clear(i32 undef)
%52 = tail call i32 @mpz_clear(i32 undef)
%53 = tail call i32 @mpz_clear(i32 undef)
%54 = tail call i32 @mpz_clear(i32 undef)
%55 = tail call i32 @mpz_clear(i32 undef)
ret void
}