#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 */

/* Variables and functions */
char *EVP_get_pw_prompt();
int EVP_read_pw_string_min(char *, int, int, char const *, int);
int /*<<< orphan*/ PEM_F_PEM_DEF_CALLBACK;
int /*<<< orphan*/ PEM_R_PROBLEMS_GETTING_PASSWORD;
int /*<<< orphan*/ PEMerr(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ memcpy(char *, void *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
int strlen(char *);
int PEM_def_callback(char *buf, int num, int w, void *key) {
  int i, j;
  const char *prompt;
  if (key) {
    i = strlen(key);
    i = (i > num) ? num : i;
    memcpy(buf, key, i);
    return (i);
  }
  prompt = EVP_get_pw_prompt();
  if (prompt == NULL)
    prompt = "Enter PEM pass phrase:";
  for (;;) {
    i = EVP_read_pw_string_min(buf, 4, num, prompt, w);
    if (i != 0) {
      PEMerr(PEM_F_PEM_DEF_CALLBACK, PEM_R_PROBLEMS_GETTING_PASSWORD);
      memset(buf, 0, num);
      return (-1);
    }
    j = strlen(buf);
    if (j < 4) {
      fprintf(stderr, "phrase is too short, needs to be at least %d chars\n",
              4);
    } else
      break;
  }
  return (j);
}


x86 assembly (clang, -O0):
<PEM_def_callback>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11db <PEM_def_callback+0x6b>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    11b4 <PEM_def_callback+0x44>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    11ba <PEM_def_callback+0x4a>
mov    -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129d <PEM_def_callback+0x12d>
mov    $0x0,%al
call   1080 <EVP_get_pw_prompt@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11fc <PEM_def_callback+0x8c>
lea    0xe08(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    1201 <PEM_def_callback+0x91>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%r8d
mov    $0x4,%esi
call   1090 <EVP_read_pw_string_min@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1258 <PEM_def_callback+0xe8>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
call   1040 <PEMerr@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <PEM_def_callback+0x12d>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x4,-0x28(%rbp)
jge    128d <PEM_def_callback+0x11d>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
lea    0xd99(%rip),%rsi
mov    $0x4,%edx
call   1060 <fprintf@plt>
jmp    1292 <PEM_def_callback+0x122>
jmp    1297 <PEM_def_callback+0x127>
jmp    1201 <PEM_def_callback+0x91>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PEM_def_callback>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r12
test   %rcx,%rcx
je     11ac <PEM_def_callback+0x3c>
mov    %rcx,%rbx
mov    %rcx,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
cmp    %r15d,%eax
cmovg  %r15d,%ebp
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1070 <memcpy@plt>
jmp    125a <PEM_def_callback+0xea>
mov    %edx,%r14d
xor    %eax,%eax
call   1080 <EVP_get_pw_prompt@plt>
test   %rax,%rax
lea    0xe40(%rip),%r13
cmovne %rax,%r13
mov    %r12,%rdi
mov    $0x4,%esi
mov    %r15d,%edx
mov    %r13,%rcx
mov    %r14d,%r8d
call   1090 <EVP_read_pw_string_min@plt>
test   %eax,%eax
jne    122d <PEM_def_callback+0xbd>
lea    0xe32(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
call   1030 <strlen@plt>
cmp    $0x3,%eax
jg     1258 <PEM_def_callback+0xe8>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1060 <fprintf@plt>
mov    %r12,%rdi
mov    $0x4,%esi
mov    %r15d,%edx
mov    %r13,%rcx
mov    %r14d,%r8d
call   1090 <EVP_read_pw_string_min@plt>
test   %eax,%eax
je     11f0 <PEM_def_callback+0x80>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
call   1040 <PEMerr@plt>
mov    %r12,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1050 <memset@plt>
mov    $0xffffffff,%ebp
jmp    125a <PEM_def_callback+0xea>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"Enter PEM pass phrase:\00"
@PEM_F_PEM_DEF_CALLBACK = global i32 0
@PEM_R_PROBLEMS_GETTING_PASSWORD = global i32 0
@stderr = global i32 0
@.str.1 = [52 x i8] c"phrase is too short, needs to be at least %d chars\0A\00"
define i32 @PEM_def_callback(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%13 = load i8*, i8** %9
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %32
15: ; preds = %4
%16 = load i8*, i8** %9
%17 = call i32 @strlen(i8* %16)
store i32 %17, i32* %10
%18 = load i32, i32* %10
%19 = load i32, i32* %7
%20 = icmp sgt i32 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = load i32, i32* %7
br label %25
23: ; preds = %15
%24 = load i32, i32* %10
br label %25
25: ; preds = %23, %21
%26 = phi i32 [ %22, %21 ], [ %24, %23 ]
store i32 %26, i32* %10
%27 = load i8*, i8** %6
%28 = load i8*, i8** %9
%29 = load i32, i32* %10
%30 = call i32 @memcpy(i8* %27, i8* %28, i32 %29)
%31 = load i32, i32* %10
store i32 %31, i32* %5
br label %65
32: ; preds = %4
%33 = call i8* (...) @EVP_get_pw_prompt()
store i8* %33, i8** %12
%34 = load i8*, i8** %12
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %37
36: ; preds = %32
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8** %12
br label %37
37: ; preds = %36, %32
br label %38
38: ; preds = %62, %37
%39 = load i8*, i8** %6
%40 = load i32, i32* %7
%41 = load i8*, i8** %12
%42 = load i32, i32* %8
%43 = call i32 @EVP_read_pw_string_min(i8* %39, i32 4, i32 %40, i8* %41, i32 %42)
store i32 %43, i32* %10
%44 = load i32, i32* %10
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %53
46: ; preds = %38
%47 = load i32, i32* @PEM_F_PEM_DEF_CALLBACK
%48 = load i32, i32* @PEM_R_PROBLEMS_GETTING_PASSWORD
%49 = call i32 @PEMerr(i32 %47, i32 %48)
%50 = load i8*, i8** %6
%51 = load i32, i32* %7
%52 = call i32 @memset(i8* %50, i32 0, i32 %51)
store i32 -1, i32* %5
br label %65
53: ; preds = %38
%54 = load i8*, i8** %6
%55 = call i32 @strlen(i8* %54)
store i32 %55, i32* %11
%56 = load i32, i32* %11
%57 = icmp slt i32 %56, 4
br i1 %57, label %58, label %61
58: ; preds = %53
%59 = load i32, i32* @stderr
%60 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i32 4)
br label %62
61: ; preds = %53
br label %63
62: ; preds = %58
br label %38
63: ; preds = %61
%64 = load i32, i32* %11
store i32 %64, i32* %5
br label %65
65: ; preds = %63, %46, %25
%66 = load i32, i32* %5
ret i32 %66
}

LLVM IR (-O2):
@.str = [23 x i8] c"Enter PEM pass phrase:\00"
@PEM_F_PEM_DEF_CALLBACK = global i32 0
@PEM_R_PROBLEMS_GETTING_PASSWORD = global i32 0
@stderr = global i32 0
@.str.1 = [52 x i8] c"phrase is too short, needs to be at least %d chars\0A\00"
define i32 @PEM_def_callback(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = icmp eq i8* %3, null
br i1 %5, label %11, label %6
6: ; preds = %4
%7 = tail call i32 @strlen(i8* %3)
%8 = icmp sgt i32 %7, %1
%9 = select i1 %8, i32 %1, i32 %7
%10 = tail call i32 @memcpy(i8* %0, i8* %3, i32 %9)
br label %30
11: ; preds = %4
%12 = tail call i8* (...) @EVP_get_pw_prompt()
%13 = icmp eq i8* %12, null
%14 = select i1 %13, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %12
%15 = tail call i32 @EVP_read_pw_string_min(i8* %0, i32 4, i32 %1, i8* %14, i32 %2)
%16 = icmp eq i32 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %25, %11
%18 = load i32, i32* @PEM_F_PEM_DEF_CALLBACK
%19 = load i32, i32* @PEM_R_PROBLEMS_GETTING_PASSWORD
%20 = tail call i32 @PEMerr(i32 %18, i32 %19)
%21 = tail call i32 @memset(i8* %0, i32 0, i32 %1)
br label %30
22: ; preds = %11, %25
%23 = tail call i32 @strlen(i8* %0)
%24 = icmp slt i32 %23, 4
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i32 4)
%28 = tail call i32 @EVP_read_pw_string_min(i8* %0, i32 4, i32 %1, i8* %14, i32 %2)
%29 = icmp eq i32 %28, 0
br i1 %29, label %22, label %17
30: ; preds = %22, %17, %6
%31 = phi i32 [ %9, %6 ], [ -1, %17 ], [ %23, %22 ]
ret i32 %31
}