#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*/ 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_mod_inverse(int /*<<< orphan*/ *,
                                  int /*<<< orphan*/ const *,
                                  int /*<<< orphan*/ const *,
                                  int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_mod_mul(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                              int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                              int /*<<< orphan*/ *);
char *ERR_error_string(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ ERR_get_error();
int /*<<< orphan*/ ERR_load_crypto_strings();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
int sub(BIGNUM *result, const BIGNUM *a, const BIGNUM *b, const BIGNUM *n2,
        BN_CTX *ctx) {
  int ret = 0;
  BN_CTX_start(ctx);
  BIGNUM *b_inv = BN_CTX_get(ctx);
  if (!BN_mod_inverse(b_inv, b, n2, ctx))
    goto end;
  if (!BN_mod_mul(result, a, b_inv, n2, ctx))
    goto end;
  ret = 0;
end:
  if (ret) {
    ERR_load_crypto_strings();
    fprintf(stderr, "sub: %s", ERR_error_string(ERR_get_error(), NULL));
  }
  BN_CTX_end(ctx);
  return ret;
}


x86 assembly (clang, -O0):
<sub>:
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,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <BN_CTX_start@plt>
mov    -0x28(%rbp),%rdi
call   1030 <BN_CTX_get@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1040 <BN_mod_inverse@plt>
cmp    $0x0,%eax
jne    11ec <sub+0x5c>
jmp    121a <sub+0x8a>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   1080 <BN_mod_mul@plt>
cmp    $0x0,%eax
jne    1213 <sub+0x83>
jmp    121a <sub+0x8a>
movl   $0x0,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     125b <sub+0xcb>
mov    $0x0,%al
call   10b0 <ERR_load_crypto_strings@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    $0x0,%al
call   10a0 <ERR_get_error@plt>
mov    %eax,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1090 <ERR_error_string@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xdaa(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1050 <BN_CTX_end@plt>
mov    -0x2c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sub>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%rbx
mov    %rcx,%r14
mov    %rdx,%rbp
mov    %rsi,%r15
mov    %rdi,%r12
mov    %r8,%rdi
call   1060 <BN_CTX_start@plt>
mov    %rbx,%rdi
call   1030 <BN_CTX_get@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1040 <BN_mod_inverse@plt>
test   %eax,%eax
je     11a6 <sub+0x56>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
mov    %r14,%rcx
mov    %rbx,%r8
call   1070 <BN_mod_mul@plt>
mov    %rbx,%rdi
call   1050 <BN_CTX_end@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [8 x i8] c"sub: %s\00"
define i32 @sub(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* %11
%13 = load i32*, i32** %10
%14 = call i32 @BN_CTX_start(i32* %13)
%15 = load i32*, i32** %10
%16 = call i32* @BN_CTX_get(i32* %15)
store i32* %16, i32** %12
%17 = load i32*, i32** %12
%18 = load i32*, i32** %8
%19 = load i32*, i32** %9
%20 = load i32*, i32** %10
%21 = call i32 @BN_mod_inverse(i32* %17, i32* %18, i32* %19, i32* %20)
%22 = icmp ne i32 %21, 0
br i1 %22, label %24, label %23
23: ; preds = %5
br label %34
24: ; preds = %5
%25 = load i32*, i32** %6
%26 = load i32*, i32** %7
%27 = load i32*, i32** %12
%28 = load i32*, i32** %9
%29 = load i32*, i32** %10
%30 = call i32 @BN_mod_mul(i32* %25, i32* %26, i32* %27, i32* %28, i32* %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %24
br label %34
33: ; preds = %24
store i32 0, i32* %11
br label %34
34: ; preds = %33, %32, %23
%35 = load i32, i32* %11
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %34
%38 = call i32 (...) @ERR_load_crypto_strings()
%39 = load i32, i32* @stderr
%40 = call i32 (...) @ERR_get_error()
%41 = call i8* @ERR_error_string(i32 %40, i32* null)
%42 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %41)
br label %43
43: ; preds = %37, %34
%44 = load i32*, i32** %10
%45 = call i32 @BN_CTX_end(i32* %44)
%46 = load i32, i32* %11
ret i32 %46
}

LLVM IR (-O2):
@stderr = global i32 0
define i32 @sub(i32* %0, i32* %1, i32* %2, i32* %3, i32* %4) {
%6 = tail call i32 @BN_CTX_start(i32* %4)
%7 = tail call i32* @BN_CTX_get(i32* %4)
%8 = tail call i32 @BN_mod_inverse(i32* %7, i32* %2, i32* %3, i32* %4)
%9 = icmp eq i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %5
%11 = tail call i32 @BN_mod_mul(i32* %0, i32* %1, i32* %7, i32* %3, i32* %4)
br label %12
12: ; preds = %10, %5
%13 = tail call i32 @BN_CTX_end(i32* %4)
ret i32 0
}