#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*/ BN_MONT_CTX;
typedef int /*<<< orphan*/ BN_CTX;
typedef int /*<<< orphan*/ BIO;
typedef int /*<<< orphan*/ BIGNUM;

/* Variables and functions */
int /*<<< orphan*/ BN_CTX_end(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_CTX_get(int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_CTX_start(int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_MONT_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_MONT_CTX_new();
int /*<<< orphan*/ BN_MONT_CTX_set(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                   int /*<<< orphan*/ *);
char *BN_bn2hex(int /*<<< orphan*/ *);
scalar_t__ BN_cmp(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_generate_prime_ex(int /*<<< orphan*/ *, int,
                                        int /*<<< orphan*/,
                                        int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_mod_exp(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                              int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                              int /*<<< orphan*/ *);
int /*<<< orphan*/
BN_mod_exp_mont_consttime(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_rand(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, int, char *, char *);
int test_mod_exp_sizes(BIO *bp, BN_CTX *ctx) {
  BN_MONT_CTX *mont_ctx;
  BIGNUM *p, *x, *y, *r, *r2;
  int size;
  int ok = 0;
  BN_CTX_start(ctx);
  p = BN_CTX_get(ctx);
  x = BN_CTX_get(ctx);
  y = BN_CTX_get(ctx);
  r = BN_CTX_get(ctx);
  r2 = BN_CTX_get(ctx);
  mont_ctx = BN_MONT_CTX_new();
  if (r2 == NULL || mont_ctx == NULL)
    goto err;
  if (!BN_generate_prime_ex(p, 32, 0, NULL, NULL, NULL) ||
      !BN_MONT_CTX_set(mont_ctx, p, ctx))
    goto err;
  for (size = 32; size < 1024; size += 8) {
    if (!BN_rand(x, size, -1, 0) || !BN_rand(y, size, -1, 0) ||
        !BN_mod_exp_mont_consttime(r, x, y, p, ctx, mont_ctx) ||
        !BN_mod_exp(r2, x, y, p, ctx))
      goto err;
    if (BN_cmp(r, r2) != 0) {
      char *r_str = BN_bn2hex(r);
      char *r2_str = BN_bn2hex(r2);
      printf("Incorrect answer at size %d: %s vs %s\n", size, r_str, r2_str);
      free(r_str);
      free(r2_str);
      goto err;
    }
  }
  ok = 1;
err:
  BN_MONT_CTX_free(mont_ctx);
  BN_CTX_end(ctx);
  return ok;
}


x86 assembly (clang, -O0):
<test_mod_exp_sizes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   10a0 <BN_CTX_start@plt>
mov    -0x10(%rbp),%rdi
call   1050 <BN_CTX_get@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <BN_CTX_get@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <BN_CTX_get@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <BN_CTX_get@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <BN_CTX_get@plt>
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1060 <BN_MONT_CTX_new@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1262 <test_mod_exp_sizes+0x82>
cmpq   $0x0,-0x18(%rbp)
jne    1267 <test_mod_exp_sizes+0x87>
jmp    13bc <test_mod_exp_sizes+0x1dc>
mov    -0x20(%rbp),%rdi
mov    $0x20,%esi
xor    %edx,%edx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%rcx
mov    %r9,%r8
call   1080 <BN_generate_prime_ex@plt>
cmp    $0x0,%eax
je     12a5 <test_mod_exp_sizes+0xc5>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   10d0 <BN_MONT_CTX_set@plt>
cmp    $0x0,%eax
jne    12aa <test_mod_exp_sizes+0xca>
jmp    13bc <test_mod_exp_sizes+0x1dc>
movl   $0x20,-0x44(%rbp)
cmpl   $0x400,-0x44(%rbp)
jge    13b5 <test_mod_exp_sizes+0x1d5>
mov    -0x28(%rbp),%rdi
mov    -0x44(%rbp),%esi
mov    $0xffffffff,%edx
xor    %ecx,%ecx
call   1030 <BN_rand@plt>
cmp    $0x0,%eax
je     133e <test_mod_exp_sizes+0x15e>
mov    -0x30(%rbp),%rdi
mov    -0x44(%rbp),%esi
mov    $0xffffffff,%edx
xor    %ecx,%ecx
call   1030 <BN_rand@plt>
cmp    $0x0,%eax
je     133e <test_mod_exp_sizes+0x15e>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    -0x18(%rbp),%r9
call   10f0 <BN_mod_exp_mont_consttime@plt>
cmp    $0x0,%eax
je     133e <test_mod_exp_sizes+0x15e>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%r8
call   10c0 <BN_mod_exp@plt>
cmp    $0x0,%eax
jne    1343 <test_mod_exp_sizes+0x163>
jmp    13bc <test_mod_exp_sizes+0x1dc>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   10e0 <BN_cmp@plt>
cmp    $0x0,%rax
je     13a2 <test_mod_exp_sizes+0x1c2>
mov    -0x38(%rbp),%rdi
call   10b0 <BN_bn2hex@plt>
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%rdi
call   10b0 <BN_bn2hex@plt>
mov    %rax,-0x58(%rbp)
mov    -0x44(%rbp),%esi
mov    -0x50(%rbp),%rdx
mov    -0x58(%rbp),%rcx
lea    0xc7a(%rip),%rdi
call   1070 <printf@plt>
mov    -0x50(%rbp),%rdi
call   1040 <free@plt>
mov    -0x58(%rbp),%rdi
call   1040 <free@plt>
jmp    13bc <test_mod_exp_sizes+0x1dc>
jmp    13a7 <test_mod_exp_sizes+0x1c7>
mov    -0x44(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0x44(%rbp)
jmp    12b1 <test_mod_exp_sizes+0xd1>
movl   $0x1,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1100 <BN_MONT_CTX_free@plt>
mov    -0x10(%rbp),%rdi
call   1090 <BN_CTX_end@plt>
mov    -0x48(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_mod_exp_sizes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r13
mov    %rsi,%rdi
call   10a0 <BN_CTX_start@plt>
mov    %r13,%rdi
call   1050 <BN_CTX_get@plt>
mov    %rax,%r12
mov    %r13,%rdi
call   1050 <BN_CTX_get@plt>
mov    %rax,0x18(%rsp)
mov    %r13,%rdi
call   1050 <BN_CTX_get@plt>
mov    %rax,%r15
mov    %r13,%rdi
call   1050 <BN_CTX_get@plt>
mov    %rax,%r14
mov    %r13,%rdi
call   1050 <BN_CTX_get@plt>
mov    %rax,%rbp
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <BN_MONT_CTX_new@plt>
test   %rbp,%rbp
mov    %rax,(%rsp)
je     13be <test_mod_exp_sizes+0x1de>
mov    %rbp,0x8(%rsp)
mov    %r12,0x20(%rsp)
test   %rax,%rax
je     13be <test_mod_exp_sizes+0x1de>
xor    %ebx,%ebx
mov    0x20(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x20,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1080 <BN_generate_prime_ex@plt>
test   %eax,%eax
je     13be <test_mod_exp_sizes+0x1de>
mov    (%rsp),%rdi
mov    %rbp,%rsi
mov    %r13,%rdx
call   10d0 <BN_MONT_CTX_set@plt>
test   %eax,%eax
mov    0x18(%rsp),%rbp
je     13be <test_mod_exp_sizes+0x1de>
mov    $0x20,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %ebx,%ebx
mov    %rbp,%rdi
mov    %r12d,%esi
mov    $0xffffffff,%edx
xor    %ecx,%ecx
call   1030 <BN_rand@plt>
test   %eax,%eax
je     13be <test_mod_exp_sizes+0x1de>
xor    %ebx,%ebx
mov    %r15,%rdi
mov    %r12d,%esi
mov    $0xffffffff,%edx
xor    %ecx,%ecx
call   1030 <BN_rand@plt>
test   %eax,%eax
je     13be <test_mod_exp_sizes+0x1de>
mov    %r15,%rbx
mov    %r14,%r15
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
mov    %rbp,%r14
mov    0x20(%rsp),%rbp
mov    %rbp,%rcx
mov    %r13,%r8
mov    (%rsp),%r9
call   10f0 <BN_mod_exp_mont_consttime@plt>
test   %eax,%eax
je     13bc <test_mod_exp_sizes+0x1dc>
mov    %r12,0x10(%rsp)
mov    0x8(%rsp),%r12
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r13,%r8
mov    %rbx,%r13
mov    %rbx,%rdx
mov    %rbp,%rcx
mov    %r8,%rbx
call   10c0 <BN_mod_exp@plt>
test   %eax,%eax
je     13b9 <test_mod_exp_sizes+0x1d9>
mov    %r15,%r14
mov    %r15,%rdi
mov    %r12,%rsi
call   10e0 <BN_cmp@plt>
test   %rax,%rax
jne    137a <test_mod_exp_sizes+0x19a>
mov    0x10(%rsp),%rcx
lea    0x8(%rcx),%eax
cmp    $0x3f8,%ecx
mov    %eax,%r12d
mov    0x18(%rsp),%rbp
mov    %r13,%r15
mov    %rbx,%r13
jb     12b0 <test_mod_exp_sizes+0xd0>
mov    $0x1,%ebx
jmp    13be <test_mod_exp_sizes+0x1de>
mov    %r14,%rdi
call   10b0 <BN_bn2hex@plt>
mov    %rax,%r14
mov    0x8(%rsp),%rdi
call   10b0 <BN_bn2hex@plt>
mov    %rax,%rbp
lea    0xc67(%rip),%rdi
mov    0x10(%rsp),%rsi
mov    %r14,%rdx
mov    %rax,%rcx
call   1070 <printf@plt>
mov    %r14,%rdi
call   1040 <free@plt>
mov    %rbp,%rdi
call   1040 <free@plt>
mov    %rbx,%r13
xor    %ebx,%ebx
mov    (%rsp),%rdi
call   1100 <BN_MONT_CTX_free@plt>
mov    %r13,%rdi
call   1090 <BN_CTX_end@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [39 x i8] c"Incorrect answer at size %d: %s vs %s\0A\00"
define i32 @test_mod_exp_sizes(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%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 i8*
%14 = alloca i8*
store i32* %0, i32** %3
store i32* %1, i32** %4
store i32 0, i32* %12
%15 = load i32*, i32** %4
%16 = call i32 @BN_CTX_start(i32* %15)
%17 = load i32*, i32** %4
%18 = call i32* @BN_CTX_get(i32* %17)
store i32* %18, i32** %6
%19 = load i32*, i32** %4
%20 = call i32* @BN_CTX_get(i32* %19)
store i32* %20, i32** %7
%21 = load i32*, i32** %4
%22 = call i32* @BN_CTX_get(i32* %21)
store i32* %22, i32** %8
%23 = load i32*, i32** %4
%24 = call i32* @BN_CTX_get(i32* %23)
store i32* %24, i32** %9
%25 = load i32*, i32** %4
%26 = call i32* @BN_CTX_get(i32* %25)
store i32* %26, i32** %10
%27 = call i32* (...) @BN_MONT_CTX_new()
store i32* %27, i32** %5
%28 = load i32*, i32** %10
%29 = icmp eq i32* %28, null
br i1 %29, label %33, label %30
30: ; preds = %2
%31 = load i32*, i32** %5
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %34
33: ; preds = %30, %2
br label %100
34: ; preds = %30
%35 = load i32*, i32** %6
%36 = call i32 @BN_generate_prime_ex(i32* %35, i32 32, i32 0, i32* null, i32* null, i32* null)
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %34
%39 = load i32*, i32** %5
%40 = load i32*, i32** %6
%41 = load i32*, i32** %4
%42 = call i32 @BN_MONT_CTX_set(i32* %39, i32* %40, i32* %41)
%43 = icmp ne i32 %42, 0
br i1 %43, label %45, label %44
44: ; preds = %38, %34
br label %100
45: ; preds = %38
store i32 32, i32* %11
br label %46
46: ; preds = %96, %45
%47 = load i32, i32* %11
%48 = icmp slt i32 %47, 1024
br i1 %48, label %49, label %99
49: ; preds = %46
%50 = load i32*, i32** %7
%51 = load i32, i32* %11
%52 = call i32 @BN_rand(i32* %50, i32 %51, i32 -1, i32 0)
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %76
54: ; preds = %49
%55 = load i32*, i32** %8
%56 = load i32, i32* %11
%57 = call i32 @BN_rand(i32* %55, i32 %56, i32 -1, i32 0)
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %76
59: ; preds = %54
%60 = load i32*, i32** %9
%61 = load i32*, i32** %7
%62 = load i32*, i32** %8
%63 = load i32*, i32** %6
%64 = load i32*, i32** %4
%65 = load i32*, i32** %5
%66 = call i32 @BN_mod_exp_mont_consttime(i32* %60, i32* %61, i32* %62, i32* %63, i32* %64, i32* %65)
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %76
68: ; preds = %59
%69 = load i32*, i32** %10
%70 = load i32*, i32** %7
%71 = load i32*, i32** %8
%72 = load i32*, i32** %6
%73 = load i32*, i32** %4
%74 = call i32 @BN_mod_exp(i32* %69, i32* %70, i32* %71, i32* %72, i32* %73)
%75 = icmp ne i32 %74, 0
br i1 %75, label %77, label %76
76: ; preds = %68, %59, %54, %49
br label %100
77: ; preds = %68
%78 = load i32*, i32** %9
%79 = load i32*, i32** %10
%80 = call i64 @BN_cmp(i32* %78, i32* %79)
%81 = icmp ne i64 %80, 0
br i1 %81, label %82, label %95
82: ; preds = %77
%83 = load i32*, i32** %9
%84 = call i8* @BN_bn2hex(i32* %83)
store i8* %84, i8** %13
%85 = load i32*, i32** %10
%86 = call i8* @BN_bn2hex(i32* %85)
store i8* %86, i8** %14
%87 = load i32, i32* %11
%88 = load i8*, i8** %13
%89 = load i8*, i8** %14
%90 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %87, i8* %88, i8* %89)
%91 = load i8*, i8** %13
%92 = call i32 @free(i8* %91)
%93 = load i8*, i8** %14
%94 = call i32 @free(i8* %93)
br label %100
95: ; preds = %77
br label %96
96: ; preds = %95
%97 = load i32, i32* %11
%98 = add nsw i32 %97, 8
store i32 %98, i32* %11
br label %46
99: ; preds = %46
store i32 1, i32* %12
br label %100
100: ; preds = %99, %82, %76, %44, %33
%101 = load i32*, i32** %5
%102 = call i32 @BN_MONT_CTX_free(i32* %101)
%103 = load i32*, i32** %4
%104 = call i32 @BN_CTX_end(i32* %103)
%105 = load i32, i32* %12
ret i32 %105
}

LLVM IR (-O2):
@.str = [39 x i8] c"Incorrect answer at size %d: %s vs %s\0A\00"
define i32 @test_mod_exp_sizes(i32* nocapture readnone %0, i32* %1) {
%3 = tail call i32 @BN_CTX_start(i32* %1)
%4 = tail call i32* @BN_CTX_get(i32* %1)
%5 = tail call i32* @BN_CTX_get(i32* %1)
%6 = tail call i32* @BN_CTX_get(i32* %1)
%7 = tail call i32* @BN_CTX_get(i32* %1)
%8 = tail call i32* @BN_CTX_get(i32* %1)
%9 = tail call i32* (...) @BN_MONT_CTX_new()
%10 = icmp eq i32* %8, null
%11 = icmp eq i32* %9, null
%12 = select i1 %10, i1 true, i1 %11
br i1 %12, label %44, label %13
13: ; preds = %2
%14 = tail call i32 @BN_generate_prime_ex(i32* %4, i32 32, i32 0, i32* null, i32* null, i32* null)
%15 = icmp eq i32 %14, 0
br i1 %15, label %44, label %16
16: ; preds = %13
%17 = tail call i32 @BN_MONT_CTX_set(i32* %9, i32* %4, i32* %1)
%18 = icmp eq i32 %17, 0
br i1 %18, label %44, label %19
19: ; preds = %16, %41
%20 = phi i32 [ %42, %41 ], [ 32, %16 ]
%21 = tail call i32 @BN_rand(i32* %5, i32 %20, i32 -1, i32 0)
%22 = icmp eq i32 %21, 0
br i1 %22, label %44, label %23
23: ; preds = %19
%24 = tail call i32 @BN_rand(i32* %6, i32 %20, i32 -1, i32 0)
%25 = icmp eq i32 %24, 0
br i1 %25, label %44, label %26
26: ; preds = %23
%27 = tail call i32 @BN_mod_exp_mont_consttime(i32* %7, i32* %5, i32* %6, i32* %4, i32* %1, i32* %9)
%28 = icmp eq i32 %27, 0
br i1 %28, label %44, label %29
29: ; preds = %26
%30 = tail call i32 @BN_mod_exp(i32* %8, i32* %5, i32* %6, i32* %4, i32* %1)
%31 = icmp eq i32 %30, 0
br i1 %31, label %44, label %32
32: ; preds = %29
%33 = tail call i64 @BN_cmp(i32* %7, i32* %8)
%34 = icmp eq i64 %33, 0
br i1 %34, label %41, label %35
35: ; preds = %32
%36 = tail call i8* @BN_bn2hex(i32* %7)
%37 = tail call i8* @BN_bn2hex(i32* %8)
%38 = tail call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %20, i8* %36, i8* %37)
%39 = tail call i32 @free(i8* %36)
%40 = tail call i32 @free(i8* %37)
br label %44
41: ; preds = %32
%42 = add nuw nsw i32 %20, 8
%43 = icmp ult i32 %20, 1016
br i1 %43, label %19, label %44
44: ; preds = %41, %19, %23, %26, %29, %35, %13, %16, %2
%45 = phi i32 [ 0, %2 ], [ 0, %35 ], [ 0, %16 ], [ 0, %13 ], [ 1, %41 ], [ 0, %19 ], [ 0, %23 ], [ 0, %26 ], [ 0, %29 ]
%46 = tail call i32 @BN_MONT_CTX_free(i32* %9)
%47 = tail call i32 @BN_CTX_end(i32* %1)
ret i32 %45
}