#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*/ BIGNUM;

/* Variables and functions */
int /*<<< orphan*/ BN_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_new();
int /*<<< orphan*/ BN_set_word(int /*<<< orphan*/ *, int);
int BN_sub(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ *my_pow(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                           int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int);
BIGNUM *invm(BIGNUM *x, BIGNUM *m) {
  BIGNUM *bnTwo = BN_new();
  BN_set_word(bnTwo, 2);
  BIGNUM *postSubtract = BN_new();
  int ret = BN_sub(postSubtract, m, bnTwo);
  if (ret != 1) {
    printf("invm: BN_sub failed: %d", ret);
  }
  BIGNUM *retN = my_pow(x, postSubtract, m);
  BN_free(bnTwo);
  BN_free(postSubtract);
  return retN;
}


x86 assembly (clang, -O0):
<invm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <BN_new@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x2,%esi
call   1030 <BN_set_word@plt>
mov    $0x0,%al
call   1040 <BN_new@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1070 <BN_sub@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
je     11c1 <invm+0x61>
mov    -0x24(%rbp),%esi
lea    0xe44(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1050 <my_pow@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <BN_free@plt>
mov    -0x20(%rbp),%rdi
call   1080 <BN_free@plt>
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<invm>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
xor    %eax,%eax
call   1040 <BN_new@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x2,%esi
call   1030 <BN_set_word@plt>
xor    %eax,%eax
call   1040 <BN_new@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1070 <BN_sub@plt>
cmp    $0x1,%eax
je     11b0 <invm+0x50>
lea    0xe57(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1050 <my_pow@plt>
mov    %rax,%r14
mov    %r12,%rdi
call   1080 <BN_free@plt>
mov    %rbx,%rdi
call   1080 <BN_free@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [24 x i8] c"invm: BN_sub failed: %d\00"
define i32* @invm(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
store i32* %0, i32** %3
store i32* %1, i32** %4
%9 = call i32* (...) @BN_new()
store i32* %9, i32** %5
%10 = load i32*, i32** %5
%11 = call i32 @BN_set_word(i32* %10, i32 2)
%12 = call i32* (...) @BN_new()
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = load i32*, i32** %4
%15 = load i32*, i32** %5
%16 = call i32 @BN_sub(i32* %13, i32* %14, i32* %15)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp ne i32 %17, 1
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = load i32, i32* %7
%21 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %19, %2
%23 = load i32*, i32** %3
%24 = load i32*, i32** %6
%25 = load i32*, i32** %4
%26 = call i32* @my_pow(i32* %23, i32* %24, i32* %25)
store i32* %26, i32** %8
%27 = load i32*, i32** %5
%28 = call i32 @BN_free(i32* %27)
%29 = load i32*, i32** %6
%30 = call i32 @BN_free(i32* %29)
%31 = load i32*, i32** %8
ret i32* %31
}

LLVM IR (-O2):
@.str = [24 x i8] c"invm: BN_sub failed: %d\00"
define i32* @invm(i32* %0, i32* %1) {
%3 = tail call i32* (...) @BN_new()
%4 = tail call i32 @BN_set_word(i32* %3, i32 2)
%5 = tail call i32* (...) @BN_new()
%6 = tail call i32 @BN_sub(i32* %5, i32* %1, i32* %3)
%7 = icmp eq i32 %6, 1
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %6)
br label %10
10: ; preds = %8, %2
%11 = tail call i32* @my_pow(i32* %0, i32* %5, i32* %1)
%12 = tail call i32 @BN_free(i32* %3)
%13 = tail call i32 @BN_free(i32* %5)
ret i32* %11
}