#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;
  int *c_cc;
  int /*<<< orphan*/ c_iflag;
};

/* Variables and functions */
int ECHO;
int ECHOE;
int ECHOK;
int ECHONL;
int EOF;
int ICANON;
int /*<<< orphan*/ ISTRIP;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int fgetc(int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
void getpassword(char *prompt, char *buf, int maxlen) {
  struct termios term, saved_term;
  int c, n;
  if (buf == NULL || maxlen <= 1)
    return;
  tcgetattr(0, &term);
  tcgetattr(0, &saved_term);
  term.c_iflag |= ISTRIP;
  term.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL);
  term.c_cc[VMIN] = 1;
  term.c_cc[VTIME] = 0;
  tcsetattr(0, TCSANOW, &term);
  if (prompt != NULL) {
    printf("%s", prompt);
    fflush(stdout);
  }
  memset(buf, 0, maxlen);
  n = 0;
  while ((c = fgetc(stdin)) != EOF) {
    if (c == 0x7f || c == '\b') {
      if (n > 0) {
        buf[--n] = 0;
        printf("\b \b");
        fflush(stdout);
      }
      continue;
    }
    if (c == '\n' || c == '\r')
      break;
    if (n < maxlen) {
      buf[n++] = c;
      printf("*");
      fflush(stdout);
    }
  }
  buf[n] = 0;
  buf[maxlen - 1] = 0;
  tcsetattr(0, TCSANOW, &saved_term);
  printf("\n");
}


