#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 */
unsigned short mulmod_REDC(unsigned short, int, unsigned short const,
                           unsigned short const);
unsigned int invpowmod_REDClr(const unsigned int N, const unsigned short int P,
                              const unsigned short int Ps, int bbits,
                              unsigned short int r) {
  if (r >= P)
    r %= P;
  for (; bbits >= 0; --bbits) {
    r = mulmod_REDC(r, r, P, Ps);
    if (N & (1u << bbits)) {
      r += (r & 1) ? P : 0;
      r >>= 1;
    }
  }
  r = mulmod_REDC(r, 1, P, Ps);
  return r;
}


x86 assembly (clang, -O0):
<invpowmod_REDClr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %r8w,%ax
mov    %edi,-0x4(%rbp)
mov    %si,-0x6(%rbp)
mov    %dx,-0x8(%rbp)
mov    %ecx,-0xc(%rbp)
mov    %ax,-0xe(%rbp)
movzwl -0xe(%rbp),%eax
movzwl -0x6(%rbp),%ecx
cmp    %ecx,%eax
jl     1150 <invpowmod_REDClr+0x40>
movzwl -0x6(%rbp),%ecx
movzwl -0xe(%rbp),%eax
cltd
idiv   %ecx
mov    %dx,%ax
mov    %ax,-0xe(%rbp)
jmp    1155 <invpowmod_REDClr+0x45>
cmpl   $0x0,-0xc(%rbp)
jl     11e9 <invpowmod_REDClr+0xd9>
mov    -0xe(%rbp),%cx
movzwl -0xe(%rbp),%esi
mov    -0x6(%rbp),%ax
movzwl %cx,%edi
movzwl %ax,%edx
movzwl -0x8(%rbp),%ecx
call   1030 <mulmod_REDC@plt>
mov    %ax,-0xe(%rbp)
mov    -0x4(%rbp),%eax
mov    -0xc(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     11d6 <invpowmod_REDClr+0xc6>
movzwl -0xe(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     11b4 <invpowmod_REDClr+0xa4>
movzwl -0x6(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    11be <invpowmod_REDClr+0xae>
xor    %eax,%eax
mov    %eax,-0x14(%rbp)
jmp    11be <invpowmod_REDClr+0xae>
mov    -0x14(%rbp),%ecx
movzwl -0xe(%rbp),%eax
add    %ecx,%eax
mov    %ax,-0xe(%rbp)
movzwl -0xe(%rbp),%eax
sar    $0x1,%eax
mov    %ax,-0xe(%rbp)
jmp    11db <invpowmod_REDClr+0xcb>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1155 <invpowmod_REDClr+0x45>
mov    -0xe(%rbp),%cx
mov    -0x6(%rbp),%ax
mov    $0x1,%esi
movzwl %cx,%edi
movzwl %ax,%edx
movzwl -0x8(%rbp),%ecx
call   1030 <mulmod_REDC@plt>
mov    %ax,-0xe(%rbp)
movzwl -0xe(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<invpowmod_REDClr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8d,%eax
mov    %ecx,%ebx
mov    %edx,%ecx
mov    %esi,%r14d
mov    %edi,%r15d
cmp    %r14w,%ax
jb     1133 <invpowmod_REDClr+0x23>
xor    %edx,%edx
div    %r14w
mov    %edx,%eax
movzwl %r14w,%ebp
movzwl %cx,%r12d
test   %ebx,%ebx
jns    1169 <invpowmod_REDClr+0x59>
movzwl %ax,%edi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r12d,%ecx
call   1030 <mulmod_REDC@plt>
movzwl %ax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
nopl   (%rax)
lea    -0x1(%rbx),%ecx
test   %ebx,%ebx
mov    %ecx,%ebx
jle    113f <invpowmod_REDClr+0x2f>
movzwl %ax,%edi
mov    %edi,%esi
mov    %ebp,%edx
mov    %r12d,%ecx
call   1030 <mulmod_REDC@plt>
bt     %ebx,%r15d
jae    1160 <invpowmod_REDClr+0x50>
mov    %eax,%ecx
and    $0x1,%ecx
neg    %ecx
and    %r14d,%ecx
add    %eax,%ecx
movzwl %cx,%eax
shr    %eax
jmp    1160 <invpowmod_REDClr+0x50>

LLVM IR (-O0):
define i32 @invpowmod_REDClr(i32 %0, i16 zeroext %1, i16 zeroext %2, i32 %3, i16 zeroext %4) {
%6 = alloca i32
%7 = alloca i16
%8 = alloca i16
%9 = alloca i32
%10 = alloca i16
store i32 %0, i32* %6
store i16 %1, i16* %7
store i16 %2, i16* %8
store i32 %3, i32* %9
store i16 %4, i16* %10
%11 = load i16, i16* %10
%12 = zext i16 %11 to i32
%13 = load i16, i16* %7
%14 = zext i16 %13 to i32
%15 = icmp sge i32 %12, %14
br i1 %15, label %16, label %23
16: ; preds = %5
%17 = load i16, i16* %7
%18 = zext i16 %17 to i32
%19 = load i16, i16* %10
%20 = zext i16 %19 to i32
%21 = srem i32 %20, %18
%22 = trunc i32 %21 to i16
store i16 %22, i16* %10
br label %23
23: ; preds = %16, %5
br label %24
24: ; preds = %59, %23
%25 = load i32, i32* %9
%26 = icmp sge i32 %25, 0
br i1 %26, label %27, label %62
27: ; preds = %24
%28 = load i16, i16* %10
%29 = load i16, i16* %10
%30 = zext i16 %29 to i32
%31 = load i16, i16* %7
%32 = load i16, i16* %8
%33 = call zeroext i16 @mulmod_REDC(i16 zeroext %28, i32 %30, i16 zeroext %31, i16 zeroext %32)
store i16 %33, i16* %10
%34 = load i32, i32* %6
%35 = load i32, i32* %9
%36 = shl i32 1, %35
%37 = and i32 %34, %36
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %58
39: ; preds = %27
%40 = load i16, i16* %10
%41 = zext i16 %40 to i32
%42 = and i32 %41, 1
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %39
%45 = load i16, i16* %7
%46 = zext i16 %45 to i32
br label %48
47: ; preds = %39
br label %48
48: ; preds = %47, %44
%49 = phi i32 [ %46, %44 ], [ 0, %47 ]
%50 = load i16, i16* %10
%51 = zext i16 %50 to i32
%52 = add nsw i32 %51, %49
%53 = trunc i32 %52 to i16
store i16 %53, i16* %10
%54 = load i16, i16* %10
%55 = zext i16 %54 to i32
%56 = ashr i32 %55, 1
%57 = trunc i32 %56 to i16
store i16 %57, i16* %10
br label %58
58: ; preds = %48, %27
br label %59
59: ; preds = %58
%60 = load i32, i32* %9
%61 = add nsw i32 %60, -1
store i32 %61, i32* %9
br label %24
62: ; preds = %24
%63 = load i16, i16* %10
%64 = load i16, i16* %7
%65 = load i16, i16* %8
%66 = call zeroext i16 @mulmod_REDC(i16 zeroext %63, i32 1, i16 zeroext %64, i16 zeroext %65)
store i16 %66, i16* %10
%67 = load i16, i16* %10
%68 = zext i16 %67 to i32
ret i32 %68
}

LLVM IR (-O2):
define i32 @invpowmod_REDClr(i32 %0, i16 zeroext %1, i16 zeroext %2, i32 %3, i16 zeroext %4) {
%6 = icmp ult i16 %4, %1
br i1 %6, label %9, label %7
7: ; preds = %5
%8 = urem i16 %4, %1
br label %9
9: ; preds = %7, %5
%10 = phi i16 [ %8, %7 ], [ %4, %5 ]
%11 = icmp sgt i32 %3, -1
br i1 %11, label %12, label %30
12: ; preds = %9, %26
%13 = phi i16 [ %27, %26 ], [ %10, %9 ]
%14 = phi i32 [ %28, %26 ], [ %3, %9 ]
%15 = zext i16 %13 to i32
%16 = tail call zeroext i16 @mulmod_REDC(i16 zeroext %13, i32 %15, i16 zeroext %1, i16 zeroext %2)
%17 = shl nuw i32 1, %14
%18 = and i32 %17, %0
%19 = icmp eq i32 %18, 0
br i1 %19, label %26, label %20
20: ; preds = %12
%21 = and i16 %16, 1
%22 = icmp eq i16 %21, 0
%23 = select i1 %22, i16 0, i16 %1
%24 = add i16 %23, %16
%25 = lshr i16 %24, 1
br label %26
26: ; preds = %12, %20
%27 = phi i16 [ %25, %20 ], [ %16, %12 ]
%28 = add nsw i32 %14, -1
%29 = icmp sgt i32 %14, 0
br i1 %29, label %12, label %30
30: ; preds = %26, %9
%31 = phi i16 [ %10, %9 ], [ %27, %26 ]
%32 = tail call zeroext i16 @mulmod_REDC(i16 zeroext %31, i32 1, i16 zeroext %1, i16 zeroext %2)
%33 = zext i16 %32 to i32
ret i32 %33
}