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

/* Variables and functions */
int /*<<< orphan*/ EC_KEY_generate_key(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EC_KEY_new_by_curve_name(int /*<<< orphan*/);
int /*<<< orphan*/ EC_KEY_set_asn1_flag(int /*<<< orphan*/ *,
                                        int /*<<< orphan*/);
int /*<<< orphan*/ *EVP_PKEY_new();
int /*<<< orphan*/ EVP_PKEY_set1_EC_KEY(int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_sha256();
int /*<<< orphan*/ NID_X9_62_prime256v1;
int /*<<< orphan*/ OPENSSL_EC_NAMED_CURVE;
int /*<<< orphan*/
PEM_write_ECPrivateKey(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                       int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                       int /*<<< orphan*/, int /*<<< orphan*/ *,
                       int /*<<< orphan*/ *);
int /*<<< orphan*/ PEM_write_X509(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ *makeselfcert(int /*<<< orphan*/ *, int, char *,
                                 int /*<<< orphan*/);
void makeeccert256(int days, char *commonname) {
  EC_KEY *key;
  EVP_PKEY *pkey;
  X509 *x;
  FILE *out;
  key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  EC_KEY_generate_key(key);
  EC_KEY_set_asn1_flag(key, OPENSSL_EC_NAMED_CURVE);
  pkey = EVP_PKEY_new();
  EVP_PKEY_set1_EC_KEY(pkey, key);
  x = makeselfcert(pkey, days, commonname, EVP_sha256());
  out = fopen("ec256_cert.pem", "w");
  if (out == NULL)
    exit(-1);
  PEM_write_X509(out, x);
  fclose(out);
  out = fopen("ec256_cert_key.pem", "w");
  if (out == NULL)
    exit(-1);
  PEM_write_ECPrivateKey(out, key, NULL, NULL, 0, NULL, NULL);
  fclose(out);
}


x86 assembly (clang, -O0):
<makeeccert256>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   10e0 <EC_KEY_new_by_curve_name@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <EC_KEY_generate_key@plt>
mov    -0x18(%rbp),%rdi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
call   1080 <EC_KEY_set_asn1_flag@plt>
mov    $0x0,%al
call   1090 <EVP_PKEY_new@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10c0 <EVP_PKEY_set1_EC_KEY@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   10d0 <EVP_sha256@plt>
mov    -0x48(%rbp),%rdi
mov    -0x3c(%rbp),%esi
mov    -0x38(%rbp),%rdx
mov    %eax,%ecx
call   1050 <makeselfcert@plt>
mov    %rax,-0x28(%rbp)
lea    0xdb2(%rip),%rdi
lea    0xdba(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1273 <makeeccert256+0xb3>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <PEM_write_X509@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd81(%rip),%rdi
lea    0xd78(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12b5 <makeeccert256+0xf5>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r9d
xor    %r8d,%r8d
mov    %r9,%rdx
mov    %r9,%rcx
movq   $0x0,(%rsp)
call   1030 <PEM_write_ECPrivateKey@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<makeeccert256>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r15d
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
call   10e0 <EC_KEY_new_by_curve_name@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <EC_KEY_generate_key@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <EC_KEY_set_asn1_flag@plt>
xor    %eax,%eax
call   1090 <EVP_PKEY_new@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
call   10c0 <EVP_PKEY_set1_EC_KEY@plt>
xor    %eax,%eax
call   10d0 <EVP_sha256@plt>
mov    %rbp,%rdi
mov    %r15d,%esi
mov    %r14,%rdx
mov    %eax,%ecx
call   1050 <makeselfcert@plt>
mov    %rax,%r14
lea    0xdd3(%rip),%rdi
lea    0xddb(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     129e <makeeccert256+0xde>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r14,%rsi
call   1070 <PEM_write_X509@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
lea    0xdb6(%rip),%rdi
lea    0xdad(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     129e <makeeccert256+0xde>
mov    %rax,%rbp
movq   $0x0,(%rsp)
mov    %rax,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1030 <PEM_write_ECPrivateKey@plt>
mov    %rbp,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@NID_X9_62_prime256v1 = global i32 0
@OPENSSL_EC_NAMED_CURVE = global i32 0
@.str = [15 x i8] c"ec256_cert.pem\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [19 x i8] c"ec256_cert_key.pem\00"
define void @makeeccert256(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
store i32 %0, i32* %3
store i8* %1, i8** %4
%9 = load i32, i32* @NID_X9_62_prime256v1
%10 = call i32* @EC_KEY_new_by_curve_name(i32 %9)
store i32* %10, i32** %5
%11 = load i32*, i32** %5
%12 = call i32 @EC_KEY_generate_key(i32* %11)
%13 = load i32*, i32** %5
%14 = load i32, i32* @OPENSSL_EC_NAMED_CURVE
%15 = call i32 @EC_KEY_set_asn1_flag(i32* %13, i32 %14)
%16 = call i32* (...) @EVP_PKEY_new()
store i32* %16, i32** %6
%17 = load i32*, i32** %6
%18 = load i32*, i32** %5
%19 = call i32 @EVP_PKEY_set1_EC_KEY(i32* %17, i32* %18)
%20 = load i32*, i32** %6
%21 = load i32, i32* %3
%22 = load i8*, i8** %4
%23 = call i32 (...) @EVP_sha256()
%24 = call i32* @makeselfcert(i32* %20, i32 %21, i8* %22, i32 %23)
store i32* %24, i32** %7
%25 = call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %25, i32** %8
%26 = load i32*, i32** %8
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %30
28: ; preds = %2
%29 = call i32 @exit(i32 -1)
unreachable
30: ; preds = %2
%31 = load i32*, i32** %8
%32 = load i32*, i32** %7
%33 = call i32 @PEM_write_X509(i32* %31, i32* %32)
%34 = load i32*, i32** %8
%35 = call i32 @fclose(i32* %34)
%36 = call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %36, i32** %8
%37 = load i32*, i32** %8
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %41
39: ; preds = %30
%40 = call i32 @exit(i32 -1)
unreachable
41: ; preds = %30
%42 = load i32*, i32** %8
%43 = load i32*, i32** %5
%44 = call i32 @PEM_write_ECPrivateKey(i32* %42, i32* %43, i32* null, i32* null, i32 0, i32* null, i32* null)
%45 = load i32*, i32** %8
%46 = call i32 @fclose(i32* %45)
ret void
}

LLVM IR (-O2):
@NID_X9_62_prime256v1 = global i32 0
@OPENSSL_EC_NAMED_CURVE = global i32 0
@.str = [15 x i8] c"ec256_cert.pem\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [19 x i8] c"ec256_cert_key.pem\00"
define void @makeeccert256(i32 %0, i8* %1) {
%3 = load i32, i32* @NID_X9_62_prime256v1
%4 = tail call i32* @EC_KEY_new_by_curve_name(i32 %3)
%5 = tail call i32 @EC_KEY_generate_key(i32* %4)
%6 = load i32, i32* @OPENSSL_EC_NAMED_CURVE
%7 = tail call i32 @EC_KEY_set_asn1_flag(i32* %4, i32 %6)
%8 = tail call i32* (...) @EVP_PKEY_new()
%9 = tail call i32 @EVP_PKEY_set1_EC_KEY(i32* %8, i32* %4)
%10 = tail call i32 (...) @EVP_sha256()
%11 = tail call i32* @makeselfcert(i32* %8, i32 %0, i8* %1, i32 %10)
%12 = tail call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = tail call i32 @exit(i32 -1)
unreachable
16: ; preds = %2
%17 = tail call i32 @PEM_write_X509(i32* %12, i32* %11)
%18 = tail call i32 @fclose(i32* %12)
%19 = tail call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = tail call i32 @exit(i32 -1)
unreachable
23: ; preds = %16
%24 = tail call i32 @PEM_write_ECPrivateKey(i32* %19, i32* %4, i32* null, i32* null, i32 0, i32* null, i32* null)
%25 = tail call i32 @fclose(i32* %19)
ret void
}