#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 c_lflag;
  int c_iflag;
};
typedef int ssize_t;

/* Variables and functions */
int ECHO;
int ICANON;
int ICRNL;
int IGNCR;
int INLCR;
int ISIG;
int O_NOCTTY;
int O_RDONLY;
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char const *, int /*<<< orphan*/);
int open(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ tcdrain(int);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
char *getpass(const char *prompt) {
  int fd;
  struct termios s, t;
  ssize_t l;
  static char password[128];
  if ((fd = open("/dev/tty", O_RDONLY | O_NOCTTY)) < 0)
    fd = 0;
  tcgetattr(fd, &t);
  s = t;
  t.c_lflag &= ~(ECHO | ISIG);
  t.c_lflag |= ICANON;
  t.c_iflag &= ~(INLCR | IGNCR);
  t.c_iflag |= ICRNL;
  tcsetattr(fd, TCSAFLUSH, &t);
  tcdrain(fd);
  fputs(prompt, stderr);
  fflush(stderr);
  l = read(fd, password, sizeof password);
  if (l >= 0) {
    if (l > 0 && password[l - 1] == '\n')
      l--;
    password[l] = 0;
  }
  tcsetattr(fd, TCSAFLUSH, &s);
  if (fd > 2)
    close(fd);
  return password;
}


x86 assembly (clang, -O0):
<getpass>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
mov    0x2e0c(%rip),%rax
or     (%rax),%esi
lea    0xe5b(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11bd <getpass+0x3d>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1080 <tcgetattr@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    0x2dd7(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    0x2dc5(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1090 <tcsetattr@plt>
mov    -0xc(%rbp),%edi
call   1030 <tcdrain@plt>
mov    -0x8(%rbp),%rdi
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
call   1040 <fputs@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0xc(%rbp),%edi
lea    0x2e04(%rip),%rsi
mov    $0x80,%edx
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     12c2 <getpass+0x142>
cmpl   $0x0,-0x24(%rbp)
jle    12b3 <getpass+0x133>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
movslq %eax,%rcx
lea    0x2dd3(%rip),%rax
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    12b3 <getpass+0x133>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rcx
lea    0x2db2(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0xc(%rbp),%edi
mov    0x2d0c(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1090 <tcsetattr@plt>
cmpl   $0x2,-0xc(%rbp)
jle    12e9 <getpass+0x169>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
lea    0x2d80(%rip),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getpass>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e33(%rip),%rax
mov    0x2e0c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe59(%rip),%rdi
call   10a0 <open@plt>
xor    %ebx,%ebx
test   %eax,%eax
cmovns %eax,%ebx
lea    0x8(%rsp),%r15
mov    %ebx,%edi
mov    %r15,%rsi
call   1080 <tcgetattr@plt>
mov    0x8(%rsp),%rax
mov    0x2df2(%rip),%rcx
mov    0x2de3(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
not    %edx
and    %eax,%edx
mov    0x2db4(%rip),%rcx
or     (%rcx),%edx
mov    0x2df3(%rip),%rcx
mov    0x2ddc(%rip),%rsi
mov    (%rsi),%esi
or     (%rcx),%esi
mov    %rax,0x10(%rsp)
not    %esi
shr    $0x20,%rax
and    %esi,%eax
mov    0x2ddc(%rip),%rcx
or     (%rcx),%eax
mov    %edx,0x8(%rsp)
mov    %eax,0xc(%rsp)
mov    0x2dbb(%rip),%r12
mov    (%r12),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1090 <tcsetattr@plt>
mov    %ebx,%edi
call   1030 <tcdrain@plt>
mov    0x2d67(%rip),%r15
mov    (%r15),%esi
mov    %r14,%rdi
call   1040 <fputs@plt>
mov    (%r15),%edi
call   1070 <fflush@plt>
lea    0x2e1d(%rip),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x80,%edx
call   1060 <read@plt>
test   %eax,%eax
js     127c <getpass+0xfc>
je     1275 <getpass+0xf5>
lea    -0x1(%rax),%ecx
cmpb   $0xa,(%rcx,%r14,1)
cmove  %ecx,%eax
jmp    1277 <getpass+0xf7>
xor    %eax,%eax
movb   $0x0,(%rax,%r14,1)
mov    (%r12),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1090 <tcsetattr@plt>
cmp    $0x3,%ebx
jb     1298 <getpass+0x118>
mov    %ebx,%edi
call   1050 <close@plt>
lea    0x2dd1(%rip),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32 }
@getpass.password = internal global [128 x i8] zeroinitializer
@.str = [9 x i8] c"/dev/tty\00"
@O_RDONLY = global i32 0
@O_NOCTTY = global i32 0
@ECHO = global i32 0
@ISIG = global i32 0
@ICANON = global i32 0
@INLCR = global i32 0
@IGNCR = global i32 0
@ICRNL = global i32 0
@TCSAFLUSH = global i32 0
@stderr = global i32 0
define i8* @getpass(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.termios
%5 = alloca %struct.termios
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i32, i32* @O_RDONLY
%8 = load i32, i32* @O_NOCTTY
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 0, i32* %3
br label %13
13: ; preds = %12, %1
%14 = load i32, i32* %3
%15 = call i32 @tcgetattr(i32 %14, %struct.termios* %5)
%16 = bitcast %struct.termios* %4 to i8*
%17 = bitcast %struct.termios* %5 to i8*
%18 = load i32, i32* @ECHO
%19 = load i32, i32* @ISIG
%20 = or i32 %18, %19
%21 = xor i32 %20, -1
%22 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%23 = load i32, i32* %22
%24 = and i32 %23, %21
store i32 %24, i32* %22
%25 = load i32, i32* @ICANON
%26 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%27 = load i32, i32* %26
%28 = or i32 %27, %25
store i32 %28, i32* %26
%29 = load i32, i32* @INLCR
%30 = load i32, i32* @IGNCR
%31 = or i32 %29, %30
%32 = xor i32 %31, -1
%33 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%34 = load i32, i32* %33
%35 = and i32 %34, %32
store i32 %35, i32* %33
%36 = load i32, i32* @ICRNL
%37 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%38 = load i32, i32* %37
%39 = or i32 %38, %36
store i32 %39, i32* %37
%40 = load i32, i32* %3
%41 = load i32, i32* @TCSAFLUSH
%42 = call i32 @tcsetattr(i32 %40, i32 %41, %struct.termios* %5)
%43 = load i32, i32* %3
%44 = call i32 @tcdrain(i32 %43)
%45 = load i8*, i8** %2
%46 = load i32, i32* @stderr
%47 = call i32 @fputs(i8* %45, i32 %46)
%48 = load i32, i32* @stderr
%49 = call i32 @fflush(i32 %48)
%50 = load i32, i32* %3
%51 = call i32 @read(i32 %50, i8* getelementptr inbounds ([128 x i8], [128 x i8]* @getpass.password, i64 0, i64 0), i32 128)
store i32 %51, i32* %6
%52 = load i32, i32* %6
%53 = icmp sge i32 %52, 0
br i1 %53, label %54, label %72
54: ; preds = %13
%55 = load i32, i32* %6
%56 = icmp sgt i32 %55, 0
br i1 %56, label %57, label %68
57: ; preds = %54
%58 = load i32, i32* %6
%59 = sub nsw i32 %58, 1
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds [128 x i8], [128 x i8]* @getpass.password, i64 0, i64 %60
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = icmp eq i32 %63, 10
br i1 %64, label %65, label %68
65: ; preds = %57
%66 = load i32, i32* %6
%67 = add nsw i32 %66, -1
store i32 %67, i32* %6
br label %68
68: ; preds = %65, %57, %54
%69 = load i32, i32* %6
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds [128 x i8], [128 x i8]* @getpass.password, i64 0, i64 %70
store i8 0, i8* %71
br label %72
72: ; preds = %68, %13
%73 = load i32, i32* %3
%74 = load i32, i32* @TCSAFLUSH
%75 = call i32 @tcsetattr(i32 %73, i32 %74, %struct.termios* %4)
%76 = load i32, i32* %3
%77 = icmp sgt i32 %76, 2
br i1 %77, label %78, label %81
78: ; preds = %72
%79 = load i32, i32* %3
%80 = call i32 @close(i32 %79)
br label %81
81: ; preds = %78, %72
ret i8* getelementptr inbounds ([128 x i8], [128 x i8]* @getpass.password, i64 0, i64 0)
}

LLVM IR (-O2):
%struct.termios = type { i32, i32 }
@getpass.password = internal global [128 x i8] zeroinitializer
@.str = [9 x i8] c"/dev/tty\00"
@O_RDONLY = global i32 0
@O_NOCTTY = global i32 0
@ECHO = global i32 0
@ISIG = global i32 0
@ICANON = global i32 0
@INLCR = global i32 0
@IGNCR = global i32 0
@ICRNL = global i32 0
@TCSAFLUSH = global i32 0
@stderr = global i32 0
define i8* @getpass(i8* %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to %struct.termios*
%4 = alloca i64
%5 = bitcast i64* %4 to %struct.termios*
%6 = bitcast i64* %2 to i8*
%7 = bitcast i64* %4 to i8*
%8 = load i32, i32* @O_RDONLY
%9 = load i32, i32* @O_NOCTTY
%10 = or i32 %9, %8
%11 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp sgt i32 %11, 0
%13 = select i1 %12, i32 %11, i32 0
%14 = call i32 @tcgetattr(i32 %13, %struct.termios* %5)
%15 = load i64, i64* %4
store i64 %15, i64* %2
%16 = load i32, i32* @ECHO
%17 = load i32, i32* @ISIG
%18 = or i32 %17, %16
%19 = xor i32 %18, -1
%20 = bitcast i64* %4 to i32*
%21 = trunc i64 %15 to i32
%22 = and i32 %21, %19
%23 = load i32, i32* @ICANON
%24 = or i32 %23, %22
store i32 %24, i32* %20
%25 = load i32, i32* @INLCR
%26 = load i32, i32* @IGNCR
%27 = or i32 %26, %25
%28 = xor i32 %27, -1
%29 = getelementptr inbounds %struct.termios, %struct.termios* %5, i64 0, i32 1
%30 = lshr i64 %15, 32
%31 = trunc i64 %30 to i32
%32 = and i32 %31, %28
%33 = load i32, i32* @ICRNL
%34 = or i32 %33, %32
store i32 %34, i32* %29
%35 = load i32, i32* @TCSAFLUSH
%36 = call i32 @tcsetattr(i32 %13, i32 %35, %struct.termios* %5)
%37 = call i32 @tcdrain(i32 %13)
%38 = load i32, i32* @stderr
%39 = call i32 @fputs(i8* %0, i32 %38)
%40 = load i32, i32* @stderr
%41 = call i32 @fflush(i32 %40)
%42 = call i32 @read(i32 %13, i8* getelementptr inbounds ([128 x i8], [128 x i8]* @getpass.password, i64 0, i64 0), i32 128)
%43 = icmp sgt i32 %42, -1
br i1 %43, label %44, label %57
44: ; preds = %1
%45 = icmp eq i32 %42, 0
br i1 %45, label %54, label %46
46: ; preds = %44
%47 = add nsw i32 %42, -1
%48 = zext i32 %47 to i64
%49 = getelementptr inbounds [128 x i8], [128 x i8]* @getpass.password, i64 0, i64 %48
%50 = load i8, i8* %49
%51 = icmp eq i8 %50, 10
%52 = select i1 %51, i32 %47, i32 %42
%53 = zext i32 %52 to i64
br label %54
54: ; preds = %46, %44
%55 = phi i64 [ 0, %44 ], [ %53, %46 ]
%56 = getelementptr inbounds [128 x i8], [128 x i8]* @getpass.password, i64 0, i64 %55
store i8 0, i8* %56
br label %57
57: ; preds = %54, %1
%58 = load i32, i32* @TCSAFLUSH
%59 = call i32 @tcsetattr(i32 %13, i32 %58, %struct.termios* %3)
%60 = icmp ugt i32 %13, 2
br i1 %60, label %61, label %63
61: ; preds = %57
%62 = call i32 @close(i32 %13)
br label %63
63: ; preds = %61, %57
ret i8* getelementptr inbounds ([128 x i8], [128 x i8]* @getpass.password, i64 0, i64 0)
}