#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;
char EOF;
int ICANON;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
char 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++) {
    char c = getchar();
    if (c == '\n' || c == EOF)
      break;
    pass[i] = 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    122f <read_password+0xef>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %al,-0x2d(%rbp)
movsbl -0x2d(%rbp),%eax
cmp    $0xa,%eax
je     120e <read_password+0xce>
movsbl -0x2d(%rbp),%eax
mov    0x2dd5(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1213 <read_password+0xd3>
jmp    122f <read_password+0xef>
mov    -0x2d(%rbp),%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     1260 <read_password+0x120>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <tcsetattr@plt>
lea    0xd9c(%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     11c4 <read_password+0x74>
test   %r13,%r13
je     11fc <read_password+0xac>
mov    0x2e3a(%rip),%rbp
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xa,%al
je     11c0 <read_password+0x70>
cmp    0x0(%rbp),%al
je     11c0 <read_password+0x70>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r13
jne    11a0 <read_password+0x50>
mov    %r13,%rbx
mov    %ebx,%eax
jmp    11fe <read_password+0xae>
mov    0x2df5(%rip),%rax
mov    0x2e0e(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    0x8(%rsp),%ecx
mov    %ecx,0x10(%rsp)
mov    (%r12),%edi
mov    0x2dfd(%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    121f <read_password+0xcf>
mov    (%r12),%edi
mov    0x2dd5(%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 i8 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 i8
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 = %54, %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 %57
36: ; preds = %31
%37 = call signext i8 (...) @getchar()
store i8 %37, i8* %11
%38 = load i8, i8* %11
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 10
br i1 %40, label %47, label %41
41: ; preds = %36
%42 = load i8, i8* %11
%43 = sext i8 %42 to i32
%44 = load i8, i8* @EOF
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %43, %45
br i1 %46, label %47, label %48
47: ; preds = %41, %36
br label %57
48: ; preds = %41
%49 = load i8, i8* %11
%50 = load i8*, i8** %5
%51 = load i32, i32* %10
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
store i8 %49, i8* %53
br label %54
54: ; preds = %48
%55 = load i32, i32* %10
%56 = add nsw i32 %55, 1
store i32 %56, i32* %10
br label %31
57: ; preds = %47, %31
%58 = load i8*, i8** %5
%59 = load i32, i32* %10
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %58, i64 %60
store i8 0, i8* %61
%62 = load i32, i32* %7
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %68
64: ; preds = %57
%65 = load i32, i32* @STDIN_FILENO
%66 = load i32, i32* @TCSANOW
%67 = call i32 @tcsetattr(i32 %65, i32 %66, %struct.termios* %8)
br label %68
68: ; preds = %64, %57
%69 = 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 i8 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 %40, label %26
26: ; preds = %24, %33
%27 = phi i64 [ %35, %33 ], [ 0, %24 ]
%28 = call signext i8 (...) @getchar()
%29 = icmp eq i8 %28, 10
%30 = load i8, i8* @EOF
%31 = icmp eq i8 %28, %30
%32 = select i1 %29, i1 true, i1 %31
br i1 %32, label %37, label %33
33: ; preds = %26
%34 = getelementptr inbounds i8, i8* %1, i64 %27
store i8 %28, i8* %34
%35 = add nuw i64 %27, 1
%36 = icmp eq i64 %35, %2
br i1 %36, label %37, label %26
37: ; preds = %26, %33
%38 = phi i64 [ %2, %33 ], [ %27, %26 ]
%39 = and i64 %38, 4294967295
br label %40
40: ; preds = %37, %24
%41 = phi i64 [ 0, %24 ], [ %39, %37 ]
%42 = getelementptr inbounds i8, i8* %1, i64 %41
store i8 0, i8* %42
br i1 %11, label %43, label %47
43: ; preds = %40
%44 = load i32, i32* @STDIN_FILENO
%45 = load i32, i32* @TCSANOW
%46 = call i32 @tcsetattr(i32 %44, i32 %45, %struct.termios* %4)
br label %47
47: ; preds = %43, %40
%48 = call i32 @putchar(i32 10)
ret void
}