#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 */
struct termios {
  int /*<<< orphan*/ c_lflag;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ ECHO;
int /*<<< orphan*/ ICANON;
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ bzero(unsigned char *, size_t);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int read(int, unsigned char *, size_t);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
ssize_t read_passkey(unsigned char *key, size_t max_keylen) {
  struct termios orig, noecho;
  ssize_t ret = -1;
  FILE *ftty = NULL;
  int tty;
  ftty = fopen("/dev/tty", "r+");
  if (!ftty)
    goto out;
  tty = fileno(ftty);
  if (tcgetattr(tty, &orig))
    goto out;
  noecho = orig;
  noecho.c_lflag &= ~ECHO;
  noecho.c_lflag |= ICANON;
  fprintf(ftty, "Grey Goo password: ");
  fflush(ftty);
  if (tcsetattr(tty, TCSAFLUSH, &noecho))
    goto out;
  ret = read(tty, key, max_keylen);
  fprintf(ftty, "\n");
  if (ret > 0 && key[ret - 1] == '\n')
    key[--ret] = '\0';
  else
    ret = -1;
  if (tcsetattr(tty, TCSAFLUSH, &orig))
    ret = -1;
out:
  if (ftty && fclose(ftty))
    ret = -1;
  if (ret == -1)
    bzero(key, max_keylen);
  return ret;
}


x86 assembly (clang, -O0):
<read_passkey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
lea    0xe4a(%rip),%rdi
lea    0xe4c(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11d6 <read_passkey+0x46>
jmp    12f3 <read_passkey+0x163>
mov    -0x30(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1080 <tcgetattr@plt>
cmp    $0x0,%rax
je     11fd <read_passkey+0x6d>
jmp    12f3 <read_passkey+0x163>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2db4(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xddd(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1070 <fflush@plt>
mov    -0x34(%rbp),%edi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1090 <tcsetattr@plt>
cmp    $0x0,%rax
je     1261 <read_passkey+0xd1>
jmp    12f3 <read_passkey+0x163>
mov    -0x34(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xda1(%rip),%rsi
call   1050 <fprintf@plt>
cmpl   $0x0,-0x24(%rbp)
jle    12c1 <read_passkey+0x131>
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    12c1 <read_passkey+0x131>
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x24(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    12c8 <read_passkey+0x138>
movl   $0xffffffff,-0x24(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2d16(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1090 <tcsetattr@plt>
cmp    $0x0,%rax
je     12ee <read_passkey+0x15e>
movl   $0xffffffff,-0x24(%rbp)
jmp    12f3 <read_passkey+0x163>
cmpq   $0x0,-0x30(%rbp)
je     1318 <read_passkey+0x188>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     1318 <read_passkey+0x188>
movl   $0xffffffff,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    132f <read_passkey+0x19f>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10b0 <bzero@plt>
mov    -0x24(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_passkey>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe45(%rip),%rdi
lea    0xe47(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12b7 <read_passkey+0x117>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <fileno@plt>
mov    %eax,%r12d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1080 <tcgetattr@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    12a5 <read_passkey+0x105>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0x8(%rsp),%eax
mov    0x2dc2(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x10(%rsp)
lea    0xdf1(%rip),%rdi
mov    $0x13,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10c0 <fwrite@plt>
mov    %rbx,%rdi
call   1070 <fflush@plt>
mov    0x2dac(%rip),%r13
mov    0x0(%r13),%esi
lea    0x10(%rsp),%rdx
mov    %r12d,%edi
call   1090 <tcsetattr@plt>
test   %rax,%rax
jne    12a5 <read_passkey+0x105>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <read@plt>
mov    %eax,%ebp
mov    $0xa,%edi
mov    %rbx,%rsi
call   1040 <fputc@plt>
mov    $0xffffffff,%eax
test   %ebp,%ebp
jle    1289 <read_passkey+0xe9>
add    $0xffffffff,%ebp
cmpb   $0xa,(%r15,%rbp,1)
jne    1289 <read_passkey+0xe9>
movb   $0x0,(%r15,%rbp,1)
mov    %ebp,%eax
mov    0x0(%r13),%esi
lea    0x8(%rsp),%rdx
mov    %r12d,%edi
mov    %eax,%r12d
call   1090 <tcsetattr@plt>
neg    %rax
sbb    %ebp,%ebp
or     %r12d,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
jne    12b7 <read_passkey+0x117>
cmp    $0xffffffff,%ebp
jne    12c7 <read_passkey+0x127>
mov    %r15,%rdi
mov    %r14,%rsi
call   10b0 <bzero@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [9 x i8] c"/dev/tty\00"
@.str.1 = [3 x i8] c"r+\00"
@ECHO = global i32 0
@ICANON = global i32 0
@.str.2 = [20 x i8] c"Grey Goo password: \00"
@TCSAFLUSH = global i32 0
@.str.3 = [2 x i8] c"\0A\00"
define i32 @read_passkey(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca %struct.termios
%6 = alloca %struct.termios
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
store i32 -1, i32* %7
store i32* null, i32** %8
%10 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %8
%11 = load i32*, i32** %8
%12 = icmp ne i32* %11, null
br i1 %12, label %14, label %13
13: ; preds = %2
br label %74
14: ; preds = %2
%15 = load i32*, i32** %8
%16 = call i32 @fileno(i32* %15)
store i32 %16, i32* %9
%17 = load i32, i32* %9
%18 = call i64 @tcgetattr(i32 %17, %struct.termios* %5)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %14
br label %74
21: ; preds = %14
%22 = bitcast %struct.termios* %6 to i8*
%23 = bitcast %struct.termios* %5 to i8*
%24 = load i32, i32* @ECHO
%25 = xor i32 %24, -1
%26 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%27 = load i32, i32* %26
%28 = and i32 %27, %25
store i32 %28, i32* %26
%29 = load i32, i32* @ICANON
%30 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%31 = load i32, i32* %30
%32 = or i32 %31, %29
store i32 %32, i32* %30
%33 = load i32*, i32** %8
%34 = call i32 @fprintf(i32* %33, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32*, i32** %8
%36 = call i32 @fflush(i32* %35)
%37 = load i32, i32* %9
%38 = load i32, i32* @TCSAFLUSH
%39 = call i64 @tcsetattr(i32 %37, i32 %38, %struct.termios* %6)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %21
br label %74
42: ; preds = %21
%43 = load i32, i32* %9
%44 = load i8*, i8** %3
%45 = load i64, i64* %4
%46 = call i32 @read(i32 %43, i8* %44, i64 %45)
store i32 %46, i32* %7
%47 = load i32*, i32** %8
%48 = call i32 @fprintf(i32* %47, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%49 = load i32, i32* %7
%50 = icmp sgt i32 %49, 0
br i1 %50, label %51, label %66
51: ; preds = %42
%52 = load i8*, i8** %3
%53 = load i32, i32* %7
%54 = sub nsw i32 %53, 1
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %52, i64 %55
%57 = load i8, i8* %56
%58 = zext i8 %57 to i32
%59 = icmp eq i32 %58, 10
br i1 %59, label %60, label %66
60: ; preds = %51
%61 = load i8*, i8** %3
%62 = load i32, i32* %7
%63 = add nsw i32 %62, -1
store i32 %63, i32* %7
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %61, i64 %64
store i8 0, i8* %65
br label %67
66: ; preds = %51, %42
store i32 -1, i32* %7
br label %67
67: ; preds = %66, %60
%68 = load i32, i32* %9
%69 = load i32, i32* @TCSAFLUSH
%70 = call i64 @tcsetattr(i32 %68, i32 %69, %struct.termios* %5)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %73
72: ; preds = %67
store i32 -1, i32* %7
br label %73
73: ; preds = %72, %67
br label %74
74: ; preds = %73, %41, %20, %13
%75 = load i32*, i32** %8
%76 = icmp ne i32* %75, null
br i1 %76, label %77, label %82
77: ; preds = %74
%78 = load i32*, i32** %8
%79 = call i64 @fclose(i32* %78)
%80 = icmp ne i64 %79, 0
br i1 %80, label %81, label %82
81: ; preds = %77
store i32 -1, i32* %7
br label %82
82: ; preds = %81, %77, %74
%83 = load i32, i32* %7
%84 = icmp eq i32 %83, -1
br i1 %84, label %85, label %89
85: ; preds = %82
%86 = load i8*, i8** %3
%87 = load i64, i64* %4
%88 = call i32 @bzero(i8* %86, i64 %87)
br label %89
89: ; preds = %85, %82
%90 = load i32, i32* %7
ret i32 %90
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [9 x i8] c"/dev/tty\00"
@.str.1 = [3 x i8] c"r+\00"
@ECHO = global i32 0
@ICANON = global i32 0
@.str.2 = [20 x i8] c"Grey Goo password: \00"
@TCSAFLUSH = global i32 0
define i32 @read_passkey(i8* nocapture %0, i64 %1) {
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = bitcast %struct.termios* %3 to i8*
%6 = bitcast %struct.termios* %4 to i8*
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %50, label %9
9: ; preds = %2
%10 = tail call i32 @fileno(i32* %7)
%11 = call i64 @tcgetattr(i32 %10, %struct.termios* %3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %44
13: ; preds = %9
%14 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%15 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%16 = load i32, i32* %14
%17 = load i32, i32* @ECHO
%18 = xor i32 %17, -1
%19 = and i32 %16, %18
%20 = load i32, i32* @ICANON
%21 = or i32 %20, %19
store i32 %21, i32* %15
%22 = call i64 @fwrite(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i64 19, i64 1, i32* %7)
%23 = call i32 @fflush(i32* %7)
%24 = load i32, i32* @TCSAFLUSH
%25 = call i64 @tcsetattr(i32 %10, i32 %24, %struct.termios* %4)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %44
27: ; preds = %13
%28 = call i32 @read(i32 %10, i8* %0, i64 %1)
%29 = call i32 @fputc(i32 10, i32* %7)
%30 = icmp sgt i32 %28, 0
br i1 %30, label %31, label %38
31: ; preds = %27
%32 = add nsw i32 %28, -1
%33 = zext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %0, i64 %33
%35 = load i8, i8* %34
%36 = icmp eq i8 %35, 10
br i1 %36, label %37, label %38
37: ; preds = %31
store i8 0, i8* %34
br label %38
38: ; preds = %27, %31, %37
%39 = phi i32 [ %32, %37 ], [ -1, %31 ], [ -1, %27 ]
%40 = load i32, i32* @TCSAFLUSH
%41 = call i64 @tcsetattr(i32 %10, i32 %40, %struct.termios* %3)
%42 = icmp eq i64 %41, 0
%43 = select i1 %42, i32 %39, i32 -1
br label %44
44: ; preds = %38, %13, %9
%45 = phi i32 [ %43, %38 ], [ -1, %13 ], [ -1, %9 ]
%46 = call i64 @fclose(i32* %7)
%47 = icmp ne i64 %46, 0
%48 = icmp eq i32 %45, -1
%49 = select i1 %47, i1 true, i1 %48
br i1 %49, label %50, label %52
50: ; preds = %2, %44
%51 = call i32 @bzero(i8* %0, i64 %1)
br label %52
52: ; preds = %44, %50
%53 = phi i32 [ -1, %50 ], [ %45, %44 ]
ret i32 %53
}