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

/* Variables and functions */
int /*<<< orphan*/ EVP_PKEY_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_PKEY_CTX_new_id(int /*<<< orphan*/,
                                        int /*<<< orphan*/ *);
scalar_t__ EVP_PKEY_CTX_set_rsa_keygen_bits(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ EVP_PKEY_RSA;
int /*<<< orphan*/ EVP_PKEY_free(int /*<<< orphan*/ *);
scalar_t__ EVP_PKEY_keygen(int /*<<< orphan*/ *, int /*<<< orphan*/ **);
scalar_t__ EVP_PKEY_keygen_init(int /*<<< orphan*/ *);
scalar_t__ PEM_write_PrivateKey(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                int /*<<< orphan*/, int /*<<< orphan*/ *,
                                int /*<<< orphan*/ *);
int /*<<< orphan*/ warnx(char *, ...);
EVP_PKEY *rsa_key_create(FILE *f, const char *fname) {
  EVP_PKEY_CTX *ctx = NULL;
  EVP_PKEY *pkey = NULL;
  if (NULL == (ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL))) {
    warnx("EVP_PKEY_CTX_new_id");
    goto err;
  } else if (EVP_PKEY_keygen_init(ctx) <= 0) {
    warnx("EVP_PKEY_keygen_init");
    goto err;
  } else if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 4096) <= 0) {
    warnx("EVP_PKEY_set_rsa_keygen_bits");
    goto err;
  } else if (EVP_PKEY_keygen(ctx, &pkey) <= 0) {
    warnx("EVP_PKEY_keygen");
    goto err;
  }
  if (PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL))
    goto out;
  warnx("%s: PEM_write_PrivateKey", fname);
err:
  if (NULL != pkey)
    EVP_PKEY_free(pkey);
  pkey = NULL;
out:
  if (NULL != ctx)
    EVP_PKEY_CTX_free(ctx);
  return (pkey);
}


