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

/* Variables and functions */
int /*<<< orphan*/ mpz_clear(int /*<<< orphan*/);
int /*<<< orphan*/ mpz_init(int /*<<< orphan*/);
int /*<<< orphan*/ mpz_mod(int /*<<< orphan*/, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int /*<<< orphan*/ mpz_mul(int /*<<< orphan*/, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int /*<<< orphan*/ mpz_set(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_set_ui(int /*<<< orphan*/, int);
scalar_t__ mpz_sgn(int /*<<< orphan*/);
int mpz_sizeinbase(int /*<<< orphan*/, int);
scalar_t__ mpz_tstbit(int /*<<< orphan*/, int);
void number_theory_exp_modp(mpz_t R, mpz_t a, mpz_t k, mpz_t P) {
  mpz_t A;
  mpz_init(A);
  mpz_t b;
  mpz_init(b);
  int i;
  int t = mpz_sizeinbase(k, 2);
  mpz_t t1;
  mpz_init(t1);
  mpz_t t2;
  mpz_init(t2);
  mpz_set_ui(b, 1);
  if (mpz_sgn(k)) {
    mpz_set(A, a);
    if (mpz_tstbit(k, 0))
      mpz_set(b, a);
    for (i = 1; i < t; i++) {
      mpz_set(t1, A);
      mpz_mul(t2, t1, A);
      mpz_mod(A, t2, P);
      if (mpz_tstbit(k, i)) {
        mpz_mul(t1, A, b);
        mpz_mod(b, t1, P);
      }
    }
  }
  mpz_set(R, b);
  mpz_clear(A);
  mpz_clear(b);
  mpz_clear(t1);
  mpz_clear(t2);
}


x86 assembly (clang, -O0):
<number_theory_exp_modp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x18(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0xc(%rbp),%edi
mov    $0x2,%esi
call   1070 <mpz_sizeinbase@plt>
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x28(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   1080 <mpz_set_ui@plt>
mov    -0xc(%rbp),%edi
call   10b0 <mpz_sgn@plt>
cmp    $0x0,%rax
je     12a0 <number_theory_exp_modp+0x110>
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1040 <mpz_set@plt>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1030 <mpz_tstbit@plt>
cmp    $0x0,%rax
je     121d <number_theory_exp_modp+0x8d>
mov    -0x18(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1040 <mpz_set@plt>
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    129b <number_theory_exp_modp+0x10b>
mov    -0x24(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1040 <mpz_set@plt>
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1060 <mpz_mul@plt>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1090 <mpz_mod@plt>
mov    -0xc(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1030 <mpz_tstbit@plt>
cmp    $0x0,%rax
je     1288 <number_theory_exp_modp+0xf8>
mov    -0x24(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1060 <mpz_mul@plt>
mov    -0x18(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1090 <mpz_mod@plt>
jmp    128d <number_theory_exp_modp+0xfd>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1224 <number_theory_exp_modp+0x94>
jmp    12a0 <number_theory_exp_modp+0x110>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1040 <mpz_set@plt>
mov    -0x14(%rbp),%edi
call   10a0 <mpz_clear@plt>
mov    -0x18(%rbp),%edi
call   10a0 <mpz_clear@plt>
mov    -0x24(%rbp),%edi
call   10a0 <mpz_clear@plt>
mov    -0x28(%rbp),%edi
call   10a0 <mpz_clear@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<number_theory_exp_modp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%ebx
mov    %edx,%r13d
mov    %esi,%r12d
mov    %edi,%r14d
call   1050 <mpz_init@plt>
call   1050 <mpz_init@plt>
mov    %r13d,%edi
mov    $0x2,%esi
call   1070 <mpz_sizeinbase@plt>
mov    %eax,%r15d
call   1050 <mpz_init@plt>
call   1050 <mpz_init@plt>
mov    $0x1,%esi
call   1080 <mpz_set_ui@plt>
mov    %r13d,%edi
call   10b0 <mpz_sgn@plt>
test   %rax,%rax
je     1246 <number_theory_exp_modp+0xb6>
mov    %r12d,%esi
call   1040 <mpz_set@plt>
mov    %r13d,%edi
xor    %esi,%esi
call   1030 <mpz_tstbit@plt>
test   %rax,%rax
je     1200 <number_theory_exp_modp+0x70>
mov    %r12d,%esi
call   1040 <mpz_set@plt>
cmp    $0x2,%r15d
jl     1246 <number_theory_exp_modp+0xb6>
mov    $0x1,%ebp
jmp    1218 <number_theory_exp_modp+0x88>
nopl   (%rax)
add    $0x1,%ebp
cmp    %ebp,%r15d
je     1246 <number_theory_exp_modp+0xb6>
call   1040 <mpz_set@plt>
call   1060 <mpz_mul@plt>
mov    %ebx,%edx
call   1090 <mpz_mod@plt>
mov    %r13d,%edi
mov    %ebp,%esi
call   1030 <mpz_tstbit@plt>
test   %rax,%rax
je     1210 <number_theory_exp_modp+0x80>
call   1060 <mpz_mul@plt>
mov    %ebx,%edx
call   1090 <mpz_mod@plt>
jmp    1210 <number_theory_exp_modp+0x80>
mov    %r14d,%edi
call   1040 <mpz_set@plt>
call   10a0 <mpz_clear@plt>
call   10a0 <mpz_clear@plt>
call   10a0 <mpz_clear@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10a0 <mpz_clear@plt>

LLVM IR (-O0):
define void @number_theory_exp_modp(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
%14 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%15 = load i32, i32* %9
%16 = call i32 @mpz_init(i32 %15)
%17 = load i32, i32* %10
%18 = call i32 @mpz_init(i32 %17)
%19 = load i32, i32* %7
%20 = call i32 @mpz_sizeinbase(i32 %19, i32 2)
store i32 %20, i32* %12
%21 = load i32, i32* %13
%22 = call i32 @mpz_init(i32 %21)
%23 = load i32, i32* %14
%24 = call i32 @mpz_init(i32 %23)
%25 = load i32, i32* %10
%26 = call i32 @mpz_set_ui(i32 %25, i32 1)
%27 = load i32, i32* %7
%28 = call i64 @mpz_sgn(i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %76
30: ; preds = %4
%31 = load i32, i32* %9
%32 = load i32, i32* %6
%33 = call i32 @mpz_set(i32 %31, i32 %32)
%34 = load i32, i32* %7
%35 = call i64 @mpz_tstbit(i32 %34, i32 0)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %30
%38 = load i32, i32* %10
%39 = load i32, i32* %6
%40 = call i32 @mpz_set(i32 %38, i32 %39)
br label %41
41: ; preds = %37, %30
store i32 1, i32* %11
br label %42
42: ; preds = %72, %41
%43 = load i32, i32* %11
%44 = load i32, i32* %12
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %75
46: ; preds = %42
%47 = load i32, i32* %13
%48 = load i32, i32* %9
%49 = call i32 @mpz_set(i32 %47, i32 %48)
%50 = load i32, i32* %14
%51 = load i32, i32* %13
%52 = load i32, i32* %9
%53 = call i32 @mpz_mul(i32 %50, i32 %51, i32 %52)
%54 = load i32, i32* %9
%55 = load i32, i32* %14
%56 = load i32, i32* %8
%57 = call i32 @mpz_mod(i32 %54, i32 %55, i32 %56)
%58 = load i32, i32* %7
%59 = load i32, i32* %11
%60 = call i64 @mpz_tstbit(i32 %58, i32 %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %71
62: ; preds = %46
%63 = load i32, i32* %13
%64 = load i32, i32* %9
%65 = load i32, i32* %10
%66 = call i32 @mpz_mul(i32 %63, i32 %64, i32 %65)
%67 = load i32, i32* %10
%68 = load i32, i32* %13
%69 = load i32, i32* %8
%70 = call i32 @mpz_mod(i32 %67, i32 %68, i32 %69)
br label %71
71: ; preds = %62, %46
br label %72
72: ; preds = %71
%73 = load i32, i32* %11
%74 = add nsw i32 %73, 1
store i32 %74, i32* %11
br label %42
75: ; preds = %42
br label %76
76: ; preds = %75, %4
%77 = load i32, i32* %5
%78 = load i32, i32* %10
%79 = call i32 @mpz_set(i32 %77, i32 %78)
%80 = load i32, i32* %9
%81 = call i32 @mpz_clear(i32 %80)
%82 = load i32, i32* %10
%83 = call i32 @mpz_clear(i32 %82)
%84 = load i32, i32* %13
%85 = call i32 @mpz_clear(i32 %84)
%86 = load i32, i32* %14
%87 = call i32 @mpz_clear(i32 %86)
ret void
}

LLVM IR (-O2):
define void @number_theory_exp_modp(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_sizeinbase(i32 %2, i32 2)
%8 = tail call i32 @mpz_init(i32 undef)
%9 = tail call i32 @mpz_init(i32 undef)
%10 = tail call i32 @mpz_set_ui(i32 undef, i32 1)
%11 = tail call i64 @mpz_sgn(i32 %2)
%12 = icmp eq i64 %11, 0
br i1 %12, label %34, label %13
13: ; preds = %4
%14 = tail call i32 @mpz_set(i32 undef, i32 %1)
%15 = tail call i64 @mpz_tstbit(i32 %2, i32 0)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %13
%18 = tail call i32 @mpz_set(i32 undef, i32 %1)
br label %19
19: ; preds = %17, %13
%20 = icmp sgt i32 %7, 1
br i1 %20, label %21, label %34
21: ; preds = %19, %31
%22 = phi i32 [ %32, %31 ], [ 1, %19 ]
%23 = tail call i32 @mpz_set(i32 undef, i32 undef)
%24 = tail call i32 @mpz_mul(i32 undef, i32 undef, i32 undef)
%25 = tail call i32 @mpz_mod(i32 undef, i32 undef, i32 %3)
%26 = tail call i64 @mpz_tstbit(i32 %2, i32 %22)
%27 = icmp eq i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %21
%29 = tail call i32 @mpz_mul(i32 undef, i32 undef, i32 undef)
%30 = tail call i32 @mpz_mod(i32 undef, i32 undef, i32 %3)
br label %31
31: ; preds = %21, %28
%32 = add nuw nsw i32 %22, 1
%33 = icmp eq i32 %32, %7
br i1 %33, label %34, label %21
34: ; preds = %31, %19, %4
%35 = tail call i32 @mpz_set(i32 %0, i32 undef)
%36 = tail call i32 @mpz_clear(i32 undef)
%37 = tail call i32 @mpz_clear(i32 undef)
%38 = tail call i32 @mpz_clear(i32 undef)
%39 = tail call i32 @mpz_clear(i32 undef)
ret void
}