#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 */
int /*<<< orphan*/ asprintf(char **, char *, ...);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *readpassphrase(char *, char *, int, int);
int strlen(char *);
int _potato_cb(char *buf, int sz, int rwflag, void *u) {
  int len;
  char *prompt = NULL;
  if (u)
    asprintf(&prompt, "Enter passphrase for %s:", (char *)u);
  else
    asprintf(&prompt, "Enter passphrase :");
  char *secret = malloc(128);
  memset(secret, '\0', 128);
  if (!secret) {
    perror("malloc()");
    free(prompt);
    return 0;
  }
  if (readpassphrase(prompt, secret, 128, 0x00) == NULL) {
    perror("readpassphrase()");
    memset(secret, '\0', 128);
    len = 0;
  } else {
    len = strlen(secret);
    memcpy(buf, secret, len);
    memset(secret, '\0', len);
  }
  free(secret);
  free(prompt);
  return len;
}


x86 assembly (clang, -O0):
<_potato_cb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11c4 <_potato_cb+0x44>
mov    -0x20(%rbp),%rdx
lea    -0x30(%rbp),%rdi
lea    0xe48(%rip),%rsi
mov    $0x0,%al
call   1090 <asprintf@plt>
jmp    11d6 <_potato_cb+0x56>
lea    -0x30(%rbp),%rdi
lea    0xe4a(%rip),%rsi
mov    $0x0,%al
call   1090 <asprintf@plt>
mov    $0x80,%edi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
cmpq   $0x0,-0x38(%rbp)
jne    1220 <_potato_cb+0xa0>
lea    0xe26(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a8 <_potato_cb+0x128>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    $0x80,%edx
xor    %ecx,%ecx
call   1040 <readpassphrase@plt>
cmp    $0x0,%rax
jne    1266 <_potato_cb+0xe6>
lea    0xdf0(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
movl   $0x0,-0x24(%rbp)
jmp    1290 <_potato_cb+0x110>
mov    -0x38(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x38(%rbp),%rdi
mov    -0x24(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_potato_cb>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
movq   $0x0,(%rsp)
test   %rcx,%rcx
je     11ad <_potato_cb+0x2d>
lea    0xe62(%rip),%rsi
mov    %rsp,%rdi
mov    %rcx,%rdx
xor    %eax,%eax
call   1090 <asprintf@plt>
jmp    11be <_potato_cb+0x3e>
lea    0xe65(%rip),%rsi
mov    %rsp,%rdi
xor    %eax,%eax
call   1090 <asprintf@plt>
mov    $0x80,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
xor    %r15d,%r15d
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
test   %rbx,%rbx
je     1247 <_potato_cb+0xc7>
mov    (%rsp),%rdi
mov    $0x80,%ebp
xor    %r15d,%r15d
mov    %rbx,%rsi
mov    $0x80,%edx
xor    %ecx,%ecx
call   1040 <readpassphrase@plt>
test   %rax,%rax
je     1255 <_potato_cb+0xd5>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    %ebp,%r15d
mov    %rbx,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdde(%rip),%rdi
call   10a0 <perror@plt>
jmp    1230 <_potato_cb+0xb0>
lea    0xdd9(%rip),%rdi
call   10a0 <perror@plt>
jmp    121c <_potato_cb+0x9c>

LLVM IR (-O0):
@.str = [25 x i8] c"Enter passphrase for %s:\00"
@.str.1 = [19 x i8] c"Enter passphrase :\00"
@.str.2 = [9 x i8] c"malloc()\00"
@.str.3 = [17 x i8] c"readpassphrase()\00"
define i32 @_potato_cb(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 i8*
%12 = alloca i8*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i8* null, i8** %11
%13 = load i8*, i8** %9
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %4
%16 = load i8*, i8** %9
%17 = call i32 (i8**, i8*, ...) @asprintf(i8** %11, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %20
18: ; preds = %4
%19 = call i32 (i8**, i8*, ...) @asprintf(i8** %11, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %15
%21 = call i8* @malloc(i32 128)
store i8* %21, i8** %12
%22 = load i8*, i8** %12
%23 = call i32 @memset(i8* %22, i8 signext 0, i32 128)
%24 = load i8*, i8** %12
%25 = icmp ne i8* %24, null
br i1 %25, label %30, label %26
26: ; preds = %20
%27 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%28 = load i8*, i8** %11
%29 = call i32 @free(i8* %28)
store i32 0, i32* %5
br label %55
30: ; preds = %20
%31 = load i8*, i8** %11
%32 = load i8*, i8** %12
%33 = call i32* @readpassphrase(i8* %31, i8* %32, i32 128, i32 0)
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %39
35: ; preds = %30
%36 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%37 = load i8*, i8** %12
%38 = call i32 @memset(i8* %37, i8 signext 0, i32 128)
store i32 0, i32* %10
br label %49
39: ; preds = %30
%40 = load i8*, i8** %12
%41 = call i32 @strlen(i8* %40)
store i32 %41, i32* %10
%42 = load i8*, i8** %6
%43 = load i8*, i8** %12
%44 = load i32, i32* %10
%45 = call i32 @memcpy(i8* %42, i8* %43, i32 %44)
%46 = load i8*, i8** %12
%47 = load i32, i32* %10
%48 = call i32 @memset(i8* %46, i8 signext 0, i32 %47)
br label %49
49: ; preds = %39, %35
%50 = load i8*, i8** %12
%51 = call i32 @free(i8* %50)
%52 = load i8*, i8** %11
%53 = call i32 @free(i8* %52)
%54 = load i32, i32* %10
store i32 %54, i32* %5
br label %55
55: ; preds = %49, %26
%56 = load i32, i32* %5
ret i32 %56
}

LLVM IR (-O2):
@.str = [25 x i8] c"Enter passphrase for %s:\00"
@.str.1 = [19 x i8] c"Enter passphrase :\00"
@.str.2 = [9 x i8] c"malloc()\00"
@.str.3 = [17 x i8] c"readpassphrase()\00"
define i32 @_potato_cb(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i8*
%6 = bitcast i8** %5 to i8*
store i8* null, i8** %5
%7 = icmp eq i8* %3, null
br i1 %7, label %10, label %8
8: ; preds = %4
%9 = call i32 (i8**, i8*, ...) @asprintf(i8** %5, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %3)
br label %12
10: ; preds = %4
%11 = call i32 (i8**, i8*, ...) @asprintf(i8** %5, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %8
%13 = call dereferenceable_or_null(128) i8* @malloc(i32 128)
%14 = call i32 @memset(i8* %13, i8 signext 0, i32 128)
%15 = icmp eq i8* %13, null
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %32
18: ; preds = %12
%19 = load i8*, i8** %5
%20 = call i32* @readpassphrase(i8* %19, i8* %13, i32 128, i32 0)
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %27
24: ; preds = %18
%25 = call i32 @strlen(i8* %13)
%26 = call i32 @memcpy(i8* %0, i8* %13, i32 %25)
br label %27
27: ; preds = %24, %22
%28 = phi i32 [ %25, %24 ], [ 128, %22 ]
%29 = phi i32 [ %25, %24 ], [ 0, %22 ]
%30 = call i32 @memset(i8* %13, i8 signext 0, i32 %28)
%31 = call i32 @free(i8* %13)
br label %32
32: ; preds = %27, %16
%33 = phi i32 [ %29, %27 ], [ 0, %16 ]
%34 = load i8*, i8** %5
%35 = call i32 @free(i8* %34)
ret i32 %33
}