#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_cc;
  int /*<<< orphan*/ c_lflag;
};

/* Variables and functions */
int /*<<< orphan*/ ECHO;
int /*<<< orphan*/ ICANON;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
int getchar();
int /*<<< orphan*/ putchar(int);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int get_password(char buf[], int maxlen, int star) {
  struct termios ttystate, orig;
  int c;
  int pos = 0;
  tcgetattr(0, &ttystate);
  orig = ttystate;
  ttystate.c_lflag &= ~ICANON;
  ttystate.c_lflag &= ~ECHO;
  ttystate.c_cc[VMIN] = 1;
  tcsetattr(0, TCSANOW, &ttystate);
  while ((c = getchar()) != '\n' && c != '\r') {
    if (pos < maxlen - 1) {
      buf[pos++] = c;
      putchar(star);
    }
  }
  buf[pos] = '\0';
  tcsetattr(0, TCSANOW, &orig);
  return pos;
}


x86 assembly (clang, -O0):
<get_password>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x38(%rbp)
xor    %edi,%edi
lea    -0x20(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e35(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x20(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,%ecx
mov    %ecx,-0x34(%rbp)
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x39(%rbp)
je     11e5 <get_password+0xa5>
cmpl   $0xd,-0x34(%rbp)
setne  %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    11f5 <get_password+0xb5>
jmp    122d <get_password+0xed>
mov    -0x38(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1228 <get_password+0xe8>
mov    -0x34(%rbp),%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    -0x38(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x38(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%edi
call   1030 <putchar@plt>
jmp    11c1 <get_password+0x81>
mov    -0x8(%rbp),%rax
movslq -0x38(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x30(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    -0x38(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_password>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%r15
mov    %rsp,%rbx
xor    %edi,%edi
mov    %rbx,%rsi
call   1050 <tcgetattr@plt>
movups (%rsp),%xmm0
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
mov    0x2e6b(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    %eax,0x8(%rsp)
movaps %xmm0,0x10(%rsp)
mov    (%rsp),%rax
mov    0x2e4b(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e4a(%rip),%r12
mov    (%r12),%esi
xor    %edi,%edi
mov    %rbx,%rdx
call   1060 <tcsetattr@plt>
add    $0xffffffff,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    $0xa,%eax
je     11e8 <get_password+0xa8>
cmp    $0xd,%eax
je     11e8 <get_password+0xa8>
cmp    %ebp,%ebx
jge    11c0 <get_password+0x80>
mov    %ebx,%ecx
add    $0x1,%ebx
mov    %al,(%r15,%rcx,1)
mov    %r14d,%edi
call   1030 <putchar@plt>
jmp    11c0 <get_password+0x80>
mov    %ebx,%eax
movb   $0x0,(%r15,%rax,1)
mov    (%r12),%esi
lea    0x10(%rsp),%rdx
xor    %edi,%edi
call   1060 <tcsetattr@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32*, i32 }
@ICANON = global i32 0
@ECHO = global i32 0
@VMIN = global i64 0
@TCSANOW = global i32 0
define i32 @get_password(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.termios
%8 = alloca %struct.termios
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %10
%11 = call i32 @tcgetattr(i32 0, %struct.termios* %7)
%12 = bitcast %struct.termios* %8 to i8*
%13 = bitcast %struct.termios* %7 to i8*
%14 = load i32, i32* @ICANON
%15 = xor i32 %14, -1
%16 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%17 = load i32, i32* %16
%18 = and i32 %17, %15
store i32 %18, i32* %16
%19 = load i32, i32* @ECHO
%20 = xor i32 %19, -1
%21 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%22 = load i32, i32* %21
%23 = and i32 %22, %20
store i32 %23, i32* %21
%24 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%25 = load i32*, i32** %24
%26 = load i64, i64* @VMIN
%27 = getelementptr inbounds i32, i32* %25, i64 %26
store i32 1, i32* %27
%28 = load i32, i32* @TCSANOW
%29 = call i32 @tcsetattr(i32 0, i32 %28, %struct.termios* %7)
br label %30
30: ; preds = %53, %3
%31 = call i32 (...) @getchar()
store i32 %31, i32* %9
%32 = icmp ne i32 %31, 10
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i32, i32* %9
%35 = icmp ne i32 %34, 13
br label %36
36: ; preds = %33, %30
%37 = phi i1 [ false, %30 ], [ %35, %33 ]
br i1 %37, label %38, label %54
38: ; preds = %36
%39 = load i32, i32* %10
%40 = load i32, i32* %5
%41 = sub nsw i32 %40, 1
%42 = icmp slt i32 %39, %41
br i1 %42, label %43, label %53
43: ; preds = %38
%44 = load i32, i32* %9
%45 = trunc i32 %44 to i8
%46 = load i8*, i8** %4
%47 = load i32, i32* %10
%48 = add nsw i32 %47, 1
store i32 %48, i32* %10
%49 = sext i32 %47 to i64
%50 = getelementptr inbounds i8, i8* %46, i64 %49
store i8 %45, i8* %50
%51 = load i32, i32* %6
%52 = call i32 @putchar(i32 %51)
br label %53
53: ; preds = %43, %38
br label %30
54: ; preds = %36
%55 = load i8*, i8** %4
%56 = load i32, i32* %10
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
store i8 0, i8* %58
%59 = load i32, i32* @TCSANOW
%60 = call i32 @tcsetattr(i32 0, i32 %59, %struct.termios* %8)
%61 = load i32, i32* %10
ret i32 %61
}

LLVM IR (-O2):
%struct.termios = type { i32*, i32 }
@ICANON = global i32 0
@ECHO = global i32 0
@VMIN = global i64 0
@TCSANOW = global i32 0
define i32 @get_password(i8* nocapture writeonly %0, i32 %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 = call i32 @tcgetattr(i32 0, %struct.termios* %4)
%9 = load i32, i32* @ICANON
%10 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 1
%11 = load i32, i32* %10
%12 = load i32, i32* @ECHO
%13 = or i32 %9, %12
%14 = xor i32 %13, -1
%15 = and i32 %11, %14
store i32 %15, i32* %10
%16 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%17 = load i32*, i32** %16
%18 = load i64, i64* @VMIN
%19 = getelementptr inbounds i32, i32* %17, i64 %18
store i32 1, i32* %19
%20 = load i32, i32* @TCSANOW
%21 = call i32 @tcsetattr(i32 0, i32 %20, %struct.termios* %4)
%22 = add nsw i32 %1, -1
br label %23
23: ; preds = %29, %3
%24 = phi i32 [ %31, %29 ], [ 0, %3 ]
%25 = icmp slt i32 %24, %22
br label %26
26: ; preds = %23, %28
%27 = call i32 (...) @getchar()
switch i32 %27, label %28 [
i32 13, label %35
i32 10, label %35
]
28: ; preds = %26
br i1 %25, label %29, label %26
29: ; preds = %28
%30 = trunc i32 %27 to i8
%31 = add nuw nsw i32 %24, 1
%32 = zext i32 %24 to i64
%33 = getelementptr inbounds i8, i8* %0, i64 %32
store i8 %30, i8* %33
%34 = call i32 @putchar(i32 %2)
br label %23
35: ; preds = %26, %26
%36 = zext i32 %24 to i64
%37 = getelementptr inbounds i8, i8* %0, i64 %36
store i8 0, i8* %37
%38 = load i32, i32* @TCSANOW
%39 = call i32 @tcsetattr(i32 0, i32 %38, %struct.termios* %5)
ret i32 %24
}