#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 EOF;
int ICANON;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
int getchar();
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
void read_password(const char *prompt, char *pass, size_t len) {
  int masked = 0;
  struct termios oldt, newt;
  int i;
  printf("%s", prompt);
  if (tcgetattr(STDIN_FILENO, &oldt) == 0) {
    newt = oldt;
    newt.c_lflag &= ~(ICANON | ECHO);
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);
    masked = 1;
  }
  for (i = 0; i < len; i++) {
    int c = getchar();
    if (c == '\n' || c == EOF)
      break;
    pass[i] = (char)c;
  }
  pass[i] = '\0';
  if (masked) {
    tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  }
  printf("\n");
}


x86 assembly (clang, -O0):
<read_password>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0x0,%rax
jne    11cc <read_password+0x8c>
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    0x2e41(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <tcsetattr@plt>
movl   $0x1,-0x1c(%rbp)
movl   $0x0,-0x2c(%rbp)
movslq -0x2c(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    122a <read_password+0xea>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xa,-0x30(%rbp)
je     1207 <read_password+0xc7>
mov    -0x30(%rbp),%eax
mov    0x2dd9(%rip),%rcx
cmp    (%rcx),%eax
jne    120c <read_password+0xcc>
jmp    122a <read_password+0xea>
mov    -0x30(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11d3 <read_password+0x93>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0x0,-0x1c(%rbp)
je     125b <read_password+0x11b>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <tcsetattr@plt>
lea    0xda1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_password>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r13
mov    %rsi,%r15
mov    %rdi,%rsi
lea    0xe92(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e4c(%rip),%r12
mov    (%r12),%edi
lea    0x8(%rsp),%rsi
call   1060 <tcgetattr@plt>
mov    %rax,%r14
test   %rax,%rax
je     11c5 <read_password+0x75>
test   %r13,%r13
je     11fd <read_password+0xad>
mov    0x2e3a(%rip),%rbp
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xa,%eax
je     11c1 <read_password+0x71>
cmp    0x0(%rbp),%eax
je     11c1 <read_password+0x71>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r13
jne    11a0 <read_password+0x50>
mov    %r13,%rbx
mov    %ebx,%eax
jmp    11ff <read_password+0xaf>
mov    0x2df4(%rip),%rax
mov    0x2e0d(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    0x8(%rsp),%ecx
mov    %ecx,0x10(%rsp)
mov    (%r12),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
call   1070 <tcsetattr@plt>
test   %r13,%r13
jne    1197 <read_password+0x47>
xor    %eax,%eax
movb   $0x0,(%r15,%rax,1)
test   %r14,%r14
jne    1220 <read_password+0xd0>
mov    (%r12),%edi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1070 <tcsetattr@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [3 x i8] c"%s\00"
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
@EOF = global i32 0
@.str.1 = [2 x i8] c"\0A\00"
define void @read_password(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca %struct.termios
%9 = alloca %struct.termios
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i32 0, i32* %7
%12 = load i8*, i8** %4
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = load i32, i32* @STDIN_FILENO
%15 = call i64 @tcgetattr(i32 %14, %struct.termios* %8)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %30
17: ; preds = %3
%18 = bitcast %struct.termios* %9 to i8*
%19 = bitcast %struct.termios* %8 to i8*
%20 = load i32, i32* @ICANON
%21 = load i32, i32* @ECHO
%22 = or i32 %20, %21
%23 = xor i32 %22, -1
%24 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 0
%25 = load i32, i32* %24
%26 = and i32 %25, %23
store i32 %26, i32* %24
%27 = load i32, i32* @STDIN_FILENO
%28 = load i32, i32* @TCSANOW
%29 = call i32 @tcsetattr(i32 %27, i32 %28, %struct.termios* %9)
store i32 1, i32* %7
br label %30
30: ; preds = %17, %3
store i32 0, i32* %10
br label %31
31: ; preds = %52, %30
%32 = load i32, i32* %10
%33 = sext i32 %32 to i64
%34 = load i64, i64* %6
%35 = icmp ult i64 %33, %34
br i1 %35, label %36, label %55
36: ; preds = %31
%37 = call i32 (...) @getchar()
store i32 %37, i32* %11
%38 = load i32, i32* %11
%39 = icmp eq i32 %38, 10
br i1 %39, label %44, label %40
40: ; preds = %36
%41 = load i32, i32* %11
%42 = load i32, i32* @EOF
%43 = icmp eq i32 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %40, %36
br label %55
45: ; preds = %40
%46 = load i32, i32* %11
%47 = trunc i32 %46 to i8
%48 = load i8*, i8** %5
%49 = load i32, i32* %10
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
store i8 %47, i8* %51
br label %52
52: ; preds = %45
%53 = load i32, i32* %10
%54 = add nsw i32 %53, 1
store i32 %54, i32* %10
br label %31
55: ; preds = %44, %31
%56 = load i8*, i8** %5
%57 = load i32, i32* %10
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %56, i64 %58
store i8 0, i8* %59
%60 = load i32, i32* %7
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %66
62: ; preds = %55
%63 = load i32, i32* @STDIN_FILENO
%64 = load i32, i32* @TCSANOW
%65 = call i32 @tcsetattr(i32 %63, i32 %64, %struct.termios* %8)
br label %66
66: ; preds = %62, %55
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [3 x i8] c"%s\00"
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
@EOF = global i32 0
define void @read_password(i8* %0, i8* nocapture writeonly %1, i64 %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 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = load i32, i32* @STDIN_FILENO
%10 = call i64 @tcgetattr(i32 %9, %struct.termios* %4)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %24
12: ; preds = %3
%13 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%14 = getelementptr inbounds %struct.termios, %struct.termios* %5, i64 0, i32 0
%15 = load i32, i32* %13
%16 = load i32, i32* @ICANON
%17 = load i32, i32* @ECHO
%18 = or i32 %17, %16
%19 = xor i32 %18, -1
%20 = and i32 %15, %19
store i32 %20, i32* %14
%21 = load i32, i32* @STDIN_FILENO
%22 = load i32, i32* @TCSANOW
%23 = call i32 @tcsetattr(i32 %21, i32 %22, %struct.termios* %5)
br label %24
24: ; preds = %12, %3
%25 = icmp eq i64 %2, 0
br i1 %25, label %41, label %26
26: ; preds = %24, %33
%27 = phi i64 [ %36, %33 ], [ 0, %24 ]
%28 = call i32 (...) @getchar()
%29 = icmp eq i32 %28, 10
%30 = load i32, i32* @EOF
%31 = icmp eq i32 %28, %30
%32 = select i1 %29, i1 true, i1 %31
br i1 %32, label %38, label %33
33: ; preds = %26
%34 = trunc i32 %28 to i8
%35 = getelementptr inbounds i8, i8* %1, i64 %27
store i8 %34, i8* %35
%36 = add nuw i64 %27, 1
%37 = icmp eq i64 %36, %2
br i1 %37, label %38, label %26
38: ; preds = %26, %33
%39 = phi i64 [ %2, %33 ], [ %27, %26 ]
%40 = and i64 %39, 4294967295
br label %41
41: ; preds = %38, %24
%42 = phi i64 [ 0, %24 ], [ %40, %38 ]
%43 = getelementptr inbounds i8, i8* %1, i64 %42
store i8 0, i8* %43
br i1 %11, label %44, label %48
44: ; preds = %41
%45 = load i32, i32* @STDIN_FILENO
%46 = load i32, i32* @TCSANOW
%47 = call i32 @tcsetattr(i32 %45, i32 %46, %struct.termios* %4)
br label %48
48: ; preds = %44, %41
%49 = call i32 @putchar(i32 10)
ret void
}