#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_clear_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_mod_mul(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                              int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                              int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_new();
int /*<<< orphan*/ BN_sub(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *);
BIGNUM *calculate_h(BIGNUM *q_inv, BIGNUM *mp, BIGNUM *mq, BIGNUM *p,
                    BN_CTX *ctx) {
  BIGNUM *m_diff = BN_new();
  BN_sub(m_diff, mp, mq);
  BIGNUM *h = BN_new();
  BN_mod_mul(h, q_inv, m_diff, p, ctx);
  BN_clear_free(m_diff);
  return h;
}


x86 assembly (clang, -O0):
<calculate_h>:
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)
mov    $0x0,%al
call   1030 <BN_new@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <BN_sub@plt>
mov    $0x0,%al
call   1030 <BN_new@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   1050 <BN_mod_mul@plt>
mov    -0x30(%rbp),%rdi
call   1060 <BN_clear_free@plt>
mov    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calculate_h>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r13
xor    %eax,%eax
call   1030 <BN_new@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1040 <BN_sub@plt>
xor    %eax,%eax
call   1030 <BN_new@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
mov    %r15,%rcx
mov    %r14,%r8
call   1050 <BN_mod_mul@plt>
mov    %rbp,%rdi
call   1060 <BN_clear_free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32* @calculate_h(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
%13 = call i32* (...) @BN_new()
store i32* %13, i32** %11
%14 = load i32*, i32** %11
%15 = load i32*, i32** %7
%16 = load i32*, i32** %8
%17 = call i32 @BN_sub(i32* %14, i32* %15, i32* %16)
%18 = call i32* (...) @BN_new()
store i32* %18, i32** %12
%19 = load i32*, i32** %12
%20 = load i32*, i32** %6
%21 = load i32*, i32** %11
%22 = load i32*, i32** %9
%23 = load i32*, i32** %10
%24 = call i32 @BN_mod_mul(i32* %19, i32* %20, i32* %21, i32* %22, i32* %23)
%25 = load i32*, i32** %11
%26 = call i32 @BN_clear_free(i32* %25)
%27 = load i32*, i32** %12
ret i32* %27
}

LLVM IR (-O2):
define i32* @calculate_h(i32* %0, i32* %1, i32* %2, i32* %3, i32* %4) {
%6 = tail call i32* (...) @BN_new()
%7 = tail call i32 @BN_sub(i32* %6, i32* %1, i32* %2)
%8 = tail call i32* (...) @BN_new()
%9 = tail call i32 @BN_mod_mul(i32* %8, i32* %0, i32* %6, i32* %3, i32* %4)
%10 = tail call i32 @BN_clear_free(i32* %6)
ret i32* %8
}