#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;
};

/* Variables and functions */
int /*<<< orphan*/ ECHO;
int /*<<< orphan*/ ECHONL;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, size_t);
size_t strlen(char const *);
scalar_t__ tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
size_t write(int, char const *, size_t);
int readpass(char *password, size_t len, const char *prompt) {
  int ret = 0;
  int term = open("/dev/tty", O_RDWR);
  if (term < 0) {
    return -1;
  }
  struct termios oldf, newf;
  if (tcgetattr(term, &oldf) < 0) {
    ret = -1;
    goto clean_close;
  }
  newf = oldf;
  newf.c_lflag &= ~ECHO;
  newf.c_lflag |= ECHONL;
  if (tcsetattr(term, TCSANOW, &newf) < 0) {
    ret = -1;
    goto clean_close;
  }
  size_t prompt_sz = strlen(prompt);
  if (write(term, prompt, prompt_sz) != prompt_sz) {
    ret = -1;
    goto clean_attr;
  }
  int n;
  if ((n = read(term, password, len - 1)) < 0) {
    ret = -1;
    goto clean_attr;
  }
  password[n] = '\0';
  if (n > 0 && password[n - 1] == '\n') {
    password[n - 1] = '\0';
  }
clean_attr:
  ret = tcsetattr(term, TCSANOW, &oldf);
clean_close:
  close(term);
  return ret;
}


