#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 ERR_R_INTERNAL_ERROR;
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);
  if (!group || !EC_GROUP_get_order(group, order, NULL)) {
    ERR_GOST_error(
        (107), (ERR_R_INTERNAL_ERROR),
        "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
        "2022-01-22/02-19-57/repos/Orav/kbengine/refs/heads/master/kbe/src/lib/"
        "dependencies/openssl/engines/ccgost/gost2001.c",
        440);
    BN_free(d);
    BN_free(order);
    return 0;
  }
  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/Orav/kbengine/refs/heads/master/kbe/src/"
          "lib/dependencies/openssl/engines/ccgost/gost2001.c",
          448);
      BN_free(d);
      BN_free(order);
      return 0;
    }
  } while (BN_is_zero(d));
  if (!EC_KEY_set_private_key(ec, d)) {
    ERR_GOST_error(
        (107), (ERR_R_INTERNAL_ERROR),
        "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
        "2022-01-22/02-19-57/repos/Orav/kbengine/refs/heads/master/kbe/src/lib/"
        "dependencies/openssl/engines/ccgost/gost2001.c",
        458);
    BN_free(d);
    BN_free(order);
    return 0;
  }
  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)
cmpq   $0x0,-0x28(%rbp)
je     11e4 <gost2001_keygen+0x54>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <EC_GROUP_get_order@plt>
cmp    $0x0,%eax
jne    1221 <gost2001_keygen+0x91>
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
mov    $0x6b,%edi
lea    0xe07(%rip),%rdx
mov    $0x1b8,%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    12fe <gost2001_keygen+0x16e>
jmp    1226 <gost2001_keygen+0x96>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <BN_rand_range@plt>
cmp    $0x0,%eax
jne    1275 <gost2001_keygen+0xe5>
mov    $0x6b,%edi
mov    $0x7d,%esi
lea    0xdb3(%rip),%rdx
mov    $0x1c0,%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    12fe <gost2001_keygen+0x16e>
jmp    127a <gost2001_keygen+0xea>
mov    -0x20(%rbp),%rdi
call   1090 <BN_is_zero@plt>
cmp    $0x0,%rax
jne    1226 <gost2001_keygen+0x96>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <EC_KEY_set_private_key@plt>
cmp    $0x0,%eax
jne    12e0 <gost2001_keygen+0x150>
mov    0x2d3e(%rip),%rax
mov    (%rax),%esi
mov    $0x6b,%edi
lea    0xd48(%rip),%rdx
mov    $0x1ca,%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    12fe <gost2001_keygen+0x16e>
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,%r15
xor    %eax,%eax
call   1040 <BN_new@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1040 <BN_new@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   10a0 <EC_KEY_get0_group@plt>
test   %rax,%rax
je     1218 <gost2001_keygen+0x88>
mov    %rax,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1060 <EC_GROUP_get_order@plt>
test   %eax,%eax
je     1218 <gost2001_keygen+0x88>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <BN_rand_range@plt>
test   %eax,%eax
je     1234 <gost2001_keygen+0xa4>
mov    %rbx,%rdi
call   1090 <BN_is_zero@plt>
test   %rax,%rax
jne    11d0 <gost2001_keygen+0x40>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1080 <EC_KEY_set_private_key@plt>
test   %eax,%eax
je     124c <gost2001_keygen+0xbc>
mov    %rbx,%rdi
call   1070 <BN_free@plt>
mov    %r14,%rdi
call   1070 <BN_free@plt>
mov    %r15,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <gost2001_compute_public@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
lea    0xdd8(%rip),%rdx
mov    $0x6b,%edi
mov    $0x1b8,%ecx
jmp    1266 <gost2001_keygen+0xd6>
lea    0xdc5(%rip),%rdx
mov    $0x6b,%edi
mov    $0x7d,%esi
mov    $0x1c0,%ecx
jmp    1266 <gost2001_keygen+0xd6>
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
lea    0xda4(%rip),%rdx
mov    $0x6b,%edi
mov    $0x1ca,%ecx
call   10b0 <ERR_GOST_error@plt>
mov    %rbx,%rdi
call   1070 <BN_free@plt>
mov    %r14,%rdi
call   1070 <BN_free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@ERR_R_INTERNAL_ERROR = global i32 0
@.str = [181 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Orav/kbengine/refs/heads/master/kbe/src/lib/dependencies/openssl/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 = icmp ne i32* %11, null
br i1 %12, label %13, label %18
13: ; preds = %1
%14 = load i32*, i32** %6
%15 = load i32*, i32** %4
%16 = call i32 @EC_GROUP_get_order(i32* %14, i32* %15, i32* null)
%17 = icmp ne i32 %16, 0
br i1 %17, label %25, label %18
18: ; preds = %13, %1
%19 = load i32, i32* @ERR_R_INTERNAL_ERROR
%20 = call i32 @ERR_GOST_error(i32 107, i32 %19, i8* getelementptr inbounds ([181 x i8], [181 x i8]* @.str, i64 0, i64 0), i32 440)
%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 %61
25: ; preds = %13
br label %26
26: ; preds = %38, %25
%27 = load i32*, i32** %5
%28 = load i32*, i32** %4
%29 = call i32 @BN_rand_range(i32* %27, i32* %28)
%30 = icmp ne i32 %29, 0
br i1 %30, label %37, label %31
31: ; preds = %26
%32 = call i32 @ERR_GOST_error(i32 107, i32 125, i8* getelementptr inbounds ([181 x i8], [181 x i8]* @.str, i64 0, i64 0), i32 448)
%33 = load i32*, i32** %5
%34 = call i32 @BN_free(i32* %33)
%35 = load i32*, i32** %4
%36 = call i32 @BN_free(i32* %35)
store i32 0, i32* %2
br label %61
37: ; preds = %26
br label %38
38: ; preds = %37
%39 = load i32*, i32** %5
%40 = call i64 @BN_is_zero(i32* %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %26, label %42
42: ; preds = %38
%43 = load i32*, i32** %3
%44 = load i32*, i32** %5
%45 = call i32 @EC_KEY_set_private_key(i32* %43, i32* %44)
%46 = icmp ne i32 %45, 0
br i1 %46, label %54, label %47
47: ; preds = %42
%48 = load i32, i32* @ERR_R_INTERNAL_ERROR
%49 = call i32 @ERR_GOST_error(i32 107, i32 %48, i8* getelementptr inbounds ([181 x i8], [181 x i8]* @.str, i64 0, i64 0), i32 458)
%50 = load i32*, i32** %5
%51 = call i32 @BN_free(i32* %50)
%52 = load i32*, i32** %4
%53 = call i32 @BN_free(i32* %52)
store i32 0, i32* %2
br label %61
54: ; preds = %42
%55 = load i32*, i32** %5
%56 = call i32 @BN_free(i32* %55)
%57 = load i32*, i32** %4
%58 = call i32 @BN_free(i32* %57)
%59 = load i32*, i32** %3
%60 = call i32 @gost2001_compute_public(i32* %59)
store i32 %60, i32* %2
br label %61
61: ; preds = %54, %47, %31, %18
%62 = load i32, i32* %2
ret i32 %62
}

LLVM IR (-O2):
@ERR_R_INTERNAL_ERROR = global i32 0
@.str = [181 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Orav/kbengine/refs/heads/master/kbe/src/lib/dependencies/openssl/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 = icmp eq i32* %4, null
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = tail call i32 @EC_GROUP_get_order(i32* %4, i32* %2, i32* null)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %6, %1
%10 = load i32, i32* @ERR_R_INTERNAL_ERROR
%11 = tail call i32 @ERR_GOST_error(i32 107, i32 %10, i8* getelementptr inbounds ([181 x i8], [181 x i8]* @.str, i64 0, i64 0), i32 440)
%12 = tail call i32 @BN_free(i32* %3)
%13 = tail call i32 @BN_free(i32* %2)
br label %36
14: ; preds = %6, %21
%15 = tail call i32 @BN_rand_range(i32* %3, i32* %2)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = tail call i32 @ERR_GOST_error(i32 107, i32 125, i8* getelementptr inbounds ([181 x i8], [181 x i8]* @.str, i64 0, i64 0), i32 448)
%19 = tail call i32 @BN_free(i32* %3)
%20 = tail call i32 @BN_free(i32* %2)
br label %36
21: ; preds = %14
%22 = tail call i64 @BN_is_zero(i32* %3)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %14
24: ; preds = %21
%25 = tail call i32 @EC_KEY_set_private_key(i32* %0, i32* %3)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %24
%28 = load i32, i32* @ERR_R_INTERNAL_ERROR
%29 = tail call i32 @ERR_GOST_error(i32 107, i32 %28, i8* getelementptr inbounds ([181 x i8], [181 x i8]* @.str, i64 0, i64 0), i32 458)
%30 = tail call i32 @BN_free(i32* %3)
%31 = tail call i32 @BN_free(i32* %2)
br label %36
32: ; preds = %24
%33 = tail call i32 @BN_free(i32* %3)
%34 = tail call i32 @BN_free(i32* %2)
%35 = tail call i32 @gost2001_compute_public(i32* %0)
br label %36
36: ; preds = %32, %27, %17, %9
%37 = phi i32 [ %35, %32 ], [ 0, %27 ], [ 0, %17 ], [ 0, %9 ]
ret i32 %37
}