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

/* Variables and functions */
int /*<<< orphan*/ BIO_CLOSE;
int /*<<< orphan*/ BIO_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_new(int /*<<< orphan*/);
int /*<<< orphan*/ *BIO_new_mem_buf(char *, int);
int /*<<< orphan*/ BIO_read(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ BIO_reset(int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_s_file();
int /*<<< orphan*/ BIO_set_fp(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                              int /*<<< orphan*/);
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ *PEM_read_bio_PrivateKey(int /*<<< orphan*/ *,
                                            int /*<<< orphan*/ *,
                                            int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ SSL_library_init();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
char *strchr(char *, char);
int main(int argc, char *argv[]) {
  BIO *mem;
  BIO *file;
  EVP_PKEY *pkey;
  FILE *fp;
  char *ptr;
  char pwd[8192];
  char key[8192];
  if (argc < 2) {
    fprintf(stderr, "Usage:\n\t%s <rsa key .pem>\n\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  fp = fopen(argv[1], "r");
  memset(key, 0x00, 8192);
  SSL_library_init();
  file = BIO_new(BIO_s_file());
  BIO_set_fp(file, fp, BIO_CLOSE);
  BIO_read(file, key, 8192);
  BIO_free(file);
  mem = BIO_new_mem_buf(key, -1);
  while (fgets(pwd, sizeof(pwd), stdin)) {
    ptr = strchr(pwd, '\n');
    if (ptr)
      *ptr = '\0';
    BIO_reset(mem);
    pkey = PEM_read_bio_PrivateKey(mem, NULL, NULL, (char *)pwd);
    if (pkey) {
      printf("\nPassphrase is: %s\n", pwd);
      goto end;
    }
  }
  printf("Passphrase not found.\n");
end:
  if (mem)
    BIO_free(mem);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    124d <main+0x4d>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdc6(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdc0(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x30(%rbp)
lea    -0x4040(%rbp),%rdi
xor    %esi,%esi
mov    $0x2000,%edx
call   1060 <memset@plt>
mov    $0x0,%al
call   1070 <SSL_library_init@plt>
mov    $0x0,%al
call   1120 <BIO_s_file@plt>
mov    %eax,%edi
call   10b0 <BIO_new@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    0x2d20(%rip),%rax
mov    (%rax),%edx
call   10d0 <BIO_set_fp@plt>
mov    -0x20(%rbp),%rdi
lea    -0x4040(%rbp),%rsi
mov    $0x2000,%edx
call   1030 <BIO_read@plt>
mov    -0x20(%rbp),%rdi
call   10c0 <BIO_free@plt>
lea    -0x4040(%rbp),%rdi
mov    $0xffffffff,%esi
call   10a0 <BIO_new_mem_buf@plt>
mov    %rax,-0x18(%rbp)
lea    -0x2040(%rbp),%rdi
mov    0x2d00(%rip),%rax
mov    (%rax),%edx
mov    $0x2000,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     1373 <main+0x173>
lea    -0x2040(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1325 <main+0x125>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
call   10f0 <BIO_reset@plt>
mov    -0x18(%rbp),%rdi
lea    -0x2040(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1100 <PEM_read_bio_PrivateKey@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     136e <main+0x16e>
lea    -0x2040(%rbp),%rsi
lea    0xcbc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1381 <main+0x181>
jmp    12da <main+0xda>
lea    0xcb8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpq   $0x0,-0x18(%rbp)
je     1395 <main+0x195>
mov    -0x18(%rbp),%rdi
call   10c0 <BIO_free@plt>
mov    0x2c2c(%rip),%rax
mov    (%rax),%eax
add    $0x4040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x4000,%rsp
cmp    $0x1,%edi
jle    133f <main+0x12f>
mov    0x8(%rsi),%rdi
lea    0xdec(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,%r15
lea    0x2000(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x2000,%edx
call   1070 <memset@plt>
xor    %eax,%eax
call   1080 <SSL_library_init@plt>
xor    %eax,%eax
call   1130 <BIO_s_file@plt>
mov    %eax,%edi
call   10c0 <BIO_new@plt>
mov    %rax,%rbx
mov    0x2d52(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   10e0 <BIO_set_fp@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x2000,%edx
call   1040 <BIO_read@plt>
mov    %rbx,%rdi
call   10d0 <BIO_free@plt>
mov    %r14,%rdi
mov    $0xffffffff,%esi
call   10b0 <BIO_new_mem_buf@plt>
mov    %rax,%rbx
mov    0x2d3e(%rip),%r15
mov    %rsp,%r14
jmp    12cc <main+0xbc>
nop
mov    %rbx,%rdi
call   1100 <BIO_reset@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
mov    %r14,%rcx
call   1110 <PEM_read_bio_PrivateKey@plt>
test   %rax,%rax
jne    1327 <main+0x117>
mov    (%r15),%edx
mov    %r14,%rdi
mov    $0x2000,%esi
call   1090 <fgets@plt>
test   %rax,%rax
je     12f8 <main+0xe8>
mov    %r14,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     12b0 <main+0xa0>
movb   $0x0,(%rax)
jmp    12b0 <main+0xa0>
lea    0xd33(%rip),%rdi
call   1030 <puts@plt>
test   %rbx,%rbx
je     1311 <main+0x101>
mov    %rbx,%rdi
call   10d0 <BIO_free@plt>
mov    0x2cb0(%rip),%rax
mov    (%rax),%eax
add    $0x4000,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xcf0(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
test   %rbx,%rbx
jne    1309 <main+0xf9>
jmp    1311 <main+0x101>
mov    0x2c92(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xcae(%rip),%rsi
call   10a0 <fprintf@plt>
mov    0x2c72(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"Usage:\0A\09%s <rsa key .pem>\0A\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"r\00"
@BIO_CLOSE = global i32 0
@stdin = global i32 0
@.str.2 = [20 x i8] c"\0APassphrase is: %s\0A\00"
@.str.3 = [23 x i8] c"Passphrase not found.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca [8192 x i8]
%12 = alloca [8192 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %23
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %27, i32** %9
%28 = getelementptr inbounds [8192 x i8], [8192 x i8]* %12, i64 0, i64 0
%29 = call i32 @memset(i8* %28, i32 0, i32 8192)
%30 = call i32 (...) @SSL_library_init()
%31 = call i32 (...) @BIO_s_file()
%32 = call i32* @BIO_new(i32 %31)
store i32* %32, i32** %7
%33 = load i32*, i32** %7
%34 = load i32*, i32** %9
%35 = load i32, i32* @BIO_CLOSE
%36 = call i32 @BIO_set_fp(i32* %33, i32* %34, i32 %35)
%37 = load i32*, i32** %7
%38 = getelementptr inbounds [8192 x i8], [8192 x i8]* %12, i64 0, i64 0
%39 = call i32 @BIO_read(i32* %37, i8* %38, i32 8192)
%40 = load i32*, i32** %7
%41 = call i32 @BIO_free(i32* %40)
%42 = getelementptr inbounds [8192 x i8], [8192 x i8]* %12, i64 0, i64 0
%43 = call i32* @BIO_new_mem_buf(i8* %42, i32 -1)
store i32* %43, i32** %6
br label %44
44: ; preds = %67, %23
%45 = getelementptr inbounds [8192 x i8], [8192 x i8]* %11, i64 0, i64 0
%46 = load i32, i32* @stdin
%47 = call i64 @fgets(i8* %45, i32 8192, i32 %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %68
49: ; preds = %44
%50 = getelementptr inbounds [8192 x i8], [8192 x i8]* %11, i64 0, i64 0
%51 = call i8* @strchr(i8* %50, i8 signext 10)
store i8* %51, i8** %10
%52 = load i8*, i8** %10
%53 = icmp ne i8* %52, null
br i1 %53, label %54, label %56
54: ; preds = %49
%55 = load i8*, i8** %10
store i8 0, i8* %55
br label %56
56: ; preds = %54, %49
%57 = load i32*, i32** %6
%58 = call i32 @BIO_reset(i32* %57)
%59 = load i32*, i32** %6
%60 = getelementptr inbounds [8192 x i8], [8192 x i8]* %11, i64 0, i64 0
%61 = call i32* @PEM_read_bio_PrivateKey(i32* %59, i32* null, i32* null, i8* %60)
store i32* %61, i32** %8
%62 = load i32*, i32** %8
%63 = icmp ne i32* %62, null
br i1 %63, label %64, label %67
64: ; preds = %56
%65 = getelementptr inbounds [8192 x i8], [8192 x i8]* %11, i64 0, i64 0
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %65)
br label %70
67: ; preds = %56
br label %44
68: ; preds = %44
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %70
70: ; preds = %68, %64
%71 = load i32*, i32** %6
%72 = icmp ne i32* %71, null
br i1 %72, label %73, label %76
73: ; preds = %70
%74 = load i32*, i32** %6
%75 = call i32 @BIO_free(i32* %74)
br label %76
76: ; preds = %73, %70
%77 = load i32, i32* @EXIT_SUCCESS
ret i32 %77
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"Usage:\0A\09%s <rsa key .pem>\0A\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"r\00"
@BIO_CLOSE = global i32 0
@stdin = global i32 0
@.str.2 = [20 x i8] c"\0APassphrase is: %s\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [22 x i8] c"Passphrase not found.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [8192 x i8]
%4 = alloca [8192 x i8]
%5 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 0
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %14
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = tail call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @memset(i8* %6, i32 0, i32 8192)
%19 = call i32 (...) @SSL_library_init()
%20 = call i32 (...) @BIO_s_file()
%21 = call i32* @BIO_new(i32 %20)
%22 = load i32, i32* @BIO_CLOSE
%23 = call i32 @BIO_set_fp(i32* %21, i32* %17, i32 %22)
%24 = call i32 @BIO_read(i32* %21, i8* %6, i32 8192)
%25 = call i32 @BIO_free(i32* %21)
%26 = call i32* @BIO_new_mem_buf(i8* %6, i32 -1)
br label %27
27: ; preds = %35, %14
%28 = load i32, i32* @stdin
%29 = call i64 @fgets(i8* %5, i32 8192, i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %41, label %31
31: ; preds = %27
%32 = call i8* @strchr(i8* %5, i8 signext 10)
%33 = icmp eq i8* %32, null
br i1 %33, label %35, label %34
34: ; preds = %31
store i8 0, i8* %32
br label %35
35: ; preds = %34, %31
%36 = call i32 @BIO_reset(i32* %26)
%37 = call i32* @PEM_read_bio_PrivateKey(i32* %26, i32* null, i32* null, i8* %5)
%38 = icmp eq i32* %37, null
br i1 %38, label %27, label %39
39: ; preds = %35
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %5)
br label %43
41: ; preds = %27
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %43
43: ; preds = %41, %39
%44 = icmp eq i32* %26, null
br i1 %44, label %47, label %45
45: ; preds = %43
%46 = call i32 @BIO_free(i32* %26)
br label %47
47: ; preds = %45, %43
%48 = load i32, i32* @EXIT_SUCCESS
ret i32 %48
}