#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*/ EXIT_FAILURE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
char *strerror(int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
char *inputpassphrase(int fd) {
  char buffer[1];
  char *return_pp = NULL;
  char *tmp_pp = NULL;
  int n;
  while ((n = read(fd, buffer, 1)) > 0) {
    if (buffer[0] == '\n' || buffer[0] == '\r')
      return return_pp;
    if (return_pp) {
      if ((tmp_pp = (char *)malloc((strlen(return_pp) + 1) * sizeof(char))) ==
          NULL) {
        fprintf(stderr, "Unable to allocate memory: %s\n", strerror(errno));
        exit(EXIT_FAILURE);
      }
      strcpy(tmp_pp, return_pp);
      free(return_pp);
    }
    if (tmp_pp) {
      if ((return_pp = (char *)malloc((strlen(tmp_pp) + 2) * sizeof(char))) ==
          NULL) {
        fprintf(stderr, "Unable to allocate memory: %s\n", strerror(errno));
        exit(EXIT_FAILURE);
      }
      strcpy(return_pp, tmp_pp);
      strncat(return_pp, buffer, 1);
    } else {
      if ((return_pp = (char *)malloc(2 * sizeof(char))) == NULL) {
        fprintf(stderr, "Unable to allocate memory: %s\n", strerror(errno));
        exit(EXIT_FAILURE);
      }
      return_pp[0] = buffer[0];
      return_pp[1] = '\0';
    }
  }
  if (tmp_pp)
    free(tmp_pp);
  return return_pp;
}


x86 assembly (clang, -O0):
<inputpassphrase>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    1369 <inputpassphrase+0x1d9>
movsbl -0xd(%rbp),%eax
cmp    $0xa,%eax
je     11e2 <inputpassphrase+0x52>
movsbl -0xd(%rbp),%eax
cmp    $0xd,%eax
jne    11ef <inputpassphrase+0x5f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1385 <inputpassphrase+0x1f5>
cmpq   $0x0,-0x18(%rbp)
je     1271 <inputpassphrase+0xe1>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    125b <inputpassphrase+0xcb>
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xdb8(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x20(%rbp)
je     1301 <inputpassphrase+0x171>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x2,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    12dd <inputpassphrase+0x14d>
mov    0x2d36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xd36(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1060 <strncat@plt>
jmp    1364 <inputpassphrase+0x1d4>
mov    $0x2,%edi
call   1090 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1353 <inputpassphrase+0x1c3>
mov    0x2cc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2cac(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xcc0(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2c84(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0xd(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
movb   $0x0,0x1(%rax)
jmp    11ab <inputpassphrase+0x1b>
cmpq   $0x0,-0x20(%rbp)
je     137d <inputpassphrase+0x1ed>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inputpassphrase>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r15d
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    1271 <inputpassphrase+0xe1>
xor    %ebx,%ebx
lea    0x7(%rsp),%r14
xor    %ebp,%ebp
jmp    120b <inputpassphrase+0x7b>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x2(%rax),%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1290 <inputpassphrase+0x100>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
call   1060 <strncat@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    1275 <inputpassphrase+0xe5>
movzbl 0x7(%rsp),%eax
cmp    $0xa,%al
je     1282 <inputpassphrase+0xf2>
cmp    $0xd,%al
je     1282 <inputpassphrase+0xf2>
test   %rbp,%rbp
je     1248 <inputpassphrase+0xb8>
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1290 <inputpassphrase+0x100>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
call   1040 <strcpy@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
test   %rbx,%rbx
jne    11c0 <inputpassphrase+0x30>
mov    $0x2,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1290 <inputpassphrase+0x100>
mov    %rax,%rbp
movzbl 0x7(%rsp),%eax
mov    %al,0x0(%rbp)
movb   $0x0,0x1(%rbp)
jmp    11f7 <inputpassphrase+0x67>
xor    %ebp,%ebp
jmp    1282 <inputpassphrase+0xf2>
test   %rbx,%rbx
je     1282 <inputpassphrase+0xf2>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d49(%rip),%rax
mov    (%rax),%ebx
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd52(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1080 <fprintf@plt>
mov    0x2d11(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [31 x i8] c"Unable to allocate memory: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define i8* @inputpassphrase(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [1 x i8]
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %3
store i8* null, i8** %5
store i8* null, i8** %6
br label %8
8: ; preds = %96, %1
%9 = load i32, i32* %3
%10 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
%11 = call i32 @read(i32 %9, i8* %10, i32 1)
store i32 %11, i32* %7
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %97
13: ; preds = %8
%14 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 10
br i1 %17, label %23, label %18
18: ; preds = %13
%19 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 13
br i1 %22, label %23, label %25
23: ; preds = %18, %13
%24 = load i8*, i8** %5
store i8* %24, i8** %2
br label %105
25: ; preds = %18
%26 = load i8*, i8** %5
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %51
28: ; preds = %25
%29 = load i8*, i8** %5
%30 = call i32 @strlen(i8* %29)
%31 = add nsw i32 %30, 1
%32 = sext i32 %31 to i64
%33 = mul i64 %32, 1
%34 = trunc i64 %33 to i32
%35 = call i64 @malloc(i32 %34)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %6
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %45
38: ; preds = %28
%39 = load i32, i32* @stderr
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %41)
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %28
%46 = load i8*, i8** %6
%47 = load i8*, i8** %5
%48 = call i32 @strcpy(i8* %46, i8* %47)
%49 = load i8*, i8** %5
%50 = call i32 @free(i8* %49)
br label %51
51: ; preds = %45, %25
%52 = load i8*, i8** %6
%53 = icmp ne i8* %52, null
br i1 %53, label %54, label %78
54: ; preds = %51
%55 = load i8*, i8** %6
%56 = call i32 @strlen(i8* %55)
%57 = add nsw i32 %56, 2
%58 = sext i32 %57 to i64
%59 = mul i64 %58, 1
%60 = trunc i64 %59 to i32
%61 = call i64 @malloc(i32 %60)
%62 = inttoptr i64 %61 to i8*
store i8* %62, i8** %5
%63 = icmp eq i8* %62, null
br i1 %63, label %64, label %71
64: ; preds = %54
%65 = load i32, i32* @stderr
%66 = load i32, i32* @errno
%67 = call i8* @strerror(i32 %66)
%68 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %67)
%69 = load i32, i32* @EXIT_FAILURE
%70 = call i32 @exit(i32 %69)
unreachable
71: ; preds = %54
%72 = load i8*, i8** %5
%73 = load i8*, i8** %6
%74 = call i32 @strcpy(i8* %72, i8* %73)
%75 = load i8*, i8** %5
%76 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
%77 = call i32 @strncat(i8* %75, i8* %76, i32 1)
br label %96
78: ; preds = %51
%79 = call i64 @malloc(i32 2)
%80 = inttoptr i64 %79 to i8*
store i8* %80, i8** %5
%81 = icmp eq i8* %80, null
br i1 %81, label %82, label %89
82: ; preds = %78
%83 = load i32, i32* @stderr
%84 = load i32, i32* @errno
%85 = call i8* @strerror(i32 %84)
%86 = call i32 @fprintf(i32 %83, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %85)
%87 = load i32, i32* @EXIT_FAILURE
%88 = call i32 @exit(i32 %87)
unreachable
89: ; preds = %78
%90 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
%91 = load i8, i8* %90
%92 = load i8*, i8** %5
%93 = getelementptr inbounds i8, i8* %92, i64 0
store i8 %91, i8* %93
%94 = load i8*, i8** %5
%95 = getelementptr inbounds i8, i8* %94, i64 1
store i8 0, i8* %95
br label %96
96: ; preds = %89, %71
br label %8
97: ; preds = %8
%98 = load i8*, i8** %6
%99 = icmp ne i8* %98, null
br i1 %99, label %100, label %103
100: ; preds = %97
%101 = load i8*, i8** %6
%102 = call i32 @free(i8* %101)
br label %103
103: ; preds = %100, %97
%104 = load i8*, i8** %5
store i8* %104, i8** %2
br label %105
105: ; preds = %103, %23
%106 = load i8*, i8** %2
ret i8* %106
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [31 x i8] c"Unable to allocate memory: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define i8* @inputpassphrase(i32 %0) {
%2 = alloca [1 x i8]
%3 = getelementptr inbounds [1 x i8], [1 x i8]* %2, i64 0, i64 0
%4 = call i32 @read(i32 %0, i8* %3, i32 1)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %69
6: ; preds = %1, %61
%7 = phi i8* [ %29, %61 ], [ null, %1 ]
%8 = phi i8* [ %62, %61 ], [ null, %1 ]
%9 = load i8, i8* %3
switch i8 %9, label %10 [
i8 13, label %69
i8 10, label %69
]
10: ; preds = %6
%11 = icmp eq i8* %8, null
br i1 %11, label %28, label %12
12: ; preds = %10
%13 = call i32 @strlen(i8* %8)
%14 = add nsw i32 %13, 1
%15 = call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
%17 = icmp eq i64 %15, 0
br i1 %17, label %18, label %25
18: ; preds = %12
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %12
%26 = call i32 @strcpy(i8* %16, i8* %8)
%27 = call i32 @free(i8* %8)
br label %28
28: ; preds = %25, %10
%29 = phi i8* [ %16, %25 ], [ %7, %10 ]
%30 = icmp eq i8* %29, null
br i1 %30, label %47, label %31
31: ; preds = %28
%32 = call i32 @strlen(i8* %29)
%33 = add nsw i32 %32, 2
%34 = call i64 @malloc(i32 %33)
%35 = inttoptr i64 %34 to i8*
%36 = icmp eq i64 %34, 0
br i1 %36, label %37, label %44
37: ; preds = %31
%38 = load i32, i32* @stderr
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %40)
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %31
%45 = call i32 @strcpy(i8* %35, i8* %29)
%46 = call i32 @strncat(i8* %35, i8* %3, i32 1)
br label %61
47: ; preds = %28
%48 = call i64 @malloc(i32 2)
%49 = inttoptr i64 %48 to i8*
%50 = icmp eq i64 %48, 0
br i1 %50, label %51, label %58
51: ; preds = %47
%52 = load i32, i32* @stderr
%53 = load i32, i32* @errno
%54 = call i8* @strerror(i32 %53)
%55 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %54)
%56 = load i32, i32* @EXIT_FAILURE
%57 = call i32 @exit(i32 %56)
unreachable
58: ; preds = %47
%59 = load i8, i8* %3
store i8 %59, i8* %49
%60 = getelementptr inbounds i8, i8* %49, i64 1
store i8 0, i8* %60
br label %61
61: ; preds = %58, %44
%62 = phi i8* [ %35, %44 ], [ %49, %58 ]
%63 = call i32 @read(i32 %0, i8* %3, i32 1)
%64 = icmp sgt i32 %63, 0
br i1 %64, label %6, label %65
65: ; preds = %61
%66 = icmp eq i8* %29, null
br i1 %66, label %69, label %67
67: ; preds = %65
%68 = call i32 @free(i8* %29)
br label %69
69: ; preds = %6, %6, %1, %65, %67
%70 = phi i8* [ %62, %65 ], [ %62, %67 ], [ null, %1 ], [ %8, %6 ], [ %8, %6 ]
ret i8* %70
}