#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_REQ;
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_REQ(int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ *makereq(int /*<<< orphan*/ *, char *, int /*<<< orphan*/);
void makeecreq256(char *commonname) {
  EC_KEY *key;
  EVP_PKEY *pkey;
  X509_REQ *req;
  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);
  req = makereq(pkey, commonname, EVP_sha256());
  out = fopen("ec256_req.pem", "w");
  if (out == NULL)
    exit(-1);
  PEM_write_X509_REQ(out, req);
  fclose(out);
  out = fopen("ec256_req_key.pem", "w");
  if (out == NULL)
    exit(-1);
  PEM_write_ECPrivateKey(out, key, NULL, NULL, 0, NULL, NULL);
  fclose(out);
}


x86 assembly (clang, -O0):
<makeecreq256>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   10e0 <EC_KEY_new_by_curve_name@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <EC_KEY_generate_key@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
call   1070 <EC_KEY_set_asn1_flag@plt>
mov    $0x0,%al
call   1080 <EVP_PKEY_new@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10c0 <EVP_PKEY_set1_EC_KEY@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   10d0 <EVP_sha256@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   10a0 <makereq@plt>
mov    %rax,-0x20(%rbp)
lea    0xdbe(%rip),%rdi
lea    0xdc5(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1267 <makeecreq256+0xa7>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <PEM_write_X509_REQ@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd8c(%rip),%rdi
lea    0xd83(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12a9 <makeecreq256+0xe9>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%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    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<makeecreq256>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   10e0 <EC_KEY_new_by_curve_name@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1050 <EC_KEY_generate_key@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1070 <EC_KEY_set_asn1_flag@plt>
xor    %eax,%eax
call   1080 <EVP_PKEY_new@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   10c0 <EVP_PKEY_set1_EC_KEY@plt>
xor    %eax,%eax
call   10d0 <EVP_sha256@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   10a0 <makereq@plt>
mov    %rax,%r14
lea    0xdd7(%rip),%rdi
lea    0xdde(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1299 <makeecreq256+0xd9>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1060 <PEM_write_X509_REQ@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xdb9(%rip),%rdi
lea    0xdb0(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1299 <makeecreq256+0xd9>
mov    %rax,%rbx
movq   $0x0,(%rsp)
mov    %rax,%rdi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1030 <PEM_write_ECPrivateKey@plt>
mov    %rbx,%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
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 = [14 x i8] c"ec256_req.pem\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [18 x i8] c"ec256_req_key.pem\00"
define void @makeecreq256(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
store i8* %0, i8** %2
%7 = load i32, i32* @NID_X9_62_prime256v1
%8 = call i32* @EC_KEY_new_by_curve_name(i32 %7)
store i32* %8, i32** %3
%9 = load i32*, i32** %3
%10 = call i32 @EC_KEY_generate_key(i32* %9)
%11 = load i32*, i32** %3
%12 = load i32, i32* @OPENSSL_EC_NAMED_CURVE
%13 = call i32 @EC_KEY_set_asn1_flag(i32* %11, i32 %12)
%14 = call i32* (...) @EVP_PKEY_new()
store i32* %14, i32** %4
%15 = load i32*, i32** %4
%16 = load i32*, i32** %3
%17 = call i32 @EVP_PKEY_set1_EC_KEY(i32* %15, i32* %16)
%18 = load i32*, i32** %4
%19 = load i8*, i8** %2
%20 = call i32 (...) @EVP_sha256()
%21 = call i32* @makereq(i32* %18, i8* %19, i32 %20)
store i32* %21, i32** %5
%22 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %6
%23 = load i32*, i32** %6
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %27
25: ; preds = %1
%26 = call i32 @exit(i32 -1)
unreachable
27: ; preds = %1
%28 = load i32*, i32** %6
%29 = load i32*, i32** %5
%30 = call i32 @PEM_write_X509_REQ(i32* %28, i32* %29)
%31 = load i32*, i32** %6
%32 = call i32 @fclose(i32* %31)
%33 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 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* %33, i32** %6
%34 = load i32*, i32** %6
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %38
36: ; preds = %27
%37 = call i32 @exit(i32 -1)
unreachable
38: ; preds = %27
%39 = load i32*, i32** %6
%40 = load i32*, i32** %3
%41 = call i32 @PEM_write_ECPrivateKey(i32* %39, i32* %40, i32* null, i32* null, i32 0, i32* null, i32* null)
%42 = load i32*, i32** %6
%43 = call i32 @fclose(i32* %42)
ret void
}

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