#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;
};
typedef int /*<<< orphan*/ sigset_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int ECHO;
int ECHOE;
int ECHOK;
int ECHONL;
int EOF;
int /*<<< orphan*/ SIGINT;
int /*<<< orphan*/ SIGTSTP;
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ SIG_SETMASK;
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ ctermid(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fputs(char const *, int /*<<< orphan*/ *);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ putc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ setbuf(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
scalar_t__ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                       int /*<<< orphan*/ *);
char *strdup(char *);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
char *w_readpass(const char *prompt) {
  char buf[(128)];
  char *ptr;
  sigset_t sig, osig;
  struct termios ts, ots;
  FILE *fp;
  int c;
  if ((fp = fopen(ctermid(NULL), "r+")) == NULL) {
    return NULL;
  }
  setbuf(fp, NULL);
  sigemptyset(&sig);
  sigaddset(&sig, SIGINT);
  sigaddset(&sig, SIGTSTP);
  if (sigprocmask(SIG_BLOCK, &sig, &osig) != 0) {
    return NULL;
  }
  int fd = fileno(fp);
  tcgetattr(fd, &ts);
  ots = ts;
  ts.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
  if (tcsetattr(fd, TCSAFLUSH, &ts) != 0) {
    sigprocmask(SIG_SETMASK, &osig, NULL);
    return NULL;
  }
  fputs(prompt, fp);
  ptr = buf;
  while ((c = getc(fp)) != EOF && c != '\n' && c != '\r') {
    if (ptr < &buf[(128)]) {
      *ptr = c;
      ptr++;
    }
  }
  *ptr = '\0';
  putc('\n', fp);
  tcsetattr(fd, TCSAFLUSH, &ots);
  sigprocmask(SIG_SETMASK, &osig, NULL);
  fclose(fp);
  return strdup(buf);
}


x86 assembly (clang, -O0):
<w_readpass>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    %rdi,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <ctermid@plt>
mov    %eax,%edi
lea    0xdff(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0xb8(%rbp)
cmp    $0x0,%rax
jne    1224 <w_readpass+0x44>
movq   $0x0,-0x8(%rbp)
jmp    147d <w_readpass+0x29d>
mov    -0xb8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <setbuf@plt>
lea    -0x9c(%rbp),%rdi
call   1080 <sigemptyset@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
lea    -0x9c(%rbp),%rdi
call   1100 <sigaddset@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
lea    -0x9c(%rbp),%rdi
call   1100 <sigaddset@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
lea    -0x9c(%rbp),%rsi
lea    -0xa0(%rbp),%rdx
call   1030 <sigprocmask@plt>
cmp    $0x0,%rax
je     129d <w_readpass+0xbd>
movq   $0x0,-0x8(%rbp)
jmp    147d <w_readpass+0x29d>
mov    -0xb8(%rbp),%rdi
call   1090 <fileno@plt>
mov    %eax,-0xc0(%rbp)
mov    -0xc0(%rbp),%edi
lea    -0xa8(%rbp),%rsi
call   10a0 <tcgetattr@plt>
mov    -0xa8(%rbp),%eax
mov    %eax,-0xb0(%rbp)
mov    0x2d04(%rip),%rax
mov    (%rax),%eax
mov    0x2ceb(%rip),%rcx
or     (%rcx),%eax
mov    0x2cc2(%rip),%rcx
or     (%rcx),%eax
mov    0x2ca9(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0xa8(%rbp),%eax
mov    %eax,-0xa8(%rbp)
mov    -0xc0(%rbp),%edi
mov    0x2cdb(%rip),%rax
mov    (%rax),%esi
lea    -0xa8(%rbp),%rdx
call   10b0 <tcsetattr@plt>
cmp    $0x0,%rax
je     134b <w_readpass+0x16b>
mov    0x2ca4(%rip),%rax
mov    (%rax),%edi
lea    -0xa0(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
movq   $0x0,-0x8(%rbp)
jmp    147d <w_readpass+0x29d>
mov    -0x10(%rbp),%rdi
mov    -0xb8(%rbp),%rsi
call   1060 <fputs@plt>
lea    -0x90(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    -0xb8(%rbp),%rdi
call   10f0 <getc@plt>
mov    %eax,%ecx
mov    %ecx,-0xbc(%rbp)
xor    %eax,%eax
mov    0x2c32(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0xc1(%rbp)
je     13b9 <w_readpass+0x1d9>
xor    %eax,%eax
cmpl   $0xa,-0xbc(%rbp)
mov    %al,-0xc1(%rbp)
je     13b9 <w_readpass+0x1d9>
cmpl   $0xd,-0xbc(%rbp)
setne  %al
mov    %al,-0xc1(%rbp)
mov    -0xc1(%rbp),%al
test   $0x1,%al
jne    13cc <w_readpass+0x1ec>
jmp    1412 <w_readpass+0x232>
mov    -0x98(%rbp),%rax
lea    -0x90(%rbp),%rcx
add    $0x80,%rcx
cmp    %rcx,%rax
jae    140d <w_readpass+0x22d>
mov    -0xbc(%rbp),%eax
mov    %al,%cl
mov    -0x98(%rbp),%rax
mov    %cl,(%rax)
mov    -0x98(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x98(%rbp)
jmp    1369 <w_readpass+0x189>
mov    -0x98(%rbp),%rax
movb   $0x0,(%rax)
mov    -0xb8(%rbp),%rsi
mov    $0xa,%edi
call   1070 <putc@plt>
mov    -0xc0(%rbp),%edi
mov    0x2bae(%rip),%rax
mov    (%rax),%esi
lea    -0xb0(%rbp),%rdx
call   10b0 <tcsetattr@plt>
mov    0x2b81(%rip),%rax
mov    (%rax),%edi
lea    -0xa0(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    -0xb8(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x90(%rbp),%rdi
call   10e0 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<w_readpass>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %rdi,%r15
xor    %ebp,%ebp
xor    %edi,%edi
call   10c0 <ctermid@plt>
lea    0xdfc(%rip),%rsi
mov    %eax,%edi
call   10d0 <fopen@plt>
test   %rax,%rax
je     135e <w_readpass+0x17e>
mov    %rax,%r12
xor    %ebp,%ebp
mov    %rax,%rdi
xor    %esi,%esi
call   1050 <setbuf@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <sigemptyset@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1100 <sigaddset@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1100 <sigaddset@plt>
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
lea    0x4(%rsp),%rdx
mov    %rbx,%rsi
call   1030 <sigprocmask@plt>
test   %rax,%rax
jne    135e <w_readpass+0x17e>
mov    %r12,%rdi
call   1090 <fileno@plt>
mov    %eax,%r14d
mov    %rsp,%rbx
mov    %eax,%edi
mov    %rbx,%rsi
call   10a0 <tcgetattr@plt>
mov    (%rsp),%eax
mov    0x2d45(%rip),%rcx
mov    0x2d2e(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    %eax,0x8(%rsp)
mov    0x2cff(%rip),%rcx
or     (%rcx),%edx
mov    0x2ce6(%rip),%rcx
or     (%rcx),%edx
not    %edx
and    %eax,%edx
mov    %edx,(%rsp)
mov    0x2d26(%rip),%r13
mov    0x0(%r13),%esi
mov    %r14d,%edi
mov    %rbx,%rdx
call   10b0 <tcsetattr@plt>
test   %rax,%rax
je     12ef <w_readpass+0x10f>
mov    0x2cf3(%rip),%rax
mov    (%rax),%edi
xor    %ebp,%ebp
lea    0x4(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
jmp    135e <w_readpass+0x17e>
mov    %r15,%rdi
mov    %r12,%rsi
call   1060 <fputs@plt>
mov    %r12,%rdi
call   10f0 <getc@plt>
mov    0x2caf(%rip),%rbp
cmp    0x0(%rbp),%eax
jne    1373 <w_readpass+0x193>
lea    0x10(%rsp),%rbx
movb   $0x0,(%rbx)
mov    $0xa,%edi
mov    %r12,%rsi
call   1070 <putc@plt>
mov    0x0(%r13),%esi
lea    0x8(%rsp),%rdx
mov    %r14d,%edi
call   10b0 <tcsetattr@plt>
mov    0x2c95(%rip),%rax
mov    (%rax),%edi
lea    0x4(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
lea    0x10(%rsp),%rdi
call   10e0 <strdup@plt>
mov    %rax,%rbp
mov    %rbp,%rax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x10(%rsp),%rbx
lea    0x90(%rsp),%r15
jmp    13a1 <w_readpass+0x1c1>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rdi
call   10f0 <getc@plt>
cmp    0x0(%rbp),%eax
je     1313 <w_readpass+0x133>
cmp    $0xa,%eax
je     1313 <w_readpass+0x133>
cmp    $0xd,%eax
je     1313 <w_readpass+0x133>
cmp    %r15,%rbx
jae    1390 <w_readpass+0x1b0>
mov    %al,(%rbx)
add    $0x1,%rbx
jmp    1390 <w_readpass+0x1b0>

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [3 x i8] c"r+\00"
@SIGINT = global i32 0
@SIGTSTP = global i32 0
@SIG_BLOCK = global i32 0
@ECHO = global i32 0
@ECHOE = global i32 0
@ECHOK = global i32 0
@ECHONL = global i32 0
@TCSAFLUSH = global i32 0
@SIG_SETMASK = global i32 0
@EOF = global i32 0
define i8* @w_readpass(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [128 x i8]
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.termios
%9 = alloca %struct.termios
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %3
%13 = call i32 @ctermid(i32* null)
%14 = call i32* @fopen(i32 %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %10
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %17
16: ; preds = %1
store i8* null, i8** %2
br label %96
17: ; preds = %1
%18 = load i32*, i32** %10
%19 = call i32 @setbuf(i32* %18, i32* null)
%20 = call i32 @sigemptyset(i32* %6)
%21 = load i32, i32* @SIGINT
%22 = call i32 @sigaddset(i32* %6, i32 %21)
%23 = load i32, i32* @SIGTSTP
%24 = call i32 @sigaddset(i32* %6, i32 %23)
%25 = load i32, i32* @SIG_BLOCK
%26 = call i64 @sigprocmask(i32 %25, i32* %6, i32* %7)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %17
store i8* null, i8** %2
br label %96
29: ; preds = %17
%30 = load i32*, i32** %10
%31 = call i32 @fileno(i32* %30)
store i32 %31, i32* %12
%32 = load i32, i32* %12
%33 = call i32 @tcgetattr(i32 %32, %struct.termios* %8)
%34 = bitcast %struct.termios* %9 to i8*
%35 = bitcast %struct.termios* %8 to i8*
%36 = load i32, i32* @ECHO
%37 = load i32, i32* @ECHOE
%38 = or i32 %36, %37
%39 = load i32, i32* @ECHOK
%40 = or i32 %38, %39
%41 = load i32, i32* @ECHONL
%42 = or i32 %40, %41
%43 = xor i32 %42, -1
%44 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%45 = load i32, i32* %44
%46 = and i32 %45, %43
store i32 %46, i32* %44
%47 = load i32, i32* %12
%48 = load i32, i32* @TCSAFLUSH
%49 = call i64 @tcsetattr(i32 %47, i32 %48, %struct.termios* %8)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %29
%52 = load i32, i32* @SIG_SETMASK
%53 = call i64 @sigprocmask(i32 %52, i32* %7, i32* null)
store i8* null, i8** %2
br label %96
54: ; preds = %29
%55 = load i8*, i8** %3
%56 = load i32*, i32** %10
%57 = call i32 @fputs(i8* %55, i32* %56)
%58 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
store i8* %58, i8** %5
br label %59
59: ; preds = %82, %54
%60 = load i32*, i32** %10
%61 = call i32 @getc(i32* %60)
store i32 %61, i32* %11
%62 = load i32, i32* @EOF
%63 = icmp ne i32 %61, %62
br i1 %63, label %64, label %70
64: ; preds = %59
%65 = load i32, i32* %11
%66 = icmp ne i32 %65, 10
br i1 %66, label %67, label %70
67: ; preds = %64
%68 = load i32, i32* %11
%69 = icmp ne i32 %68, 13
br label %70
70: ; preds = %67, %64, %59
%71 = phi i1 [ false, %64 ], [ false, %59 ], [ %69, %67 ]
br i1 %71, label %72, label %83
72: ; preds = %70
%73 = load i8*, i8** %5
%74 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 128
%75 = icmp ult i8* %73, %74
br i1 %75, label %76, label %82
76: ; preds = %72
%77 = load i32, i32* %11
%78 = trunc i32 %77 to i8
%79 = load i8*, i8** %5
store i8 %78, i8* %79
%80 = load i8*, i8** %5
%81 = getelementptr inbounds i8, i8* %80, i32 1
store i8* %81, i8** %5
br label %82
82: ; preds = %76, %72
br label %59
83: ; preds = %70
%84 = load i8*, i8** %5
store i8 0, i8* %84
%85 = load i32*, i32** %10
%86 = call i32 @putc(i8 signext 10, i32* %85)
%87 = load i32, i32* %12
%88 = load i32, i32* @TCSAFLUSH
%89 = call i64 @tcsetattr(i32 %87, i32 %88, %struct.termios* %9)
%90 = load i32, i32* @SIG_SETMASK
%91 = call i64 @sigprocmask(i32 %90, i32* %7, i32* null)
%92 = load i32*, i32** %10
%93 = call i32 @fclose(i32* %92)
%94 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%95 = call i8* @strdup(i8* %94)
store i8* %95, i8** %2
br label %96
96: ; preds = %83, %51, %28, %16
%97 = load i8*, i8** %2
ret i8* %97
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [3 x i8] c"r+\00"
@SIGINT = global i32 0
@SIGTSTP = global i32 0
@SIG_BLOCK = global i32 0
@ECHO = global i32 0
@ECHOE = global i32 0
@ECHOK = global i32 0
@ECHONL = global i32 0
@TCSAFLUSH = global i32 0
@SIG_SETMASK = global i32 0
@EOF = global i32 0
define noalias i8* @w_readpass(i8* nocapture %0) {
%2 = alloca [128 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.termios
%6 = alloca %struct.termios
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%8 = bitcast i32* %3 to i8*
%9 = bitcast i32* %4 to i8*
%10 = bitcast %struct.termios* %5 to i8*
%11 = bitcast %struct.termios* %6 to i8*
%12 = tail call i32 @ctermid(i32* null)
%13 = tail call i32* @fopen(i32 %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %77, label %15
15: ; preds = %1
%16 = tail call i32 @setbuf(i32* %13, i32* null)
%17 = call i32 @sigemptyset(i32* %3)
%18 = load i32, i32* @SIGINT
%19 = call i32 @sigaddset(i32* %3, i32 %18)
%20 = load i32, i32* @SIGTSTP
%21 = call i32 @sigaddset(i32* %3, i32 %20)
%22 = load i32, i32* @SIG_BLOCK
%23 = call i64 @sigprocmask(i32 %22, i32* %3, i32* %4)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %77
25: ; preds = %15
%26 = call i32 @fileno(i32* %13)
%27 = call i32 @tcgetattr(i32 %26, %struct.termios* %5)
%28 = getelementptr inbounds %struct.termios, %struct.termios* %5, i64 0, i32 0
%29 = getelementptr inbounds %struct.termios, %struct.termios* %6, i64 0, i32 0
%30 = load i32, i32* %28
store i32 %30, i32* %29
%31 = load i32, i32* @ECHO
%32 = load i32, i32* @ECHOE
%33 = or i32 %32, %31
%34 = load i32, i32* @ECHOK
%35 = or i32 %33, %34
%36 = load i32, i32* @ECHONL
%37 = or i32 %35, %36
%38 = xor i32 %37, -1
%39 = and i32 %30, %38
store i32 %39, i32* %28
%40 = load i32, i32* @TCSAFLUSH
%41 = call i64 @tcsetattr(i32 %26, i32 %40, %struct.termios* %5)
%42 = icmp eq i64 %41, 0
br i1 %42, label %46, label %43
43: ; preds = %25
%44 = load i32, i32* @SIG_SETMASK
%45 = call i64 @sigprocmask(i32 %44, i32* %4, i32* null)
br label %77
46: ; preds = %25
%47 = call i32 @fputs(i8* %0, i32* %13)
%48 = call i32 @getc(i32* %13)
%49 = load i32, i32* @EOF
%50 = freeze i32 %49
%51 = icmp eq i32 %48, %50
br i1 %51, label %68, label %52
52: ; preds = %46
%53 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 128
br label %54
54: ; preds = %52, %62
%55 = phi i32 [ %48, %52 ], [ %64, %62 ]
%56 = phi i8* [ %7, %52 ], [ %63, %62 ]
switch i32 %55, label %57 [
i32 13, label %68
i32 10, label %68
]
57: ; preds = %54
%58 = icmp ult i8* %56, %53
br i1 %58, label %59, label %62
59: ; preds = %57
%60 = trunc i32 %55 to i8
store i8 %60, i8* %56
%61 = getelementptr inbounds i8, i8* %56, i64 1
br label %62
62: ; preds = %59, %57
%63 = phi i8* [ %61, %59 ], [ %56, %57 ]
%64 = call i32 @getc(i32* %13)
%65 = load i32, i32* @EOF
%66 = freeze i32 %65
%67 = icmp eq i32 %64, %66
br i1 %67, label %68, label %54
68: ; preds = %62, %54, %54, %46
%69 = phi i8* [ %7, %46 ], [ %56, %54 ], [ %56, %54 ], [ %63, %62 ]
store i8 0, i8* %69
%70 = call i32 @putc(i8 signext 10, i32* %13)
%71 = load i32, i32* @TCSAFLUSH
%72 = call i64 @tcsetattr(i32 %26, i32 %71, %struct.termios* %6)
%73 = load i32, i32* @SIG_SETMASK
%74 = call i64 @sigprocmask(i32 %73, i32* %4, i32* null)
%75 = call i32 @fclose(i32* %13)
%76 = call i8* @strdup(i8* %7)
br label %77
77: ; preds = %43, %68, %15, %1
%78 = phi i8* [ null, %1 ], [ null, %15 ], [ null, %43 ], [ %76, %68 ]
ret i8* %78
}