#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*/ uint8_t;
struct bignum {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ bignum_deinit(struct bignum *);
scalar_t__ bignum_exptmod(struct bignum *, struct bignum *, struct bignum *,
                          struct bignum *);
int bignum_get_unsigned_bin(struct bignum *, int /*<<< orphan*/ *, size_t *);
struct bignum *bignum_init();
scalar_t__ bignum_set_unsigned_bin(struct bignum *, int /*<<< orphan*/ const *,
                                   size_t);
int crypto_mod_exp(const uint8_t *base, size_t base_len, const uint8_t *power,
                   size_t power_len, const uint8_t *modulus, size_t modulus_len,
                   uint8_t *result, size_t *result_len) {
  struct bignum *bn_base, *bn_exp, *bn_modulus, *bn_result;
  int ret = -1;
  bn_base = bignum_init();
  bn_exp = bignum_init();
  bn_modulus = bignum_init();
  bn_result = bignum_init();
  if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
      bn_result == NULL)
    goto error;
  if (bignum_set_unsigned_bin(bn_base, base, base_len) < 0 ||
      bignum_set_unsigned_bin(bn_exp, power, power_len) < 0 ||
      bignum_set_unsigned_bin(bn_modulus, modulus, modulus_len) < 0)
    goto error;
  if (bignum_exptmod(bn_base, bn_exp, bn_modulus, bn_result) < 0)
    goto error;
  ret = bignum_get_unsigned_bin(bn_result, result, result_len);
error:
  bignum_deinit(bn_base);
  bignum_deinit(bn_exp);
  bignum_deinit(bn_modulus);
  bignum_deinit(bn_result);
  return ret;
}


