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

/* Variables and functions */
int /*<<< orphan*/ EC_KEY_check_key(int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_KEY_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EC_KEY_new_by_curve_name(int /*<<< orphan*/);
int /*<<< orphan*/ NID_secp256k1;
scalar_t__ compute_public_key(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/);
unsigned char *get_der_key(int /*<<< orphan*/ *, int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
scalar_t__ store_private_key(int /*<<< orphan*/ *, unsigned char *, int);
int main() {
  EC_KEY *key;
  unsigned char priv_buf[32];
  unsigned char *buf;
  int len;
  key = EC_KEY_new_by_curve_name(NID_secp256k1);
  if (key == NULL) {
    fprintf(stderr, "EC_KEY_new_by_curve_name failed\n");
    return 1;
  }
  fread(priv_buf, 1, 32, stdin);
  if (feof(stdin)) {
    fprintf(stderr, "Requires %d bytes of input\n", 32);
    return 1;
  }
  if (store_private_key(key, priv_buf, sizeof(priv_buf)))
    return 1;
  if (compute_public_key(key))
    return 1;
  if (!EC_KEY_check_key(key)) {
    fprintf(stderr, "EC_KEY_check_key failed\n");
    return 1;
  }
  buf = get_der_key(key, &len);
  if (!buf)
    return 1;
  fwrite(buf, 1, len, stdout);
  free(buf);
  EC_KEY_free(key);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
call   10d0 <EC_KEY_new_by_curve_name@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ff <main+0x4f>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe14(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1332 <main+0x182>
lea    -0x30(%rbp),%rdi
mov    0x2dde(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
mov    $0x20,%edx
call   1050 <fread@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   1090 <feof@plt>
cmp    $0x0,%rax
je     125b <main+0xab>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
lea    0xdde(%rip),%rsi
mov    $0x20,%edx
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1332 <main+0x182>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x20,%edx
call   1060 <store_private_key@plt>
cmp    $0x0,%rax
je     1283 <main+0xd3>
movl   $0x1,-0x4(%rbp)
jmp    1332 <main+0x182>
mov    -0x10(%rbp),%rdi
call   10a0 <compute_public_key@plt>
cmp    $0x0,%rax
je     12a2 <main+0xf2>
movl   $0x1,-0x4(%rbp)
jmp    1332 <main+0x182>
mov    -0x10(%rbp),%rdi
call   1040 <EC_KEY_check_key@plt>
cmp    $0x0,%eax
jne    12d7 <main+0x127>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
lea    0xd79(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1332 <main+0x182>
mov    -0x10(%rbp),%rdi
lea    -0x3c(%rbp),%rsi
call   1070 <get_der_key@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12ff <main+0x14f>
movl   $0x1,-0x4(%rbp)
jmp    1332 <main+0x182>
mov    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%edx
mov    0x2cbb(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   10b0 <EC_KEY_free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
call   10d0 <EC_KEY_new_by_curve_name@plt>
test   %rax,%rax
je     121f <main+0x6f>
mov    %rax,%rbx
mov    0x2e11(%rip),%rbp
mov    0x0(%rbp),%ecx
lea    0x10(%rsp),%rdi
mov    $0x1,%r15d
mov    $0x1,%esi
mov    $0x20,%edx
call   1050 <fread@plt>
mov    0x0(%rbp),%edi
call   1090 <feof@plt>
test   %rax,%rax
je     123e <main+0x8e>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
mov    $0x20,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    1262 <main+0xb2>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xdd1(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%r15d
jmp    1262 <main+0xb2>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x20,%edx
call   1060 <store_private_key@plt>
test   %rax,%rax
jne    1262 <main+0xb2>
mov    %rbx,%rdi
call   10a0 <compute_public_key@plt>
test   %rax,%rax
je     1270 <main+0xc0>
mov    %r15d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1040 <EC_KEY_check_key@plt>
test   %eax,%eax
je     12c0 <main+0x110>
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
call   1070 <get_der_key@plt>
test   %rax,%rax
je     1262 <main+0xb2>
mov    %rax,%r14
mov    0xc(%rsp),%edx
mov    0x2d2c(%rip),%rax
mov    (%rax),%ecx
mov    %r14,%rdi
mov    $0x1,%esi
call   10c0 <fwrite@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   10b0 <EC_KEY_free@plt>
xor    %r15d,%r15d
jmp    1262 <main+0xb2>
mov    0x2d11(%rip),%rax
mov    (%rax),%edi
lea    0xd6d(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    1262 <main+0xb2>

LLVM IR (-O0):
@NID_secp256k1 = global i32 0
@stderr = global i32 0
@.str = [33 x i8] c"EC_KEY_new_by_curve_name failed\0A\00"
@stdin = global i32 0
@.str.1 = [28 x i8] c"Requires %d bytes of input\0A\00"
@.str.2 = [25 x i8] c"EC_KEY_check_key failed\0A\00"
@stdout = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [32 x i8]
%4 = alloca i8*
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @NID_secp256k1
%7 = call i32* @EC_KEY_new_by_curve_name(i32 %6)
store i32* %7, i32** %2
%8 = load i32*, i32** %2
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %56
13: ; preds = %0
%14 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%15 = load i32, i32* @stdin
%16 = call i32 @fread(i8* %14, i32 1, i32 32, i32 %15)
%17 = load i32, i32* @stdin
%18 = call i64 @feof(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %13
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 32)
store i32 1, i32* %1
br label %56
23: ; preds = %13
%24 = load i32*, i32** %2
%25 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%26 = call i64 @store_private_key(i32* %24, i8* %25, i32 32)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %23
store i32 1, i32* %1
br label %56
29: ; preds = %23
%30 = load i32*, i32** %2
%31 = call i64 @compute_public_key(i32* %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %29
store i32 1, i32* %1
br label %56
34: ; preds = %29
%35 = load i32*, i32** %2
%36 = call i32 @EC_KEY_check_key(i32* %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %34
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %56
41: ; preds = %34
%42 = load i32*, i32** %2
%43 = call i8* @get_der_key(i32* %42, i32* %5)
store i8* %43, i8** %4
%44 = load i8*, i8** %4
%45 = icmp ne i8* %44, null
br i1 %45, label %47, label %46
46: ; preds = %41
store i32 1, i32* %1
br label %56
47: ; preds = %41
%48 = load i8*, i8** %4
%49 = load i32, i32* %5
%50 = load i32, i32* @stdout
%51 = call i32 @fwrite(i8* %48, i32 1, i32 %49, i32 %50)
%52 = load i8*, i8** %4
%53 = call i32 @free(i8* %52)
%54 = load i32*, i32** %2
%55 = call i32 @EC_KEY_free(i32* %54)
store i32 0, i32* %1
br label %56
56: ; preds = %47, %46, %38, %33, %28, %20, %10
%57 = load i32, i32* %1
ret i32 %57
}

LLVM IR (-O2):
@NID_secp256k1 = global i32 0
@stderr = global i32 0
@.str = [33 x i8] c"EC_KEY_new_by_curve_name failed\0A\00"
@stdin = global i32 0
@.str.1 = [28 x i8] c"Requires %d bytes of input\0A\00"
@.str.2 = [25 x i8] c"EC_KEY_check_key failed\0A\00"
@stdout = global i32 0
define i32 @main() {
%1 = alloca [32 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
%5 = load i32, i32* @NID_secp256k1
%6 = tail call i32* @EC_KEY_new_by_curve_name(i32 %5)
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %41
11: ; preds = %0
%12 = load i32, i32* @stdin
%13 = call i32 @fread(i8* %3, i32 1, i32 32, i32 %12)
%14 = load i32, i32* @stdin
%15 = call i64 @feof(i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %11
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 32)
br label %41
20: ; preds = %11
%21 = call i64 @store_private_key(i32* %6, i8* %3, i32 32)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %41
23: ; preds = %20
%24 = call i64 @compute_public_key(i32* %6)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %41
26: ; preds = %23
%27 = call i32 @EC_KEY_check_key(i32* %6)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %41
32: ; preds = %26
%33 = call i8* @get_der_key(i32* %6, i32* %2)
%34 = icmp eq i8* %33, null
br i1 %34, label %41, label %35
35: ; preds = %32
%36 = load i32, i32* %2
%37 = load i32, i32* @stdout
%38 = call i32 @fwrite(i8* %33, i32 1, i32 %36, i32 %37)
%39 = call i32 @free(i8* %33)
%40 = call i32 @EC_KEY_free(i32* %6)
br label %41
41: ; preds = %32, %23, %20, %35, %29, %17, %8
%42 = phi i32 [ 1, %8 ], [ 1, %17 ], [ 0, %35 ], [ 1, %29 ], [ 1, %20 ], [ 1, %23 ], [ 1, %32 ]
ret i32 %42
}