#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 *, size_t);
int /*<<< orphan*/ memcpy(char *, void *, size_t);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
size_t strlen(char *);
int PEM_def_callback(char *buf, int num, int w, void *key) {
  size_t l;
  int i;
  const char *prompt;
  if (num < 0)
    return -1;
  if (key) {
    l = strlen(key);
    if (l > (size_t)num)
      l = (size_t)num;
    memcpy(buf, key, l);
    return (int)l;
  }
  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);
    }
    l = strlen(buf);
    if (l < 4) {
      fprintf(stderr,
              "phrase is too short, "
              "needs to be at least %zu chars\n",
              (size_t)4);
    } else
      break;
  }
  return (int)l;
}


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)
cmpl   $0x0,-0x14(%rbp)
jge    119c <PEM_def_callback+0x2c>
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <PEM_def_callback+0x13f>
cmpq   $0x0,-0x20(%rbp)
je     11ea <PEM_def_callback+0x7a>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x14(%rbp),%rcx
cmp    %rcx,%rax
jbe    11cd <PEM_def_callback+0x5d>
movslq -0x14(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    12af <PEM_def_callback+0x13f>
mov    $0x0,%al
call   1080 <EVP_get_pw_prompt@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    120b <PEM_def_callback+0x9b>
lea    0xdf9(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    1210 <PEM_def_callback+0xa0>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%r8d
mov    $0x4,%esi
call   1090 <EVP_read_pw_string_min@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1267 <PEM_def_callback+0xf7>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
mov    0x2d9a(%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    12af <PEM_def_callback+0x13f>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x4,-0x28(%rbp)
jae    129e <PEM_def_callback+0x12e>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
lea    0xd88(%rip),%rsi
mov    $0x4,%edx
call   1060 <fprintf@plt>
jmp    12a3 <PEM_def_callback+0x133>
jmp    12a8 <PEM_def_callback+0x138>
jmp    1210 <PEM_def_callback+0xa0>
mov    -0x28(%rbp),%rax
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    $0xffffffff,%r14d
test   %esi,%esi
js     1267 <PEM_def_callback+0xf7>
mov    %rcx,%rbp
mov    %esi,%r13d
mov    %rdi,%rbx
test   %rcx,%rcx
je     11bf <PEM_def_callback+0x4f>
mov    %rbp,%rdi
call   1030 <strlen@plt>
mov    %rax,%r14
mov    %r13d,%eax
cmp    %rax,%r14
cmova  %rax,%r14
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r14,%rdx
call   1070 <memcpy@plt>
jmp    1267 <PEM_def_callback+0xf7>
mov    %edx,%r15d
xor    %eax,%eax
call   1080 <EVP_get_pw_prompt@plt>
test   %rax,%rax
lea    0xe2d(%rip),%r12
cmovne %rax,%r12
mov    %rbx,%rdi
mov    $0x4,%esi
mov    %r13d,%edx
mov    %r12,%rcx
mov    %r15d,%r8d
call   1090 <EVP_read_pw_string_min@plt>
test   %eax,%eax
jne    123e <PEM_def_callback+0xce>
lea    0xe1f(%rip),%rbp
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmp    $0x3,%rax
ja     1264 <PEM_def_callback+0xf4>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
mov    $0x4,%edx
mov    %rbp,%rsi
call   1060 <fprintf@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
mov    %r13d,%edx
mov    %r12,%rcx
mov    %r15d,%r8d
call   1090 <EVP_read_pw_string_min@plt>
test   %eax,%eax
je     1200 <PEM_def_callback+0x90>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
call   1040 <PEMerr@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r13d,%edx
call   1050 <memset@plt>
jmp    1267 <PEM_def_callback+0xf7>
mov    %eax,%r14d
mov    %r14d,%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 = [53 x i8] c"phrase is too short, needs to be at least %zu 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 i64
%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 i32, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %4
store i32 -1, i32* %5
br label %70
16: ; preds = %4
%17 = load i8*, i8** %9
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %36
19: ; preds = %16
%20 = load i8*, i8** %9
%21 = call i64 @strlen(i8* %20)
store i64 %21, i64* %10
%22 = load i64, i64* %10
%23 = load i32, i32* %7
%24 = sext i32 %23 to i64
%25 = icmp ugt i64 %22, %24
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = load i32, i32* %7
%28 = sext i32 %27 to i64
store i64 %28, i64* %10
br label %29
29: ; preds = %26, %19
%30 = load i8*, i8** %6
%31 = load i8*, i8** %9
%32 = load i64, i64* %10
%33 = call i32 @memcpy(i8* %30, i8* %31, i64 %32)
%34 = load i64, i64* %10
%35 = trunc i64 %34 to i32
store i32 %35, i32* %5
br label %70
36: ; preds = %16
%37 = call i8* (...) @EVP_get_pw_prompt()
store i8* %37, i8** %12
%38 = load i8*, i8** %12
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %41
40: ; preds = %36
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8** %12
br label %41
41: ; preds = %40, %36
br label %42
42: ; preds = %66, %41
%43 = load i8*, i8** %6
%44 = load i32, i32* %7
%45 = load i8*, i8** %12
%46 = load i32, i32* %8
%47 = call i32 @EVP_read_pw_string_min(i8* %43, i32 4, i32 %44, i8* %45, i32 %46)
store i32 %47, i32* %11
%48 = load i32, i32* %11
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %57
50: ; preds = %42
%51 = load i32, i32* @PEM_F_PEM_DEF_CALLBACK
%52 = load i32, i32* @PEM_R_PROBLEMS_GETTING_PASSWORD
%53 = call i32 @PEMerr(i32 %51, i32 %52)
%54 = load i8*, i8** %6
%55 = load i32, i32* %7
%56 = call i32 @memset(i8* %54, i32 0, i32 %55)
store i32 -1, i32* %5
br label %70
57: ; preds = %42
%58 = load i8*, i8** %6
%59 = call i64 @strlen(i8* %58)
store i64 %59, i64* %10
%60 = load i64, i64* %10
%61 = icmp ult i64 %60, 4
br i1 %61, label %62, label %65
62: ; preds = %57
%63 = load i32, i32* @stderr
%64 = call i32 @fprintf(i32 %63, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i64 4)
br label %66
65: ; preds = %57
br label %67
66: ; preds = %62
br label %42
67: ; preds = %65
%68 = load i64, i64* %10
%69 = trunc i64 %68 to i32
store i32 %69, i32* %5
br label %70
70: ; preds = %67, %50, %29, %15
%71 = load i32, i32* %5
ret i32 %71
}

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 = [53 x i8] c"phrase is too short, needs to be at least %zu chars\0A\00"
define i32 @PEM_def_callback(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = icmp slt i32 %1, 0
br i1 %5, label %36, label %6
6: ; preds = %4
%7 = icmp eq i8* %3, null
br i1 %7, label %15, label %8
8: ; preds = %6
%9 = tail call i64 @strlen(i8* %3)
%10 = zext i32 %1 to i64
%11 = icmp ugt i64 %9, %10
%12 = select i1 %11, i64 %10, i64 %9
%13 = tail call i32 @memcpy(i8* %0, i8* %3, i64 %12)
%14 = trunc i64 %12 to i32
br label %36
15: ; preds = %6
%16 = tail call i8* (...) @EVP_get_pw_prompt()
%17 = icmp eq i8* %16, null
%18 = select i1 %17, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %16
%19 = tail call i32 @EVP_read_pw_string_min(i8* %0, i32 4, i32 %1, i8* %18, i32 %2)
%20 = icmp eq i32 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %29, %15
%22 = load i32, i32* @PEM_F_PEM_DEF_CALLBACK
%23 = load i32, i32* @PEM_R_PROBLEMS_GETTING_PASSWORD
%24 = tail call i32 @PEMerr(i32 %22, i32 %23)
%25 = tail call i32 @memset(i8* %0, i32 0, i32 %1)
br label %36
26: ; preds = %15, %29
%27 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%28 = icmp ult i64 %27, 4
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = tail call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i64 4)
%32 = tail call i32 @EVP_read_pw_string_min(i8* %0, i32 4, i32 %1, i8* %18, i32 %2)
%33 = icmp eq i32 %32, 0
br i1 %33, label %26, label %21
34: ; preds = %26
%35 = trunc i64 %27 to i32
br label %36
36: ; preds = %4, %34, %21, %8
%37 = phi i32 [ %14, %8 ], [ -1, %21 ], [ %35, %34 ], [ -1, %4 ]
ret i32 %37
}