x86 assembly (clang, -O0):
<getpassword>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1188 <getpassword+0x28>
cmpl   $0x1,-0x14(%rbp)
jg     118d <getpassword+0x2d>
jmp    1378 <getpassword+0x218>
xor    %edi,%edi
lea    -0x30(%rbp),%rsi
call   1070 <tcgetattr@plt>
xor    %edi,%edi
lea    -0x48(%rbp),%rsi
call   1070 <tcgetattr@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    0x2e16(%rip),%rcx
or     (%rcx),%eax
mov    0x2e05(%rip),%rcx
or     (%rcx),%eax
mov    0x2ddc(%rip),%rcx
or     (%rcx),%eax
mov    0x2dc3(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2dcd(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x28(%rbp),%rax
mov    0x2d88(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x30(%rbp),%rdx
call   1080 <tcsetattr@plt>
cmpq   $0x0,-0x8(%rbp)
je     1251 <getpassword+0xf1>
mov    -0x8(%rbp),%rsi
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
movl   $0x0,-0x50(%rbp)
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   1050 <fgetc@plt>
mov    %eax,-0x4c(%rbp)
mov    0x2d4a(%rip),%rcx
cmp    (%rcx),%eax
je     1339 <getpassword+0x1d9>
cmpl   $0x7f,-0x4c(%rbp)
je     129a <getpassword+0x13a>
cmpl   $0x8,-0x4c(%rbp)
jne    12d9 <getpassword+0x179>
cmpl   $0x0,-0x50(%rbp)
jle    12d4 <getpassword+0x174>
mov    -0x10(%rbp),%rax
mov    -0x50(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x50(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
lea    0xd44(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
jmp    1266 <getpassword+0x106>
cmpl   $0xa,-0x4c(%rbp)
je     12ed <getpassword+0x18d>
cmpl   $0xd,-0x4c(%rbp)
jne    12f2 <getpassword+0x192>
jmp    1339 <getpassword+0x1d9>
mov    -0x50(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1334 <getpassword+0x1d4>
mov    -0x4c(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x50(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x50(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
lea    0xce8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2c6b(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
jmp    1266 <getpassword+0x106>
mov    -0x10(%rbp),%rax
movslq -0x50(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2c83(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x48(%rbp),%rdx
call   1080 <tcsetattr@plt>
lea    0xc98(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getpassword>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
test   %rsi,%rsi
je     12af <getpassword+0x13f>
mov    %edx,%r14d
cmp    $0x2,%edx
jl     12af <getpassword+0x13f>
mov    %rsi,%r13
mov    %rdi,%r15
xor    %ebx,%ebx
lea    0x8(%rsp),%r12
xor    %edi,%edi
mov    %r12,%rsi
call   1080 <tcgetattr@plt>
lea    0x20(%rsp),%rsi
xor    %edi,%edi
call   1080 <tcgetattr@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
or     %eax,0x18(%rsp)
mov    0x2dbe(%rip),%rax
mov    0x2e07(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2df4(%rip),%rax
or     (%rax),%ecx
mov    0x2dcb(%rip),%rax
or     (%rax),%ecx
mov    0x2db2(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x8(%rsp)
mov    0x10(%rsp),%rax
mov    0x2dbe(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d7d(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
mov    %r12,%rdx
call   1090 <tcsetattr@plt>
test   %r15,%r15
je     1254 <getpassword+0xe4>
lea    0xdc4(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    %r13,%rdi
xor    %esi,%esi
mov    %r14d,%edx
call   1050 <memset@plt>
mov    0x2d80(%rip),%r12
mov    (%r12),%edi
call   1060 <fgetc@plt>
mov    0x2d50(%rip),%rbp
cmp    0x0(%rbp),%eax
jne    12be <getpassword+0x14e>
movslq %ebx,%rax
movb   $0x0,0x0(%r13,%rax,1)
add    $0xffffffff,%r14d
movb   $0x0,0x0(%r13,%r14,1)
mov    0x2d49(%rip),%rax
mov    (%rax),%esi
lea    0x20(%rsp),%rdx
xor    %edi,%edi
call   1090 <tcsetattr@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cd3(%rip),%r15
xor    %ebx,%ebx
jmp    12e6 <getpassword+0x176>
nopl   0x0(%rax)
mov    (%r15),%edi
call   1070 <fflush@plt>
mov    (%r12),%edi
call   1060 <fgetc@plt>
cmp    0x0(%rbp),%eax
je     127d <getpassword+0x10d>
cmp    $0xc,%eax
jg     1300 <getpassword+0x190>
cmp    $0x8,%eax
je     1305 <getpassword+0x195>
cmp    $0xa,%eax
jne    132b <getpassword+0x1bb>
jmp    127d <getpassword+0x10d>
nopw   0x0(%rax,%rax,1)
cmp    $0x7f,%eax
jne    1322 <getpassword+0x1b2>
test   %ebx,%ebx
jle    12d8 <getpassword+0x168>
add    $0xffffffff,%ebx
movb   $0x0,0x0(%r13,%rbx,1)
lea    0xcea(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12d0 <getpassword+0x160>
cmp    $0xd,%eax
je     127d <getpassword+0x10d>
cmp    %r14d,%ebx
jge    12d8 <getpassword+0x168>
movslq %ebx,%rcx
add    $0x1,%ebx
mov    %al,0x0(%r13,%rcx,1)
mov    $0x2a,%edi
call   1030 <putchar@plt>
jmp    12d0 <getpassword+0x160>

LLVM IR (-O0):
%struct.termios = type { i32, i32*, i32 }
@ISTRIP = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ECHOE = global i32 0
@ECHOK = global i32 0
@ECHONL = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@.str = [3 x i8] c"%s\00"
@stdout = global i32 0
@stdin = global i32 0
@EOF = global i32 0
@.str.1 = [4 x i8] c"\08 \08\00"
@.str.2 = [2 x i8] c"*\00"
@.str.3 = [2 x i8] c"\0A\00"
define void @getpassword(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.termios
%8 = alloca %struct.termios
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%11 = load i8*, i8** %5
%12 = icmp eq i8* %11, null
br i1 %12, label %16, label %13
13: ; preds = %3
%14 = load i32, i32* %6
%15 = icmp sle i32 %14, 1
br i1 %15, label %16, label %17
16: ; preds = %13, %3
br label %118
17: ; preds = %13
%18 = call i32 @tcgetattr(i32 0, %struct.termios* %7)
%19 = call i32 @tcgetattr(i32 0, %struct.termios* %8)
%20 = load i32, i32* @ISTRIP
%21 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 2
%22 = load i32, i32* %21
%23 = or i32 %22, %20
store i32 %23, i32* %21
%24 = load i32, i32* @ICANON
%25 = load i32, i32* @ECHO
%26 = or i32 %24, %25
%27 = load i32, i32* @ECHOE
%28 = or i32 %26, %27
%29 = load i32, i32* @ECHOK
%30 = or i32 %28, %29
%31 = load i32, i32* @ECHONL
%32 = or i32 %30, %31
%33 = xor i32 %32, -1
%34 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%35 = load i32, i32* %34
%36 = and i32 %35, %33
store i32 %36, i32* %34
%37 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%38 = load i32*, i32** %37
%39 = load i64, i64* @VMIN
%40 = getelementptr inbounds i32, i32* %38, i64 %39
store i32 1, i32* %40
%41 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%42 = load i32*, i32** %41
%43 = load i64, i64* @VTIME
%44 = getelementptr inbounds i32, i32* %42, i64 %43
store i32 0, i32* %44
%45 = load i32, i32* @TCSANOW
%46 = call i32 @tcsetattr(i32 0, i32 %45, %struct.termios* %7)
%47 = load i8*, i8** %4
%48 = icmp ne i8* %47, null
br i1 %48, label %49, label %54
49: ; preds = %17
%50 = load i8*, i8** %4
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %50)
%52 = load i32, i32* @stdout
%53 = call i32 @fflush(i32 %52)
br label %54
54: ; preds = %49, %17
%55 = load i8*, i8** %5
%56 = load i32, i32* %6
%57 = call i32 @memset(i8* %55, i32 0, i32 %56)
store i32 0, i32* %10
br label %58
58: ; preds = %104, %81, %54
%59 = load i32, i32* @stdin
%60 = call i32 @fgetc(i32 %59)
store i32 %60, i32* %9
%61 = load i32, i32* @EOF
%62 = icmp ne i32 %60, %61
br i1 %62, label %63, label %105
63: ; preds = %58
%64 = load i32, i32* %9
%65 = icmp eq i32 %64, 127
br i1 %65, label %69, label %66
66: ; preds = %63
%67 = load i32, i32* %9
%68 = icmp eq i32 %67, 8
br i1 %68, label %69, label %82
69: ; preds = %66, %63
%70 = load i32, i32* %10
%71 = icmp sgt i32 %70, 0
br i1 %71, label %72, label %81
72: ; preds = %69
%73 = load i8*, i8** %5
%74 = load i32, i32* %10
%75 = add nsw i32 %74, -1
store i32 %75, i32* %10
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i8, i8* %73, i64 %76
store i8 0, i8* %77
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%79 = load i32, i32* @stdout
%80 = call i32 @fflush(i32 %79)
br label %81
81: ; preds = %72, %69
br label %58
82: ; preds = %66
%83 = load i32, i32* %9
%84 = icmp eq i32 %83, 10
br i1 %84, label %88, label %85
85: ; preds = %82
%86 = load i32, i32* %9
%87 = icmp eq i32 %86, 13
br i1 %87, label %88, label %89
88: ; preds = %85, %82
br label %105
89: ; preds = %85
%90 = load i32, i32* %10
%91 = load i32, i32* %6
%92 = icmp slt i32 %90, %91
br i1 %92, label %93, label %104
93: ; preds = %89
%94 = load i32, i32* %9
%95 = trunc i32 %94 to i8
%96 = load i8*, i8** %5
%97 = load i32, i32* %10
%98 = add nsw i32 %97, 1
store i32 %98, i32* %10
%99 = sext i32 %97 to i64
%100 = getelementptr inbounds i8, i8* %96, i64 %99
store i8 %95, i8* %100
%101 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%102 = load i32, i32* @stdout
%103 = call i32 @fflush(i32 %102)
br label %104
104: ; preds = %93, %89
br label %58
105: ; preds = %88, %58
%106 = load i8*, i8** %5
%107 = load i32, i32* %10
%108 = sext i32 %107 to i64
%109 = getelementptr inbounds i8, i8* %106, i64 %108
store i8 0, i8* %109
%110 = load i8*, i8** %5
%111 = load i32, i32* %6
%112 = sub nsw i32 %111, 1
%113 = sext i32 %112 to i64
%114 = getelementptr inbounds i8, i8* %110, i64 %113
store i8 0, i8* %114
%115 = load i32, i32* @TCSANOW
%116 = call i32 @tcsetattr(i32 0, i32 %115, %struct.termios* %8)
%117 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %118
118: ; preds = %105, %16
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32, i32*, i32 }
@ISTRIP = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ECHOE = global i32 0
@ECHOK = global i32 0
@ECHONL = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@.str = [3 x i8] c"%s\00"
@stdout = global i32 0
@stdin = global i32 0
@EOF = global i32 0
@.str.1 = [4 x i8] c"\08 \08\00"
define void @getpassword(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.termios
%5 = alloca %struct.termios
%6 = bitcast %struct.termios* %4 to i8*
%7 = bitcast %struct.termios* %5 to i8*
%8 = icmp eq i8* %1, null
%9 = icmp slt i32 %2, 2
%10 = or i1 %8, %9
br i1 %10, label %88, label %11
11: ; preds = %3
%12 = call i32 @tcgetattr(i32 0, %struct.termios* %4)
%13 = call i32 @tcgetattr(i32 0, %struct.termios* %5)
%14 = load i32, i32* @ISTRIP
%15 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 2
%16 = load i32, i32* %15
%17 = or i32 %16, %14
store i32 %17, i32* %15
%18 = load i32, i32* @ICANON
%19 = load i32, i32* @ECHO
%20 = or i32 %19, %18
%21 = load i32, i32* @ECHOE
%22 = or i32 %20, %21
%23 = load i32, i32* @ECHOK
%24 = or i32 %22, %23
%25 = load i32, i32* @ECHONL
%26 = or i32 %24, %25
%27 = xor i32 %26, -1
%28 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%29 = load i32, i32* %28
%30 = and i32 %29, %27
store i32 %30, i32* %28
%31 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 1
%32 = load i32*, i32** %31
%33 = load i64, i64* @VMIN
%34 = getelementptr inbounds i32, i32* %32, i64 %33
store i32 1, i32* %34
%35 = load i64, i64* @VTIME
%36 = getelementptr inbounds i32, i32* %32, i64 %35
store i32 0, i32* %36
%37 = load i32, i32* @TCSANOW
%38 = call i32 @tcsetattr(i32 0, i32 %37, %struct.termios* %4)
%39 = icmp eq i8* %0, null
br i1 %39, label %44, label %40
40: ; preds = %11
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%42 = load i32, i32* @stdout
%43 = call i32 @fflush(i32 %42)
br label %44
44: ; preds = %40, %11
%45 = call i32 @memset(i8* %1, i32 0, i32 %2)
%46 = load i32, i32* @stdin
%47 = call i32 @fgetc(i32 %46)
%48 = load i32, i32* @EOF
%49 = icmp eq i32 %47, %48
br i1 %49, label %78, label %50
50: ; preds = %44, %64
%51 = phi i32 [ %67, %64 ], [ %47, %44 ]
%52 = phi i32 [ %65, %64 ], [ 0, %44 ]
switch i32 %51, label %70 [
i32 127, label %53
i32 8, label %53
i32 13, label %78
i32 10, label %78
]
53: ; preds = %50, %50
%54 = icmp sgt i32 %52, 0
br i1 %54, label %55, label %64
55: ; preds = %53
%56 = add nsw i32 %52, -1
%57 = zext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %1, i64 %57
store i8 0, i8* %58
%59 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %60
60: ; preds = %55, %72
%61 = phi i32 [ %74, %72 ], [ %56, %55 ]
%62 = load i32, i32* @stdout
%63 = call i32 @fflush(i32 %62)
br label %64
64: ; preds = %60, %70, %53
%65 = phi i32 [ %52, %53 ], [ %52, %70 ], [ %61, %60 ]
%66 = load i32, i32* @stdin
%67 = call i32 @fgetc(i32 %66)
%68 = load i32, i32* @EOF
%69 = icmp eq i32 %67, %68
br i1 %69, label %78, label %50
70: ; preds = %50
%71 = icmp slt i32 %52, %2
br i1 %71, label %72, label %64
72: ; preds = %70
%73 = trunc i32 %51 to i8
%74 = add nsw i32 %52, 1
%75 = sext i32 %52 to i64
%76 = getelementptr inbounds i8, i8* %1, i64 %75
store i8 %73, i8* %76
%77 = call i32 @putchar(i32 42)
br label %60
78: ; preds = %64, %50, %50, %44
%79 = phi i32 [ 0, %44 ], [ %52, %50 ], [ %52, %50 ], [ %65, %64 ]
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds i8, i8* %1, i64 %80
store i8 0, i8* %81
%82 = add nsw i32 %2, -1
%83 = zext i32 %82 to i64
%84 = getelementptr inbounds i8, i8* %1, i64 %83
store i8 0, i8* %84
%85 = load i32, i32* @TCSANOW
%86 = call i32 @tcsetattr(i32 0, i32 %85, %struct.termios* %5)
%87 = call i32 @putchar(i32 10)
br label %88
88: ; preds = %3, %78
ret void
}