#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*/ BIO;

/* Variables and functions */
int /*<<< orphan*/ BIO_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_new_mem_buf(unsigned char *, int);
int /*<<< orphan*/ *PEM_read_bio_RSAPrivateKey(int /*<<< orphan*/ *,
                                               int /*<<< orphan*/ **,
                                               int /*<<< orphan*/ *,
                                               int /*<<< orphan*/ *);
int /*<<< orphan*/ *PEM_read_bio_RSA_PUBKEY(int /*<<< orphan*/ *,
                                            int /*<<< orphan*/ **,
                                            int /*<<< orphan*/ *,
                                            int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
RSA *createRSA(unsigned char *key, int public) {
  RSA *rsa = NULL;
  BIO *keybio = NULL;
  keybio = BIO_new_mem_buf(key, -1);
  if (keybio == NULL) {
    fprintf(stderr, "Failed to create key BIO\n");
    return NULL;
  }
  if (public) {
    rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa, NULL, NULL);
  } else {
    rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa, NULL, NULL);
  }
  if (rsa == NULL) {
    fprintf(stderr, "Failed to create RSA\n");
    free(keybio);
    return NULL;
  }
  if (keybio) {
    BIO_free(keybio);
  }
  return rsa;
}


x86 assembly (clang, -O0):
<createRSA>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0xffffffff,%esi
call   1060 <BIO_new_mem_buf@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11be <createRSA+0x5e>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
lea    0xe54(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    124f <createRSA+0xef>
cmpl   $0x0,-0x14(%rbp)
je     11e5 <createRSA+0x85>
mov    -0x28(%rbp),%rdi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1070 <PEM_read_bio_RSA_PUBKEY@plt>
mov    %rax,-0x20(%rbp)
jmp    11fd <createRSA+0x9d>
mov    -0x28(%rbp),%rdi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1040 <PEM_read_bio_RSAPrivateKey@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1233 <createRSA+0xd3>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
lea    0xe02(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    124f <createRSA+0xef>
cmpq   $0x0,-0x28(%rbp)
je     1247 <createRSA+0xe7>
mov    -0x28(%rbp),%rdi
call   1080 <BIO_free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createRSA>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
movq   $0x0,(%rsp)
mov    $0xffffffff,%esi
call   1060 <BIO_new_mem_buf@plt>
test   %rax,%rax
je     1194 <createRSA+0x34>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    %rax,%rdi
xor    %edx,%edx
xor    %ecx,%ecx
test   %ebp,%ebp
je     11ab <createRSA+0x4b>
call   1070 <PEM_read_bio_RSA_PUBKEY@plt>
jmp    11b0 <createRSA+0x50>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
call   1050 <fprintf@plt>
jmp    11e4 <createRSA+0x84>
call   1040 <PEM_read_bio_RSAPrivateKey@plt>
mov    %rax,(%rsp)
test   %rax,%rax
je     11c7 <createRSA+0x67>
mov    %rbx,%rdi
call   1080 <BIO_free@plt>
mov    (%rsp),%rax
jmp    11e6 <createRSA+0x86>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"Failed to create key BIO\0A\00"
@.str.1 = [22 x i8] c"Failed to create RSA\0A\00"
define i32* @createRSA(i8* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32* null, i32** %6
store i32* null, i32** %7
%8 = load i8*, i8** %4
%9 = call i32* @BIO_new_mem_buf(i8* %8, i32 -1)
store i32* %9, i32** %7
%10 = load i32*, i32** %7
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32* null, i32** %3
br label %40
15: ; preds = %2
%16 = load i32, i32* %5
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32*, i32** %7
%20 = call i32* @PEM_read_bio_RSA_PUBKEY(i32* %19, i32** %6, i32* null, i32* null)
store i32* %20, i32** %6
br label %24
21: ; preds = %15
%22 = load i32*, i32** %7
%23 = call i32* @PEM_read_bio_RSAPrivateKey(i32* %22, i32** %6, i32* null, i32* null)
store i32* %23, i32** %6
br label %24
24: ; preds = %21, %18
%25 = load i32*, i32** %6
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %32
27: ; preds = %24
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32*, i32** %7
%31 = call i32 @free(i32* %30)
store i32* null, i32** %3
br label %40
32: ; preds = %24
%33 = load i32*, i32** %7
%34 = icmp ne i32* %33, null
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = load i32*, i32** %7
%37 = call i32 @BIO_free(i32* %36)
br label %38
38: ; preds = %35, %32
%39 = load i32*, i32** %6
store i32* %39, i32** %3
br label %40
40: ; preds = %38, %27, %12
%41 = load i32*, i32** %3
ret i32* %41
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"Failed to create key BIO\0A\00"
@.str.1 = [22 x i8] c"Failed to create RSA\0A\00"
define i32* @createRSA(i8* %0, i32 %1) {
%3 = alloca i32*
%4 = bitcast i32** %3 to i8*
store i32* null, i32** %3
%5 = tail call i32* @BIO_new_mem_buf(i8* %0, i32 -1)
%6 = icmp eq i32* %5, null
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 ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %26
10: ; preds = %2
%11 = icmp eq i32 %1, 0
br i1 %11, label %14, label %12
12: ; preds = %10
%13 = call i32* @PEM_read_bio_RSA_PUBKEY(i32* %5, i32** %3, i32* null, i32* null)
br label %16
14: ; preds = %10
%15 = call i32* @PEM_read_bio_RSAPrivateKey(i32* %5, i32** %3, i32* null, i32* null)
br label %16
16: ; preds = %14, %12
%17 = phi i32* [ %15, %14 ], [ %13, %12 ]
store i32* %17, i32** %3
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @free(i32* %5)
br label %26
23: ; preds = %16
%24 = call i32 @BIO_free(i32* %5)
%25 = load i32*, i32** %3
br label %26
26: ; preds = %23, %19, %7
%27 = phi i32* [ null, %7 ], [ null, %19 ], [ %25, %23 ]
ret i32* %27
}