#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*/ BIGNUM;

/* Variables and functions */
int /*<<< orphan*/ bn2gmp(int /*<<< orphan*/ const *, int /*<<< orphan*/);
scalar_t__ gmp2bn(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ mpz_clear(int /*<<< orphan*/);
int /*<<< orphan*/ mpz_init(int /*<<< orphan*/);
int /*<<< orphan*/ mpz_powm(int /*<<< orphan*/, int /*<<< orphan*/,
                            int /*<<< orphan*/, int /*<<< orphan*/);
int deadcafe_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                        const BIGNUM *m) {
  mpz_t rop;
  mpz_t base;
  mpz_t exp;
  mpz_t mod;
  int ret = 0;
  mpz_init(rop);
  mpz_init(base);
  mpz_init(exp);
  mpz_init(mod);
  if (!bn2gmp(a, base))
    goto end;
  if (!bn2gmp(p, exp))
    goto end;
  if (!bn2gmp(m, mod))
    goto end;
  mpz_powm(rop, base, exp, mod);
  if (gmp2bn(rop, r))
    ret = 1;
end:
  mpz_clear(rop);
  mpz_clear(base);
  mpz_clear(exp);
  mpz_clear(mod);
  return ret;
}


x86 assembly (clang, -O0):
<deadcafe_bn_mod_exp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <mpz_init@plt>
mov    -0x28(%rbp),%edi
call   1040 <mpz_init@plt>
mov    -0x2c(%rbp),%edi
call   1040 <mpz_init@plt>
mov    -0x30(%rbp),%edi
call   1040 <mpz_init@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1070 <bn2gmp@plt>
cmp    $0x0,%eax
jne    11a9 <deadcafe_bn_mod_exp+0x59>
jmp    1210 <deadcafe_bn_mod_exp+0xc0>
mov    -0x18(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1070 <bn2gmp@plt>
cmp    $0x0,%eax
jne    11c3 <deadcafe_bn_mod_exp+0x73>
jmp    1210 <deadcafe_bn_mod_exp+0xc0>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%esi
call   1070 <bn2gmp@plt>
cmp    $0x0,%eax
jne    11dd <deadcafe_bn_mod_exp+0x8d>
jmp    1210 <deadcafe_bn_mod_exp+0xc0>
mov    -0x24(%rbp),%edi
mov    -0x28(%rbp),%esi
mov    -0x2c(%rbp),%edx
mov    -0x30(%rbp),%ecx
call   1030 <mpz_powm@plt>
mov    -0x24(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1060 <gmp2bn@plt>
cmp    $0x0,%rax
je     120b <deadcafe_bn_mod_exp+0xbb>
movl   $0x1,-0x34(%rbp)
jmp    1210 <deadcafe_bn_mod_exp+0xc0>
mov    -0x24(%rbp),%edi
call   1050 <mpz_clear@plt>
mov    -0x28(%rbp),%edi
call   1050 <mpz_clear@plt>
mov    -0x2c(%rbp),%edi
call   1050 <mpz_clear@plt>
mov    -0x30(%rbp),%edi
call   1050 <mpz_clear@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<deadcafe_bn_mod_exp>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r14
call   1040 <mpz_init@plt>
call   1040 <mpz_init@plt>
call   1040 <mpz_init@plt>
call   1040 <mpz_init@plt>
mov    %rbx,%rdi
call   1070 <bn2gmp@plt>
xor    %ebx,%ebx
test   %eax,%eax
je     11b3 <deadcafe_bn_mod_exp+0x63>
mov    %r12,%rdi
call   1070 <bn2gmp@plt>
test   %eax,%eax
je     11b3 <deadcafe_bn_mod_exp+0x63>
mov    %r15,%rdi
call   1070 <bn2gmp@plt>
test   %eax,%eax
je     11b3 <deadcafe_bn_mod_exp+0x63>
call   1030 <mpz_powm@plt>
mov    %r14,%rsi
call   1060 <gmp2bn@plt>
xor    %ebx,%ebx
test   %rax,%rax
setne  %bl
call   1050 <mpz_clear@plt>
call   1050 <mpz_clear@plt>
call   1050 <mpz_clear@plt>
call   1050 <mpz_clear@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @deadcafe_bn_mod_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** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i32* %3, i32** %8
store i32 0, i32* %13
%14 = load i32, i32* %9
%15 = call i32 @mpz_init(i32 %14)
%16 = load i32, i32* %10
%17 = call i32 @mpz_init(i32 %16)
%18 = load i32, i32* %11
%19 = call i32 @mpz_init(i32 %18)
%20 = load i32, i32* %12
%21 = call i32 @mpz_init(i32 %20)
%22 = load i32*, i32** %6
%23 = load i32, i32* %10
%24 = call i32 @bn2gmp(i32* %22, i32 %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %4
br label %51
27: ; preds = %4
%28 = load i32*, i32** %7
%29 = load i32, i32* %11
%30 = call i32 @bn2gmp(i32* %28, i32 %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %27
br label %51
33: ; preds = %27
%34 = load i32*, i32** %8
%35 = load i32, i32* %12
%36 = call i32 @bn2gmp(i32* %34, i32 %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %39, label %38
38: ; preds = %33
br label %51
39: ; preds = %33
%40 = load i32, i32* %9
%41 = load i32, i32* %10
%42 = load i32, i32* %11
%43 = load i32, i32* %12
%44 = call i32 @mpz_powm(i32 %40, i32 %41, i32 %42, i32 %43)
%45 = load i32, i32* %9
%46 = load i32*, i32** %5
%47 = call i64 @gmp2bn(i32 %45, i32* %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %39
store i32 1, i32* %13
br label %50
50: ; preds = %49, %39
br label %51
51: ; preds = %50, %38, %32, %26
%52 = load i32, i32* %9
%53 = call i32 @mpz_clear(i32 %52)
%54 = load i32, i32* %10
%55 = call i32 @mpz_clear(i32 %54)
%56 = load i32, i32* %11
%57 = call i32 @mpz_clear(i32 %56)
%58 = load i32, i32* %12
%59 = call i32 @mpz_clear(i32 %58)
%60 = load i32, i32* %13
ret i32 %60
}

LLVM IR (-O2):
define i32 @deadcafe_bn_mod_exp(i32* %0, i32* %1, i32* %2, i32* %3) {
%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 @bn2gmp(i32* %1, i32 undef)
%10 = icmp eq i32 %9, 0
br i1 %10, label %22, label %11
11: ; preds = %4
%12 = tail call i32 @bn2gmp(i32* %2, i32 undef)
%13 = icmp eq i32 %12, 0
br i1 %13, label %22, label %14
14: ; preds = %11
%15 = tail call i32 @bn2gmp(i32* %3, i32 undef)
%16 = icmp eq i32 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %14
%18 = tail call i32 @mpz_powm(i32 undef, i32 undef, i32 undef, i32 undef)
%19 = tail call i64 @gmp2bn(i32 undef, i32* %0)
%20 = icmp ne i64 %19, 0
%21 = zext i1 %20 to i32
br label %22
22: ; preds = %17, %14, %11, %4
%23 = phi i32 [ 0, %14 ], [ 0, %11 ], [ 0, %4 ], [ %21, %17 ]
%24 = tail call i32 @mpz_clear(i32 undef)
%25 = tail call i32 @mpz_clear(i32 undef)
%26 = tail call i32 @mpz_clear(i32 undef)
%27 = tail call i32 @mpz_clear(i32 undef)
ret i32 %23
}