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

/* Variables and functions */
int /*<<< orphan*/ eea(int, int, int *, long long *, long long *);
int /*<<< orphan*/ lehmann(int);
int rand();
void generateKeys(uint64_t *pub, uint64_t *priv, uint64_t *n) {
  uint64_t p, q, phiN;
  do {
    p = (rand() % (10000 - 1000)) + 1000;
  } while (!lehmann(p));
  do {
    q = (rand() % (10000 - 1000)) + 1000;
  } while (!lehmann(q));
  *n = p * q;
  phiN = (p - 1) * (q - 1);
  uint64_t e, gcd, d;
  long long int xi, yi;
  do {
    e = rand() % (phiN - 3) + 2;
    eea(e, phiN, &gcd, &xi, &yi);
  } while (gcd != 1);
  d = (uint64_t)((xi + phiN) % phiN);
  *pub = e;
  *priv = d;
}


x86 assembly (clang, -O0):
<generateKeys>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0x2328,%ecx
cltd
idiv   %ecx
add    $0x3e8,%edx
mov    %edx,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <lehmann@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1144 <generateKeys+0x14>
jmp    1179 <generateKeys+0x49>
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0x2328,%ecx
cltd
idiv   %ecx
add    $0x3e8,%edx
mov    %edx,-0x20(%rbp)
mov    -0x20(%rbp),%edi
call   1030 <lehmann@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1179 <generateKeys+0x49>
mov    -0x1c(%rbp),%ecx
imul   -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
imul   %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
mov    -0x24(%rbp),%ecx
sub    $0x3,%ecx
cltd
idiv   %ecx
add    $0x2,%edx
mov    %edx,-0x28(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%esi
lea    -0x2c(%rbp),%rdx
lea    -0x38(%rbp),%rcx
lea    -0x40(%rbp),%r8
call   1050 <eea@plt>
cmpl   $0x1,-0x2c(%rbp)
jne    11c8 <generateKeys+0x98>
mov    -0x38(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
movslq -0x24(%rbp),%rcx
cqto
idiv   %rcx
mov    %edx,%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generateKeys>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r15
mov    %rsi,0x10(%rsp)
mov    %rdi,0x8(%rsp)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <rand@plt>
mov    %eax,%r13d
cltq
imul   $0x7482296b,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2c,%rax
add    %ecx,%eax
imul   $0x2328,%eax,%ebx
mov    %ebx,%eax
neg    %eax
lea    (%rax,%r13,1),%r14d
add    $0x3e8,%r14d
mov    %r14d,%edi
call   1030 <lehmann@plt>
test   %eax,%eax
je     1150 <generateKeys+0x20>
sub    %ebx,%r13d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1040 <rand@plt>
mov    %eax,%r12d
cltq
imul   $0x7482296b,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2c,%rax
add    %ecx,%eax
imul   $0x2328,%eax,%ebx
mov    %ebx,%eax
neg    %eax
lea    (%r12,%rax,1),%ebp
add    $0x3e8,%ebp
mov    %ebp,%edi
call   1030 <lehmann@plt>
test   %eax,%eax
je     11a0 <generateKeys+0x70>
sub    %ebx,%r12d
imul   %r14d,%ebp
mov    %ebp,(%r15)
add    $0x3e7,%r13d
add    $0x3e7,%r12d
imul   %r13d,%r12d
lea    -0x3(%r12),%r15d
lea    0x4(%rsp),%rbx
lea    0x18(%rsp),%r13
lea    0x20(%rsp),%r14
nop
xor    %eax,%eax
call   1040 <rand@plt>
cltd
idiv   %r15d
mov    %edx,%ebp
add    $0x2,%ebp
mov    %ebp,%edi
mov    %r12d,%esi
mov    %rbx,%rdx
mov    %r13,%rcx
mov    %r14,%r8
call   1050 <eea@plt>
cmpl   $0x1,0x4(%rsp)
jne    1210 <generateKeys+0xe0>
movslq %r12d,%rcx
mov    0x18(%rsp),%rax
add    %rcx,%rax
mov    %rax,%rdx
or     %rcx,%rdx
shr    $0x20,%rdx
je     1258 <generateKeys+0x128>
cqto
idiv   %rcx
jmp    125d <generateKeys+0x12d>
xor    %edx,%edx
div    %r12d
mov    0x8(%rsp),%rax
mov    %ebp,(%rax)
mov    0x10(%rsp),%rax
mov    %edx,(%rax)
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @generateKeys(i32* %0, i32* %1, i32* %2) {
%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 i64
%14 = alloca i64
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
br label %15
15: ; preds = %19, %3
%16 = call i32 (...) @rand()
%17 = srem i32 %16, 9000
%18 = add nsw i32 %17, 1000
store i32 %18, i32* %7
br label %19
19: ; preds = %15
%20 = load i32, i32* %7
%21 = call i32 @lehmann(i32 %20)
%22 = icmp ne i32 %21, 0
%23 = xor i1 %22, true
br i1 %23, label %15, label %24
24: ; preds = %19
br label %25
25: ; preds = %29, %24
%26 = call i32 (...) @rand()
%27 = srem i32 %26, 9000
%28 = add nsw i32 %27, 1000
store i32 %28, i32* %8
br label %29
29: ; preds = %25
%30 = load i32, i32* %8
%31 = call i32 @lehmann(i32 %30)
%32 = icmp ne i32 %31, 0
%33 = xor i1 %32, true
br i1 %33, label %25, label %34
34: ; preds = %29
%35 = load i32, i32* %7
%36 = load i32, i32* %8
%37 = mul nsw i32 %35, %36
%38 = load i32*, i32** %6
store i32 %37, i32* %38
%39 = load i32, i32* %7
%40 = sub nsw i32 %39, 1
%41 = load i32, i32* %8
%42 = sub nsw i32 %41, 1
%43 = mul nsw i32 %40, %42
store i32 %43, i32* %9
br label %44
44: ; preds = %53, %34
%45 = call i32 (...) @rand()
%46 = load i32, i32* %9
%47 = sub nsw i32 %46, 3
%48 = srem i32 %45, %47
%49 = add nsw i32 %48, 2
store i32 %49, i32* %10
%50 = load i32, i32* %10
%51 = load i32, i32* %9
%52 = call i32 @eea(i32 %50, i32 %51, i32* %11, i64* %13, i64* %14)
br label %53
53: ; preds = %44
%54 = load i32, i32* %11
%55 = icmp ne i32 %54, 1
br i1 %55, label %44, label %56
56: ; preds = %53
%57 = load i64, i64* %13
%58 = load i32, i32* %9
%59 = sext i32 %58 to i64
%60 = add nsw i64 %57, %59
%61 = load i32, i32* %9
%62 = sext i32 %61 to i64
%63 = srem i64 %60, %62
%64 = trunc i64 %63 to i32
store i32 %64, i32* %12
%65 = load i32, i32* %10
%66 = load i32*, i32** %4
store i32 %65, i32* %66
%67 = load i32, i32* %12
%68 = load i32*, i32** %5
store i32 %67, i32* %68
ret void
}

LLVM IR (-O2):
define void @generateKeys(i32* nocapture writeonly %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
br label %7
7: ; preds = %7, %3
%8 = tail call i32 (...) @rand()
%9 = srem i32 %8, 9000
%10 = add nsw i32 %9, 1000
%11 = tail call i32 @lehmann(i32 %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %7, label %13
13: ; preds = %7, %13
%14 = tail call i32 (...) @rand()
%15 = srem i32 %14, 9000
%16 = add nsw i32 %15, 1000
%17 = tail call i32 @lehmann(i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %13, label %19
19: ; preds = %13
%20 = mul nsw i32 %16, %10
store i32 %20, i32* %2
%21 = add nsw i32 %9, 999
%22 = add nsw i32 %15, 999
%23 = mul nsw i32 %22, %21
%24 = bitcast i32* %4 to i8*
%25 = bitcast i64* %5 to i8*
%26 = bitcast i64* %6 to i8*
%27 = add nsw i32 %23, -3
br label %28
28: ; preds = %28, %19
%29 = call i32 (...) @rand()
%30 = srem i32 %29, %27
%31 = add nsw i32 %30, 2
%32 = call i32 @eea(i32 %31, i32 %23, i32* %4, i64* %5, i64* %6)
%33 = load i32, i32* %4
%34 = icmp eq i32 %33, 1
br i1 %34, label %35, label %28
35: ; preds = %28
%36 = load i64, i64* %5
%37 = sext i32 %23 to i64
%38 = add nsw i64 %36, %37
%39 = srem i64 %38, %37
%40 = trunc i64 %39 to i32
store i32 %31, i32* %0
store i32 %40, i32* %1
ret void
}