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

/* Variables and functions */
int /*<<< orphan*/ ECHO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSAFLUSH;
char *fgets(char *, size_t, int /*<<< orphan*/);
int isatty(int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
char *strchr(char *, char);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int term_read_password(char *password, size_t max_len) {
  struct termios old_term;
  int disable_echo;
  char *ret;
  disable_echo = isatty(STDIN_FILENO);
  if (disable_echo) {
    struct termios term;
    tcgetattr(STDIN_FILENO, &old_term);
    term = old_term;
    term.c_lflag &= ~ECHO;
    tcsetattr(STDIN_FILENO, TCSAFLUSH, &term);
  }
  ret = fgets(password, max_len, stdin);
  if (disable_echo)
    tcsetattr(STDIN_FILENO, TCSAFLUSH, &old_term);
  if (ret == NULL)
    return -1;
  if ((ret = strchr(password, '\r')) != NULL)
    *ret = '\0';
  if ((ret = strchr(password, '\n')) != NULL)
    *ret = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<term_read_password>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
call   1030 <isatty@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11c0 <term_read_password+0x70>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1060 <tcgetattr@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
call   1050 <fgets@plt>
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11ff <term_read_password+0xaf>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmpq   $0x0,-0x30(%rbp)
jne    1216 <term_read_password+0xc6>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <term_read_password+0x113>
mov    -0x10(%rbp),%rdi
mov    $0xd,%esi
call   1040 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1239 <term_read_password+0xe9>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     125c <term_read_password+0x10c>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<term_read_password>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%r12
mov    0x2e5d(%rip),%r13
mov    0x0(%r13),%edi
call   1030 <isatty@plt>
test   %eax,%eax
je     1214 <term_read_password+0xc4>
mov    0x0(%r13),%edi
lea    0x10(%rsp),%r15
mov    %r15,%rsi
call   1060 <tcgetattr@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0x10(%rsp),%eax
mov    %eax,0x8(%rsp)
mov    0x0(%r13),%edi
mov    0x2e3d(%rip),%rbp
mov    0x0(%rbp),%esi
lea    0x8(%rsp),%rdx
call   1070 <tcsetattr@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    %r14,%rsi
call   1050 <fgets@plt>
mov    %rax,%rbx
mov    0x0(%r13),%edi
mov    0x0(%rbp),%esi
mov    %r15,%rdx
call   1070 <tcsetattr@plt>
test   %rbx,%rbx
je     1230 <term_read_password+0xe0>
mov    %r12,%rdi
mov    $0xd,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11f8 <term_read_password+0xa8>
movb   $0x0,(%rax)
mov    %r12,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     1235 <term_read_password+0xe5>
movb   $0x0,(%rcx)
jmp    1235 <term_read_password+0xe5>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    %r14,%rsi
call   1050 <fgets@plt>
mov    %rax,%rbx
test   %rbx,%rbx
jne    11e3 <term_read_password+0x93>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
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 i32 @term_read_password(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.termios
%7 = alloca i32
%8 = alloca i8*
%9 = alloca %struct.termios
store i8* %0, i8** %4
store i64 %1, i64* %5
%10 = load i32, i32* @STDIN_FILENO
%11 = call i32 @isatty(i32 %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %27
14: ; preds = %2
%15 = load i32, i32* @STDIN_FILENO
%16 = call i32 @tcgetattr(i32 %15, %struct.termios* %6)
%17 = bitcast %struct.termios* %9 to i8*
%18 = bitcast %struct.termios* %6 to i8*
%19 = load i32, i32* @ECHO
%20 = xor i32 %19, -1
%21 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 0
%22 = load i32, i32* %21
%23 = and i32 %22, %20
store i32 %23, i32* %21
%24 = load i32, i32* @STDIN_FILENO
%25 = load i32, i32* @TCSAFLUSH
%26 = call i32 @tcsetattr(i32 %24, i32 %25, %struct.termios* %9)
br label %27
27: ; preds = %14, %2
%28 = load i8*, i8** %4
%29 = load i64, i64* %5
%30 = load i32, i32* @stdin
%31 = call i8* @fgets(i8* %28, i64 %29, i32 %30)
store i8* %31, i8** %8
%32 = load i32, i32* %7
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %27
%35 = load i32, i32* @STDIN_FILENO
%36 = load i32, i32* @TCSAFLUSH
%37 = call i32 @tcsetattr(i32 %35, i32 %36, %struct.termios* %6)
br label %38
38: ; preds = %34, %27
%39 = load i8*, i8** %8
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %42
41: ; preds = %38
store i32 -1, i32* %3
br label %55
42: ; preds = %38
%43 = load i8*, i8** %4
%44 = call i8* @strchr(i8* %43, i8 signext 13)
store i8* %44, i8** %8
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = load i8*, i8** %8
store i8 0, i8* %47
br label %48
48: ; preds = %46, %42
%49 = load i8*, i8** %4
%50 = call i8* @strchr(i8* %49, i8 signext 10)
store i8* %50, i8** %8
%51 = icmp ne i8* %50, null
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = load i8*, i8** %8
store i8 0, i8* %53
br label %54
54: ; preds = %52, %48
store i32 0, i32* %3
br label %55
55: ; preds = %54, %41
%56 = load i32, i32* %3
ret i32 %56
}

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 i32 @term_read_password(i8* %0, i64 %1) {
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = bitcast %struct.termios* %3 to i8*
%6 = load i32, i32* @STDIN_FILENO
%7 = tail call i32 @isatty(i32 %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @stdin
%11 = tail call i8* @fgets(i8* %0, i64 %1, i32 %10)
br label %30
12: ; preds = %2
%13 = bitcast %struct.termios* %4 to i8*
%14 = load i32, i32* @STDIN_FILENO
%15 = call i32 @tcgetattr(i32 %14, %struct.termios* %3)
%16 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%17 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%18 = load i32, i32* %16
%19 = load i32, i32* @ECHO
%20 = xor i32 %19, -1
%21 = and i32 %18, %20
store i32 %21, i32* %17
%22 = load i32, i32* @STDIN_FILENO
%23 = load i32, i32* @TCSAFLUSH
%24 = call i32 @tcsetattr(i32 %22, i32 %23, %struct.termios* %4)
%25 = load i32, i32* @stdin
%26 = call i8* @fgets(i8* %0, i64 %1, i32 %25)
%27 = load i32, i32* @STDIN_FILENO
%28 = load i32, i32* @TCSAFLUSH
%29 = call i32 @tcsetattr(i32 %27, i32 %28, %struct.termios* %3)
br label %30
30: ; preds = %9, %12
%31 = phi i8* [ %11, %9 ], [ %26, %12 ]
%32 = icmp eq i8* %31, null
br i1 %32, label %41, label %33
33: ; preds = %30
%34 = call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 13)
%35 = icmp eq i8* %34, null
br i1 %35, label %37, label %36
36: ; preds = %33
store i8 0, i8* %34
br label %37
37: ; preds = %36, %33
%38 = call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 10)
%39 = icmp eq i8* %38, null
br i1 %39, label %41, label %40
40: ; preds = %37
store i8 0, i8* %38
br label %41
41: ; preds = %37, %40, %30
%42 = phi i32 [ -1, %30 ], [ 0, %40 ], [ 0, %37 ]
ret i32 %42
}