#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 */
unsigned long ERR_get_error();
int /*<<< orphan*/ ERR_reason_error_string(unsigned long);
int /*<<< orphan*/ *PEM_read_RSAPrivateKey(int /*<<< orphan*/ *,
                                           int /*<<< orphan*/ *,
                                           int (*)(char *, int, int, void *),
                                           void *);
int /*<<< orphan*/ RSA_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ strcmp(char *, int /*<<< orphan*/);
int check_pkey_passphrase(const char *pkey_file,
                          int (*password_cb)(char *buf, int size, int rwflag,
                                             void *userdata),
                          void *userdata) {
  RSA *rsa;
  FILE *f;
  f = fopen(pkey_file, "r");
  if (!f)
    return -23;
  rsa = PEM_read_RSAPrivateKey(f, NULL, password_cb, userdata);
  fclose(f);
  if (!rsa) {
    unsigned long int e = ERR_get_error();
    if (e == 0x6065064 ||
        strcmp("bad decrypt", ERR_reason_error_string(e)) == 0)
      return -22;
    return -23;
  }
  RSA_free(rsa);
  return 0;
}


x86 assembly (clang, -O0):
<check_pkey_passphrase>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe71(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11af <check_pkey_passphrase+0x3f>
movl   $0xffffffe9,-0x4(%rbp)
jmp    123e <check_pkey_passphrase+0xce>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <PEM_read_RSAPrivateKey@plt>
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x28(%rbp)
jne    122e <check_pkey_passphrase+0xbe>
mov    $0x0,%al
call   1090 <ERR_get_error@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x6065064,-0x38(%rbp)
06
je     1216 <check_pkey_passphrase+0xa6>
mov    -0x38(%rbp),%rdi
call   1050 <ERR_reason_error_string@plt>
mov    %eax,%esi
lea    0xdfb(%rip),%rdi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1222 <check_pkey_passphrase+0xb2>
movl   $0xffffffea,-0x4(%rbp)
jmp    123e <check_pkey_passphrase+0xce>
movl   $0xffffffe9,-0x4(%rbp)
jmp    123e <check_pkey_passphrase+0xce>
mov    -0x28(%rbp),%rdi
call   1040 <RSA_free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_pkey_passphrase>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
lea    0xe7b(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11bf <check_pkey_passphrase+0x4f>
mov    %rax,%rbx
xor    %r12d,%r12d
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15,%rdx
mov    %r14,%rcx
call   1070 <PEM_read_RSAPrivateKey@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %r14,%r14
je     11c7 <check_pkey_passphrase+0x57>
mov    %r14,%rdi
call   1040 <RSA_free@plt>
jmp    1200 <check_pkey_passphrase+0x90>
mov    $0xffffffe9,%r12d
jmp    1200 <check_pkey_passphrase+0x90>
xor    %eax,%eax
call   1090 <ERR_get_error@plt>
mov    $0xffffffea,%r12d
cmp    $0x6065064,%rax
je     1200 <check_pkey_passphrase+0x90>
mov    %rax,%rdi
call   1050 <ERR_reason_error_string@plt>
lea    0xe17(%rip),%rdi
mov    %eax,%esi
call   1060 <strcmp@plt>
cmp    $0x1,%rax
mov    $0xffffffe9,%r12d
adc    $0x0,%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"
@.str.1 = [12 x i8] c"bad decrypt\00"
define i32 @check_pkey_passphrase(i8* %0, i32 (i8*, i32, i32, i8*)* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32 (i8*, i32, i32, i8*)*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i64
store i8* %0, i8** %5
store i32 (i8*, i32, i32, i8*)* %1, i32 (i8*, i32, i32, i8*)** %6
store i8* %2, i8** %7
%11 = load i8*, i8** %5
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %9
%13 = load i32*, i32** %9
%14 = icmp ne i32* %13, null
br i1 %14, label %16, label %15
15: ; preds = %3
store i32 -23, i32* %4
br label %39
16: ; preds = %3
%17 = load i32*, i32** %9
%18 = load i32 (i8*, i32, i32, i8*)*, i32 (i8*, i32, i32, i8*)** %6
%19 = load i8*, i8** %7
%20 = call i32* @PEM_read_RSAPrivateKey(i32* %17, i32* null, i32 (i8*, i32, i32, i8*)* %18, i8* %19)
store i32* %20, i32** %8
%21 = load i32*, i32** %9
%22 = call i32 @fclose(i32* %21)
%23 = load i32*, i32** %8
%24 = icmp ne i32* %23, null
br i1 %24, label %36, label %25
25: ; preds = %16
%26 = call i64 (...) @ERR_get_error()
store i64 %26, i64* %10
%27 = load i64, i64* %10
%28 = icmp eq i64 %27, 101077092
br i1 %28, label %34, label %29
29: ; preds = %25
%30 = load i64, i64* %10
%31 = call i32 @ERR_reason_error_string(i64 %30)
%32 = call i64 @strcmp(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %29, %25
store i32 -22, i32* %4
br label %39
35: ; preds = %29
store i32 -23, i32* %4
br label %39
36: ; preds = %16
%37 = load i32*, i32** %8
%38 = call i32 @RSA_free(i32* %37)
store i32 0, i32* %4
br label %39
39: ; preds = %36, %35, %34, %15
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [12 x i8] c"bad decrypt\00"
define i32 @check_pkey_passphrase(i8* nocapture %0, i32 (i8*, i32, i32, i8*)* %1, i8* %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %20, label %6
6: ; preds = %3
%7 = tail call i32* @PEM_read_RSAPrivateKey(i32* %4, i32* null, i32 (i8*, i32, i32, i8*)* %1, i8* %2)
%8 = tail call i32 @fclose(i32* %4)
%9 = icmp eq i32* %7, null
br i1 %9, label %10, label %18
10: ; preds = %6
%11 = tail call i64 (...) @ERR_get_error()
%12 = icmp eq i64 %11, 101077092
br i1 %12, label %20, label %13
13: ; preds = %10
%14 = tail call i32 @ERR_reason_error_string(i64 %11)
%15 = tail call i64 @strcmp(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = icmp eq i64 %15, 0
%17 = select i1 %16, i32 -22, i32 -23
br label %20
18: ; preds = %6
%19 = tail call i32 @RSA_free(i32* %7)
br label %20
20: ; preds = %13, %10, %3, %18
%21 = phi i32 [ 0, %18 ], [ -23, %3 ], [ -22, %10 ], [ %17, %13 ]
ret i32 %21
}