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

/* Variables and functions */
int /*<<< orphan*/ BN_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_CTX_new();
int /*<<< orphan*/ BN_bin2bn(int /*<<< orphan*/ *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_mod_add(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                              int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                              int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_mod_exp(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                              int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                              int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_mod_inverse(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_mod_mul(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                              int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                              int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_mod_sub(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                              int /*<<< orphan*/ const *, int /*<<< orphan*/ *,
                              int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_mul(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                          int /*<<< orphan*/ const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_new();
int extract(BIGNUM *r, const BIGNUM *p, const BIGNUM *q, const BIGNUM *a) {
  BN_CTX *ctx = BN_CTX_new();
  BIGNUM *t1, *t2, *m, *bn8, *bn5;
  t1 = BN_new();
  t2 = BN_new();
  m = BN_new();
  bn5 = BN_new();
  bn8 = BN_new();
  BN_mul(m, p, q, ctx);
  BN_bin2bn((uint8_t *)"\x8", 1, t1);
  BN_mod_inverse(bn8, t1, m, ctx);
  BN_bin2bn((uint8_t *)"\x5", 1, bn5);
  BN_mod_add(t1, m, bn5, m, ctx);
  BN_mod_sub(t2, t1, p, m, ctx);
  BN_mod_sub(t1, t2, q, m, ctx);
  BN_mod_mul(t2, t1, bn8, m, ctx);
  BN_mod_exp(r, a, t2, m, ctx);
  BN_free(t1);
  BN_free(t2);
  BN_free(m);
  BN_free(bn5);
  BN_free(bn8);
  BN_CTX_free(ctx);
  return 0;
}


x86 assembly (clang, -O0):
<extract>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    $0x0,%al
call   10b0 <BN_CTX_new@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1040 <BN_new@plt>
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1040 <BN_new@plt>
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1040 <BN_new@plt>
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1040 <BN_new@plt>
mov    %rax,-0x50(%rbp)
mov    $0x0,%al
call   1040 <BN_new@plt>
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1080 <BN_mul@plt>
mov    -0x30(%rbp),%rdx
lea    0xdd6(%rip),%rdi
mov    $0x1,%esi
call   1060 <BN_bin2bn@plt>
mov    -0x48(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1030 <BN_mod_inverse@plt>
mov    -0x50(%rbp),%rdx
lea    0xdae(%rip),%rdi
mov    $0x1,%esi
call   1060 <BN_bin2bn@plt>
mov    -0x30(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x50(%rbp),%rdx
mov    -0x40(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   10d0 <BN_mod_add@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x40(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   1050 <BN_mod_sub@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x40(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   1050 <BN_mod_sub@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x48(%rbp),%rdx
mov    -0x40(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   10c0 <BN_mod_mul@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x40(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   10a0 <BN_mod_exp@plt>
mov    -0x30(%rbp),%rdi
call   1090 <BN_free@plt>
mov    -0x38(%rbp),%rdi
call   1090 <BN_free@plt>
mov    -0x40(%rbp),%rdi
call   1090 <BN_free@plt>
mov    -0x50(%rbp),%rdi
call   1090 <BN_free@plt>
mov    -0x48(%rbp),%rdi
call   1090 <BN_free@plt>
mov    -0x28(%rbp),%rdi
call   1070 <BN_CTX_free@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extract>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,0x20(%rsp)
mov    %rdx,(%rsp)
mov    %rsi,%r15
mov    %rsi,0x10(%rsp)
mov    %rdi,0x18(%rsp)
xor    %eax,%eax
call   10b0 <BN_CTX_new@plt>
mov    %rax,%r12
xor    %eax,%eax
call   1040 <BN_new@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <BN_new@plt>
mov    %rax,%r13
xor    %eax,%eax
call   1040 <BN_new@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   1040 <BN_new@plt>
mov    %rax,0x8(%rsp)
xor    %eax,%eax
call   1040 <BN_new@plt>
mov    %rax,%r14
mov    %rbp,%rdi
mov    %r15,%rsi
mov    (%rsp),%rdx
mov    %r12,%rcx
call   1080 <BN_mul@plt>
lea    0xdd5(%rip),%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
call   1060 <BN_bin2bn@plt>
mov    %r14,%r15
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
mov    %r12,%rcx
call   1030 <BN_mod_inverse@plt>
lea    0xdaf(%rip),%rdi
mov    $0x1,%esi
mov    0x8(%rsp),%r14
mov    %r14,%rdx
call   1060 <BN_bin2bn@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r14,%rdx
mov    %rbp,%rcx
mov    %r12,%r8
call   10d0 <BN_mod_add@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    0x10(%rsp),%rdx
mov    %rbp,%rcx
mov    %r12,%r8
call   1050 <BN_mod_sub@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    (%rsp),%rdx
mov    %rbp,%rcx
mov    %r12,%r8
call   1050 <BN_mod_sub@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %rbp,%rcx
mov    %r12,%r8
call   10c0 <BN_mod_mul@plt>
mov    0x18(%rsp),%rdi
mov    0x20(%rsp),%rsi
mov    %r13,%rdx
mov    %rbp,%rcx
mov    %r12,%r8
call   10a0 <BN_mod_exp@plt>
mov    %rbx,%rdi
call   1090 <BN_free@plt>
mov    %r13,%rdi
call   1090 <BN_free@plt>
mov    %rbp,%rdi
call   1090 <BN_free@plt>
mov    %r14,%rdi
call   1090 <BN_free@plt>
mov    %r15,%rdi
call   1090 <BN_free@plt>
mov    %r12,%rdi
call   1070 <BN_CTX_free@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"\08\00"
@.str.1 = [2 x i8] c"\05\00"
define i32 @extract(i32* %0, i32* %1, i32* %2, i32* %3) {
%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 i32*
%14 = alloca i32*
store i32* %0, i32** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i32* %3, i32** %8
%15 = call i32* (...) @BN_CTX_new()
store i32* %15, i32** %9
%16 = call i32* (...) @BN_new()
store i32* %16, i32** %10
%17 = call i32* (...) @BN_new()
store i32* %17, i32** %11
%18 = call i32* (...) @BN_new()
store i32* %18, i32** %12
%19 = call i32* (...) @BN_new()
store i32* %19, i32** %14
%20 = call i32* (...) @BN_new()
store i32* %20, i32** %13
%21 = load i32*, i32** %12
%22 = load i32*, i32** %6
%23 = load i32*, i32** %7
%24 = load i32*, i32** %9
%25 = call i32 @BN_mul(i32* %21, i32* %22, i32* %23, i32* %24)
%26 = load i32*, i32** %10
%27 = call i32 @BN_bin2bn(i32* bitcast ([2 x i8]* @.str to i32*), i32 1, i32* %26)
%28 = load i32*, i32** %13
%29 = load i32*, i32** %10
%30 = load i32*, i32** %12
%31 = load i32*, i32** %9
%32 = call i32 @BN_mod_inverse(i32* %28, i32* %29, i32* %30, i32* %31)
%33 = load i32*, i32** %14
%34 = call i32 @BN_bin2bn(i32* bitcast ([2 x i8]* @.str.1 to i32*), i32 1, i32* %33)
%35 = load i32*, i32** %10
%36 = load i32*, i32** %12
%37 = load i32*, i32** %14
%38 = load i32*, i32** %12
%39 = load i32*, i32** %9
%40 = call i32 @BN_mod_add(i32* %35, i32* %36, i32* %37, i32* %38, i32* %39)
%41 = load i32*, i32** %11
%42 = load i32*, i32** %10
%43 = load i32*, i32** %6
%44 = load i32*, i32** %12
%45 = load i32*, i32** %9
%46 = call i32 @BN_mod_sub(i32* %41, i32* %42, i32* %43, i32* %44, i32* %45)
%47 = load i32*, i32** %10
%48 = load i32*, i32** %11
%49 = load i32*, i32** %7
%50 = load i32*, i32** %12
%51 = load i32*, i32** %9
%52 = call i32 @BN_mod_sub(i32* %47, i32* %48, i32* %49, i32* %50, i32* %51)
%53 = load i32*, i32** %11
%54 = load i32*, i32** %10
%55 = load i32*, i32** %13
%56 = load i32*, i32** %12
%57 = load i32*, i32** %9
%58 = call i32 @BN_mod_mul(i32* %53, i32* %54, i32* %55, i32* %56, i32* %57)
%59 = load i32*, i32** %5
%60 = load i32*, i32** %8
%61 = load i32*, i32** %11
%62 = load i32*, i32** %12
%63 = load i32*, i32** %9
%64 = call i32 @BN_mod_exp(i32* %59, i32* %60, i32* %61, i32* %62, i32* %63)
%65 = load i32*, i32** %10
%66 = call i32 @BN_free(i32* %65)
%67 = load i32*, i32** %11
%68 = call i32 @BN_free(i32* %67)
%69 = load i32*, i32** %12
%70 = call i32 @BN_free(i32* %69)
%71 = load i32*, i32** %14
%72 = call i32 @BN_free(i32* %71)
%73 = load i32*, i32** %13
%74 = call i32 @BN_free(i32* %73)
%75 = load i32*, i32** %9
%76 = call i32 @BN_CTX_free(i32* %75)
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c"\08\00"
@.str.1 = [2 x i8] c"\05\00"
define i32 @extract(i32* %0, i32* %1, i32* %2, i32* %3) {
%5 = tail call i32* (...) @BN_CTX_new()
%6 = tail call i32* (...) @BN_new()
%7 = tail call i32* (...) @BN_new()
%8 = tail call i32* (...) @BN_new()
%9 = tail call i32* (...) @BN_new()
%10 = tail call i32* (...) @BN_new()
%11 = tail call i32 @BN_mul(i32* %8, i32* %1, i32* %2, i32* %5)
%12 = tail call i32 @BN_bin2bn(i32* bitcast ([2 x i8]* @.str to i32*), i32 1, i32* %6)
%13 = tail call i32 @BN_mod_inverse(i32* %10, i32* %6, i32* %8, i32* %5)
%14 = tail call i32 @BN_bin2bn(i32* bitcast ([2 x i8]* @.str.1 to i32*), i32 1, i32* %9)
%15 = tail call i32 @BN_mod_add(i32* %6, i32* %8, i32* %9, i32* %8, i32* %5)
%16 = tail call i32 @BN_mod_sub(i32* %7, i32* %6, i32* %1, i32* %8, i32* %5)
%17 = tail call i32 @BN_mod_sub(i32* %6, i32* %7, i32* %2, i32* %8, i32* %5)
%18 = tail call i32 @BN_mod_mul(i32* %7, i32* %6, i32* %10, i32* %8, i32* %5)
%19 = tail call i32 @BN_mod_exp(i32* %0, i32* %3, i32* %7, i32* %8, i32* %5)
%20 = tail call i32 @BN_free(i32* %6)
%21 = tail call i32 @BN_free(i32* %7)
%22 = tail call i32 @BN_free(i32* %8)
%23 = tail call i32 @BN_free(i32* %9)
%24 = tail call i32 @BN_free(i32* %10)
%25 = tail call i32 @BN_CTX_free(i32* %5)
ret i32 0
}