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

/* Variables and functions */
int EACCES;
int EINVAL;
int /*<<< orphan*/ EVP_PKEY_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_PKEY_get1_RSA(int /*<<< orphan*/ *);
int /*<<< orphan*/ PEM_read_X509(int /*<<< orphan*/ *, int /*<<< orphan*/ **,
                                 int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *X509_get_pubkey(int /*<<< orphan*/ *);
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_pub_key(const char *path, RSA **rsap) {
  EVP_PKEY *key;
  X509 *cert;
  RSA *rsa;
  FILE *f;
  int ret;
  *rsap = NULL;
  f = fopen(path, "r");
  if (!f) {
    fprintf(stderr, "Couldn't open RSA certificate: '%s': %s\n", path,
            strerror(errno));
    return -EACCES;
  }
  cert = NULL;
  if (!PEM_read_X509(f, &cert, NULL, NULL)) {
    rsa_err("Couldn't read certificate");
    ret = -EINVAL;
    goto err_cert;
  }
  key = X509_get_pubkey(cert);
  if (!key) {
    rsa_err("Couldn't read public key\n");
    ret = -EINVAL;
    goto err_pubkey;
  }
  rsa = EVP_PKEY_get1_RSA(key);
  if (!rsa) {
    rsa_err("Couldn't convert to a RSA style key");
    goto err_rsa;
  }
  fclose(f);
  EVP_PKEY_free(key);
  X509_free(cert);
  *rsap = rsa;
  return 0;
err_rsa:
  EVP_PKEY_free(key);
err_pubkey:
  X509_free(cert);
err_cert:
  fclose(f);
  return ret;
}


x86 assembly (clang, -O0):
<rsa_get_pub_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    0xe3a(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1225 <rsa_get_pub_key+0x85>
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xdf5(%rip),%rsi
call   1070 <fprintf@plt>
xor    %eax,%eax
mov    0x2dcd(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    131c <rsa_get_pub_key+0x17c>
movq   $0x0,-0x28(%rbp)
mov    -0x38(%rbp),%rdi
lea    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1030 <PEM_read_X509@plt>
cmp    $0x0,%eax
jne    1269 <rsa_get_pub_key+0xc9>
lea    0xdda(%rip),%rdi
call   1060 <rsa_err@plt>
xor    %eax,%eax
mov    0x2d69(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x3c(%rbp)
jmp    130d <rsa_get_pub_key+0x16d>
mov    -0x28(%rbp),%rdi
call   1080 <X509_get_pubkey@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12a0 <rsa_get_pub_key+0x100>
lea    0xdbd(%rip),%rdi
call   1060 <rsa_err@plt>
xor    %eax,%eax
mov    0x2d32(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x3c(%rbp)
jmp    1304 <rsa_get_pub_key+0x164>
mov    -0x20(%rbp),%rdi
call   1090 <EVP_PKEY_get1_RSA@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12c9 <rsa_get_pub_key+0x129>
lea    0xda0(%rip),%rdi
call   1060 <rsa_err@plt>
jmp    12fb <rsa_get_pub_key+0x15b>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1050 <EVP_PKEY_free@plt>
mov    -0x28(%rbp),%rdi
call   10a0 <X509_free@plt>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    131c <rsa_get_pub_key+0x17c>
mov    -0x20(%rbp),%rdi
call   1050 <EVP_PKEY_free@plt>
mov    -0x28(%rbp),%rdi
call   10a0 <X509_free@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rsa_get_pub_key>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
movq   $0x0,(%rsi)
lea    0xe40(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     123f <rsa_get_pub_key+0x9f>
mov    %rax,%r15
movq   $0x0,0x8(%rsp)
xor    %ebx,%ebx
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <PEM_read_X509@plt>
test   %eax,%eax
je     1277 <rsa_get_pub_key+0xd7>
mov    0x8(%rsp),%rdi
call   1080 <X509_get_pubkey@plt>
test   %rax,%rax
je     128e <rsa_get_pub_key+0xee>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <EVP_PKEY_get1_RSA@plt>
test   %rax,%rax
je     12a7 <rsa_get_pub_key+0x107>
mov    %rax,%r12
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
call   1050 <EVP_PKEY_free@plt>
mov    0x8(%rsp),%rdi
call   10a0 <X509_free@plt>
mov    %r12,(%r14)
xor    %ebx,%ebx
jmp    12cd <rsa_get_pub_key+0x12d>
mov    0x2d92(%rip),%rax
mov    (%rax),%ebp
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xda5(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1070 <fprintf@plt>
mov    0x2d77(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    12cd <rsa_get_pub_key+0x12d>
lea    0xdad(%rip),%rdi
call   1060 <rsa_err@plt>
mov    0x2d3e(%rip),%rax
sub    (%rax),%ebx
jmp    12c5 <rsa_get_pub_key+0x125>
lea    0xdb0(%rip),%rdi
call   1060 <rsa_err@plt>
mov    0x2d27(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    12bb <rsa_get_pub_key+0x11b>
lea    0xdb1(%rip),%rdi
call   1060 <rsa_err@plt>
mov    %rbx,%rdi
call   1050 <EVP_PKEY_free@plt>
mov    0x8(%rsp),%rdi
call   10a0 <X509_free@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
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 certificate: '%s': %s\0A\00"
@errno = global i32 0
@EACCES = global i32 0
@.str.2 = [26 x i8] c"Couldn't read certificate\00"
@EINVAL = global i32 0
@.str.3 = [26 x i8] c"Couldn't read public key\0A\00"
@.str.4 = [36 x i8] c"Couldn't convert to a RSA style key\00"
define i32 @rsa_get_pub_key(i8* %0, i32** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
store i8* %0, i8** %4
store i32** %1, i32*** %5
%11 = load i32**, i32*** %5
store i32* null, i32** %11
%12 = load i8*, i8** %4
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %9
%14 = load i32*, i32** %9
%15 = icmp ne i32* %14, null
br i1 %15, label %24, label %16
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %4
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %18, i8* %20)
%22 = load i32, i32* @EACCES
%23 = sub nsw i32 0, %22
store i32 %23, i32* %3
br label %67
24: ; preds = %2
store i32* null, i32** %7
%25 = load i32*, i32** %9
%26 = call i32 @PEM_read_X509(i32* %25, i32** %7, i32* null, i32* null)
%27 = icmp ne i32 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %24
%29 = call i32 @rsa_err(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%30 = load i32, i32* @EINVAL
%31 = sub nsw i32 0, %30
store i32 %31, i32* %10
br label %63
32: ; preds = %24
%33 = load i32*, i32** %7
%34 = call i32* @X509_get_pubkey(i32* %33)
store i32* %34, i32** %6
%35 = load i32*, i32** %6
%36 = icmp ne i32* %35, null
br i1 %36, label %41, label %37
37: ; preds = %32
%38 = call i32 @rsa_err(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%39 = load i32, i32* @EINVAL
%40 = sub nsw i32 0, %39
store i32 %40, i32* %10
br label %60
41: ; preds = %32
%42 = load i32*, i32** %6
%43 = call i32* @EVP_PKEY_get1_RSA(i32* %42)
store i32* %43, i32** %8
%44 = load i32*, i32** %8
%45 = icmp ne i32* %44, null
br i1 %45, label %48, label %46
46: ; preds = %41
%47 = call i32 @rsa_err(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
br label %57
48: ; preds = %41
%49 = load i32*, i32** %9
%50 = call i32 @fclose(i32* %49)
%51 = load i32*, i32** %6
%52 = call i32 @EVP_PKEY_free(i32* %51)
%53 = load i32*, i32** %7
%54 = call i32 @X509_free(i32* %53)
%55 = load i32*, i32** %8
%56 = load i32**, i32*** %5
store i32* %55, i32** %56
store i32 0, i32* %3
br label %67
57: ; preds = %46
%58 = load i32*, i32** %6
%59 = call i32 @EVP_PKEY_free(i32* %58)
br label %60
60: ; preds = %57, %37
%61 = load i32*, i32** %7
%62 = call i32 @X509_free(i32* %61)
br label %63
63: ; preds = %60, %28
%64 = load i32*, i32** %9
%65 = call i32 @fclose(i32* %64)
%66 = load i32, i32* %10
store i32 %66, i32* %3
br label %67
67: ; preds = %63, %48, %16
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [41 x i8] c"Couldn't open RSA certificate: '%s': %s\0A\00"
@errno = global i32 0
@EACCES = global i32 0
@.str.2 = [26 x i8] c"Couldn't read certificate\00"
@EINVAL = global i32 0
@.str.3 = [26 x i8] c"Couldn't read public key\0A\00"
@.str.4 = [36 x i8] c"Couldn't convert to a RSA style key\00"
define i32 @rsa_get_pub_key(i8* %0, i32** nocapture writeonly %1) {
%3 = alloca i32*
%4 = bitcast i32** %3 to i8*
store i32* null, i32** %1
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %14
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %10)
%12 = load i32, i32* @EACCES
%13 = sub nsw i32 0, %12
br label %47
14: ; preds = %2
store i32* null, i32** %3
%15 = call i32 @PEM_read_X509(i32* %5, i32** %3, i32* null, i32* null)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = call i32 @rsa_err(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* @EINVAL
%20 = sub nsw i32 0, %19
br label %44
21: ; preds = %14
%22 = load i32*, i32** %3
%23 = call i32* @X509_get_pubkey(i32* %22)
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = call i32 @rsa_err(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%27 = load i32, i32* @EINVAL
%28 = sub nsw i32 0, %27
br label %40
29: ; preds = %21
%30 = call i32* @EVP_PKEY_get1_RSA(i32* %23)
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = call i32 @rsa_err(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
%34 = call i32 @EVP_PKEY_free(i32* %23)
br label %40
35: ; preds = %29
%36 = call i32 @fclose(i32* %5)
%37 = call i32 @EVP_PKEY_free(i32* %23)
%38 = load i32*, i32** %3
%39 = call i32 @X509_free(i32* %38)
store i32* %30, i32** %1
br label %47
40: ; preds = %32, %25
%41 = phi i32 [ undef, %32 ], [ %28, %25 ]
%42 = load i32*, i32** %3
%43 = call i32 @X509_free(i32* %42)
br label %44
44: ; preds = %40, %17
%45 = phi i32 [ %41, %40 ], [ %20, %17 ]
%46 = call i32 @fclose(i32* %5)
br label %47
47: ; preds = %44, %35, %7
%48 = phi i32 [ 0, %35 ], [ %45, %44 ], [ %13, %7 ]
ret i32 %48
}