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

/* Variables and functions */
int ECHO;
int ICANON;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int getchar();
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int /*<<< orphan*/ tl_showpassword(int, char *, int);
int tl_strlen(char *);
void tl_getpassword(char *password, char *passchar) {
  int c;
  struct termios old, new;
  int passchar_len = 0;
  int password_pos = 0;
  password[0] = 0;
  tcgetattr(STDIN_FILENO, &old);
  new = old;
  new.c_lflag &= ~(ICANON | ECHO);
  new.c_cc[VMIN] = 1;
  new.c_cc[VTIME] = 0;
  passchar_len = tl_strlen(passchar);
  tl_showpassword(0, "", 0);
  while (1) {
    tcsetattr(STDIN_FILENO, TCSANOW, &new);
    c = getchar();
    tcsetattr(STDIN_FILENO, TCSANOW, &old);
    if ((c == 127 || c == 8)) {
      if (password_pos > 0) {
        password[--password_pos] = 0;
        tl_showpassword(password_pos, passchar, passchar_len);
      }
      continue;
    }
    if (c == '\n')
      break;
    password[password_pos++] = c;
    password[password_pos] = 0;
    tl_showpassword(password_pos, passchar, passchar_len);
  }
}


x86 assembly (clang, -O0):
<tl_getpassword>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1060 <tcgetattr@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    0x2e39(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2e1b(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x30(%rbp),%rax
mov    0x2df6(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x10(%rbp),%rdi
call   1050 <tl_strlen@plt>
mov    %eax,-0x3c(%rbp)
xor    %edx,%edx
lea    0xe0f(%rip),%rsi
mov    %edx,%edi
call   1030 <tl_showpassword@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmpl   $0x7f,-0x14(%rbp)
je     124c <tl_getpassword+0xfc>
cmpl   $0x8,-0x14(%rbp)
jne    127e <tl_getpassword+0x12e>
cmpl   $0x0,-0x40(%rbp)
jle    1279 <tl_getpassword+0x129>
mov    -0x8(%rbp),%rax
mov    -0x40(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x40(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x40(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1030 <tl_showpassword@plt>
jmp    11f8 <tl_getpassword+0xa8>
cmpl   $0xa,-0x14(%rbp)
jne    128d <tl_getpassword+0x13d>
jmp    12c7 <tl_getpassword+0x177>
mov    -0x14(%rbp),%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    -0x40(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x40(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
movslq -0x40(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x40(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1030 <tl_showpassword@plt>
jmp    11f8 <tl_getpassword+0xa8>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tl_getpassword>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%rbx
mov    %rdi,0x8(%rsp)
movb   $0x0,(%rdi)
mov    0x2e50(%rip),%r12
mov    (%r12),%edi
lea    0x28(%rsp),%r13
mov    %r13,%rsi
call   1060 <tcgetattr@plt>
movups 0x28(%rsp),%xmm0
movaps %xmm0,0x10(%rsp)
mov    0x2e26(%rip),%rax
mov    0x2e47(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    %ecx,0x10(%rsp)
mov    0x18(%rsp),%rax
mov    0x2e29(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e08(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    %rbx,0x20(%rsp)
mov    %rbx,%rdi
call   1050 <tl_strlen@plt>
mov    %eax,0x4(%rsp)
lea    0xe1e(%rip),%rsi
xor    %edi,%edi
xor    %edx,%edx
call   1030 <tl_showpassword@plt>
mov    0x2df6(%rip),%r15
lea    0x10(%rsp),%rbp
xor    %ebx,%ebx
jmp    1204 <tl_getpassword+0xb4>
nopl   0x0(%rax,%rax,1)
test   %ebx,%ebx
jg     1252 <tl_getpassword+0x102>
mov    (%r12),%edi
mov    (%r15),%esi
mov    %rbp,%rdx
call   1070 <tcsetattr@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
mov    %eax,%r14d
mov    (%r12),%edi
mov    (%r15),%esi
mov    %r13,%rdx
call   1070 <tcsetattr@plt>
cmp    $0x8,%r14d
je     1200 <tl_getpassword+0xb0>
cmp    $0x7f,%r14d
je     1200 <tl_getpassword+0xb0>
cmp    $0xa,%r14d
je     1273 <tl_getpassword+0x123>
movslq %ebx,%rax
add    $0x1,%ebx
mov    0x8(%rsp),%rcx
mov    %r14b,(%rcx,%rax,1)
movslq %ebx,%rax
jmp    125d <tl_getpassword+0x10d>
add    $0xffffffff,%ebx
mov    %rbx,%rax
mov    0x8(%rsp),%rcx
movb   $0x0,(%rcx,%rax,1)
mov    %ebx,%edi
mov    0x20(%rsp),%rsi
mov    0x4(%rsp),%edx
call   1030 <tl_showpassword@plt>
jmp    1204 <tl_getpassword+0xb4>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32* }
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@.str = [1 x i8] zeroinitializer
@TCSANOW = global i32 0
define void @tl_getpassword(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.termios
%7 = alloca %struct.termios
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %8
store i32 0, i32* %9
%10 = load i8*, i8** %3
%11 = getelementptr inbounds i8, i8* %10, i64 0
store i8 0, i8* %11
%12 = load i32, i32* @STDIN_FILENO
%13 = call i32 @tcgetattr(i32 %12, %struct.termios* %6)
%14 = bitcast %struct.termios* %7 to i8*
%15 = bitcast %struct.termios* %6 to i8*
%16 = load i32, i32* @ICANON
%17 = load i32, i32* @ECHO
%18 = or i32 %16, %17
%19 = xor i32 %18, -1
%20 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%21 = load i32, i32* %20
%22 = and i32 %21, %19
store i32 %22, i32* %20
%23 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%24 = load i32*, i32** %23
%25 = load i64, i64* @VMIN
%26 = getelementptr inbounds i32, i32* %24, i64 %25
store i32 1, i32* %26
%27 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%28 = load i32*, i32** %27
%29 = load i64, i64* @VTIME
%30 = getelementptr inbounds i32, i32* %28, i64 %29
store i32 0, i32* %30
%31 = load i8*, i8** %4
%32 = call i32 @tl_strlen(i8* %31)
store i32 %32, i32* %8
%33 = call i32 @tl_showpassword(i32 0, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i32 0)
br label %34
34: ; preds = %2, %60, %65
%35 = load i32, i32* @STDIN_FILENO
%36 = load i32, i32* @TCSANOW
%37 = call i32 @tcsetattr(i32 %35, i32 %36, %struct.termios* %7)
%38 = call i32 (...) @getchar()
store i32 %38, i32* %5
%39 = load i32, i32* @STDIN_FILENO
%40 = load i32, i32* @TCSANOW
%41 = call i32 @tcsetattr(i32 %39, i32 %40, %struct.termios* %6)
%42 = load i32, i32* %5
%43 = icmp eq i32 %42, 127
br i1 %43, label %47, label %44
44: ; preds = %34
%45 = load i32, i32* %5
%46 = icmp eq i32 %45, 8
br i1 %46, label %47, label %61
47: ; preds = %44, %34
%48 = load i32, i32* %9
%49 = icmp sgt i32 %48, 0
br i1 %49, label %50, label %60
50: ; preds = %47
%51 = load i8*, i8** %3
%52 = load i32, i32* %9
%53 = add nsw i32 %52, -1
store i32 %53, i32* %9
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %51, i64 %54
store i8 0, i8* %55
%56 = load i32, i32* %9
%57 = load i8*, i8** %4
%58 = load i32, i32* %8
%59 = call i32 @tl_showpassword(i32 %56, i8* %57, i32 %58)
br label %60
60: ; preds = %50, %47
br label %34
61: ; preds = %44
%62 = load i32, i32* %5
%63 = icmp eq i32 %62, 10
br i1 %63, label %64, label %65
64: ; preds = %61
br label %81
65: ; preds = %61
%66 = load i32, i32* %5
%67 = trunc i32 %66 to i8
%68 = load i8*, i8** %3
%69 = load i32, i32* %9
%70 = add nsw i32 %69, 1
store i32 %70, i32* %9
%71 = sext i32 %69 to i64
%72 = getelementptr inbounds i8, i8* %68, i64 %71
store i8 %67, i8* %72
%73 = load i8*, i8** %3
%74 = load i32, i32* %9
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %73, i64 %75
store i8 0, i8* %76
%77 = load i32, i32* %9
%78 = load i8*, i8** %4
%79 = load i32, i32* %8
%80 = call i32 @tl_showpassword(i32 %77, i8* %78, i32 %79)
br label %34
81: ; preds = %64
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32, i32* }
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@.str = [1 x i8] zeroinitializer
@TCSANOW = global i32 0
define void @tl_getpassword(i8* nocapture writeonly %0, i8* %1) {
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = bitcast %struct.termios* %3 to i8*
%6 = bitcast %struct.termios* %4 to i8*
store i8 0, i8* %0
%7 = load i32, i32* @STDIN_FILENO
%8 = call i32 @tcgetattr(i32 %7, %struct.termios* %3)
%9 = load i32, i32* @ICANON
%10 = load i32, i32* @ECHO
%11 = or i32 %10, %9
%12 = xor i32 %11, -1
%13 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%14 = load i32, i32* %13
%15 = and i32 %14, %12
store i32 %15, i32* %13
%16 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 1
%17 = load i32*, i32** %16
%18 = load i64, i64* @VMIN
%19 = getelementptr inbounds i32, i32* %17, i64 %18
store i32 1, i32* %19
%20 = load i64, i64* @VTIME
%21 = getelementptr inbounds i32, i32* %17, i64 %20
store i32 0, i32* %21
%22 = call i32 @tl_strlen(i8* %1)
%23 = call i32 @tl_showpassword(i32 0, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i32 0)
br label %24
24: ; preds = %39, %2
%25 = phi i32 [ %41, %39 ], [ 0, %2 ]
%26 = icmp sgt i32 %25, 0
br label %27
27: ; preds = %24, %35
%28 = load i32, i32* @STDIN_FILENO
%29 = load i32, i32* @TCSANOW
%30 = call i32 @tcsetattr(i32 %28, i32 %29, %struct.termios* %4)
%31 = call i32 (...) @getchar()
%32 = load i32, i32* @STDIN_FILENO
%33 = load i32, i32* @TCSANOW
%34 = call i32 @tcsetattr(i32 %32, i32 %33, %struct.termios* %3)
switch i32 %31, label %44 [
i32 127, label %35
i32 8, label %35
i32 10, label %50
]
35: ; preds = %27, %27
br i1 %26, label %36, label %27
36: ; preds = %35
%37 = add nsw i32 %25, -1
%38 = zext i32 %37 to i64
br label %39
39: ; preds = %44, %36
%40 = phi i64 [ %38, %36 ], [ %49, %44 ]
%41 = phi i32 [ %37, %36 ], [ %46, %44 ]
%42 = getelementptr inbounds i8, i8* %0, i64 %40
store i8 0, i8* %42
%43 = call i32 @tl_showpassword(i32 %41, i8* %1, i32 %22)
br label %24
44: ; preds = %27
%45 = trunc i32 %31 to i8
%46 = add nsw i32 %25, 1
%47 = sext i32 %25 to i64
%48 = getelementptr inbounds i8, i8* %0, i64 %47
store i8 %45, i8* %48
%49 = sext i32 %46 to i64
br label %39
50: ; preds = %27
ret void
}