#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_pem_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");
    ret = -EINVAL;
    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_pem_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_pem_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    132a <rsa_pem_get_pub_key+0x18a>
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_pem_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    131b <rsa_pem_get_pub_key+0x17b>
mov    -0x28(%rbp),%rdi
call   1080 <X509_get_pubkey@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12a0 <rsa_pem_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    1312 <rsa_pem_get_pub_key+0x172>
mov    -0x20(%rbp),%rdi
call   1090 <EVP_PKEY_get1_RSA@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12d7 <rsa_pem_get_pub_key+0x137>
lea    0xda0(%rip),%rdi
call   1060 <rsa_err@plt>
xor    %eax,%eax
mov    0x2cfb(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x3c(%rbp)
jmp    1309 <rsa_pem_get_pub_key+0x169>
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    132a <rsa_pem_get_pub_key+0x18a>
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_pem_get_pub_key>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbp
movq   $0x0,(%rsi)
lea    0xe45(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1233 <rsa_pem_get_pub_key+0x93>
mov    %rax,%rbx
movq   $0x0,(%rsp)
mov    %rsp,%rsi
mov    %rax,%rdi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <PEM_read_X509@plt>
test   %eax,%eax
je     126b <rsa_pem_get_pub_key+0xcb>
mov    (%rsp),%rdi
call   1080 <X509_get_pubkey@plt>
test   %rax,%rax
je     1282 <rsa_pem_get_pub_key+0xe2>
mov    %rax,%r15
mov    %rax,%rdi
call   1090 <EVP_PKEY_get1_RSA@plt>
test   %rax,%rax
je     1299 <rsa_pem_get_pub_key+0xf9>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1050 <EVP_PKEY_free@plt>
mov    (%rsp),%rdi
call   10a0 <X509_free@plt>
mov    %rbp,(%r14)
xor    %ebp,%ebp
jmp    12c9 <rsa_pem_get_pub_key+0x129>
mov    0x2d9e(%rip),%rax
mov    (%rax),%ebx
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xdb1(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
call   1070 <fprintf@plt>
mov    0x2d83(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    12c9 <rsa_pem_get_pub_key+0x129>
lea    0xdb9(%rip),%rdi
call   1060 <rsa_err@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%ebp
jmp    12bf <rsa_pem_get_pub_key+0x11f>
lea    0xdbc(%rip),%rdi
call   1060 <rsa_err@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%ebp
jmp    12b6 <rsa_pem_get_pub_key+0x116>
lea    0xdbf(%rip),%rdi
call   1060 <rsa_err@plt>
mov    0x2d1c(%rip),%rax
mov    (%rax),%ebp
mov    %r15,%rdi
call   1050 <EVP_PKEY_free@plt>
mov    (%rsp),%rdi
call   10a0 <X509_free@plt>
neg    %ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
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_pem_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 %69
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 %65
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 %62
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 %50, 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))
%48 = load i32, i32* @EINVAL
%49 = sub nsw i32 0, %48
store i32 %49, i32* %10
br label %59
50: ; preds = %41
%51 = load i32*, i32** %9
%52 = call i32 @fclose(i32* %51)
%53 = load i32*, i32** %6
%54 = call i32 @EVP_PKEY_free(i32* %53)
%55 = load i32*, i32** %7
%56 = call i32 @X509_free(i32* %55)
%57 = load i32*, i32** %8
%58 = load i32**, i32*** %5
store i32* %57, i32** %58
store i32 0, i32* %3
br label %69
59: ; preds = %46
%60 = load i32*, i32** %6
%61 = call i32 @EVP_PKEY_free(i32* %60)
br label %62
62: ; preds = %59, %37
%63 = load i32*, i32** %7
%64 = call i32 @X509_free(i32* %63)
br label %65
65: ; preds = %62, %28
%66 = load i32*, i32** %9
%67 = call i32 @fclose(i32* %66)
%68 = load i32, i32* %10
store i32 %68, i32* %3
br label %69
69: ; preds = %65, %50, %16
%70 = load i32, i32* %3
ret i32 %70
}

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_pem_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 %20
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
br label %43
20: ; preds = %14
%21 = load i32*, i32** %3
%22 = call i32* @X509_get_pubkey(i32* %21)
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = call i32 @rsa_err(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* @EINVAL
br label %39
27: ; preds = %20
%28 = call i32* @EVP_PKEY_get1_RSA(i32* %22)
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = call i32 @rsa_err(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
%32 = load i32, i32* @EINVAL
%33 = call i32 @EVP_PKEY_free(i32* %22)
br label %39
34: ; preds = %27
%35 = call i32 @fclose(i32* %5)
%36 = call i32 @EVP_PKEY_free(i32* %22)
%37 = load i32*, i32** %3
%38 = call i32 @X509_free(i32* %37)
store i32* %28, i32** %1
br label %47
39: ; preds = %30, %24
%40 = phi i32 [ %32, %30 ], [ %26, %24 ]
%41 = load i32*, i32** %3
%42 = call i32 @X509_free(i32* %41)
br label %43
43: ; preds = %39, %17
%44 = phi i32 [ %40, %39 ], [ %19, %17 ]
%45 = sub nsw i32 0, %44
%46 = call i32 @fclose(i32* %5)
br label %47
47: ; preds = %43, %34, %7
%48 = phi i32 [ 0, %34 ], [ %45, %43 ], [ %13, %7 ]
ret i32 %48
}