#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*/ mpz_srcptr;
typedef int /*<<< orphan*/ mpz_ptr;

/* Variables and functions */
int /*<<< orphan*/ mpz_clear(int /*<<< orphan*/);
scalar_t__ mpz_cmp_ui(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_div_2exp(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ mpz_init(int /*<<< orphan*/);
int /*<<< orphan*/ mpz_init_set(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_mod(int /*<<< orphan*/, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int /*<<< orphan*/ mpz_mod_2exp(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ mpz_mul(int /*<<< orphan*/, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int /*<<< orphan*/ mpz_set_ui(int /*<<< orphan*/, int);
void modexp(mpz_ptr result, mpz_srcptr base, mpz_srcptr exp, mpz_srcptr mod) {
  mpz_t b, e, exp_lsb;
  mpz_init_set(b, base);
  mpz_init_set(e, exp);
  mpz_init(exp_lsb);
  mpz_set_ui(result, 1);
  while (mpz_cmp_ui(e, 0) > 0) {
    mpz_mod_2exp(exp_lsb, e, 1);
    if (mpz_cmp_ui(exp_lsb, 0) != 0) {
      mpz_mul(result, result, b);
      mpz_mod(result, result, mod);
    }
    mpz_div_2exp(e, e, 1);
    mpz_mul(b, b, b);
    mpz_mod(b, b, mod);
  }
  mpz_clear(b);
  mpz_clear(e);
  mpz_clear(exp_lsb);
}


x86 assembly (clang, -O0):
<modexp>:
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    -0x14(%rbp),%edi
mov    -0x8(%rbp),%esi
call   10b0 <mpz_init_set@plt>
mov    -0x18(%rbp),%edi
mov    -0xc(%rbp),%esi
call   10b0 <mpz_init_set@plt>
mov    -0x1c(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x4(%rbp),%edi
mov    $0x1,%esi
call   1080 <mpz_set_ui@plt>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1070 <mpz_cmp_ui@plt>
cmp    $0x0,%rax
jle    1254 <modexp+0xc4>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    $0x1,%edx
call   1040 <mpz_mod_2exp@plt>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   1070 <mpz_cmp_ui@plt>
cmp    $0x0,%rax
je     1223 <modexp+0x93>
mov    -0x4(%rbp),%edi
mov    -0x4(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1060 <mpz_mul@plt>
mov    -0x4(%rbp),%edi
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1090 <mpz_mod@plt>
mov    -0x18(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    $0x1,%edx
call   1030 <mpz_div_2exp@plt>
mov    -0x14(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1060 <mpz_mul@plt>
mov    -0x14(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1090 <mpz_mod@plt>
jmp    11cf <modexp+0x3f>
mov    -0x14(%rbp),%edi
call   10a0 <mpz_clear@plt>
mov    -0x18(%rbp),%edi
call   10a0 <mpz_clear@plt>
mov    -0x1c(%rbp),%edi
call   10a0 <mpz_clear@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<modexp>:
push   %rbp
push   %r14
push   %rbx
mov    %ecx,%ebx
mov    %edx,%r14d
mov    %edi,%ebp
call   10b0 <mpz_init_set@plt>
mov    %r14d,%esi
call   10b0 <mpz_init_set@plt>
call   1050 <mpz_init@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1080 <mpz_set_ui@plt>
jmp    11d6 <modexp+0x46>
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edx
call   1030 <mpz_div_2exp@plt>
call   1060 <mpz_mul@plt>
mov    %ebx,%edx
call   1090 <mpz_mod@plt>
xor    %esi,%esi
call   1070 <mpz_cmp_ui@plt>
test   %rax,%rax
jle    120e <modexp+0x7e>
mov    $0x1,%edx
call   1040 <mpz_mod_2exp@plt>
xor    %esi,%esi
call   1070 <mpz_cmp_ui@plt>
test   %rax,%rax
je     11c0 <modexp+0x30>
mov    %ebp,%edi
mov    %ebp,%esi
call   1060 <mpz_mul@plt>
mov    %ebp,%edi
mov    %ebp,%esi
mov    %ebx,%edx
call   1090 <mpz_mod@plt>
jmp    11c0 <modexp+0x30>
call   10a0 <mpz_clear@plt>
call   10a0 <mpz_clear@plt>
pop    %rbx
pop    %r14
pop    %rbp
jmp    10a0 <mpz_clear@plt>

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

LLVM IR (-O2):
define void @modexp(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = tail call i32 @mpz_init_set(i32 undef, i32 %1)
%6 = tail call i32 @mpz_init_set(i32 undef, i32 %2)
%7 = tail call i32 @mpz_init(i32 undef)
%8 = tail call i32 @mpz_set_ui(i32 %0, i32 1)
%9 = tail call i64 @mpz_cmp_ui(i32 undef, i32 0)
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %24
11: ; preds = %4, %18
%12 = tail call i32 @mpz_mod_2exp(i32 undef, i32 undef, i32 1)
%13 = tail call i64 @mpz_cmp_ui(i32 undef, i32 0)
%14 = icmp eq i64 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %11
%16 = tail call i32 @mpz_mul(i32 %0, i32 %0, i32 undef)
%17 = tail call i32 @mpz_mod(i32 %0, i32 %0, i32 %3)
br label %18
18: ; preds = %15, %11
%19 = tail call i32 @mpz_div_2exp(i32 undef, i32 undef, i32 1)
%20 = tail call i32 @mpz_mul(i32 undef, i32 undef, i32 undef)
%21 = tail call i32 @mpz_mod(i32 undef, i32 undef, i32 %3)
%22 = tail call i64 @mpz_cmp_ui(i32 undef, i32 0)
%23 = icmp sgt i64 %22, 0
br i1 %23, label %11, label %24
24: ; preds = %18, %4
%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 void
}