#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*/ CODESET;
int /*<<< orphan*/ ECHO;
int /*<<< orphan*/ ICANON;
int /*<<< orphan*/ TCSANOW;
int lpcli_readpassword_nc(char *, size_t);
int lpcli_readpassword_u8(char *, size_t);
char *nl_langinfo(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int lpcli_readpassword(const char *prompt, char *out, size_t outl) {
  printf("%s", prompt);
  static struct termios told, tnew;
  tcgetattr(0, &told);
  tnew = told;
  tnew.c_lflag &= ~ICANON;
  tnew.c_lflag &= ~ECHO;
  tcsetattr(0, TCSANOW, &tnew);
  int ret;
  char *codeset = nl_langinfo(CODESET);
  if (strcmp(codeset, "UTF-8") == 0) {
    ret = lpcli_readpassword_nc(out, outl);
  } else {
    ret = lpcli_readpassword_u8(out, outl);
  }
  tcsetattr(0, TCSANOW, &told);
  printf("\n");
  return ret;
}


x86 assembly (clang, -O0):
<lpcli_readpassword>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
lea    0x2ebd(%rip),%rsi
call   1070 <tcgetattr@plt>
mov    0x2eb2(%rip),%eax
mov    %eax,0x2eb0(%rip)
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    0x2e9e(%rip),%eax
mov    %eax,0x2e98(%rip)
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    0x2e86(%rip),%eax
mov    %eax,0x2e80(%rip)
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0x2e6e(%rip),%rdx
call   1080 <tcsetattr@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   1060 <nl_langinfo@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xdef(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1238 <lpcli_readpassword+0xc8>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <lpcli_readpassword_nc@plt>
mov    %eax,-0x1c(%rbp)
jmp    1248 <lpcli_readpassword+0xd8>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <lpcli_readpassword_u8@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0x2e02(%rip),%rdx
call   1080 <tcsetattr@plt>
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lpcli_readpassword>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%rsi
lea    0xe6b(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x2ec1(%rip),%rsi
xor    %edi,%edi
call   1080 <tcgetattr@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    0x2e26(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    0x2ea0(%rip),%eax
mov    %eax,0x2e9e(%rip)
lea    0x2e97(%rip),%rdx
mov    0x2e10(%rip),%r15
mov    (%r15),%esi
xor    %edi,%edi
call   1090 <tcsetattr@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   1070 <nl_langinfo@plt>
lea    0xe0c(%rip),%rsi
mov    %rax,%rdi
call   1060 <strcmp@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
test   %rax,%rax
je     1211 <lpcli_readpassword+0x91>
call   10a0 <lpcli_readpassword_u8@plt>
jmp    1216 <lpcli_readpassword+0x96>
call   1050 <lpcli_readpassword_nc@plt>
mov    %eax,%ebx
mov    (%r15),%esi
lea    0x2e42(%rip),%rdx
xor    %edi,%edi
call   1090 <tcsetattr@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [3 x i8] c"%s\00"
@lpcli_readpassword.told = internal global %struct.termios zeroinitializer
@lpcli_readpassword.tnew = internal global %struct.termios zeroinitializer
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
@CODESET = global i32 0
@.str.1 = [6 x i8] c"UTF-8\00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @lpcli_readpassword(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i8*, i8** %4
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = call i32 @tcgetattr(i32 0, %struct.termios* @lpcli_readpassword.told)
%12 = load i32, i32* @ICANON
%13 = xor i32 %12, -1
%14 = load i32, i32* getelementptr inbounds (%struct.termios, %struct.termios* @lpcli_readpassword.tnew, i32 0, i32 0)
%15 = and i32 %14, %13
store i32 %15, i32* getelementptr inbounds (%struct.termios, %struct.termios* @lpcli_readpassword.tnew, i32 0, i32 0)
%16 = load i32, i32* @ECHO
%17 = xor i32 %16, -1
%18 = load i32, i32* getelementptr inbounds (%struct.termios, %struct.termios* @lpcli_readpassword.tnew, i32 0, i32 0)
%19 = and i32 %18, %17
store i32 %19, i32* getelementptr inbounds (%struct.termios, %struct.termios* @lpcli_readpassword.tnew, i32 0, i32 0)
%20 = load i32, i32* @TCSANOW
%21 = call i32 @tcsetattr(i32 0, i32 %20, %struct.termios* @lpcli_readpassword.tnew)
%22 = load i32, i32* @CODESET
%23 = call i8* @nl_langinfo(i32 %22)
store i8* %23, i8** %8
%24 = load i8*, i8** %8
%25 = call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %3
%28 = load i8*, i8** %5
%29 = load i64, i64* %6
%30 = call i32 @lpcli_readpassword_nc(i8* %28, i64 %29)
store i32 %30, i32* %7
br label %35
31: ; preds = %3
%32 = load i8*, i8** %5
%33 = load i64, i64* %6
%34 = call i32 @lpcli_readpassword_u8(i8* %32, i64 %33)
store i32 %34, i32* %7
br label %35
35: ; preds = %31, %27
%36 = load i32, i32* @TCSANOW
%37 = call i32 @tcsetattr(i32 0, i32 %36, %struct.termios* @lpcli_readpassword.told)
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%39 = load i32, i32* %7
ret i32 %39
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [3 x i8] c"%s\00"
@lpcli_readpassword.told = internal global %struct.termios zeroinitializer
@lpcli_readpassword.tnew = internal global %struct.termios zeroinitializer
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
@CODESET = global i32 0
@.str.1 = [6 x i8] c"UTF-8\00"
define i32 @lpcli_readpassword(i8* %0, i8* %1, i64 %2) {
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = tail call i32 @tcgetattr(i32 0, %struct.termios* @lpcli_readpassword.told)
%6 = load i32, i32* getelementptr inbounds (%struct.termios, %struct.termios* @lpcli_readpassword.told, i64 0, i32 0)
%7 = load i32, i32* @ICANON
%8 = load i32, i32* @ECHO
%9 = or i32 %7, %8
%10 = xor i32 %9, -1
%11 = and i32 %6, %10
store i32 %11, i32* getelementptr inbounds (%struct.termios, %struct.termios* @lpcli_readpassword.tnew, i64 0, i32 0)
%12 = load i32, i32* @TCSANOW
%13 = tail call i32 @tcsetattr(i32 0, i32 %12, %struct.termios* @lpcli_readpassword.tnew)
%14 = load i32, i32* @CODESET
%15 = tail call i8* @nl_langinfo(i32 %14)
%16 = tail call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = tail call i32 @lpcli_readpassword_nc(i8* %1, i64 %2)
br label %22
20: ; preds = %3
%21 = tail call i32 @lpcli_readpassword_u8(i8* %1, i64 %2)
br label %22
22: ; preds = %20, %18
%23 = phi i32 [ %19, %18 ], [ %21, %20 ]
%24 = load i32, i32* @TCSANOW
%25 = tail call i32 @tcsetattr(i32 0, i32 %24, %struct.termios* @lpcli_readpassword.told)
%26 = tail call i32 @putchar(i32 10)
ret i32 %23
}