x86 assembly (clang, -O0):
<crypto_mod_exp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
movl   $0xffffffff,-0x54(%rbp)
mov    $0x0,%al
call   1040 <bignum_init@plt>
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1040 <bignum_init@plt>
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1040 <bignum_init@plt>
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1040 <bignum_init@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     11d7 <crypto_mod_exp+0x87>
cmpq   $0x0,-0x40(%rbp)
je     11d7 <crypto_mod_exp+0x87>
cmpq   $0x0,-0x48(%rbp)
je     11d7 <crypto_mod_exp+0x87>
cmpq   $0x0,-0x50(%rbp)
jne    11dc <crypto_mod_exp+0x8c>
jmp    126a <crypto_mod_exp+0x11a>
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1070 <bignum_set_unsigned_bin@plt>
cmp    $0x0,%rax
jl     122d <crypto_mod_exp+0xdd>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <bignum_set_unsigned_bin@plt>
cmp    $0x0,%rax
jl     122d <crypto_mod_exp+0xdd>
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1070 <bignum_set_unsigned_bin@plt>
cmp    $0x0,%rax
jge    1232 <crypto_mod_exp+0xe2>
jmp    126a <crypto_mod_exp+0x11a>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rdx
mov    -0x50(%rbp),%rcx
call   1060 <bignum_exptmod@plt>
cmp    $0x0,%rax
jge    1256 <crypto_mod_exp+0x106>
jmp    126a <crypto_mod_exp+0x11a>
mov    -0x50(%rbp),%rdi
mov    0x10(%rbp),%rsi
mov    0x18(%rbp),%rdx
call   1030 <bignum_get_unsigned_bin@plt>
mov    %eax,-0x54(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <bignum_deinit@plt>
mov    -0x40(%rbp),%rdi
call   1050 <bignum_deinit@plt>
mov    -0x48(%rbp),%rdi
call   1050 <bignum_deinit@plt>
mov    -0x50(%rbp),%rdi
call   1050 <bignum_deinit@plt>
mov    -0x54(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crypto_mod_exp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9,0x10(%rsp)
mov    %r8,0x8(%rsp)
mov    %rcx,0x20(%rsp)
mov    %rdx,0x18(%rsp)
mov    %rsi,%r13
mov    %rdi,%rbp
xor    %eax,%eax
call   1040 <bignum_init@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <bignum_init@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1040 <bignum_init@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1040 <bignum_init@plt>
mov    %rax,%r12
test   %rbx,%rbx
je     122a <crypto_mod_exp+0xda>
test   %r14,%r14
je     122a <crypto_mod_exp+0xda>
test   %r15,%r15
je     122a <crypto_mod_exp+0xda>
test   %r12,%r12
je     122a <crypto_mod_exp+0xda>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r13,%rdx
call   1070 <bignum_set_unsigned_bin@plt>
test   %rax,%rax
js     122a <crypto_mod_exp+0xda>
mov    %r14,%rdi
mov    0x18(%rsp),%rsi
mov    0x20(%rsp),%rdx
call   1070 <bignum_set_unsigned_bin@plt>
test   %rax,%rax
js     122a <crypto_mod_exp+0xda>
mov    %r15,%rdi
mov    0x8(%rsp),%rsi
mov    0x10(%rsp),%rdx
call   1070 <bignum_set_unsigned_bin@plt>
test   %rax,%rax
js     122a <crypto_mod_exp+0xda>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %r12,%rcx
call   1060 <bignum_exptmod@plt>
test   %rax,%rax
mov    $0xffffffff,%ebp
js     122f <crypto_mod_exp+0xdf>
mov    0x68(%rsp),%rdx
mov    0x60(%rsp),%rsi
mov    %r12,%rdi
call   1030 <bignum_get_unsigned_bin@plt>
mov    %eax,%ebp
jmp    122f <crypto_mod_exp+0xdf>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1050 <bignum_deinit@plt>
mov    %r14,%rdi
call   1050 <bignum_deinit@plt>
mov    %r15,%rdi
call   1050 <bignum_deinit@plt>
mov    %r12,%rdi
call   1050 <bignum_deinit@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.bignum = type { i32 }
define i32 @crypto_mod_exp(i32* %0, i64 %1, i32* %2, i64 %3, i32* %4, i64 %5, i32* %6, i64* %7) {
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i32*
%12 = alloca i64
%13 = alloca i32*
%14 = alloca i64
%15 = alloca i32*
%16 = alloca i64*
%17 = alloca %struct.bignum*
%18 = alloca %struct.bignum*
%19 = alloca %struct.bignum*
%20 = alloca %struct.bignum*
%21 = alloca i32
store i32* %0, i32** %9
store i64 %1, i64* %10
store i32* %2, i32** %11
store i64 %3, i64* %12
store i32* %4, i32** %13
store i64 %5, i64* %14
store i32* %6, i32** %15
store i64* %7, i64** %16
store i32 -1, i32* %21
%22 = call %struct.bignum* (...) @bignum_init()
store %struct.bignum* %22, %struct.bignum** %17
%23 = call %struct.bignum* (...) @bignum_init()
store %struct.bignum* %23, %struct.bignum** %18
%24 = call %struct.bignum* (...) @bignum_init()
store %struct.bignum* %24, %struct.bignum** %19
%25 = call %struct.bignum* (...) @bignum_init()
store %struct.bignum* %25, %struct.bignum** %20
%26 = load %struct.bignum*, %struct.bignum** %17
%27 = icmp eq %struct.bignum* %26, null
br i1 %27, label %37, label %28
28: ; preds = %8
%29 = load %struct.bignum*, %struct.bignum** %18
%30 = icmp eq %struct.bignum* %29, null
br i1 %30, label %37, label %31
31: ; preds = %28
%32 = load %struct.bignum*, %struct.bignum** %19
%33 = icmp eq %struct.bignum* %32, null
br i1 %33, label %37, label %34
34: ; preds = %31
%35 = load %struct.bignum*, %struct.bignum** %20
%36 = icmp eq %struct.bignum* %35, null
br i1 %36, label %37, label %38
37: ; preds = %34, %31, %28, %8
br label %70
38: ; preds = %34
%39 = load %struct.bignum*, %struct.bignum** %17
%40 = load i32*, i32** %9
%41 = load i64, i64* %10
%42 = call i64 @bignum_set_unsigned_bin(%struct.bignum* %39, i32* %40, i64 %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %56, label %44
44: ; preds = %38
%45 = load %struct.bignum*, %struct.bignum** %18
%46 = load i32*, i32** %11
%47 = load i64, i64* %12
%48 = call i64 @bignum_set_unsigned_bin(%struct.bignum* %45, i32* %46, i64 %47)
%49 = icmp slt i64 %48, 0
br i1 %49, label %56, label %50
50: ; preds = %44
%51 = load %struct.bignum*, %struct.bignum** %19
%52 = load i32*, i32** %13
%53 = load i64, i64* %14
%54 = call i64 @bignum_set_unsigned_bin(%struct.bignum* %51, i32* %52, i64 %53)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %50, %44, %38
br label %70
57: ; preds = %50
%58 = load %struct.bignum*, %struct.bignum** %17
%59 = load %struct.bignum*, %struct.bignum** %18
%60 = load %struct.bignum*, %struct.bignum** %19
%61 = load %struct.bignum*, %struct.bignum** %20
%62 = call i64 @bignum_exptmod(%struct.bignum* %58, %struct.bignum* %59, %struct.bignum* %60, %struct.bignum* %61)
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %65
64: ; preds = %57
br label %70
65: ; preds = %57
%66 = load %struct.bignum*, %struct.bignum** %20
%67 = load i32*, i32** %15
%68 = load i64*, i64** %16
%69 = call i32 @bignum_get_unsigned_bin(%struct.bignum* %66, i32* %67, i64* %68)
store i32 %69, i32* %21
br label %70
70: ; preds = %65, %64, %56, %37
%71 = load %struct.bignum*, %struct.bignum** %17
%72 = call i32 @bignum_deinit(%struct.bignum* %71)
%73 = load %struct.bignum*, %struct.bignum** %18
%74 = call i32 @bignum_deinit(%struct.bignum* %73)
%75 = load %struct.bignum*, %struct.bignum** %19
%76 = call i32 @bignum_deinit(%struct.bignum* %75)
%77 = load %struct.bignum*, %struct.bignum** %20
%78 = call i32 @bignum_deinit(%struct.bignum* %77)
%79 = load i32, i32* %21
ret i32 %79
}

LLVM IR (-O2):
%struct.bignum = type { i32 }
define i32 @crypto_mod_exp(i32* %0, i64 %1, i32* %2, i64 %3, i32* %4, i64 %5, i32* %6, i64* %7) {
%9 = tail call %struct.bignum* (...) @bignum_init()
%10 = tail call %struct.bignum* (...) @bignum_init()
%11 = tail call %struct.bignum* (...) @bignum_init()
%12 = tail call %struct.bignum* (...) @bignum_init()
%13 = icmp eq %struct.bignum* %9, null
%14 = icmp eq %struct.bignum* %10, null
%15 = select i1 %13, i1 true, i1 %14
%16 = icmp eq %struct.bignum* %11, null
%17 = select i1 %15, i1 true, i1 %16
%18 = icmp eq %struct.bignum* %12, null
%19 = select i1 %17, i1 true, i1 %18
br i1 %19, label %34, label %20
20: ; preds = %8
%21 = tail call i64 @bignum_set_unsigned_bin(%struct.bignum* %9, i32* %0, i64 %1)
%22 = icmp slt i64 %21, 0
br i1 %22, label %34, label %23
23: ; preds = %20
%24 = tail call i64 @bignum_set_unsigned_bin(%struct.bignum* %10, i32* %2, i64 %3)
%25 = icmp slt i64 %24, 0
br i1 %25, label %34, label %26
26: ; preds = %23
%27 = tail call i64 @bignum_set_unsigned_bin(%struct.bignum* %11, i32* %4, i64 %5)
%28 = icmp slt i64 %27, 0
br i1 %28, label %34, label %29
29: ; preds = %26
%30 = tail call i64 @bignum_exptmod(%struct.bignum* %9, %struct.bignum* %10, %struct.bignum* %11, %struct.bignum* %12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %29
%33 = tail call i32 @bignum_get_unsigned_bin(%struct.bignum* %12, i32* %6, i64* %7)
br label %34
34: ; preds = %29, %20, %23, %26, %8, %32
%35 = phi i32 [ -1, %8 ], [ -1, %20 ], [ -1, %23 ], [ -1, %26 ], [ -1, %29 ], [ %33, %32 ]
%36 = tail call i32 @bignum_deinit(%struct.bignum* %9)
%37 = tail call i32 @bignum_deinit(%struct.bignum* %10)
%38 = tail call i32 @bignum_deinit(%struct.bignum* %11)
%39 = tail call i32 @bignum_deinit(%struct.bignum* %12)
ret i32 %35
}