#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*/ ECHONL;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
char *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdin;
int strlen(char *);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
scalar_t__ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
void cli_get_passwd(char password[], int max_length) {
  struct termios oflags, nflags;
  tcgetattr(fileno(stdin), &oflags);
  nflags = oflags;
  nflags.c_lflag &= ~ECHO;
  nflags.c_lflag |= ECHONL;
  if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0)
    err(EXIT_FAILURE, "can set terminal attributes");
  printf("Enter password: ");
  char *result = fgets(password, max_length, stdin);
  if (result == NULL)
    err(EXIT_FAILURE, "could not read password from stdin");
  password[strlen(password) - 1] = '\0';
  if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0)
    err(EXIT_FAILURE, "can restore terminal attributes");
}


x86 assembly (clang, -O0):
<cli_get_passwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,%edi
lea    -0x10(%rbp),%rsi
call   1080 <tcgetattr@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1090 <tcsetattr@plt>
cmp    $0x0,%rax
je     1200 <cli_get_passwd+0x90>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xe05(%rip),%rsi
call   1070 <err@plt>
lea    0xe15(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    0x2dce(%rip),%rax
mov    (%rax),%edx
call   1050 <fgets@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1245 <cli_get_passwd+0xd5>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
call   1070 <err@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    0x2d63(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1090 <tcsetattr@plt>
cmp    $0x0,%rax
je     12a7 <cli_get_passwd+0x137>
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
lea    0xdae(%rip),%rsi
call   1070 <err@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cli_get_passwd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
mov    0x2e62(%rip),%r14
mov    (%r14),%edi
call   1060 <fileno@plt>
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1080 <tcgetattr@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0x8(%rsp),%eax
mov    0x2e18(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x10(%rsp)
mov    (%r14),%edi
call   1060 <fileno@plt>
mov    0x2e1b(%rip),%r15
mov    (%r15),%esi
lea    0x10(%rsp),%rdx
mov    %eax,%edi
call   1090 <tcsetattr@plt>
test   %rax,%rax
je     11ee <cli_get_passwd+0x7e>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
lea    0xe17(%rip),%rsi
call   1070 <err@plt>
lea    0xe27(%rip),%rdi
call   1040 <printf@plt>
mov    (%r14),%edx
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <fgets@plt>
test   %rax,%rax
jne    1221 <cli_get_passwd+0xb1>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
call   1070 <err@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    (%r14),%edi
call   1060 <fileno@plt>
mov    (%r15),%esi
lea    0x8(%rsp),%rdx
mov    %eax,%edi
call   1090 <tcsetattr@plt>
test   %rax,%rax
je     1261 <cli_get_passwd+0xf1>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
call   1070 <err@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@stdin = global i32 0
@ECHO = global i32 0
@ECHONL = global i32 0
@TCSANOW = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [28 x i8] c"can set terminal attributes\00"
@.str.1 = [17 x i8] c"Enter password: \00"
@.str.2 = [35 x i8] c"could not read password from stdin\00"
@.str.3 = [32 x i8] c"can restore terminal attributes\00"
define void @cli_get_passwd(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.termios
%6 = alloca %struct.termios
%7 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i32, i32* @stdin
%9 = call i32 @fileno(i32 %8)
%10 = call i32 @tcgetattr(i32 %9, %struct.termios* %5)
%11 = bitcast %struct.termios* %6 to i8*
%12 = bitcast %struct.termios* %5 to i8*
%13 = load i32, i32* @ECHO
%14 = xor i32 %13, -1
%15 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%16 = load i32, i32* %15
%17 = and i32 %16, %14
store i32 %17, i32* %15
%18 = load i32, i32* @ECHONL
%19 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%20 = load i32, i32* %19
%21 = or i32 %20, %18
store i32 %21, i32* %19
%22 = load i32, i32* @stdin
%23 = call i32 @fileno(i32 %22)
%24 = load i32, i32* @TCSANOW
%25 = call i64 @tcsetattr(i32 %23, i32 %24, %struct.termios* %6)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %2
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @err(i32 %28, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %27, %2
%31 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%32 = load i8*, i8** %3
%33 = load i32, i32* %4
%34 = load i32, i32* @stdin
%35 = call i8* @fgets(i8* %32, i32 %33, i32 %34)
store i8* %35, i8** %7
%36 = load i8*, i8** %7
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %41
38: ; preds = %30
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @err(i32 %39, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %38, %30
%42 = load i8*, i8** %3
%43 = load i8*, i8** %3
%44 = call i32 @strlen(i8* %43)
%45 = sub nsw i32 %44, 1
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %42, i64 %46
store i8 0, i8* %47
%48 = load i32, i32* @stdin
%49 = call i32 @fileno(i32 %48)
%50 = load i32, i32* @TCSANOW
%51 = call i64 @tcsetattr(i32 %49, i32 %50, %struct.termios* %5)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %41
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @err(i32 %54, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
br label %56
56: ; preds = %53, %41
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@stdin = global i32 0
@ECHO = global i32 0
@ECHONL = global i32 0
@TCSANOW = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [28 x i8] c"can set terminal attributes\00"
@.str.1 = [17 x i8] c"Enter password: \00"
@.str.2 = [35 x i8] c"could not read password from stdin\00"
@.str.3 = [32 x i8] c"can restore terminal attributes\00"
define void @cli_get_passwd(i8* %0, i32 %1) {
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = bitcast %struct.termios* %3 to i8*
%6 = bitcast %struct.termios* %4 to i8*
%7 = load i32, i32* @stdin
%8 = tail call i32 @fileno(i32 %7)
%9 = call i32 @tcgetattr(i32 %8, %struct.termios* %3)
%10 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%11 = getelementptr inbounds %struct.termios, %struct.termios* %4, 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 = load i32, i32* @stdin
%19 = call i32 @fileno(i32 %18)
%20 = load i32, i32* @TCSANOW
%21 = call i64 @tcsetattr(i32 %19, i32 %20, %struct.termios* %4)
%22 = icmp eq i64 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %2
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @err(i32 %24, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %23, %2
%27 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* @stdin
%29 = call i8* @fgets(i8* %0, i32 %1, i32 %28)
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @err(i32 %32, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %31, %26
%35 = call i32 @strlen(i8* %0)
%36 = add nsw i32 %35, -1
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %0, i64 %37
store i8 0, i8* %38
%39 = load i32, i32* @stdin
%40 = call i32 @fileno(i32 %39)
%41 = load i32, i32* @TCSANOW
%42 = call i64 @tcsetattr(i32 %40, i32 %41, %struct.termios* %3)
%43 = icmp eq i64 %42, 0
br i1 %43, label %47, label %44
44: ; preds = %34
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @err(i32 %45, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
br label %47
47: ; preds = %44, %34
ret void
}