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

/* Variables and functions */
int /*<<< orphan*/ ERR_print_errors_fp(int /*<<< orphan*/);
int /*<<< orphan*/ *PEM_read_X509(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                  int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ *X509_extract_key(int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ stderr;
EVP_PKEY *ReadPublicKey(const char *certfile) {
  FILE *fp = fopen(certfile, "r");
  X509 *x509;
  EVP_PKEY *pkey;
  if (!fp)
    return NULL;
  x509 = PEM_read_X509(fp, NULL, 0, NULL);
  if (x509 == NULL) {
    ERR_print_errors_fp(stderr);
    return NULL;
  }
  fclose(fp);
  pkey = X509_extract_key(x509);
  X509_free(x509);
  if (pkey == NULL)
    ERR_print_errors_fp(stderr);
  return pkey;
}


x86 assembly (clang, -O0):
<ReadPublicKey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1198 <ReadPublicKey+0x38>
movq   $0x0,-0x8(%rbp)
jmp    1214 <ReadPublicKey+0xb4>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
xor    %edx,%edx
mov    %rcx,%rsi
call   1030 <PEM_read_X509@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d4 <ReadPublicKey+0x74>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   1080 <ERR_print_errors_fp@plt>
movq   $0x0,-0x8(%rbp)
jmp    1214 <ReadPublicKey+0xb4>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1050 <X509_extract_key@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <X509_free@plt>
cmpq   $0x0,-0x28(%rbp)
jne    120c <ReadPublicKey+0xac>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1080 <ERR_print_errors_fp@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadPublicKey>:
push   %r14
push   %rbx
push   %rax
lea    0xe95(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11bc <ReadPublicKey+0x5c>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <PEM_read_X509@plt>
test   %rax,%rax
je     11ae <ReadPublicKey+0x4e>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1050 <X509_extract_key@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1060 <X509_free@plt>
test   %rbx,%rbx
jne    11be <ReadPublicKey+0x5e>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1080 <ERR_print_errors_fp@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
define i32* @ReadPublicKey(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = icmp ne i32* %9, null
br i1 %10, label %12, label %11
11: ; preds = %1
store i32* null, i32** %2
br label %34
12: ; preds = %1
%13 = load i32*, i32** %4
%14 = call i32* @PEM_read_X509(i32* %13, i32* null, i32 0, i32* null)
store i32* %14, i32** %5
%15 = load i32*, i32** %5
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = call i32 @ERR_print_errors_fp(i32 %18)
store i32* null, i32** %2
br label %34
20: ; preds = %12
%21 = load i32*, i32** %4
%22 = call i32 @fclose(i32* %21)
%23 = load i32*, i32** %5
%24 = call i32* @X509_extract_key(i32* %23)
store i32* %24, i32** %6
%25 = load i32*, i32** %5
%26 = call i32 @X509_free(i32* %25)
%27 = load i32*, i32** %6
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %32
29: ; preds = %20
%30 = load i32, i32* @stderr
%31 = call i32 @ERR_print_errors_fp(i32 %30)
br label %32
32: ; preds = %29, %20
%33 = load i32*, i32** %6
store i32* %33, i32** %2
br label %34
34: ; preds = %32, %17, %11
%35 = load i32*, i32** %2
ret i32* %35
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
define i32* @ReadPublicKey(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %15, label %4
4: ; preds = %1
%5 = tail call i32* @PEM_read_X509(i32* %2, i32* null, i32 0, i32* null)
%6 = icmp eq i32* %5, null
br i1 %6, label %12, label %7
7: ; preds = %4
%8 = tail call i32 @fclose(i32* %2)
%9 = tail call i32* @X509_extract_key(i32* %5)
%10 = tail call i32 @X509_free(i32* %5)
%11 = icmp eq i32* %9, null
br i1 %11, label %12, label %15
12: ; preds = %7, %4
%13 = load i32, i32* @stderr
%14 = tail call i32 @ERR_print_errors_fp(i32 %13)
br label %15
15: ; preds = %12, %7, %1
%16 = phi i32* [ null, %1 ], [ %9, %7 ], [ null, %12 ]
ret i32* %16
}