#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 ENOENT;
int EPROTO;
int /*<<< orphan*/ *PEM_read_RSAPrivateKey(int /*<<< orphan*/ *,
                                           int /*<<< orphan*/,
                                           int /*<<< orphan*/ *, void *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ rsa_err(char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int rsa_get_priv_key(const char *key_path, RSA **rsap) {
  RSA *rsa;
  FILE *f;
  *rsap = NULL;
  f = fopen(key_path, "r");
  if (!f) {
    fprintf(stderr, "Couldn't open RSA private key: '%s': %s\n", key_path,
            strerror(errno));
    return -ENOENT;
  }
  rsa = PEM_read_RSAPrivateKey(f, 0, NULL, (void *)key_path);
  if (!rsa) {
    rsa_err("Failure reading private key");
    fclose(f);
    return -EPROTO;
  }
  fclose(f);
  *rsap = rsa;
  return 0;
}


x86 assembly (clang, -O0):
<rsa_get_priv_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    0xe7a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e5 <rsa_get_priv_key+0x85>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe35(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
mov    0x2de5(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    124a <rsa_get_priv_key+0xea>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rcx
xor    %esi,%esi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <PEM_read_RSAPrivateKey@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    122f <rsa_get_priv_key+0xcf>
lea    0xe1d(%rip),%rdi
call   1040 <rsa_err@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
mov    0x2dbb(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    124a <rsa_get_priv_key+0xea>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rsa_get_priv_key>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
movq   $0x0,(%rsi)
lea    0xe84(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11b0 <rsa_get_priv_key+0x50>
mov    %rax,%rbx
xor    %r12d,%r12d
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
mov    %r15,%rcx
call   1060 <PEM_read_RSAPrivateKey@plt>
test   %rax,%rax
je     11e4 <rsa_get_priv_key+0x84>
mov    %rax,%r15
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r15,(%r14)
jmp    1205 <rsa_get_priv_key+0xa5>
mov    0x2e21(%rip),%rax
mov    (%rax),%ebx
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe34(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    0x2dde(%rip),%rax
jmp    11ff <rsa_get_priv_key+0x9f>
lea    0xe40(%rip),%rdi
call   1040 <rsa_err@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2de1(%rip),%rax
xor    %r12d,%r12d
sub    (%rax),%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [41 x i8] c"Couldn't open RSA private key: '%s': %s\0A\00"
@errno = global i32 0
@ENOENT = global i32 0
@.str.2 = [28 x i8] c"Failure reading private key\00"
@EPROTO = global i32 0
define i32 @rsa_get_priv_key(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
%8 = load i32**, i32*** %5
store i32* null, i32** %8
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %7
%11 = load i32*, i32** %7
%12 = icmp ne i32* %11, null
br i1 %12, label %21, label %13
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %4
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %15, i8* %17)
%19 = load i32, i32* @ENOENT
%20 = sub nsw i32 0, %19
store i32 %20, i32* %3
br label %38
21: ; preds = %2
%22 = load i32*, i32** %7
%23 = load i8*, i8** %4
%24 = call i32* @PEM_read_RSAPrivateKey(i32* %22, i32 0, i32* null, i8* %23)
store i32* %24, i32** %6
%25 = load i32*, i32** %6
%26 = icmp ne i32* %25, null
br i1 %26, label %33, label %27
27: ; preds = %21
%28 = call i32 @rsa_err(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32*, i32** %7
%30 = call i32 @fclose(i32* %29)
%31 = load i32, i32* @EPROTO
%32 = sub nsw i32 0, %31
store i32 %32, i32* %3
br label %38
33: ; preds = %21
%34 = load i32*, i32** %7
%35 = call i32 @fclose(i32* %34)
%36 = load i32*, i32** %6
%37 = load i32**, i32*** %5
store i32* %36, i32** %37
store i32 0, i32* %3
br label %38
38: ; preds = %33, %27, %13
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [41 x i8] c"Couldn't open RSA private key: '%s': %s\0A\00"
@errno = global i32 0
@ENOENT = global i32 0
@.str.2 = [28 x i8] c"Failure reading private key\00"
@EPROTO = global i32 0
define i32 @rsa_get_priv_key(i8* %0, i32** nocapture writeonly %1) {
store i32* null, i32** %1
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %12
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %8)
%10 = load i32, i32* @ENOENT
%11 = sub nsw i32 0, %10
br label %22
12: ; preds = %2
%13 = tail call i32* @PEM_read_RSAPrivateKey(i32* %3, i32 0, i32* null, i8* %0)
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = tail call i32 @rsa_err(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @fclose(i32* %3)
%18 = load i32, i32* @EPROTO
%19 = sub nsw i32 0, %18
br label %22
20: ; preds = %12
%21 = tail call i32 @fclose(i32* %3)
store i32* %13, i32** %1
br label %22
22: ; preds = %20, %15, %5
%23 = phi i32 [ 0, %20 ], [ %19, %15 ], [ %11, %5 ]
ret i32 %23
}