#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;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(unsigned char *);
unsigned int i2d_ECPrivateKey(int /*<<< orphan*/ *, unsigned char **);
scalar_t__ malloc(unsigned int);
int /*<<< orphan*/ stderr;
unsigned char *get_der_key(const EC_KEY *key, int *len) {
  unsigned int nSize = i2d_ECPrivateKey((EC_KEY *)key, NULL);
  unsigned char *buffer;
  unsigned char *pbegin;
  if (!nSize) {
    fprintf(stderr, "i2d_ECPrivateKey failed\n");
    return NULL;
  }
  buffer = (unsigned char *)malloc(nSize);
  pbegin = buffer;
  if (i2d_ECPrivateKey((EC_KEY *)key, &pbegin) != nSize) {
    fprintf(stderr, "i2d_ECPrivateKey returned unexpected size\n");
    free(buffer);
    return NULL;
  }
  *len = nSize;
  return buffer;
}


x86 assembly (clang, -O0):
<get_der_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <i2d_ECPrivateKey@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    118c <get_der_key+0x4c>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
lea    0xe86(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f2 <get_der_key+0xb2>
mov    -0x1c(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <i2d_ECPrivateKey@plt>
cmp    -0x1c(%rbp),%eax
je     11e1 <get_der_key+0xa1>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
lea    0xe53(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f2 <get_der_key+0xb2>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_der_key>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %esi,%esi
call   1050 <i2d_ECPrivateKey@plt>
test   %eax,%eax
je     117c <get_der_key+0x3c>
mov    %eax,%ebp
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rax,(%rsp)
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1050 <i2d_ECPrivateKey@plt>
cmp    %ebp,%eax
jne    1193 <get_der_key+0x53>
mov    %ebp,(%r14)
jmp    11b3 <get_der_key+0x73>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
lea    0xe74(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11b0 <get_der_key+0x70>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    0xe76(%rip),%rsi
call   1040 <fprintf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [25 x i8] c"i2d_ECPrivateKey failed\0A\00"
@.str.1 = [43 x i8] c"i2d_ECPrivateKey returned unexpected size\0A\00"
define i8* @get_der_key(i32* %0, i32* nocapture writeonly %1) {
%3 = alloca i8*
%4 = tail call i32 @i2d_ECPrivateKey(i32* %0, i8** null)
%5 = bitcast i8** %3 to i8*
%6 = icmp eq i32 %4, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %20
10: ; preds = %2
%11 = tail call i64 @malloc(i32 %4)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %3
%13 = call i32 @i2d_ECPrivateKey(i32* %0, i8** %3)
%14 = icmp eq i32 %13, %4
br i1 %14, label %19, label %15
15: ; preds = %10
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @free(i8* %12)
br label %20
19: ; preds = %10
store i32 %4, i32* %1
br label %20
20: ; preds = %19, %15, %7
%21 = phi i8* [ null, %15 ], [ %12, %19 ], [ null, %7 ]
ret i8* %21
}