#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_CTX;
typedef int /*<<< orphan*/ const BIGNUM;

/* Variables and functions */
int /*<<< orphan*/ BN_CTX_end(int /*<<< orphan*/ *);
int /*<<< orphan*/ const *BN_CTX_get(int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_CTX_start(int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_mul(int /*<<< orphan*/ const *,
                          int /*<<< orphan*/ const *,
                          int /*<<< orphan*/ const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_nnmod(int /*<<< orphan*/ const *,
                            int /*<<< orphan*/ const *,
                            int /*<<< orphan*/ const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_sqr(int /*<<< orphan*/ const *,
                          int /*<<< orphan*/ const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ bn_check_top(int /*<<< orphan*/ const *);
int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
               BN_CTX *ctx) {
  BIGNUM *t;
  int ret = 0;
  bn_check_top(a);
  bn_check_top(b);
  bn_check_top(m);
  BN_CTX_start(ctx);
  if ((t = BN_CTX_get(ctx)) == NULL)
    goto err;
  if (a == b) {
    if (!BN_sqr(t, a, ctx))
      goto err;
  } else {
    if (!BN_mul(t, a, b, ctx))
      goto err;
  }
  if (!BN_nnmod(r, t, m, ctx))
    goto err;
  bn_check_top(r);
  ret = 1;
err:
  BN_CTX_end(ctx);
  return (ret);
}


x86 assembly (clang, -O0):
<BN_mod_mul>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <bn_check_top@plt>
mov    -0x18(%rbp),%rdi
call   1050 <bn_check_top@plt>
mov    -0x20(%rbp),%rdi
call   1050 <bn_check_top@plt>
mov    -0x28(%rbp),%rdi
call   1080 <BN_CTX_start@plt>
mov    -0x28(%rbp),%rdi
call   1030 <BN_CTX_get@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11d3 <BN_mod_mul+0x63>
jmp    1260 <BN_mod_mul+0xf0>
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    1205 <BN_mod_mul+0x95>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <BN_sqr@plt>
cmp    $0x0,%eax
jne    1200 <BN_mod_mul+0x90>
jmp    1260 <BN_mod_mul+0xf0>
jmp    122d <BN_mod_mul+0xbd>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1060 <BN_mul@plt>
cmp    $0x0,%eax
jne    1228 <BN_mod_mul+0xb8>
jmp    1260 <BN_mod_mul+0xf0>
jmp    122d <BN_mod_mul+0xbd>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1090 <BN_nnmod@plt>
cmp    $0x0,%eax
jne    1250 <BN_mod_mul+0xe0>
jmp    1260 <BN_mod_mul+0xf0>
mov    -0x8(%rbp),%rdi
call   1050 <bn_check_top@plt>
movl   $0x1,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <BN_CTX_end@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BN_mod_mul>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r13
mov    %rcx,%r12
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %rdi,(%rsp)
mov    %rsi,%rdi
call   1050 <bn_check_top@plt>
mov    %rbx,%rdi
call   1050 <bn_check_top@plt>
mov    %r12,%rdi
call   1050 <bn_check_top@plt>
mov    %r13,%rdi
call   1080 <BN_CTX_start@plt>
mov    %r13,%rdi
call   1030 <BN_CTX_get@plt>
xor    %r15d,%r15d
test   %rax,%rax
je     120d <BN_mod_mul+0x9d>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbp,%rsi
cmp    %rbx,%rbp
je     11da <BN_mod_mul+0x6a>
mov    %rbx,%rdx
mov    %r13,%rcx
call   1060 <BN_mul@plt>
test   %eax,%eax
jne    11e6 <BN_mod_mul+0x76>
jmp    120d <BN_mod_mul+0x9d>
mov    %r13,%rdx
call   1040 <BN_sqr@plt>
test   %eax,%eax
je     120d <BN_mod_mul+0x9d>
mov    (%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
call   1090 <BN_nnmod@plt>
test   %eax,%eax
je     120d <BN_mod_mul+0x9d>
mov    %rbx,%rdi
call   1050 <bn_check_top@plt>
mov    $0x1,%r15d
mov    %r13,%rdi
call   1070 <BN_CTX_end@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @BN_mod_mul(i32* %0, i32* %1, i32* %2, i32* %3, i32* %4) {
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32
store i32* %0, i32** %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store i32* %4, i32** %10
store i32 0, i32* %12
%13 = load i32*, i32** %7
%14 = call i32 @bn_check_top(i32* %13)
%15 = load i32*, i32** %8
%16 = call i32 @bn_check_top(i32* %15)
%17 = load i32*, i32** %9
%18 = call i32 @bn_check_top(i32* %17)
%19 = load i32*, i32** %10
%20 = call i32 @BN_CTX_start(i32* %19)
%21 = load i32*, i32** %10
%22 = call i32* @BN_CTX_get(i32* %21)
store i32* %22, i32** %11
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %25
24: ; preds = %5
br label %57
25: ; preds = %5
%26 = load i32*, i32** %7
%27 = load i32*, i32** %8
%28 = icmp eq i32* %26, %27
br i1 %28, label %29, label %37
29: ; preds = %25
%30 = load i32*, i32** %11
%31 = load i32*, i32** %7
%32 = load i32*, i32** %10
%33 = call i32 @BN_sqr(i32* %30, i32* %31, i32* %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %36, label %35
35: ; preds = %29
br label %57
36: ; preds = %29
br label %46
37: ; preds = %25
%38 = load i32*, i32** %11
%39 = load i32*, i32** %7
%40 = load i32*, i32** %8
%41 = load i32*, i32** %10
%42 = call i32 @BN_mul(i32* %38, i32* %39, i32* %40, i32* %41)
%43 = icmp ne i32 %42, 0
br i1 %43, label %45, label %44
44: ; preds = %37
br label %57
45: ; preds = %37
br label %46
46: ; preds = %45, %36
%47 = load i32*, i32** %6
%48 = load i32*, i32** %11
%49 = load i32*, i32** %9
%50 = load i32*, i32** %10
%51 = call i32 @BN_nnmod(i32* %47, i32* %48, i32* %49, i32* %50)
%52 = icmp ne i32 %51, 0
br i1 %52, label %54, label %53
53: ; preds = %46
br label %57
54: ; preds = %46
%55 = load i32*, i32** %6
%56 = call i32 @bn_check_top(i32* %55)
store i32 1, i32* %12
br label %57
57: ; preds = %54, %53, %44, %35, %24
%58 = load i32*, i32** %10
%59 = call i32 @BN_CTX_end(i32* %58)
%60 = load i32, i32* %12
ret i32 %60
}

LLVM IR (-O2):
define i32 @BN_mod_mul(i32* %0, i32* %1, i32* %2, i32* %3, i32* %4) {
%6 = tail call i32 @bn_check_top(i32* %1)
%7 = tail call i32 @bn_check_top(i32* %2)
%8 = tail call i32 @bn_check_top(i32* %3)
%9 = tail call i32 @BN_CTX_start(i32* %4)
%10 = tail call i32* @BN_CTX_get(i32* %4)
%11 = icmp eq i32* %10, null
br i1 %11, label %25, label %12
12: ; preds = %5
%13 = icmp eq i32* %1, %2
br i1 %13, label %14, label %17
14: ; preds = %12
%15 = tail call i32 @BN_sqr(i32* %10, i32* %1, i32* %4)
%16 = icmp eq i32 %15, 0
br i1 %16, label %25, label %20
17: ; preds = %12
%18 = tail call i32 @BN_mul(i32* %10, i32* %1, i32* %2, i32* %4)
%19 = icmp eq i32 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %17, %14
%21 = tail call i32 @BN_nnmod(i32* %0, i32* %10, i32* %3, i32* %4)
%22 = icmp eq i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = tail call i32 @bn_check_top(i32* %0)
br label %25
25: ; preds = %20, %17, %14, %5, %23
%26 = phi i32 [ 0, %5 ], [ 1, %23 ], [ 0, %20 ], [ 0, %14 ], [ 0, %17 ]
%27 = tail call i32 @BN_CTX_end(i32* %4)
ret i32 %26
}