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

/* Variables and functions */
int EVP_PKEY_base_id(int /*<<< orphan*/ *);
int /*<<< orphan*/ OpenSSL_add_all_algorithms();
int /*<<< orphan*/ *PEM_read_PrivateKey(int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ printf(char *, ...);
EVP_PKEY *get_evp_pkey(const char *pkey_file) {
  FILE *fp = NULL;
  EVP_PKEY *pkey = NULL;
  do {
    if (!(fp = fopen(pkey_file, "r"))) {
      printf("Failed to open file %s\n", pkey_file);
      break;
    }
    OpenSSL_add_all_algorithms();
    pkey = PEM_read_PrivateKey(fp, NULL, NULL, NULL);
    if (!pkey)
      printf("failed to read private key\n");
    printf("type = %d\n", EVP_PKEY_base_id(pkey));
  } while (0);
  if (fp)
    fclose(fp);
  return pkey;
}


x86 assembly (clang, -O0):
<get_evp_pkey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11b1 <get_evp_pkey+0x51>
mov    -0x8(%rbp),%rsi
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1201 <get_evp_pkey+0xa1>
mov    $0x0,%al
call   1060 <OpenSSL_add_all_algorithms@plt>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1080 <PEM_read_PrivateKey@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e8 <get_evp_pkey+0x88>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <EVP_PKEY_base_id@plt>
mov    %eax,%esi
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpq   $0x0,-0x10(%rbp)
je     1215 <get_evp_pkey+0xb5>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_evp_pkey>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
lea    0xe81(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11d7 <get_evp_pkey+0x67>
mov    %rax,%r14
xor    %eax,%eax
call   1070 <OpenSSL_add_all_algorithms@plt>
mov    %r14,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1090 <PEM_read_PrivateKey@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11b5 <get_evp_pkey+0x45>
lea    0xe75(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
call   1030 <EVP_PKEY_base_id@plt>
lea    0xe56(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
jmp    11eb <get_evp_pkey+0x7b>
lea    0xe24(%rip),%rdi
xor    %r15d,%r15d
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [24 x i8] c"Failed to open file %s\0A\00"
@.str.2 = [28 x i8] c"failed to read private key\0A\00"
@.str.3 = [11 x i8] c"type = %d\0A\00"
define i32* @get_evp_pkey(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32*
store i8* %0, i8** %2
store i32* null, i32** %3
store i32* null, i32** %4
br label %5
5: ; preds = %1
%6 = load i8*, i8** %2
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %3
%8 = icmp ne i32* %7, null
br i1 %8, label %12, label %9
9: ; preds = %5
%10 = load i8*, i8** %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %24
12: ; preds = %5
%13 = call i32 (...) @OpenSSL_add_all_algorithms()
%14 = load i32*, i32** %3
%15 = call i32* @PEM_read_PrivateKey(i32* %14, i32* null, i32* null, i32* null)
store i32* %15, i32** %4
%16 = load i32*, i32** %4
%17 = icmp ne i32* %16, null
br i1 %17, label %20, label %18
18: ; preds = %12
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %12
%21 = load i32*, i32** %4
%22 = call i32 @EVP_PKEY_base_id(i32* %21)
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %20, %9
%25 = load i32*, i32** %3
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32*, i32** %3
%29 = call i32 @fclose(i32* %28)
br label %30
30: ; preds = %27, %24
%31 = load i32*, i32** %4
ret i32* %31
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [24 x i8] c"Failed to open file %s\0A\00"
@.str.3 = [11 x i8] c"type = %d\0A\00"
@str = [27 x i8] c"failed to read private key\00"
define i32* @get_evp_pkey(i8* %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 %4, label %6
4: ; preds = %1
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %16
6: ; preds = %1
%7 = tail call i32 (...) @OpenSSL_add_all_algorithms()
%8 = tail call i32* @PEM_read_PrivateKey(i32* %2, i32* null, i32* null, i32* null)
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %12
12: ; preds = %10, %6
%13 = tail call i32 @EVP_PKEY_base_id(i32* %8)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = tail call i32 @fclose(i32* %2)
br label %16
16: ; preds = %4, %12
%17 = phi i32* [ null, %4 ], [ %8, %12 ]
ret i32* %17
}