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

/* Variables and functions */
double floor(double);
int intDoubleModPow(int NbrMod, int Expon, int currentPrime) {
  int exponent = Expon;
  double dPower = 1;
  double dSquare = NbrMod;
  double dModulus = currentPrime;
  while (exponent != 0) {
    if ((exponent & 1) == 1) {
      dPower *= dSquare;
      dPower -= floor(dPower / dModulus) * dModulus;
    }
    dSquare *= dSquare;
    dSquare -= floor(dSquare / dModulus) * dModulus;
    exponent >>= 1;
  }
  return (int)dPower;
}


x86 assembly (clang, -O0):
<intDoubleModPow>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
movsd  0xed1(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
cvtsi2sdl -0x4(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
cvtsi2sdl -0xc(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1207 <intDoubleModPow+0xf7>
mov    -0x10(%rbp),%eax
and    $0x1,%eax
cmp    $0x1,%eax
jne    11ad <intDoubleModPow+0x9d>
movsd  -0x20(%rbp),%xmm0
mulsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
divsd  -0x28(%rbp),%xmm0
call   1030 <floor@plt>
movsd  -0x28(%rbp),%xmm2
movsd  -0x18(%rbp),%xmm1
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x20(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
divsd  -0x28(%rbp),%xmm0
call   1030 <floor@plt>
movsd  -0x28(%rbp),%xmm2
movsd  -0x20(%rbp),%xmm1
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x10(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1148 <intDoubleModPow+0x38>
cvttsd2si -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<intDoubleModPow>:
push   %rbx
sub    $0x20,%rsp
test   %esi,%esi
je     11bc <intDoubleModPow+0xac>
mov    %esi,%ebx
cvtsi2sd %edx,%xmm1
cvtsi2sd %edi,%xmm2
movsd  0xed1(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
movsd  %xmm1,0x18(%rsp)
jmp    1176 <intDoubleModPow+0x66>
nopl   (%rax)
mulsd  %xmm2,%xmm2
movsd  %xmm2,0x10(%rsp)
movapd %xmm2,%xmm0
divsd  %xmm1,%xmm0
call   1030 <floor@plt>
movsd  0x10(%rsp),%xmm2
movsd  0x18(%rsp),%xmm1
mulsd  %xmm1,%xmm0
subsd  %xmm0,%xmm2
mov    %ebx,%eax
sar    %eax
cmp    $0x1,%ebx
mov    %eax,%ebx
jbe    11ca <intDoubleModPow+0xba>
test   $0x1,%bl
je     1140 <intDoubleModPow+0x30>
movsd  0x8(%rsp),%xmm0
mulsd  %xmm2,%xmm0
movsd  %xmm0,0x8(%rsp)
divsd  %xmm1,%xmm0
movsd  %xmm2,0x10(%rsp)
call   1030 <floor@plt>
movsd  0x10(%rsp),%xmm2
movsd  0x18(%rsp),%xmm1
mulsd  %xmm1,%xmm0
movsd  0x8(%rsp),%xmm3
subsd  %xmm0,%xmm3
movsd  %xmm3,0x8(%rsp)
jmp    1140 <intDoubleModPow+0x30>
movsd  0xe3c(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
cvttsd2si 0x8(%rsp),%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i32 @intDoubleModPow(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca double
%9 = alloca double
%10 = alloca double
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%11 = load i32, i32* %5
store i32 %11, i32* %7
store double 1.000000e+00, double* %8
%12 = load i32, i32* %4
%13 = sitofp i32 %12 to double
store double %13, double* %9
%14 = load i32, i32* %6
%15 = sitofp i32 %14 to double
store double %15, double* %10
br label %16
16: ; preds = %35, %3
%17 = load i32, i32* %7
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %49
19: ; preds = %16
%20 = load i32, i32* %7
%21 = and i32 %20, 1
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %35
23: ; preds = %19
%24 = load double, double* %9
%25 = load double, double* %8
%26 = fmul double %25, %24
store double %26, double* %8
%27 = load double, double* %8
%28 = load double, double* %10
%29 = fdiv double %27, %28
%30 = call double @llvm.floor.f64(double %29)
%31 = load double, double* %10
%32 = load double, double* %8
%33 = fneg double %30
%34 = call double @llvm.fmuladd.f64(double %33, double %31, double %32)
store double %34, double* %8
br label %35
35: ; preds = %23, %19
%36 = load double, double* %9
%37 = load double, double* %9
%38 = fmul double %37, %36
store double %38, double* %9
%39 = load double, double* %9
%40 = load double, double* %10
%41 = fdiv double %39, %40
%42 = call double @llvm.floor.f64(double %41)
%43 = load double, double* %10
%44 = load double, double* %9
%45 = fneg double %42
%46 = call double @llvm.fmuladd.f64(double %45, double %43, double %44)
store double %46, double* %9
%47 = load i32, i32* %7
%48 = ashr i32 %47, 1
store i32 %48, i32* %7
br label %16
49: ; preds = %16
%50 = load double, double* %8
%51 = fptosi double %50 to i32
ret i32 %51
}

LLVM IR (-O2):
define i32 @intDoubleModPow(i32 %0, i32 %1, i32 %2) {
%4 = sitofp i32 %2 to double
%5 = icmp eq i32 %1, 0
br i1 %5, label %29, label %6
6: ; preds = %3
%7 = sitofp i32 %0 to double
br label %8
8: ; preds = %6, %20
%9 = phi i32 [ %27, %20 ], [ %1, %6 ]
%10 = phi double [ %26, %20 ], [ %7, %6 ]
%11 = phi double [ %21, %20 ], [ 1.000000e+00, %6 ]
%12 = and i32 %9, 1
%13 = icmp eq i32 %12, 0
br i1 %13, label %20, label %14
14: ; preds = %8
%15 = fmul double %11, %10
%16 = fdiv double %15, %4
%17 = tail call double @llvm.floor.f64(double %16)
%18 = fneg double %17
%19 = tail call double @llvm.fmuladd.f64(double %18, double %4, double %15)
br label %20
20: ; preds = %14, %8
%21 = phi double [ %19, %14 ], [ %11, %8 ]
%22 = fmul double %10, %10
%23 = fdiv double %22, %4
%24 = tail call double @llvm.floor.f64(double %23)
%25 = fneg double %24
%26 = tail call double @llvm.fmuladd.f64(double %25, double %4, double %22)
%27 = ashr i32 %9, 1
%28 = icmp ult i32 %9, 2
br i1 %28, label %29, label %8
29: ; preds = %20, %3
%30 = phi double [ 1.000000e+00, %3 ], [ %21, %20 ]
%31 = fptosi double %30 to i32
ret i32 %31
}