#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*/ TCSAFLUSH;
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
scalar_t__ tcgetattr(int /*<<< orphan*/, struct termios *);
scalar_t__ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
char *xgetpass(FILE *stream) {
  char *pw = NULL;
  size_t linecap = 0;
  ssize_t nread;
  struct termios old, new;
  if (tcgetattr(fileno(stream), &old) != 0)
    return NULL;
  new = old;
  new.c_lflag &= ~ECHO;
  if (tcsetattr(fileno(stream), TCSAFLUSH, &new) != 0)
    return NULL;
  nread = getline(&pw, &linecap, stream);
  tcsetattr(fileno(stream), TCSAFLUSH, &old);
  if (nread < 0) {
    if (pw)
      free(pw);
    return NULL;
  }
  if (nread > 0 && pw[nread - 1] == '\n')
    pw[nread - 1] = '\0';
  return pw;
}


x86 assembly (clang, -O0):
<xgetpass>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,%edi
lea    -0x28(%rbp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0x0,%rax
je     1197 <xgetpass+0x47>
movq   $0x0,-0x8(%rbp)
jmp    127c <xgetpass+0x12c>
mov    -0x28(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
je     11e3 <xgetpass+0x93>
movq   $0x0,-0x8(%rbp)
jmp    127c <xgetpass+0x12c>
mov    -0x10(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <getline@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmpl   $0x0,-0x24(%rbp)
jge    123f <xgetpass+0xef>
cmpq   $0x0,-0x18(%rbp)
je     1232 <xgetpass+0xe2>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    127c <xgetpass+0x12c>
cmpl   $0x0,-0x24(%rbp)
jle    1274 <xgetpass+0x124>
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1274 <xgetpass+0x124>
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xgetpass>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
movq   $0x0,0x8(%rsp)
movq   $0x0,0x18(%rsp)
call   1040 <fileno@plt>
mov    %rsp,%rsi
mov    %eax,%edi
call   1050 <tcgetattr@plt>
test   %rax,%rax
jne    11b7 <xgetpass+0x67>
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
not    %eax
and    (%rsp),%eax
mov    %eax,0x10(%rsp)
mov    %rbx,%rdi
call   1040 <fileno@plt>
mov    0x2e45(%rip),%r15
mov    (%r15),%esi
lea    0x10(%rsp),%rdx
mov    %eax,%edi
call   1060 <tcsetattr@plt>
test   %rax,%rax
je     11c3 <xgetpass+0x73>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0x8(%rsp),%rdi
lea    0x18(%rsp),%rsi
mov    %rbx,%rdx
call   1070 <getline@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1040 <fileno@plt>
mov    (%r15),%esi
mov    %rsp,%rdx
mov    %eax,%edi
call   1060 <tcsetattr@plt>
test   %r14d,%r14d
js     1210 <xgetpass+0xc0>
je     1209 <xgetpass+0xb9>
mov    0x8(%rsp),%rax
add    $0xffffffff,%r14d
cmpb   $0xa,(%rax,%r14,1)
jne    1209 <xgetpass+0xb9>
movb   $0x0,(%rax,%r14,1)
mov    0x8(%rsp),%rax
jmp    11b9 <xgetpass+0x69>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     11b7 <xgetpass+0x67>
call   1030 <free@plt>
jmp    11b7 <xgetpass+0x67>

LLVM IR (-O0):
%struct.termios = type { i32 }
@ECHO = global i32 0
@TCSAFLUSH = global i32 0
define i8* @xgetpass(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca %struct.termios
%8 = alloca %struct.termios
store i32* %0, i32** %3
store i8* null, i8** %4
store i64 0, i64* %5
%9 = load i32*, i32** %3
%10 = call i32 @fileno(i32* %9)
%11 = call i64 @tcgetattr(i32 %10, %struct.termios* %7)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %1
store i8* null, i8** %2
br label %64
14: ; preds = %1
%15 = bitcast %struct.termios* %8 to i8*
%16 = bitcast %struct.termios* %7 to i8*
%17 = load i32, i32* @ECHO
%18 = xor i32 %17, -1
%19 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%20 = load i32, i32* %19
%21 = and i32 %20, %18
store i32 %21, i32* %19
%22 = load i32*, i32** %3
%23 = call i32 @fileno(i32* %22)
%24 = load i32, i32* @TCSAFLUSH
%25 = call i64 @tcsetattr(i32 %23, i32 %24, %struct.termios* %8)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %14
store i8* null, i8** %2
br label %64
28: ; preds = %14
%29 = load i32*, i32** %3
%30 = call i32 @getline(i8** %4, i64* %5, i32* %29)
store i32 %30, i32* %6
%31 = load i32*, i32** %3
%32 = call i32 @fileno(i32* %31)
%33 = load i32, i32* @TCSAFLUSH
%34 = call i64 @tcsetattr(i32 %32, i32 %33, %struct.termios* %7)
%35 = load i32, i32* %6
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %28
%38 = load i8*, i8** %4
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i8*, i8** %4
%42 = call i32 @free(i8* %41)
br label %43
43: ; preds = %40, %37
store i8* null, i8** %2
br label %64
44: ; preds = %28
%45 = load i32, i32* %6
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %62
47: ; preds = %44
%48 = load i8*, i8** %4
%49 = load i32, i32* %6
%50 = sub nsw i32 %49, 1
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %48, i64 %51
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 10
br i1 %55, label %56, label %62
56: ; preds = %47
%57 = load i8*, i8** %4
%58 = load i32, i32* %6
%59 = sub nsw i32 %58, 1
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %57, i64 %60
store i8 0, i8* %61
br label %62
62: ; preds = %56, %47, %44
%63 = load i8*, i8** %4
store i8* %63, i8** %2
br label %64
64: ; preds = %62, %43, %27, %13
%65 = load i8*, i8** %2
ret i8* %65
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@ECHO = global i32 0
@TCSAFLUSH = global i32 0
define i8* @xgetpass(i32* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca %struct.termios
%5 = alloca %struct.termios
%6 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%7 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%8 = bitcast %struct.termios* %4 to i8*
%9 = bitcast %struct.termios* %5 to i8*
%10 = tail call i32 @fileno(i32* %0)
%11 = call i64 @tcgetattr(i32 %10, %struct.termios* %4)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %47
13: ; preds = %1
%14 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%15 = getelementptr inbounds %struct.termios, %struct.termios* %5, i64 0, i32 0
%16 = load i32, i32* %14
%17 = load i32, i32* @ECHO
%18 = xor i32 %17, -1
%19 = and i32 %16, %18
store i32 %19, i32* %15
%20 = call i32 @fileno(i32* %0)
%21 = load i32, i32* @TCSAFLUSH
%22 = call i64 @tcsetattr(i32 %20, i32 %21, %struct.termios* %5)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %47
24: ; preds = %13
%25 = call i32 @getline(i8** %2, i64* %3, i32* %0)
%26 = call i32 @fileno(i32* %0)
%27 = load i32, i32* @TCSAFLUSH
%28 = call i64 @tcsetattr(i32 %26, i32 %27, %struct.termios* %4)
%29 = icmp slt i32 %25, 0
br i1 %29, label %30, label %35
30: ; preds = %24
%31 = load i8*, i8** %2
%32 = icmp eq i8* %31, null
br i1 %32, label %47, label %33
33: ; preds = %30
%34 = call i32 @free(i8* %31)
br label %47
35: ; preds = %24
%36 = icmp eq i32 %25, 0
br i1 %36, label %45, label %37
37: ; preds = %35
%38 = load i8*, i8** %2
%39 = add nsw i32 %25, -1
%40 = zext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = load i8, i8* %41
%43 = icmp eq i8 %42, 10
br i1 %43, label %44, label %45
44: ; preds = %37
store i8 0, i8* %41
br label %45
45: ; preds = %44, %37, %35
%46 = load i8*, i8** %2
br label %47
47: ; preds = %30, %33, %13, %1, %45
%48 = phi i8* [ %46, %45 ], [ null, %1 ], [ null, %13 ], [ null, %33 ], [ null, %30 ]
ret i8* %48
}