#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*/ BIGNUM;

/* Variables and functions */
int /*<<< orphan*/ BN_bin2bn(unsigned char const *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_clear_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_new();
int /*<<< orphan*/ EC_KEY_set_private_key(int /*<<< orphan*/ *,
                                          int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int store_private_key(EC_KEY *key, const unsigned char *buf, int len) {
  BIGNUM *priv_key = BN_new();
  if (!priv_key) {
    fprintf(stderr, "BN_new failed\n");
    return 1;
  }
  BN_bin2bn(buf, len, priv_key);
  if (!EC_KEY_set_private_key(key, priv_key)) {
    fprintf(stderr, "EC_KEY_set_private_key failed\n");
    BN_clear_free(priv_key);
    return 1;
  }
  BN_clear_free(priv_key);
  return 0;
}


x86 assembly (clang, -O0):
<store_private_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <BN_new@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119a <store_private_key+0x4a>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
lea    0xe77(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11fa <store_private_key+0xaa>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rdx
call   1040 <BN_bin2bn@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <EC_KEY_set_private_key@plt>
cmp    $0x0,%eax
jne    11ea <store_private_key+0x9a>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe3f(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1070 <BN_clear_free@plt>
movl   $0x1,-0x4(%rbp)
jmp    11fa <store_private_key+0xaa>
mov    -0x28(%rbp),%rdi
call   1070 <BN_clear_free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<store_private_key>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %rdi,%r14
xor    %eax,%eax
call   1030 <BN_new@plt>
test   %rax,%rax
je     1198 <store_private_key+0x48>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %r15d,%esi
mov    %rax,%rdx
call   1040 <BN_bin2bn@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <EC_KEY_set_private_key@plt>
test   %eax,%eax
je     11af <store_private_key+0x5f>
mov    %rbx,%rdi
call   1070 <BN_clear_free@plt>
xor    %eax,%eax
jmp    11d1 <store_private_key+0x81>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
call   1060 <fprintf@plt>
jmp    11cc <store_private_key+0x7c>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1070 <BN_clear_free@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [15 x i8] c"BN_new failed\0A\00"
@.str.1 = [31 x i8] c"EC_KEY_set_private_key failed\0A\00"
define i32 @store_private_key(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = call i32* (...) @BN_new()
store i32* %9, i32** %8
%10 = load i32*, i32** %8
%11 = icmp ne i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %3
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %32
15: ; preds = %3
%16 = load i8*, i8** %6
%17 = load i32, i32* %7
%18 = load i32*, i32** %8
%19 = call i32 @BN_bin2bn(i8* %16, i32 %17, i32* %18)
%20 = load i32*, i32** %5
%21 = load i32*, i32** %8
%22 = call i32 @EC_KEY_set_private_key(i32* %20, i32* %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %15
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32*, i32** %8
%28 = call i32 @BN_clear_free(i32* %27)
store i32 1, i32* %4
br label %32
29: ; preds = %15
%30 = load i32*, i32** %8
%31 = call i32 @BN_clear_free(i32* %30)
store i32 0, i32* %4
br label %32
32: ; preds = %29, %24, %12
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"BN_new failed\0A\00"
@.str.1 = [31 x i8] c"EC_KEY_set_private_key failed\0A\00"
define i32 @store_private_key(i32* %0, i8* %1, i32 %2) {
%4 = tail call i32* (...) @BN_new()
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %19
9: ; preds = %3
%10 = tail call i32 @BN_bin2bn(i8* %1, i32 %2, i32* %4)
%11 = tail call i32 @EC_KEY_set_private_key(i32* %0, i32* %4)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @BN_clear_free(i32* %4)
br label %19
17: ; preds = %9
%18 = tail call i32 @BN_clear_free(i32* %4)
br label %19
19: ; preds = %17, %13, %6
%20 = phi i32 [ 0, %17 ], [ 1, %13 ], [ 1, %6 ]
ret i32 %20
}