#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*/ BN_CTX;
typedef int /*<<< orphan*/ BIGNUM;

/* Variables and functions */
int /*<<< orphan*/ BN_CTX_end(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_CTX_get(int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_CTX_start(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_mod_inverse(int /*<<< orphan*/ *,
                                   int /*<<< orphan*/ const *,
                                   int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_mul(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_sub(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                          int /*<<< orphan*/);
int /*<<< orphan*/ BN_value_one();
BIGNUM *rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p, const BIGNUM *q,
                           BN_CTX *ctx) {
  BIGNUM *ret = NULL, *r0, *r1, *r2;
  if (d == NULL || p == NULL || q == NULL)
    return NULL;
  BN_CTX_start(ctx);
  r0 = BN_CTX_get(ctx);
  r1 = BN_CTX_get(ctx);
  r2 = BN_CTX_get(ctx);
  if (r2 == NULL)
    goto err;
  if (!BN_sub(r1, p, BN_value_one()))
    goto err;
  if (!BN_sub(r2, q, BN_value_one()))
    goto err;
  if (!BN_mul(r0, r1, r2, ctx))
    goto err;
  ret = BN_mod_inverse(NULL, d, r0, ctx);
err:
  BN_CTX_end(ctx);
  return ret;
}


x86 assembly (clang, -O0):
<rsa_get_public_exp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b1 <rsa_get_public_exp+0x41>
cmpq   $0x0,-0x18(%rbp)
je     11b1 <rsa_get_public_exp+0x41>
cmpq   $0x0,-0x20(%rbp)
jne    11be <rsa_get_public_exp+0x4e>
movq   $0x0,-0x8(%rbp)
jmp    12b3 <rsa_get_public_exp+0x143>
mov    -0x28(%rbp),%rdi
call   1090 <BN_CTX_start@plt>
mov    -0x28(%rbp),%rdi
call   1040 <BN_CTX_get@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <BN_CTX_get@plt>
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <BN_CTX_get@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    11fe <rsa_get_public_exp+0x8e>
jmp    12a2 <rsa_get_public_exp+0x132>
mov    -0x40(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    $0x0,%al
call   1030 <BN_value_one@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1060 <BN_sub@plt>
cmp    $0x0,%eax
jne    1232 <rsa_get_public_exp+0xc2>
jmp    12a2 <rsa_get_public_exp+0x132>
mov    -0x48(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    $0x0,%al
call   1030 <BN_value_one@plt>
mov    -0x68(%rbp),%rdi
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
call   1060 <BN_sub@plt>
cmp    $0x0,%eax
jne    1266 <rsa_get_public_exp+0xf6>
jmp    12a2 <rsa_get_public_exp+0x132>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1070 <BN_mul@plt>
cmp    $0x0,%eax
jne    1289 <rsa_get_public_exp+0x119>
jmp    12a2 <rsa_get_public_exp+0x132>
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x28(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edi
call   1050 <BN_mod_inverse@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <BN_CTX_end@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rsa_get_public_exp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
xor    %ebx,%ebx
test   %rdi,%rdi
je     1250 <rsa_get_public_exp+0xe0>
mov    %rsi,%r12
test   %rsi,%rsi
je     1250 <rsa_get_public_exp+0xe0>
mov    %rdx,%r13
test   %rdx,%rdx
je     1250 <rsa_get_public_exp+0xe0>
mov    %rcx,%r15
mov    %rdi,0x10(%rsp)
mov    %rcx,%rdi
call   1090 <BN_CTX_start@plt>
mov    %r15,%rdi
call   1040 <BN_CTX_get@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1040 <BN_CTX_get@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1040 <BN_CTX_get@plt>
test   %rax,%rax
je     1246 <rsa_get_public_exp+0xd6>
mov    %rax,%rbp
mov    %rbx,0x8(%rsp)
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <BN_value_one@plt>
mov    %r14,(%rsp)
mov    %r14,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1060 <BN_sub@plt>
test   %eax,%eax
je     1248 <rsa_get_public_exp+0xd8>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <BN_value_one@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %eax,%edx
call   1060 <BN_sub@plt>
test   %eax,%eax
je     1248 <rsa_get_public_exp+0xd8>
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    (%rsp),%rsi
mov    %rbp,%rdx
mov    %r15,%rcx
call   1070 <BN_mul@plt>
test   %eax,%eax
je     1246 <rsa_get_public_exp+0xd6>
xor    %edi,%edi
mov    0x10(%rsp),%rsi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1050 <BN_mod_inverse@plt>
mov    %rax,%rbx
jmp    1248 <rsa_get_public_exp+0xd8>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1080 <BN_CTX_end@plt>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32* @rsa_get_public_exp(i32* %0, i32* %1, i32* %2, i32* %3) {
%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*
store i32* %0, i32** %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store i32* null, i32** %10
%14 = load i32*, i32** %6
%15 = icmp eq i32* %14, null
br i1 %15, label %22, label %16
16: ; preds = %4
%17 = load i32*, i32** %7
%18 = icmp eq i32* %17, null
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = load i32*, i32** %8
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %23
22: ; preds = %19, %16, %4
store i32* null, i32** %5
br label %66
23: ; preds = %19
%24 = load i32*, i32** %9
%25 = call i32 @BN_CTX_start(i32* %24)
%26 = load i32*, i32** %9
%27 = call i32* @BN_CTX_get(i32* %26)
store i32* %27, i32** %11
%28 = load i32*, i32** %9
%29 = call i32* @BN_CTX_get(i32* %28)
store i32* %29, i32** %12
%30 = load i32*, i32** %9
%31 = call i32* @BN_CTX_get(i32* %30)
store i32* %31, i32** %13
%32 = load i32*, i32** %13
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %35
34: ; preds = %23
br label %62
35: ; preds = %23
%36 = load i32*, i32** %12
%37 = load i32*, i32** %7
%38 = call i32 (...) @BN_value_one()
%39 = call i32 @BN_sub(i32* %36, i32* %37, i32 %38)
%40 = icmp ne i32 %39, 0
br i1 %40, label %42, label %41
41: ; preds = %35
br label %62
42: ; preds = %35
%43 = load i32*, i32** %13
%44 = load i32*, i32** %8
%45 = call i32 (...) @BN_value_one()
%46 = call i32 @BN_sub(i32* %43, i32* %44, i32 %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %49, label %48
48: ; preds = %42
br label %62
49: ; preds = %42
%50 = load i32*, i32** %11
%51 = load i32*, i32** %12
%52 = load i32*, i32** %13
%53 = load i32*, i32** %9
%54 = call i32 @BN_mul(i32* %50, i32* %51, i32* %52, i32* %53)
%55 = icmp ne i32 %54, 0
br i1 %55, label %57, label %56
56: ; preds = %49
br label %62
57: ; preds = %49
%58 = load i32*, i32** %6
%59 = load i32*, i32** %11
%60 = load i32*, i32** %9
%61 = call i32* @BN_mod_inverse(i32* null, i32* %58, i32* %59, i32* %60)
store i32* %61, i32** %10
br label %62
62: ; preds = %57, %56, %48, %41, %34
%63 = load i32*, i32** %9
%64 = call i32 @BN_CTX_end(i32* %63)
%65 = load i32*, i32** %10
store i32* %65, i32** %5
br label %66
66: ; preds = %62, %22
%67 = load i32*, i32** %5
ret i32* %67
}

LLVM IR (-O2):
define i32* @rsa_get_public_exp(i32* %0, i32* %1, i32* %2, i32* %3) {
%5 = icmp eq i32* %0, null
%6 = icmp eq i32* %1, null
%7 = or i1 %5, %6
%8 = icmp eq i32* %2, null
%9 = or i1 %7, %8
br i1 %9, label %32, label %10
10: ; preds = %4
%11 = tail call i32 @BN_CTX_start(i32* %3)
%12 = tail call i32* @BN_CTX_get(i32* %3)
%13 = tail call i32* @BN_CTX_get(i32* %3)
%14 = tail call i32* @BN_CTX_get(i32* %3)
%15 = icmp eq i32* %14, null
br i1 %15, label %29, label %16
16: ; preds = %10
%17 = tail call i32 (...) @BN_value_one()
%18 = tail call i32 @BN_sub(i32* %13, i32* %1, i32 %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %29, label %20
20: ; preds = %16
%21 = tail call i32 (...) @BN_value_one()
%22 = tail call i32 @BN_sub(i32* %14, i32* %2, i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %20
%25 = tail call i32 @BN_mul(i32* %12, i32* %13, i32* %14, i32* %3)
%26 = icmp eq i32 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = tail call i32* @BN_mod_inverse(i32* null, i32* %0, i32* %12, i32* %3)
br label %29
29: ; preds = %24, %20, %16, %10, %27
%30 = phi i32* [ null, %10 ], [ %28, %27 ], [ null, %24 ], [ null, %20 ], [ null, %16 ]
%31 = tail call i32 @BN_CTX_end(i32* %3)
br label %32
32: ; preds = %4, %29
%33 = phi i32* [ %30, %29 ], [ null, %4 ]
ret i32* %33
}