#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 uint8_t;
typedef int /*<<< orphan*/ uint16_t;

/* Variables and functions */
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ mpi_moduu(int /*<<< orphan*/ *, int /*<<< orphan*/ const *);
int /*<<< orphan*/ mpi_muluu(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                             int /*<<< orphan*/ *);
void mpi_powm65537(uint16_t *m, const uint16_t *n) {
  uint8_t count = 16;
  uint16_t bufprod[128 * 2];
  uint16_t bufmod[128];
  memcpy(bufmod, m, 128 * 2);
  do {
    mpi_muluu(bufprod, bufmod, bufmod);
    mpi_moduu(bufprod, n);
    memcpy(bufmod, bufprod, 128 * 2);
  } while (--count);
  mpi_muluu(bufprod, bufmod, m);
  mpi_moduu(bufprod, n);
  memcpy(m, bufprod, 128 * 2);
}


x86 assembly (clang, -O0):
<mpi_powm65537>:
push   %rbp
mov    %rsp,%rbp
sub    $0x620,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x10,-0x14(%rbp)
lea    -0x620(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x100,%edx
call   1050 <memcpy@plt>
lea    -0x420(%rbp),%rdi
lea    -0x620(%rbp),%rsi
lea    -0x620(%rbp),%rdx
call   1040 <mpi_muluu@plt>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <mpi_moduu@plt>
lea    -0x620(%rbp),%rdi
lea    -0x420(%rbp),%rsi
mov    $0x100,%edx
call   1050 <memcpy@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jne    115f <mpi_powm65537+0x2f>
lea    -0x420(%rbp),%rdi
lea    -0x620(%rbp),%rsi
mov    -0x8(%rbp),%rdx
call   1040 <mpi_muluu@plt>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <mpi_moduu@plt>
mov    -0x8(%rbp),%rdi
lea    -0x420(%rbp),%rsi
mov    $0x100,%edx
call   1050 <memcpy@plt>
add    $0x620,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mpi_powm65537>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x600,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsp,%r12
mov    %r12,%rdi
mov    %r14,%rsi
mov    $0x100,%edx
call   1050 <memcpy@plt>
mov    $0xfffffff0,%ebp
lea    0x200(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r12,%rdx
call   1040 <mpi_muluu@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <mpi_moduu@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1050 <memcpy@plt>
inc    %ebp
jne    1170 <mpi_powm65537+0x40>
lea    0x200(%rsp),%rbx
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1040 <mpi_muluu@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <mpi_moduu@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1050 <memcpy@plt>
add    $0x600,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @mpi_powm65537(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca [256 x i32]
%7 = alloca [128 x i32]
store i32* %0, i32** %3
store i32* %1, i32** %4
store i32 16, i32* %5
%8 = getelementptr inbounds [128 x i32], [128 x i32]* %7, i64 0, i64 0
%9 = load i32*, i32** %3
%10 = call i32 @memcpy(i32* %8, i32* %9, i32 256)
br label %11
11: ; preds = %22, %2
%12 = getelementptr inbounds [256 x i32], [256 x i32]* %6, i64 0, i64 0
%13 = getelementptr inbounds [128 x i32], [128 x i32]* %7, i64 0, i64 0
%14 = getelementptr inbounds [128 x i32], [128 x i32]* %7, i64 0, i64 0
%15 = call i32 @mpi_muluu(i32* %12, i32* %13, i32* %14)
%16 = getelementptr inbounds [256 x i32], [256 x i32]* %6, i64 0, i64 0
%17 = load i32*, i32** %4
%18 = call i32 @mpi_moduu(i32* %16, i32* %17)
%19 = getelementptr inbounds [128 x i32], [128 x i32]* %7, i64 0, i64 0
%20 = getelementptr inbounds [256 x i32], [256 x i32]* %6, i64 0, i64 0
%21 = call i32 @memcpy(i32* %19, i32* %20, i32 256)
br label %22
22: ; preds = %11
%23 = load i32, i32* %5
%24 = add nsw i32 %23, -1
store i32 %24, i32* %5
%25 = icmp ne i32 %24, 0
br i1 %25, label %11, label %26
26: ; preds = %22
%27 = getelementptr inbounds [256 x i32], [256 x i32]* %6, i64 0, i64 0
%28 = getelementptr inbounds [128 x i32], [128 x i32]* %7, i64 0, i64 0
%29 = load i32*, i32** %3
%30 = call i32 @mpi_muluu(i32* %27, i32* %28, i32* %29)
%31 = getelementptr inbounds [256 x i32], [256 x i32]* %6, i64 0, i64 0
%32 = load i32*, i32** %4
%33 = call i32 @mpi_moduu(i32* %31, i32* %32)
%34 = load i32*, i32** %3
%35 = getelementptr inbounds [256 x i32], [256 x i32]* %6, i64 0, i64 0
%36 = call i32 @memcpy(i32* %34, i32* %35, i32 256)
ret void
}

LLVM IR (-O2):
define void @mpi_powm65537(i32* %0, i32* %1) {
%3 = alloca [256 x i32]
%4 = alloca [128 x i32]
%5 = bitcast [256 x i32]* %3 to i8*
%6 = bitcast [128 x i32]* %4 to i8*
%7 = getelementptr inbounds [128 x i32], [128 x i32]* %4, i64 0, i64 0
%8 = call i32 @memcpy(i32* %7, i32* %0, i32 256)
%9 = getelementptr inbounds [256 x i32], [256 x i32]* %3, i64 0, i64 0
br label %10
10: ; preds = %10, %2
%11 = phi i32 [ 16, %2 ], [ %15, %10 ]
%12 = call i32 @mpi_muluu(i32* %9, i32* %7, i32* %7)
%13 = call i32 @mpi_moduu(i32* %9, i32* %1)
%14 = call i32 @memcpy(i32* %7, i32* %9, i32 256)
%15 = add nsw i32 %11, -1
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %10
17: ; preds = %10
%18 = call i32 @mpi_muluu(i32* %9, i32* %7, i32* %0)
%19 = call i32 @mpi_moduu(i32* %9, i32* %1)
%20 = call i32 @memcpy(i32* %0, i32* %9, i32 256)
ret void
}