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

/* Variables and functions */
int /*<<< orphan*/ *EVP_PKEY_new();
int /*<<< orphan*/ EVP_PKEY_set1_RSA(int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ RSA_F4;
int RSA_check_key(int /*<<< orphan*/ *);
int /*<<< orphan*/ RSA_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *RSA_generate_key(int, int /*<<< orphan*/,
                                     int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ jlog(int, char *);
EVP_PKEY *pki_generate_keyring() {
  jlog(0x01, "pki_generate_keyring\n");
  EVP_PKEY *keyring;
  RSA *rsa_keys;
  keyring = EVP_PKEY_new();
  rsa_keys = RSA_generate_key(2048, RSA_F4, NULL, NULL);
  if (RSA_check_key(rsa_keys) != 1) {
    RSA_free(rsa_keys);
    rsa_keys = RSA_generate_key(2048, RSA_F4, NULL, NULL);
    if (RSA_check_key(rsa_keys) != 1) {
      RSA_free(rsa_keys);
      return NULL;
    }
  }
  EVP_PKEY_set1_RSA(keyring, rsa_keys);
  RSA_free(rsa_keys);
  return keyring;
}


x86 assembly (clang, -O0):
<pki_generate_keyring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x1,%edi
lea    0xe8c(%rip),%rsi
call   1050 <jlog@plt>
mov    $0x0,%al
call   1060 <EVP_PKEY_new@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
mov    $0x800,%edi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1070 <RSA_generate_key@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <RSA_check_key@plt>
cmp    $0x1,%eax
je     1208 <pki_generate_keyring+0xa8>
mov    -0x18(%rbp),%rdi
call   1040 <RSA_free@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
mov    $0x800,%edi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1070 <RSA_generate_key@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <RSA_check_key@plt>
cmp    $0x1,%eax
je     1203 <pki_generate_keyring+0xa3>
mov    -0x18(%rbp),%rdi
call   1040 <RSA_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1226 <pki_generate_keyring+0xc6>
jmp    1208 <pki_generate_keyring+0xa8>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <EVP_PKEY_set1_RSA@plt>
mov    -0x18(%rbp),%rdi
call   1040 <RSA_free@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pki_generate_keyring>:
push   %r15
push   %r14
push   %rbx
lea    0xe94(%rip),%rsi
mov    $0x1,%edi
call   1050 <jlog@plt>
xor    %eax,%eax
call   1060 <EVP_PKEY_new@plt>
mov    %rax,%r14
mov    0x2e59(%rip),%r15
mov    (%r15),%esi
mov    $0x800,%edi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <RSA_generate_key@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <RSA_check_key@plt>
cmp    $0x1,%eax
je     11d4 <pki_generate_keyring+0x74>
mov    %rbx,%rdi
call   1040 <RSA_free@plt>
mov    (%r15),%esi
xor    %r15d,%r15d
mov    $0x800,%edi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <RSA_generate_key@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <RSA_check_key@plt>
cmp    $0x1,%eax
jne    11e2 <pki_generate_keyring+0x82>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <EVP_PKEY_set1_RSA@plt>
mov    %r14,%r15
mov    %rbx,%rdi
call   1040 <RSA_free@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [22 x i8] c"pki_generate_keyring\0A\00"
@RSA_F4 = global i32 0
define i32* @pki_generate_keyring() {
%1 = alloca i32*
%2 = alloca i32*
%3 = alloca i32*
%4 = call i32 @jlog(i32 1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%5 = call i32* (...) @EVP_PKEY_new()
store i32* %5, i32** %2
%6 = load i32, i32* @RSA_F4
%7 = call i32* @RSA_generate_key(i32 2048, i32 %6, i32* null, i32* null)
store i32* %7, i32** %3
%8 = load i32*, i32** %3
%9 = call i32 @RSA_check_key(i32* %8)
%10 = icmp ne i32 %9, 1
br i1 %10, label %11, label %23
11: ; preds = %0
%12 = load i32*, i32** %3
%13 = call i32 @RSA_free(i32* %12)
%14 = load i32, i32* @RSA_F4
%15 = call i32* @RSA_generate_key(i32 2048, i32 %14, i32* null, i32* null)
store i32* %15, i32** %3
%16 = load i32*, i32** %3
%17 = call i32 @RSA_check_key(i32* %16)
%18 = icmp ne i32 %17, 1
br i1 %18, label %19, label %22
19: ; preds = %11
%20 = load i32*, i32** %3
%21 = call i32 @RSA_free(i32* %20)
store i32* null, i32** %1
br label %30
22: ; preds = %11
br label %23
23: ; preds = %22, %0
%24 = load i32*, i32** %2
%25 = load i32*, i32** %3
%26 = call i32 @EVP_PKEY_set1_RSA(i32* %24, i32* %25)
%27 = load i32*, i32** %3
%28 = call i32 @RSA_free(i32* %27)
%29 = load i32*, i32** %2
store i32* %29, i32** %1
br label %30
30: ; preds = %23, %19
%31 = load i32*, i32** %1
ret i32* %31
}

LLVM IR (-O2):
@.str = [22 x i8] c"pki_generate_keyring\0A\00"
@RSA_F4 = global i32 0
define i32* @pki_generate_keyring() {
%1 = tail call i32 @jlog(i32 1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%2 = tail call i32* (...) @EVP_PKEY_new()
%3 = load i32, i32* @RSA_F4
%4 = tail call i32* @RSA_generate_key(i32 2048, i32 %3, i32* null, i32* null)
%5 = tail call i32 @RSA_check_key(i32* %4)
%6 = icmp eq i32 %5, 1
br i1 %6, label %13, label %7
7: ; preds = %0
%8 = tail call i32 @RSA_free(i32* %4)
%9 = load i32, i32* @RSA_F4
%10 = tail call i32* @RSA_generate_key(i32 2048, i32 %9, i32* null, i32* null)
%11 = tail call i32 @RSA_check_key(i32* %10)
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %16
13: ; preds = %7, %0
%14 = phi i32* [ %10, %7 ], [ %4, %0 ]
%15 = tail call i32 @EVP_PKEY_set1_RSA(i32* %2, i32* %14)
br label %16
16: ; preds = %7, %13
%17 = phi i32* [ %14, %13 ], [ %10, %7 ]
%18 = phi i32* [ %2, %13 ], [ null, %7 ]
%19 = tail call i32 @RSA_free(i32* %17)
ret i32* %18
}