#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;

/* Variables and functions */
int /*<<< orphan*/ find_fi(int /*<<< orphan*/, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int /*<<< orphan*/ gmp_printf(char *, int /*<<< orphan*/, int /*<<< orphan*/,
                              int /*<<< orphan*/);
int /*<<< orphan*/ mpz_add_ui(int /*<<< orphan*/, int /*<<< orphan*/, int);
scalar_t__ mpz_cmp_ui(int /*<<< orphan*/, int);
int /*<<< orphan*/ mpz_gcd(int /*<<< orphan*/, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int /*<<< orphan*/ mpz_init(int /*<<< orphan*/);
int /*<<< orphan*/ mpz_invert(int /*<<< orphan*/, int /*<<< orphan*/,
                              int /*<<< orphan*/);
int /*<<< orphan*/ mpz_set_ui(int /*<<< orphan*/, int);
void findED(mpz_t n, mpz_t p, mpz_t q, mpz_t e, mpz_t d) {
  mpz_t fi, bound, mcd;
  mpz_init(fi);
  mpz_init(bound);
  mpz_init(mcd);
  mpz_set_ui(e, 65536);
  find_fi(fi, p, q);
  do {
    mpz_add_ui(e, e, 1);
    mpz_gcd(mcd, e, fi);
  } while (mpz_cmp_ui(mcd, 1) != 0);
  mpz_invert(d, e, fi);
  gmp_printf("d: %Zd\ne: %Zd\nmcd(e,(p-1)*(q-1)): %Zd\n", d, e, mcd);
}


x86 assembly (clang, -O0):
<findED>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    %r8d,-0x14(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x1c(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x20(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x10(%rbp),%edi
mov    $0x10000,%esi
call   1070 <mpz_set_ui@plt>
mov    -0x18(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
call   10a0 <find_fi@plt>
mov    -0x10(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    $0x1,%edx
call   1030 <mpz_add_ui@plt>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1080 <mpz_gcd@plt>
mov    -0x20(%rbp),%edi
mov    $0x1,%esi
call   1060 <mpz_cmp_ui@plt>
cmp    $0x0,%rax
jne    11cb <findED+0x4b>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1040 <mpz_invert@plt>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xde2(%rip),%rdi
call   1090 <gmp_printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findED>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %r8d,%r14d
mov    %ecx,%ebx
mov    %edx,%r15d
mov    %esi,%ebp
call   1050 <mpz_init@plt>
call   1050 <mpz_init@plt>
call   1050 <mpz_init@plt>
mov    %ebx,%edi
mov    $0x10000,%esi
call   1070 <mpz_set_ui@plt>
mov    %ebp,%esi
mov    %r15d,%edx
call   10a0 <find_fi@plt>
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %ebx,%esi
mov    $0x1,%edx
call   1030 <mpz_add_ui@plt>
mov    %ebx,%esi
call   1080 <mpz_gcd@plt>
mov    $0x1,%esi
call   1060 <mpz_cmp_ui@plt>
test   %rax,%rax
jne    11c0 <findED+0x40>
mov    %r14d,%edi
mov    %ebx,%esi
call   1040 <mpz_invert@plt>
lea    0xe0b(%rip),%rdi
mov    %r14d,%esi
mov    %ebx,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <gmp_printf@plt>

LLVM IR (-O0):
@.str = [39 x i8] c"d: %Zd\0Ae: %Zd\0Amcd(e,(p-1)*(q-1)): %Zd\0A\00"
define void @findED(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%14 = load i32, i32* %11
%15 = call i32 @mpz_init(i32 %14)
%16 = load i32, i32* %12
%17 = call i32 @mpz_init(i32 %16)
%18 = load i32, i32* %13
%19 = call i32 @mpz_init(i32 %18)
%20 = load i32, i32* %9
%21 = call i32 @mpz_set_ui(i32 %20, i32 65536)
%22 = load i32, i32* %11
%23 = load i32, i32* %7
%24 = load i32, i32* %8
%25 = call i32 @find_fi(i32 %22, i32 %23, i32 %24)
br label %26
26: ; preds = %34, %5
%27 = load i32, i32* %9
%28 = load i32, i32* %9
%29 = call i32 @mpz_add_ui(i32 %27, i32 %28, i32 1)
%30 = load i32, i32* %13
%31 = load i32, i32* %9
%32 = load i32, i32* %11
%33 = call i32 @mpz_gcd(i32 %30, i32 %31, i32 %32)
br label %34
34: ; preds = %26
%35 = load i32, i32* %13
%36 = call i64 @mpz_cmp_ui(i32 %35, i32 1)
%37 = icmp ne i64 %36, 0
br i1 %37, label %26, label %38
38: ; preds = %34
%39 = load i32, i32* %10
%40 = load i32, i32* %9
%41 = load i32, i32* %11
%42 = call i32 @mpz_invert(i32 %39, i32 %40, i32 %41)
%43 = load i32, i32* %10
%44 = load i32, i32* %9
%45 = load i32, i32* %13
%46 = call i32 @gmp_printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %43, i32 %44, i32 %45)
ret void
}

LLVM IR (-O2):
@.str = [39 x i8] c"d: %Zd\0Ae: %Zd\0Amcd(e,(p-1)*(q-1)): %Zd\0A\00"
define void @findED(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%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_set_ui(i32 %3, i32 65536)
%10 = tail call i32 @find_fi(i32 undef, i32 %1, i32 %2)
br label %11
11: ; preds = %11, %5
%12 = tail call i32 @mpz_add_ui(i32 %3, i32 %3, i32 1)
%13 = tail call i32 @mpz_gcd(i32 undef, i32 %3, i32 undef)
%14 = tail call i64 @mpz_cmp_ui(i32 undef, i32 1)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %11
16: ; preds = %11
%17 = tail call i32 @mpz_invert(i32 %4, i32 %3, i32 undef)
%18 = tail call i32 @gmp_printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %4, i32 %3, i32 undef)
ret void
}