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

/* Variables and functions */
int /*<<< orphan*/ ECHO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSAFLUSH;
int getline(char **, size_t *, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int tcgetattr(int /*<<< orphan*/, struct termios *);
int tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
char *readpass() {
  struct termios term, restore;
  char *password = NULL;
  size_t pw_size = 0;
  ssize_t pw_len;
  if (tcgetattr(STDIN_FILENO, &term) == -1)
    return NULL;
  restore = term;
  term.c_lflag &= ~ECHO;
  if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &term) == -1)
    return NULL;
  pw_len = getline(&password, &pw_size, stdin);
  if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &restore) == -1)
    return NULL;
  if (password != NULL)
    password[pw_len - 1] = (char)0;
  return password;
}


x86 assembly (clang, -O0):
<readpass>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <tcgetattr@plt>
cmp    $0xffffffff,%eax
jne    1170 <readpass+0x40>
movq   $0x0,-0x8(%rbp)
jmp    1227 <readpass+0xf7>
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1040 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    11b9 <readpass+0x89>
movq   $0x0,-0x8(%rbp)
jmp    1227 <readpass+0xf7>
mov    0x2e20(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1050 <getline@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    1203 <readpass+0xd3>
movq   $0x0,-0x8(%rbp)
jmp    1227 <readpass+0xf7>
cmpq   $0x0,-0x20(%rbp)
je     121f <readpass+0xef>
mov    -0x20(%rbp),%rax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readpass>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
movq   $0x0,0x8(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x2e77(%rip),%rbx
mov    (%rbx),%edi
mov    %rsp,%rsi
call   1030 <tcgetattr@plt>
cmp    $0xffffffff,%eax
je     11d8 <readpass+0xa8>
mov    (%rsp),%eax
mov    %eax,0x10(%rsp)
mov    0x2e6a(%rip),%rcx
mov    (%rcx),%ecx
not    %ecx
and    %eax,%ecx
mov    %ecx,(%rsp)
mov    (%rbx),%edi
mov    0x2e68(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %rsp,%rdx
call   1040 <tcsetattr@plt>
cmp    $0xffffffff,%eax
je     11d8 <readpass+0xa8>
mov    0x2e49(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rdi
lea    0x18(%rsp),%rsi
call   1050 <getline@plt>
mov    %eax,%r14d
mov    (%rbx),%edi
mov    0x0(%rbp),%esi
lea    0x10(%rsp),%rdx
call   1040 <tcsetattr@plt>
cmp    $0xffffffff,%eax
je     11d8 <readpass+0xa8>
mov    0x8(%rsp),%rax
test   %rax,%rax
je     11d8 <readpass+0xa8>
movslq %r14d,%rcx
movb   $0x0,-0x1(%rcx,%rax,1)
mov    0x8(%rsp),%rax
jmp    11da <readpass+0xaa>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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