x86 assembly (clang, -O0):
<rsa_key_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <EVP_PKEY_CTX_new_id@plt>
mov    %rax,%rcx
mov    %rcx,-0x18(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    11d7 <rsa_key_create+0x57>
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1040 <warnx@plt>
jmp    12a5 <rsa_key_create+0x125>
mov    -0x18(%rbp),%rdi
call   1090 <EVP_PKEY_keygen_init@plt>
cmp    $0x0,%rax
jg     11fd <rsa_key_create+0x7d>
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1040 <warnx@plt>
jmp    12a5 <rsa_key_create+0x125>
mov    -0x18(%rbp),%rdi
mov    $0x1000,%esi
call   1060 <EVP_PKEY_CTX_set_rsa_keygen_bits@plt>
cmp    $0x0,%rax
jg     1228 <rsa_key_create+0xa8>
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1040 <warnx@plt>
jmp    12a5 <rsa_key_create+0x125>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <EVP_PKEY_keygen@plt>
cmp    $0x0,%rax
jg     1252 <rsa_key_create+0xd2>
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1040 <warnx@plt>
jmp    12a5 <rsa_key_create+0x125>
jmp    1257 <rsa_key_create+0xd7>
jmp    125c <rsa_key_create+0xdc>
jmp    1261 <rsa_key_create+0xe1>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r9d
xor    %r8d,%r8d
mov    %r9,%rdx
mov    %r9,%rcx
movq   $0x0,(%rsp)
call   1080 <PEM_write_PrivateKey@plt>
cmp    $0x0,%rax
je     1293 <rsa_key_create+0x113>
jmp    12c2 <rsa_key_create+0x142>
mov    -0x10(%rbp),%rsi
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1040 <warnx@plt>
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
je     12ba <rsa_key_create+0x13a>
mov    -0x20(%rbp),%rdi
call   1050 <EVP_PKEY_free@plt>
movq   $0x0,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
je     12d7 <rsa_key_create+0x157>
mov    -0x18(%rbp),%rdi
call   10a0 <EVP_PKEY_CTX_free@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rsa_key_create>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
movq   $0x0,0x8(%rsp)
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1070 <EVP_PKEY_CTX_new_id@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1218 <rsa_key_create+0x98>
mov    %rbx,%rdi
call   1090 <EVP_PKEY_keygen_init@plt>
test   %rax,%rax
jle    1221 <rsa_key_create+0xa1>
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1060 <EVP_PKEY_CTX_set_rsa_keygen_bits@plt>
test   %rax,%rax
jle    122a <rsa_key_create+0xaa>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1030 <EVP_PKEY_keygen@plt>
test   %rax,%rax
jle    1233 <rsa_key_create+0xb3>
mov    0x8(%rsp),%rsi
movq   $0x0,(%rsp)
mov    %r15,%rdi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1080 <PEM_write_PrivateKey@plt>
test   %rax,%rax
jne    125e <rsa_key_create+0xde>
lea    0xe4a(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <warnx@plt>
jmp    1241 <rsa_key_create+0xc1>
lea    0xde1(%rip),%rdi
jmp    123a <rsa_key_create+0xba>
lea    0xdec(%rip),%rdi
jmp    123a <rsa_key_create+0xba>
lea    0xdf8(%rip),%rdi
jmp    123a <rsa_key_create+0xba>
lea    0xe0c(%rip),%rdi
xor    %eax,%eax
call   1040 <warnx@plt>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     1250 <rsa_key_create+0xd0>
call   1050 <EVP_PKEY_free@plt>
movq   $0x0,0x8(%rsp)
test   %rbx,%rbx
je     126d <rsa_key_create+0xed>
mov    %rbx,%rdi
call   10a0 <EVP_PKEY_CTX_free@plt>
mov    0x8(%rsp),%rax
jmp    126f <rsa_key_create+0xef>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EVP_PKEY_RSA = global i32 0
@.str = [20 x i8] c"EVP_PKEY_CTX_new_id\00"
@.str.1 = [21 x i8] c"EVP_PKEY_keygen_init\00"
@.str.2 = [29 x i8] c"EVP_PKEY_set_rsa_keygen_bits\00"
@.str.3 = [16 x i8] c"EVP_PKEY_keygen\00"
@.str.4 = [25 x i8] c"%s: PEM_write_PrivateKey\00"
define i32* @rsa_key_create(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
store i32* %0, i32** %3
store i8* %1, i8** %4
store i32* null, i32** %5
store i32* null, i32** %6
%7 = load i32, i32* @EVP_PKEY_RSA
%8 = call i32* @EVP_PKEY_CTX_new_id(i32 %7, i32* null)
store i32* %8, i32** %5
%9 = icmp eq i32* null, %8
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %42
12: ; preds = %2
%13 = load i32*, i32** %5
%14 = call i64 @EVP_PKEY_keygen_init(i32* %13)
%15 = icmp sle i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %42
18: ; preds = %12
%19 = load i32*, i32** %5
%20 = call i64 @EVP_PKEY_CTX_set_rsa_keygen_bits(i32* %19, i32 4096)
%21 = icmp sle i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %42
24: ; preds = %18
%25 = load i32*, i32** %5
%26 = call i64 @EVP_PKEY_keygen(i32* %25, i32** %6)
%27 = icmp sle i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %42
30: ; preds = %24
br label %31
31: ; preds = %30
br label %32
32: ; preds = %31
br label %33
33: ; preds = %32
%34 = load i32*, i32** %3
%35 = load i32*, i32** %6
%36 = call i64 @PEM_write_PrivateKey(i32* %34, i32* %35, i32* null, i32* null, i32 0, i32* null, i32* null)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %33
br label %49
39: ; preds = %33
%40 = load i8*, i8** %4
%41 = call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %39, %28, %22, %16, %10
%43 = load i32*, i32** %6
%44 = icmp ne i32* null, %43
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i32*, i32** %6
%47 = call i32 @EVP_PKEY_free(i32* %46)
br label %48
48: ; preds = %45, %42
store i32* null, i32** %6
br label %49
49: ; preds = %48, %38
%50 = load i32*, i32** %5
%51 = icmp ne i32* null, %50
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = load i32*, i32** %5
%54 = call i32 @EVP_PKEY_CTX_free(i32* %53)
br label %55
55: ; preds = %52, %49
%56 = load i32*, i32** %6
ret i32* %56
}

LLVM IR (-O2):
@EVP_PKEY_RSA = global i32 0
@.str = [20 x i8] c"EVP_PKEY_CTX_new_id\00"
@.str.1 = [21 x i8] c"EVP_PKEY_keygen_init\00"
@.str.2 = [29 x i8] c"EVP_PKEY_set_rsa_keygen_bits\00"
@.str.3 = [16 x i8] c"EVP_PKEY_keygen\00"
@.str.4 = [25 x i8] c"%s: PEM_write_PrivateKey\00"
define i32* @rsa_key_create(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = bitcast i32** %3 to i8*
store i32* null, i32** %3
%5 = load i32, i32* @EVP_PKEY_RSA
%6 = tail call i32* @EVP_PKEY_CTX_new_id(i32 %5, i32* null)
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %31
10: ; preds = %2
%11 = tail call i64 @EVP_PKEY_keygen_init(i32* %6)
%12 = icmp slt i64 %11, 1
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %31
15: ; preds = %10
%16 = tail call i64 @EVP_PKEY_CTX_set_rsa_keygen_bits(i32* %6, i32 4096)
%17 = icmp slt i64 %16, 1
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %31
20: ; preds = %15
%21 = call i64 @EVP_PKEY_keygen(i32* %6, i32** %3)
%22 = icmp slt i64 %21, 1
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %31
25: ; preds = %20
%26 = load i32*, i32** %3
%27 = call i64 @PEM_write_PrivateKey(i32* %0, i32* %26, i32* null, i32* null, i32 0, i32* null, i32* null)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %37
29: ; preds = %25
%30 = call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %1)
br label %31
31: ; preds = %29, %23, %18, %13, %8
%32 = load i32*, i32** %3
%33 = icmp eq i32* %32, null
br i1 %33, label %36, label %34
34: ; preds = %31
%35 = call i32 @EVP_PKEY_free(i32* %32)
br label %36
36: ; preds = %31, %34
store i32* null, i32** %3
br i1 %7, label %40, label %37
37: ; preds = %25, %36
%38 = call i32 @EVP_PKEY_CTX_free(i32* %6)
%39 = load i32*, i32** %3
br label %40
40: ; preds = %37, %36
%41 = phi i32* [ %39, %37 ], [ null, %36 ]
ret i32* %41
}