#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*/ const 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*/ const *, size_t const,
                             int /*<<< orphan*/ *);
int BN_kronecker(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                 int /*<<< orphan*/ *);
int BN_num_bytes(int /*<<< orphan*/ const *);
int RIPEMD160_DIGEST_LENGTH;
int /*<<< orphan*/ fdh(int /*<<< orphan*/ const *, int,
                       int /*<<< orphan*/ const *, size_t const);
int /*<<< orphan*/ free(int /*<<< orphan*/ const *);
scalar_t__ malloc(size_t const);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ const *,
                          int /*<<< orphan*/ const *, int);
int getkey(BIGNUM *a, const uint8_t *id, int len, const BIGNUM *N) {
  uint8_t *tmp;
  BN_CTX *ctx = BN_CTX_new();
  const size_t num_bytes = BN_num_bytes(N) - 1;
  uint8_t *buf = (uint8_t *)malloc(num_bytes);
  uint8_t hash[RIPEMD160_DIGEST_LENGTH];
  fdh(id, len, buf, num_bytes);
  BN_bin2bn(hash, num_bytes, a);
  while (BN_kronecker(a, N, ctx) != 1) {
    memcpy(hash, buf, RIPEMD160_DIGEST_LENGTH);
    fdh(hash, RIPEMD160_DIGEST_LENGTH, buf, num_bytes);
    BN_bin2bn(buf, num_bytes, a);
  }
  free(buf);
  BN_CTX_free(ctx);
  return 0;
}


