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

/* Variables and functions */
int ECHO;
int ECHONL;
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ isatty(int);
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int ast_hide_password(int fd) {
  struct termios tios;
  int res;
  int old;
  if (!isatty(fd))
    return -1;
  res = tcgetattr(fd, &tios);
  if (res < 0)
    return -1;
  old = tios.c_lflag & (ECHO | ECHONL);
  tios.c_lflag &= ~ECHO;
  tios.c_lflag |= ECHONL;
  res = tcsetattr(fd, TCSAFLUSH, &tios);
  if (res < 0)
    return -1;
  return old;
}


x86 assembly (clang, -O0):
<ast_hide_password>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <isatty@plt>
cmp    $0x0,%eax
jne    1158 <ast_hide_password+0x28>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ec <ast_hide_password+0xbc>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1040 <tcgetattr@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    117d <ast_hide_password+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ec <ast_hide_password+0xbc>
mov    -0x10(%rbp),%eax
mov    0x2e59(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e40(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1050 <tcsetattr@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e6 <ast_hide_password+0xb6>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ec <ast_hide_password+0xbc>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ast_hide_password>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
call   1030 <isatty@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
je     11a5 <ast_hide_password+0x75>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
call   1040 <tcgetattr@plt>
test   %eax,%eax
js     11a5 <ast_hide_password+0x75>
mov    0x8(%rsp),%r14d
mov    0x2e77(%rip),%rax
mov    (%rax),%r12d
mov    0x2e5d(%rip),%rax
mov    (%rax),%ebx
mov    %r12d,%eax
not    %eax
and    %r14d,%eax
or     %ebx,%eax
mov    %eax,0x8(%rsp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1050 <tcsetattr@plt>
test   %eax,%eax
js     11a5 <ast_hide_password+0x75>
or     %r12d,%ebx
and    %r14d,%ebx
mov    %ebx,%r15d
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.termios = type { i32 }
@ECHO = global i32 0
@ECHONL = global i32 0
@TCSAFLUSH = global i32 0
define i32 @ast_hide_password(i32 %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = tail call i32 @isatty(i32 %0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %23, label %6
6: ; preds = %1
%7 = call i32 @tcgetattr(i32 %0, %struct.termios* %2)
%8 = icmp slt i32 %7, 0
br i1 %8, label %23, label %9
9: ; preds = %6
%10 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%11 = load i32, i32* %10
%12 = load i32, i32* @ECHO
%13 = load i32, i32* @ECHONL
%14 = xor i32 %12, -1
%15 = and i32 %11, %14
%16 = or i32 %15, %13
store i32 %16, i32* %10
%17 = load i32, i32* @TCSAFLUSH
%18 = call i32 @tcsetattr(i32 %0, i32 %17, %struct.termios* %2)
%19 = icmp slt i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %9
%21 = or i32 %13, %12
%22 = and i32 %21, %11
br label %23
23: ; preds = %9, %6, %1, %20
%24 = phi i32 [ %22, %20 ], [ -1, %1 ], [ -1, %6 ], [ -1, %9 ]
ret i32 %24
}