x86 assembly (clang, -O0):
<readpass>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
lea    0xe65(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11b9 <readpass+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f7 <readpass+0x187>
mov    -0x28(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1070 <tcgetattr@plt>
cmp    $0x0,%rax
jge    11db <readpass+0x6b>
movl   $0xffffffff,-0x24(%rbp)
jmp    12e9 <readpass+0x179>
mov    -0x30(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%edi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1080 <tcsetattr@plt>
cmp    $0x0,%eax
jge    122c <readpass+0xbc>
movl   $0xffffffff,-0x24(%rbp)
jmp    12e9 <readpass+0x179>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1030 <write@plt>
cmp    -0x40(%rbp),%rax
je     125f <readpass+0xef>
movl   $0xffffffff,-0x24(%rbp)
jmp    12d1 <readpass+0x161>
mov    -0x28(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1060 <read@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
jge    128b <readpass+0x11b>
movl   $0xffffffff,-0x24(%rbp)
jmp    12d1 <readpass+0x161>
mov    -0x10(%rbp),%rax
movslq -0x44(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0x0,-0x44(%rbp)
jle    12cc <readpass+0x15c>
mov    -0x10(%rbp),%rax
mov    -0x44(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    12cc <readpass+0x15c>
mov    -0x10(%rbp),%rax
mov    -0x44(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    12d1 <readpass+0x161>
mov    -0x28(%rbp),%edi
mov    0x2d05(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1080 <tcsetattr@plt>
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readpass>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
lea    0xe69(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     125c <readpass+0xec>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1070 <tcgetattr@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     1253 <readpass+0xe3>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0x8(%rsp),%eax
mov    0x2df2(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x10(%rsp)
mov    0x2dfd(%rip),%r13
mov    0x0(%r13),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1080 <tcsetattr@plt>
test   %eax,%eax
js     1253 <readpass+0xe3>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbp
mov    %ebx,%edi
mov    %r12,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
cmp    %rbp,%rax
jne    1241 <readpass+0xd1>
add    $0xffffffffffffffff,%r15
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1060 <read@plt>
test   %eax,%eax
js     1241 <readpass+0xd1>
mov    %eax,%ecx
movb   $0x0,(%r14,%rcx,1)
je     1241 <readpass+0xd1>
add    $0xffffffff,%eax
cmpb   $0xa,(%r14,%rax,1)
jne    1241 <readpass+0xd1>
movb   $0x0,(%r14,%rax,1)
mov    0x0(%r13),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1080 <tcsetattr@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
jmp    1261 <readpass+0xf1>
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"
@O_RDWR = global i32 0
@ECHO = global i32 0
@ECHONL = global i32 0
@TCSANOW = global i32 0
define i32 @readpass(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.termios
%11 = alloca %struct.termios
%12 = alloca i64
%13 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i32 0, i32* %8
%14 = load i32, i32* @O_RDWR
%15 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %9
%16 = load i32, i32* %9
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %3
store i32 -1, i32* %4
br label %90
19: ; preds = %3
%20 = load i32, i32* %9
%21 = call i64 @tcgetattr(i32 %20, %struct.termios* %10)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %19
store i32 -1, i32* %8
br label %86
24: ; preds = %19
%25 = bitcast %struct.termios* %11 to i8*
%26 = bitcast %struct.termios* %10 to i8*
%27 = load i32, i32* @ECHO
%28 = xor i32 %27, -1
%29 = getelementptr inbounds %struct.termios, %struct.termios* %11, i32 0, i32 0
%30 = load i32, i32* %29
%31 = and i32 %30, %28
store i32 %31, i32* %29
%32 = load i32, i32* @ECHONL
%33 = getelementptr inbounds %struct.termios, %struct.termios* %11, i32 0, i32 0
%34 = load i32, i32* %33
%35 = or i32 %34, %32
store i32 %35, i32* %33
%36 = load i32, i32* %9
%37 = load i32, i32* @TCSANOW
%38 = call i32 @tcsetattr(i32 %36, i32 %37, %struct.termios* %11)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %24
store i32 -1, i32* %8
br label %86
41: ; preds = %24
%42 = load i8*, i8** %7
%43 = call i64 @strlen(i8* %42)
store i64 %43, i64* %12
%44 = load i32, i32* %9
%45 = load i8*, i8** %7
%46 = load i64, i64* %12
%47 = call i64 @write(i32 %44, i8* %45, i64 %46)
%48 = load i64, i64* %12
%49 = icmp ne i64 %47, %48
br i1 %49, label %50, label %51
50: ; preds = %41
store i32 -1, i32* %8
br label %82
51: ; preds = %41
%52 = load i32, i32* %9
%53 = load i8*, i8** %5
%54 = load i64, i64* %6
%55 = sub i64 %54, 1
%56 = call i32 @read(i32 %52, i8* %53, i64 %55)
store i32 %56, i32* %13
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %51
store i32 -1, i32* %8
br label %82
59: ; preds = %51
%60 = load i8*, i8** %5
%61 = load i32, i32* %13
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
store i8 0, i8* %63
%64 = load i32, i32* %13
%65 = icmp sgt i32 %64, 0
br i1 %65, label %66, label %81
66: ; preds = %59
%67 = load i8*, i8** %5
%68 = load i32, i32* %13
%69 = sub nsw i32 %68, 1
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %67, i64 %70
%72 = load i8, i8* %71
%73 = sext i8 %72 to i32
%74 = icmp eq i32 %73, 10
br i1 %74, label %75, label %81
75: ; preds = %66
%76 = load i8*, i8** %5
%77 = load i32, i32* %13
%78 = sub nsw i32 %77, 1
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i8, i8* %76, i64 %79
store i8 0, i8* %80
br label %81
81: ; preds = %75, %66, %59
br label %82
82: ; preds = %81, %58, %50
%83 = load i32, i32* %9
%84 = load i32, i32* @TCSANOW
%85 = call i32 @tcsetattr(i32 %83, i32 %84, %struct.termios* %10)
store i32 %85, i32* %8
br label %86
86: ; preds = %82, %40, %23
%87 = load i32, i32* %9
%88 = call i32 @close(i32 %87)
%89 = load i32, i32* %8
store i32 %89, i32* %4
br label %90
90: ; preds = %86, %18
%91 = load i32, i32* %4
ret i32 %91
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
@ECHO = global i32 0
@ECHONL = global i32 0
@TCSANOW = global i32 0
define i32 @readpass(i8* nocapture %0, i64 %1, i8* nocapture %2) {
%4 = alloca %struct.termios
%5 = alloca %struct.termios
%6 = load i32, i32* @O_RDWR
%7 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %51, label %9
9: ; preds = %3
%10 = bitcast %struct.termios* %4 to i8*
%11 = bitcast %struct.termios* %5 to i8*
%12 = call i64 @tcgetattr(i32 %7, %struct.termios* %4)
%13 = icmp slt i64 %12, 0
br i1 %13, label %48, label %14
14: ; preds = %9
%15 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%16 = getelementptr inbounds %struct.termios, %struct.termios* %5, i64 0, i32 0
%17 = load i32, i32* %15
%18 = load i32, i32* @ECHO
%19 = xor i32 %18, -1
%20 = and i32 %17, %19
%21 = load i32, i32* @ECHONL
%22 = or i32 %21, %20
store i32 %22, i32* %16
%23 = load i32, i32* @TCSANOW
%24 = call i32 @tcsetattr(i32 %7, i32 %23, %struct.termios* %5)
%25 = icmp slt i32 %24, 0
br i1 %25, label %48, label %26
26: ; preds = %14
%27 = call i64 @strlen(i8* dereferenceable(1) %2)
%28 = call i64 @write(i32 %7, i8* %2, i64 %27)
%29 = icmp eq i64 %28, %27
br i1 %29, label %30, label %45
30: ; preds = %26
%31 = add i64 %1, -1
%32 = call i32 @read(i32 %7, i8* %0, i64 %31)
%33 = icmp slt i32 %32, 0
br i1 %33, label %45, label %34
34: ; preds = %30
%35 = zext i32 %32 to i64
%36 = getelementptr inbounds i8, i8* %0, i64 %35
store i8 0, i8* %36
%37 = icmp eq i32 %32, 0
br i1 %37, label %45, label %38
38: ; preds = %34
%39 = add nsw i32 %32, -1
%40 = zext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %0, i64 %40
%42 = load i8, i8* %41
%43 = icmp eq i8 %42, 10
br i1 %43, label %44, label %45
44: ; preds = %38
store i8 0, i8* %41
br label %45
45: ; preds = %30, %26, %34, %38, %44
%46 = load i32, i32* @TCSANOW
%47 = call i32 @tcsetattr(i32 %7, i32 %46, %struct.termios* %4)
br label %48
48: ; preds = %14, %9, %45
%49 = phi i32 [ %47, %45 ], [ -1, %9 ], [ -1, %14 ]
%50 = call i32 @close(i32 %7)
br label %51
51: ; preds = %3, %48
%52 = phi i32 [ %49, %48 ], [ -1, %3 ]
ret i32 %52
}