x86 assembly (clang, -O0):
<getkey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
call   1090 <BN_CTX_new@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   10b0 <BN_num_bytes@plt>
add    $0xffffffff,%eax
cltq
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x48(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x40(%rbp),%rdx
mov    -0x38(%rbp),%rcx
call   1050 <fdh@plt>
mov    -0x58(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x8(%rbp),%rdx
call   1060 <BN_bin2bn@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <BN_kronecker@plt>
cmp    $0x1,%eax
je     1283 <getkey+0xf3>
mov    -0x58(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    0x2dac(%rip),%rax
mov    (%rax),%edx
call   1080 <memcpy@plt>
mov    -0x58(%rbp),%rdi
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
mov    -0x40(%rbp),%rdx
mov    -0x38(%rbp),%rcx
call   1050 <fdh@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x8(%rbp),%rdx
call   1060 <BN_bin2bn@plt>
jmp    1223 <getkey+0x93>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
mov    -0x30(%rbp),%rdi
call   1070 <BN_CTX_free@plt>
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getkey>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r15
mov    %edx,-0x30(%rbp)
mov    %rsi,-0x40(%rbp)
mov    %rdi,%r12
xor    %eax,%eax
call   1090 <BN_CTX_new@plt>
mov    %rax,-0x38(%rbp)
mov    %r15,%rdi
call   10b0 <BN_num_bytes@plt>
add    $0xffffffff,%eax
movslq %eax,%r13
mov    %r13,%rdi
call   10a0 <malloc@plt>
mov    %rax,%rbx
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
lea    0xf(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    %rbx,%rdx
mov    %r13,%rcx
call   1050 <fdh@plt>
mov    %r14,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
call   1060 <BN_bin2bn@plt>
mov    %r12,%rdi
mov    %r15,-0x30(%rbp)
mov    %r15,%rsi
nopw   0x0(%rax,%rax,1)
mov    -0x38(%rbp),%rdx
call   1030 <BN_kronecker@plt>
cmp    $0x1,%eax
je     126b <getkey+0xdb>
mov    0x2dc3(%rip),%r15
mov    (%r15),%edx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <memcpy@plt>
mov    (%r15),%esi
mov    %r14,%rdi
mov    %rbx,%rdx
mov    %r13,%rcx
call   1050 <fdh@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
call   1060 <BN_bin2bn@plt>
mov    %r12,%rdi
mov    -0x30(%rbp),%rsi
jmp    1220 <getkey+0x90>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    -0x38(%rbp),%rdi
call   1070 <BN_CTX_free@plt>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@RIPEMD160_DIGEST_LENGTH = global i32 0
define i32 @getkey(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 i64
%12 = alloca i32*
%13 = alloca i8*
%14 = alloca i64
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** %10
%16 = load i32*, i32** %8
%17 = call i32 @BN_num_bytes(i32* %16)
%18 = sub nsw i32 %17, 1
%19 = sext i32 %18 to i64
store i64 %19, i64* %11
%20 = load i64, i64* %11
%21 = call i64 @malloc(i64 %20)
%22 = inttoptr i64 %21 to i32*
store i32* %22, i32** %12
%23 = load i32, i32* @RIPEMD160_DIGEST_LENGTH
%24 = zext i32 %23 to i64
%25 = call i8* @llvm.stacksave()
store i8* %25, i8** %13
%26 = alloca i32, i64 %24
store i64 %24, i64* %14
%27 = load i32*, i32** %6
%28 = load i32, i32* %7
%29 = load i32*, i32** %12
%30 = load i64, i64* %11
%31 = call i32 @fdh(i32* %27, i32 %28, i32* %29, i64 %30)
%32 = load i64, i64* %11
%33 = load i32*, i32** %5
%34 = call i32 @BN_bin2bn(i32* %26, i64 %32, i32* %33)
br label %35
35: ; preds = %41, %4
%36 = load i32*, i32** %5
%37 = load i32*, i32** %8
%38 = load i32*, i32** %10
%39 = call i32 @BN_kronecker(i32* %36, i32* %37, i32* %38)
%40 = icmp ne i32 %39, 1
br i1 %40, label %41, label %53
41: ; preds = %35
%42 = load i32*, i32** %12
%43 = load i32, i32* @RIPEMD160_DIGEST_LENGTH
%44 = call i32 @memcpy(i32* %26, i32* %42, i32 %43)
%45 = load i32, i32* @RIPEMD160_DIGEST_LENGTH
%46 = load i32*, i32** %12
%47 = load i64, i64* %11
%48 = call i32 @fdh(i32* %26, i32 %45, i32* %46, i64 %47)
%49 = load i32*, i32** %12
%50 = load i64, i64* %11
%51 = load i32*, i32** %5
%52 = call i32 @BN_bin2bn(i32* %49, i64 %50, i32* %51)
br label %35
53: ; preds = %35
%54 = load i32*, i32** %12
%55 = call i32 @free(i32* %54)
%56 = load i32*, i32** %10
%57 = call i32 @BN_CTX_free(i32* %56)
%58 = load i8*, i8** %13
ret i32 0
}

LLVM IR (-O2):
@RIPEMD160_DIGEST_LENGTH = global i32 0
define i32 @getkey(i32* %0, i32* %1, i32 %2, i32* %3) {
%5 = tail call i32* (...) @BN_CTX_new()
%6 = tail call i32 @BN_num_bytes(i32* %3)
%7 = add nsw i32 %6, -1
%8 = sext i32 %7 to i64
%9 = tail call i64 @malloc(i64 %8)
%10 = inttoptr i64 %9 to i32*
%11 = load i32, i32* @RIPEMD160_DIGEST_LENGTH
%12 = zext i32 %11 to i64
%13 = alloca i32, i64 %12
%14 = tail call i32 @fdh(i32* %1, i32 %2, i32* %10, i64 %8)
%15 = call i32 @BN_bin2bn(i32* %13, i64 %8, i32* %0)
%16 = call i32 @BN_kronecker(i32* %0, i32* %3, i32* %5)
%17 = icmp eq i32 %16, 1
br i1 %17, label %26, label %18
18: ; preds = %4, %18
%19 = load i32, i32* @RIPEMD160_DIGEST_LENGTH
%20 = call i32 @memcpy(i32* %13, i32* %10, i32 %19)
%21 = load i32, i32* @RIPEMD160_DIGEST_LENGTH
%22 = call i32 @fdh(i32* %13, i32 %21, i32* %10, i64 %8)
%23 = call i32 @BN_bin2bn(i32* %10, i64 %8, i32* %0)
%24 = call i32 @BN_kronecker(i32* %0, i32* %3, i32* %5)
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %18
26: ; preds = %18, %4
%27 = call i32 @free(i32* %10)
%28 = call i32 @BN_CTX_free(i32* %5)
ret i32 0
}