#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ ECHO;
int /*<<< orphan*/ ECHONL;
int /*<<< orphan*/ TCSANOW;
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ plib_sdie(char *);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
scalar_t__ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
char *plib_fgetpass(char *s, int size, FILE *stream) {
  struct termios old_flags, new_flags;
  tcgetattr(fileno(stream), &old_flags);
  new_flags = old_flags;
  new_flags.c_lflag &= ~ECHO;
  new_flags.c_lflag |= ECHONL;
  if (tcsetattr(fileno(stream), TCSANOW, &new_flags) != 0)
    plib_sdie("terminal set attribute:");
  s = fgets(s, size, stream);
  if (tcsetattr(fileno(stream), TCSANOW, &old_flags) != 0)
    plib_sdie("terminal set attribute:");
  return s;
}


x86 assembly (clang, -O0):
<plib_fgetpass>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,%edi
lea    -0x20(%rbp),%rsi
call   1060 <tcgetattr@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmp    $0x0,%rax
je     11d1 <plib_fgetpass+0x81>
lea    0xe34(%rip),%rdi
call   1030 <plib_sdie@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1040 <fgets@plt>
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmp    $0x0,%rax
je     1218 <plib_fgetpass+0xc8>
lea    0xded(%rip),%rdi
call   1030 <plib_sdie@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<plib_fgetpass>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %rdi,%rbp
mov    %rdx,%rdi
call   1050 <fileno@plt>
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1060 <tcgetattr@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0x8(%rsp),%eax
mov    0x2e43(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x10(%rsp)
mov    %rbx,%rdi
call   1050 <fileno@plt>
mov    0x2e46(%rip),%r15
mov    (%r15),%esi
lea    0x10(%rsp),%rdx
mov    %eax,%edi
call   1070 <tcsetattr@plt>
test   %rax,%rax
je     11c2 <plib_fgetpass+0x72>
lea    0xe43(%rip),%rdi
call   1030 <plib_sdie@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1050 <fileno@plt>
mov    (%r15),%esi
lea    0x8(%rsp),%rdx
mov    %eax,%edi
call   1070 <tcsetattr@plt>
test   %rax,%rax
je     11fb <plib_fgetpass+0xab>
lea    0xe0a(%rip),%rdi
call   1030 <plib_sdie@plt>
mov    %rbp,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@ECHO = global i32 0
@ECHONL = global i32 0
@TCSANOW = global i32 0
@.str = [24 x i8] c"terminal set attribute:\00"
define i8* @plib_fgetpass(i8* %0, i32 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca %struct.termios
%8 = alloca %struct.termios
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%9 = load i32*, i32** %6
%10 = call i32 @fileno(i32* %9)
%11 = call i32 @tcgetattr(i32 %10, %struct.termios* %7)
%12 = bitcast %struct.termios* %8 to i8*
%13 = bitcast %struct.termios* %7 to i8*
%14 = load i32, i32* @ECHO
%15 = xor i32 %14, -1
%16 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%17 = load i32, i32* %16
%18 = and i32 %17, %15
store i32 %18, i32* %16
%19 = load i32, i32* @ECHONL
%20 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%21 = load i32, i32* %20
%22 = or i32 %21, %19
store i32 %22, i32* %20
%23 = load i32*, i32** %6
%24 = call i32 @fileno(i32* %23)
%25 = load i32, i32* @TCSANOW
%26 = call i64 @tcsetattr(i32 %24, i32 %25, %struct.termios* %8)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %3
%29 = call i32 @plib_sdie(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %28, %3
%31 = load i8*, i8** %4
%32 = load i32, i32* %5
%33 = load i32*, i32** %6
%34 = call i8* @fgets(i8* %31, i32 %32, i32* %33)
store i8* %34, i8** %4
%35 = load i32*, i32** %6
%36 = call i32 @fileno(i32* %35)
%37 = load i32, i32* @TCSANOW
%38 = call i64 @tcsetattr(i32 %36, i32 %37, %struct.termios* %7)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %30
%41 = call i32 @plib_sdie(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %42
42: ; preds = %40, %30
%43 = load i8*, i8** %4
ret i8* %43
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@ECHO = global i32 0
@ECHONL = global i32 0
@TCSANOW = global i32 0
@.str = [24 x i8] c"terminal set attribute:\00"
define i8* @plib_fgetpass(i8* %0, i32 %1, i32* nocapture %2) {
%4 = alloca %struct.termios
%5 = alloca %struct.termios
%6 = bitcast %struct.termios* %4 to i8*
%7 = bitcast %struct.termios* %5 to i8*
%8 = tail call i32 @fileno(i32* %2)
%9 = call i32 @tcgetattr(i32 %8, %struct.termios* %4)
%10 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%11 = getelementptr inbounds %struct.termios, %struct.termios* %5, i64 0, i32 0
%12 = load i32, i32* %10
%13 = load i32, i32* @ECHO
%14 = xor i32 %13, -1
%15 = and i32 %12, %14
%16 = load i32, i32* @ECHONL
%17 = or i32 %16, %15
store i32 %17, i32* %11
%18 = call i32 @fileno(i32* %2)
%19 = load i32, i32* @TCSANOW
%20 = call i64 @tcsetattr(i32 %18, i32 %19, %struct.termios* %5)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %3
%23 = call i32 @plib_sdie(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %3
%25 = call i8* @fgets(i8* %0, i32 %1, i32* %2)
%26 = call i32 @fileno(i32* %2)
%27 = load i32, i32* @TCSANOW
%28 = call i64 @tcsetattr(i32 %26, i32 %27, %struct.termios* %4)
%29 = icmp eq i64 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %24
%31 = call i32 @plib_sdie(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %32
32: ; preds = %30, %24
ret i8* %25
}