#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_R_PROBLEMS_GETTING_PASSWORD;
int /*<<< orphan*/ PEMerror(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) {
      PEMerror(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    12a6 <PEM_def_callback+0x136>
cmpq   $0x0,-0x20(%rbp)
je     11ea <PEM_def_callback+0x7a>
mov    -0x20(%rbp),%rdi
call   1040 <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    12a6 <PEM_def_callback+0x136>
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     125e <PEM_def_callback+0xee>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   1030 <PEMerror@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a6 <PEM_def_callback+0x136>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x4,-0x28(%rbp)
jae    1295 <PEM_def_callback+0x125>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
lea    0xd91(%rip),%rsi
mov    $0x4,%edx
call   1060 <fprintf@plt>
jmp    129a <PEM_def_callback+0x12a>
jmp    129f <PEM_def_callback+0x12f>
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     125e <PEM_def_callback+0xee>
mov    %rcx,%rbp
mov    %esi,%r13d
mov    %rdi,%rbx
test   %rcx,%rcx
je     11bf <PEM_def_callback+0x4f>
mov    %rbp,%rdi
call   1040 <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    125e <PEM_def_callback+0xee>
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   1040 <strlen@plt>
cmp    $0x3,%rax
ja     125b <PEM_def_callback+0xeb>
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    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   1030 <PEMerror@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r13d,%edx
call   1050 <memset@plt>
jmp    125e <PEM_def_callback+0xee>
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_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 %69
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 %69
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 = %65, %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 %56
50: ; preds = %42
%51 = load i32, i32* @PEM_R_PROBLEMS_GETTING_PASSWORD
%52 = call i32 @PEMerror(i32 %51)
%53 = load i8*, i8** %6
%54 = load i32, i32* %7
%55 = call i32 @memset(i8* %53, i32 0, i32 %54)
store i32 -1, i32* %5
br label %69
56: ; preds = %42
%57 = load i8*, i8** %6
%58 = call i64 @strlen(i8* %57)
store i64 %58, i64* %10
%59 = load i64, i64* %10
%60 = icmp ult i64 %59, 4
br i1 %60, label %61, label %64
61: ; preds = %56
%62 = load i32, i32* @stderr
%63 = call i32 @fprintf(i32 %62, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i64 4)
br label %65
64: ; preds = %56
br label %66
65: ; preds = %61
br label %42
66: ; preds = %64
%67 = load i64, i64* %10
%68 = trunc i64 %67 to i32
store i32 %68, i32* %5
br label %69
69: ; preds = %66, %50, %29, %15
%70 = load i32, i32* %5
ret i32 %70
}

LLVM IR (-O2):
@.str = [23 x i8] c"Enter PEM pass phrase:\00"
@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 %35, 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 %35
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 %25, label %21
21: ; preds = %28, %15
%22 = load i32, i32* @PEM_R_PROBLEMS_GETTING_PASSWORD
%23 = tail call i32 @PEMerror(i32 %22)
%24 = tail call i32 @memset(i8* %0, i32 0, i32 %1)
br label %35
25: ; preds = %15, %28
%26 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%27 = icmp ult i64 %26, 4
br i1 %27, label %28, label %33
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = tail call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i64 4)
%31 = tail call i32 @EVP_read_pw_string_min(i8* %0, i32 4, i32 %1, i8* %18, i32 %2)
%32 = icmp eq i32 %31, 0
br i1 %32, label %25, label %21
33: ; preds = %25
%34 = trunc i64 %26 to i32
br label %35
35: ; preds = %4, %33, %21, %8
%36 = phi i32 [ %14, %8 ], [ -1, %21 ], [ %34, %33 ], [ -1, %4 ]
ret i32 %36
}