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

/* Variables and functions */
int /*<<< orphan*/ *PEM_read_RSAPrivateKey(int /*<<< orphan*/ *,
                                           int /*<<< orphan*/ **,
                                           int /*<<< orphan*/,
                                           int /*<<< orphan*/ *);
int /*<<< orphan*/ *PEM_read_RSA_PUBKEY(int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ **,
                                        int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *);
int /*<<< orphan*/ *RSA_new();
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ printf(char *);
RSA *createRSA(const char *keyFile, int public) {
  FILE *fp;
  if ((fp = fopen(keyFile, "rb")) == NULL) {
    printf("Open key file error occurred!\n");
    exit(1);
  }
  RSA *rsa = RSA_new();
  if (public) {
    printf("Reading public key...\n");
    rsa = PEM_read_RSA_PUBKEY(fp, &rsa, NULL, NULL);
  } else {
    printf("Reading private key...\n");
    rsa = PEM_read_RSAPrivateKey(fp, &rsa, 0, NULL);
  }
  if (rsa == NULL) {
    printf("Error while reading key!\n");
    exit(1);
  }
  return rsa;
}


x86 assembly (clang, -O0):
<createRSA>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe86(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11a3 <createRSA+0x43>
lea    0xe6f(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1050 <RSA_new@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11e1 <createRSA+0x81>
lea    0xe63(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1070 <PEM_read_RSA_PUBKEY@plt>
mov    %rax,-0x20(%rbp)
jmp    1204 <createRSA+0xa4>
lea    0xe51(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <PEM_read_RSAPrivateKey@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1225 <createRSA+0xc5>
lea    0xe3b(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createRSA>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
lea    0xe94(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11d0 <createRSA+0x70>
mov    %rax,%rbx
xor    %eax,%eax
call   1050 <RSA_new@plt>
mov    %rax,(%rsp)
test   %ebp,%ebp
je     11a5 <createRSA+0x45>
lea    0xea4(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <PEM_read_RSA_PUBKEY@plt>
jmp    11c0 <createRSA+0x60>
lea    0xe57(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <PEM_read_RSAPrivateKey@plt>
mov    %rax,(%rsp)
test   %rax,%rax
je     11d9 <createRSA+0x79>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe72(%rip),%rdi
jmp    11e0 <createRSA+0x80>
lea    0xe3a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [31 x i8] c"Open key file error occurred!\0A\00"
@.str.2 = [23 x i8] c"Reading public key...\0A\00"
@.str.3 = [24 x i8] c"Reading private key...\0A\00"
@.str.4 = [26 x i8] c"Error while reading key!\0A\00"
define i32* @createRSA(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %5
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = call i32* (...) @RSA_new()
store i32* %14, i32** %6
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32*, i32** %5
%20 = call i32* @PEM_read_RSA_PUBKEY(i32* %19, i32** %6, i32* null, i32* null)
store i32* %20, i32** %6
br label %25
21: ; preds = %13
%22 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%23 = load i32*, i32** %5
%24 = call i32* @PEM_read_RSAPrivateKey(i32* %23, i32** %6, i32 0, i32* null)
store i32* %24, i32** %6
br label %25
25: ; preds = %21, %17
%26 = load i32*, i32** %6
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %25
%32 = load i32*, i32** %6
ret i32* %32
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@str = [23 x i8] c"Reading private key...\00"
@str.5 = [25 x i8] c"Error while reading key!\00"
@str.6 = [22 x i8] c"Reading public key...\00"
@str.7 = [30 x i8] c"Open key file error occurred!\00"
define i32* @createRSA(i8* nocapture %0, i32 %1) {
%3 = alloca i32*
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.7, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = bitcast i32** %3 to i8*
%11 = tail call i32* (...) @RSA_new()
store i32* %11, i32** %3
%12 = icmp eq i32 %1, 0
br i1 %12, label %16, label %13
13: ; preds = %9
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.6, i64 0, i64 0))
%15 = call i32* @PEM_read_RSA_PUBKEY(i32* %4, i32** %3, i32* null, i32* null)
br label %19
16: ; preds = %9
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%18 = call i32* @PEM_read_RSAPrivateKey(i32* %4, i32** %3, i32 0, i32* null)
br label %19
19: ; preds = %16, %13
%20 = phi i32* [ %18, %16 ], [ %15, %13 ]
store i32* %20, i32** %3
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.5, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %19
ret i32* %20
}