#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*/ gmp_randstate_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ gmp_randclear(int /*<<< orphan*/);
int /*<<< orphan*/ gmp_randinit_mt(int /*<<< orphan*/);
int /*<<< orphan*/ gmp_randseed_ui(int /*<<< orphan*/, unsigned 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, unsigned seed) {
  mpz_t p, q, n, pm1, qm1, t, e, d;
  gmp_randstate_t state;
  FILE *out;
  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);
  gmp_randseed_ui(state, seed);
  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    %esi,-0x8(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <mpz_init@plt>
mov    -0x10(%rbp),%edi
call   1060 <mpz_init@plt>
mov    -0x14(%rbp),%edi
call   1060 <mpz_init@plt>
mov    -0x18(%rbp),%edi
call   1060 <mpz_init@plt>
mov    -0x1c(%rbp),%edi
call   1060 <mpz_init@plt>
mov    -0x20(%rbp),%edi
call   1060 <mpz_init@plt>
mov    -0x24(%rbp),%edi
call   1060 <mpz_init@plt>
mov    -0x28(%rbp),%edi
call   1060 <mpz_init@plt>
mov    -0x2c(%rbp),%edi
call   1120 <gmp_randinit_mt@plt>
mov    -0x2c(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1100 <gmp_randseed_ui@plt>
mov    -0xc(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    -0x4(%rbp),%edx
call   1070 <mpz_urandomb@plt>
mov    -0xc(%rbp),%edi
mov    -0xc(%rbp),%esi
call   10b0 <mpz_nextprime@plt>
mov    -0x10(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    -0x4(%rbp),%edx
call   1070 <mpz_urandomb@plt>
mov    -0x10(%rbp),%edi
mov    -0x10(%rbp),%esi
call   10b0 <mpz_nextprime@plt>
mov    -0x14(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1090 <mpz_mul@plt>
mov    -0x18(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    $0x1,%edx
call   10a0 <mpz_sub_ui@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    $0x1,%edx
call   10a0 <mpz_sub_ui@plt>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1090 <mpz_mul@plt>
mov    -0x24(%rbp),%edi
mov    $0x10001,%esi
call   10d0 <mpz_set_ui@plt>
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%edx
call   1050 <mpz_invert@plt>
mov    -0x28(%rbp),%edi
xor    %esi,%esi
call   10c0 <mpz_cmp_ui@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jge    130e <generate_keys+0x10e>
mov    -0x28(%rbp),%edi
mov    -0x28(%rbp),%esi
mov    -0x20(%rbp),%edx
call   1080 <mpz_add@plt>
jmp    1313 <generate_keys+0x113>
cmpl   $0x0,-0x3c(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1261 <generate_keys+0x61>
lea    0xcd5(%rip),%rdi
lea    0xcd6(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1110 <mpz_out_raw@plt>
mov    -0x38(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1110 <mpz_out_raw@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xca7(%rip),%rdi
lea    0xc9e(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1110 <mpz_out_raw@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1110 <mpz_out_raw@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x2c(%rbp),%edi
call   1030 <gmp_randclear@plt>
mov    -0xc(%rbp),%edi
call   10e0 <mpz_clear@plt>
mov    -0x10(%rbp),%edi
call   10e0 <mpz_clear@plt>
mov    -0x14(%rbp),%edi
call   10e0 <mpz_clear@plt>
mov    -0x18(%rbp),%edi
call   10e0 <mpz_clear@plt>
mov    -0x1c(%rbp),%edi
call   10e0 <mpz_clear@plt>
mov    -0x20(%rbp),%edi
call   10e0 <mpz_clear@plt>
mov    -0x24(%rbp),%edi
call   10e0 <mpz_clear@plt>
mov    -0x28(%rbp),%edi
call   10e0 <mpz_clear@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_keys>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
call   1060 <mpz_init@plt>
call   1060 <mpz_init@plt>
call   1060 <mpz_init@plt>
call   1060 <mpz_init@plt>
call   1060 <mpz_init@plt>
call   1060 <mpz_init@plt>
call   1060 <mpz_init@plt>
call   1060 <mpz_init@plt>
call   1120 <gmp_randinit_mt@plt>
mov    %ebp,%esi
call   1100 <gmp_randseed_ui@plt>
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edx
call   1070 <mpz_urandomb@plt>
call   10b0 <mpz_nextprime@plt>
mov    %ebx,%edx
call   1070 <mpz_urandomb@plt>
call   10b0 <mpz_nextprime@plt>
call   1090 <mpz_mul@plt>
mov    $0x1,%edx
call   10a0 <mpz_sub_ui@plt>
mov    $0x1,%edx
call   10a0 <mpz_sub_ui@plt>
call   1090 <mpz_mul@plt>
mov    $0x10001,%esi
call   10d0 <mpz_set_ui@plt>
call   1050 <mpz_invert@plt>
xor    %esi,%esi
call   10c0 <mpz_cmp_ui@plt>
test   %eax,%eax
js     1294 <generate_keys+0x94>
je     1240 <generate_keys+0x40>
jmp    1299 <generate_keys+0x99>
call   1080 <mpz_add@plt>
lea    0xd60(%rip),%rdi
lea    0xd61(%rip),%rbx
mov    %rbx,%rsi
call   10f0 <fopen@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1110 <mpz_out_raw@plt>
mov    %rbp,%rdi
call   1110 <mpz_out_raw@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
lea    0xd39(%rip),%rdi
mov    %rbx,%rsi
call   10f0 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1110 <mpz_out_raw@plt>
mov    %rbx,%rdi
call   1110 <mpz_out_raw@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
call   1030 <gmp_randclear@plt>
call   10e0 <mpz_clear@plt>
call   10e0 <mpz_clear@plt>
call   10e0 <mpz_clear@plt>
call   10e0 <mpz_clear@plt>
call   10e0 <mpz_clear@plt>
call   10e0 <mpz_clear@plt>
call   10e0 <mpz_clear@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    10e0 <mpz_clear@plt>

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

LLVM IR (-O2):
@.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, i32 %1) {
%3 = tail call i32 @mpz_init(i32 undef)
%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 @gmp_randinit_mt(i32 undef)
%12 = tail call i32 @gmp_randseed_ui(i32 undef, i32 %1)
br label %13
13: ; preds = %28, %2
%14 = tail call i32 @mpz_urandomb(i32 undef, i32 undef, i32 %0)
%15 = tail call i32 @mpz_nextprime(i32 undef, i32 undef)
%16 = tail call i32 @mpz_urandomb(i32 undef, i32 undef, i32 %0)
%17 = tail call i32 @mpz_nextprime(i32 undef, i32 undef)
%18 = tail call i32 @mpz_mul(i32 undef, i32 undef, i32 undef)
%19 = tail call i32 @mpz_sub_ui(i32 undef, i32 undef, i32 1)
%20 = tail call i32 @mpz_sub_ui(i32 undef, i32 undef, i32 1)
%21 = tail call i32 @mpz_mul(i32 undef, i32 undef, i32 undef)
%22 = tail call i32 @mpz_set_ui(i32 undef, i32 65537)
%23 = tail call i32 @mpz_invert(i32 undef, i32 undef, i32 undef)
%24 = tail call i32 @mpz_cmp_ui(i32 undef, i32 0)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %13
%27 = tail call i32 @mpz_add(i32 undef, i32 undef, i32 undef)
br label %30
28: ; preds = %13
%29 = icmp eq i32 %24, 0
br i1 %29, label %13, label %30
30: ; preds = %28, %26
%31 = 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))
%32 = tail call i32 @mpz_out_raw(i32* %31, i32 undef)
%33 = tail call i32 @mpz_out_raw(i32* %31, i32 undef)
%34 = tail call i32 @fclose(i32* %31)
%35 = 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))
%36 = tail call i32 @mpz_out_raw(i32* %35, i32 undef)
%37 = tail call i32 @mpz_out_raw(i32* %35, i32 undef)
%38 = tail call i32 @fclose(i32* %35)
%39 = tail call i32 @gmp_randclear(i32 undef)
%40 = tail call i32 @mpz_clear(i32 undef)
%41 = tail call i32 @mpz_clear(i32 undef)
%42 = tail call i32 @mpz_clear(i32 undef)
%43 = tail call i32 @mpz_clear(i32 undef)
%44 = tail call i32 @mpz_clear(i32 undef)
%45 = tail call i32 @mpz_clear(i32 undef)
%46 = tail call i32 @mpz_clear(i32 undef)
%47 = tail call i32 @mpz_clear(i32 undef)
ret void
}