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

/* Variables and functions */
int /*<<< orphan*/ BN_free(int /*<<< orphan*/ *);
scalar_t__ BN_is_zero(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_new();
int /*<<< orphan*/ BN_rand_range(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_GROUP_get_order(int /*<<< orphan*/ const *,
                                      int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ *EC_KEY_get0_group(int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_KEY_set_private_key(int /*<<< orphan*/ *,
                                          int /*<<< orphan*/ *);
int /*<<< orphan*/ ERR_GOST_error(int, int, char *, int);
int gost2001_compute_public(int /*<<< orphan*/ *);
int gost2001_keygen(EC_KEY *ec) {
  BIGNUM *order = BN_new(), *d = BN_new();
  const EC_GROUP *group = EC_KEY_get0_group(ec);
  EC_GROUP_get_order(group, order, NULL);
  do {
    if (!BN_rand_range(d, order)) {
      ERR_GOST_error(
          (107), (125),
          "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
          "2022-01-22/02-19-57/repos/caidongyun/nginx-openresty-windows/refs/"
          "heads/master/Ouroboros/External/OpenSSL/openssl-1.0.0e/engines/"
          "ccgost/gost2001.c",
          331);
      BN_free(d);
      BN_free(order);
      return 0;
    }
  } while (BN_is_zero(d));
  EC_KEY_set_private_key(ec, d);
  BN_free(d);
  BN_free(order);
  return gost2001_compute_public(ec);
}


x86 assembly (clang, -O0):
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <BN_new@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <BN_new@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   10a0 <EC_KEY_get0_group@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <EC_GROUP_get_order@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <BN_rand_range@plt>
cmp    $0x0,%eax
jne    121f <gost2001_keygen+0x8f>
mov    $0x6b,%edi
mov    $0x7d,%esi
lea    0xe09(%rip),%rdx
mov    $0x14b,%ecx
call   10b0 <ERR_GOST_error@plt>
mov    -0x20(%rbp),%rdi
call   1070 <BN_free@plt>
mov    -0x18(%rbp),%rdi
call   1070 <BN_free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1262 <gost2001_keygen+0xd2>
jmp    1224 <gost2001_keygen+0x94>
mov    -0x20(%rbp),%rdi
call   1090 <BN_is_zero@plt>
cmp    $0x0,%rax
jne    11d0 <gost2001_keygen+0x40>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <EC_KEY_set_private_key@plt>
mov    -0x20(%rbp),%rdi
call   1070 <BN_free@plt>
mov    -0x18(%rbp),%rdi
call   1070 <BN_free@plt>
mov    -0x10(%rbp),%rdi
call   1030 <gost2001_compute_public@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %eax,%eax
call   1040 <BN_new@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1040 <BN_new@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   10a0 <EC_KEY_get0_group@plt>
mov    %rax,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   1060 <EC_GROUP_get_order@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <BN_rand_range@plt>
test   %eax,%eax
je     1214 <gost2001_keygen+0x84>
mov    %rbx,%rdi
call   1090 <BN_is_zero@plt>
test   %rax,%rax
jne    11d0 <gost2001_keygen+0x40>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <EC_KEY_set_private_key@plt>
mov    %rbx,%rdi
call   1070 <BN_free@plt>
mov    %r15,%rdi
call   1070 <BN_free@plt>
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <gost2001_compute_public@plt>
lea    0xde5(%rip),%rdx
mov    $0x6b,%edi
mov    $0x7d,%esi
mov    $0x14b,%ecx
call   10b0 <ERR_GOST_error@plt>
mov    %rbx,%rdi
call   1070 <BN_free@plt>
mov    %r15,%rdi
call   1070 <BN_free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [211 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/caidongyun/nginx-openresty-windows/refs/heads/master/Ouroboros/External/OpenSSL/openssl-1.0.0e/engines/ccgost/gost2001.c\00"
define i32 @gost2001_keygen(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
store i32* %0, i32** %3
%7 = call i32* (...) @BN_new()
store i32* %7, i32** %4
%8 = call i32* (...) @BN_new()
store i32* %8, i32** %5
%9 = load i32*, i32** %3
%10 = call i32* @EC_KEY_get0_group(i32* %9)
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = load i32*, i32** %4
%13 = call i32 @EC_GROUP_get_order(i32* %11, i32* %12, i32* null)
br label %14
14: ; preds = %26, %1
%15 = load i32*, i32** %5
%16 = load i32*, i32** %4
%17 = call i32 @BN_rand_range(i32* %15, i32* %16)
%18 = icmp ne i32 %17, 0
br i1 %18, label %25, label %19
19: ; preds = %14
%20 = call i32 @ERR_GOST_error(i32 107, i32 125, i8* getelementptr inbounds ([211 x i8], [211 x i8]* @.str, i64 0, i64 0), i32 331)
%21 = load i32*, i32** %5
%22 = call i32 @BN_free(i32* %21)
%23 = load i32*, i32** %4
%24 = call i32 @BN_free(i32* %23)
store i32 0, i32* %2
br label %40
25: ; preds = %14
br label %26
26: ; preds = %25
%27 = load i32*, i32** %5
%28 = call i64 @BN_is_zero(i32* %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %14, label %30
30: ; preds = %26
%31 = load i32*, i32** %3
%32 = load i32*, i32** %5
%33 = call i32 @EC_KEY_set_private_key(i32* %31, i32* %32)
%34 = load i32*, i32** %5
%35 = call i32 @BN_free(i32* %34)
%36 = load i32*, i32** %4
%37 = call i32 @BN_free(i32* %36)
%38 = load i32*, i32** %3
%39 = call i32 @gost2001_compute_public(i32* %38)
store i32 %39, i32* %2
br label %40
40: ; preds = %30, %19
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
@.str = [211 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/caidongyun/nginx-openresty-windows/refs/heads/master/Ouroboros/External/OpenSSL/openssl-1.0.0e/engines/ccgost/gost2001.c\00"
define i32 @gost2001_keygen(i32* %0) {
%2 = tail call i32* (...) @BN_new()
%3 = tail call i32* (...) @BN_new()
%4 = tail call i32* @EC_KEY_get0_group(i32* %0)
%5 = tail call i32 @EC_GROUP_get_order(i32* %4, i32* %2, i32* null)
br label %6
6: ; preds = %13, %1
%7 = tail call i32 @BN_rand_range(i32* %3, i32* %2)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %6
%10 = tail call i32 @ERR_GOST_error(i32 107, i32 125, i8* getelementptr inbounds ([211 x i8], [211 x i8]* @.str, i64 0, i64 0), i32 331)
%11 = tail call i32 @BN_free(i32* %3)
%12 = tail call i32 @BN_free(i32* %2)
br label %21
13: ; preds = %6
%14 = tail call i64 @BN_is_zero(i32* %3)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %6
16: ; preds = %13
%17 = tail call i32 @EC_KEY_set_private_key(i32* %0, i32* %3)
%18 = tail call i32 @BN_free(i32* %3)
%19 = tail call i32 @BN_free(i32* %2)
%20 = tail call i32 @gost2001_compute_public(i32* %0)
br label %21
21: ; preds = %16, %9
%22 = phi i32 [ %20, %16 ], [ 0, %9 ]
ret i32 %22
}