#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ system(char *);
char *ttyname(int);
int /*<<< orphan*/ write(int, char *, int);
int readpasswd(char pwd[16]) {
  char *ttypath = ttyname(0);
  if (ttypath == NULL) {
    ttypath = ttyname(1);
  }
  if (ttypath == NULL) {
    ttypath = ttyname(2);
  }
  if (ttypath == NULL) {
    return (-1);
  }
  int fd = open(ttypath, O_RDWR);
  if (fd == -1) {
    return (-1);
  }
  system("stty raw -echo");
  write(1, "passwd: ", 8);
  int i;
  while (1) {
    char c;
    read(fd, &c, 1);
    if (c == '\n' || c == '\r') {
      break;
    }
    if (c == ((char)127) && i > 0) {
      --i;
      write(1, "\b \b", 3);
    } else if (i < 15) {
      write(1, "X", 1);
      pwd[i++] = c;
    }
  }
  pwd[i] = 0;
  write(1, "\n\r", 2);
  close(fd);
  system("stty -raw echo");
  return (i);
}


x86 assembly (clang, -O0):
<readpasswd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
xor    %edi,%edi
call   1070 <ttyname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1190 <readpasswd+0x30>
mov    $0x1,%edi
call   1070 <ttyname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a9 <readpasswd+0x49>
mov    $0x2,%edi
call   1070 <ttyname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c0 <readpasswd+0x60>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <readpasswd+0x193>
mov    -0x18(%rbp),%rdi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11eb <readpasswd+0x8b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <readpasswd+0x193>
lea    0xe0e(%rip),%rdi
call   1040 <system@plt>
mov    $0x1,%edi
lea    0xe0c(%rip),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
mov    -0x1c(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
movsbl -0x21(%rbp),%eax
cmp    $0xa,%eax
je     1238 <readpasswd+0xd8>
movsbl -0x21(%rbp),%eax
cmp    $0xd,%eax
jne    123d <readpasswd+0xdd>
jmp    12b7 <readpasswd+0x157>
movsbl -0x21(%rbp),%eax
cmp    $0x7f,%eax
jne    1278 <readpasswd+0x118>
cmpl   $0x0,-0x20(%rbp)
jle    1278 <readpasswd+0x118>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
mov    $0x1,%edi
lea    0xdaf(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
jmp    12b2 <readpasswd+0x152>
cmpl   $0xf,-0x20(%rbp)
jge    12ad <readpasswd+0x14d>
mov    $0x1,%edx
lea    0xd8e(%rip),%rsi
mov    %edx,%edi
call   1030 <write@plt>
mov    -0x21(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x20(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    12b2 <readpasswd+0x152>
jmp    120d <readpasswd+0xad>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    $0x1,%edi
lea    0xd4f(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
lea    0xd39(%rip),%rdi
call   1040 <system@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readpasswd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %edi,%edi
call   1070 <ttyname@plt>
test   %rax,%rax
jne    119c <readpasswd+0x3c>
mov    $0x1,%edi
call   1070 <ttyname@plt>
test   %rax,%rax
jne    119c <readpasswd+0x3c>
mov    $0x2,%edi
call   1070 <ttyname@plt>
test   %rax,%rax
je     128a <readpasswd+0x12a>
mov    0x2e45(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     128a <readpasswd+0x12a>
mov    %eax,%ebp
lea    0xe41(%rip),%rdi
call   1040 <system@plt>
lea    0xe44(%rip),%rsi
mov    $0x1,%edi
mov    $0x8,%edx
call   1030 <write@plt>
lea    0x7(%rsp),%r15
lea    0xe32(%rip),%r12
jmp    1205 <readpasswd+0xa5>
nopl   0x0(%rax,%rax,1)
add    $0xffffffff,%ebx
mov    $0x1,%edi
mov    %r12,%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
movzbl 0x7(%rsp),%r13d
cmp    $0xa,%r13b
je     1257 <readpasswd+0xf7>
cmp    $0xd,%r13b
je     1257 <readpasswd+0xf7>
cmp    $0x7f,%r13b
jne    1230 <readpasswd+0xd0>
test   %ebx,%ebx
jg     11f0 <readpasswd+0x90>
cmp    $0xe,%ebx
jg     1205 <readpasswd+0xa5>
mov    $0x1,%edi
lea    0xddb(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
movslq %ebx,%rax
add    $0x1,%ebx
mov    %r13b,(%r14,%rax,1)
jmp    1205 <readpasswd+0xa5>
movslq %ebx,%rax
movb   $0x0,(%r14,%rax,1)
lea    0xdb8(%rip),%rsi
mov    $0x1,%edi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1050 <close@plt>
lea    0xd9e(%rip),%rdi
call   1040 <system@plt>
jmp    128f <readpasswd+0x12f>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@.str = [15 x i8] c"stty raw -echo\00"
@.str.1 = [9 x i8] c"passwd: \00"
@.str.2 = [4 x i8] c"\08 \08\00"
@.str.3 = [2 x i8] c"X\00"
@.str.4 = [3 x i8] c"\0A\0D\00"
@.str.5 = [15 x i8] c"stty -raw echo\00"
define i32 @readpasswd(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
store i8* %0, i8** %3
%8 = call i8* @ttyname(i32 0)
store i8* %8, i8** %4
%9 = load i8*, i8** %4
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i8* @ttyname(i32 1)
store i8* %12, i8** %4
br label %13
13: ; preds = %11, %1
%14 = load i8*, i8** %4
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i8* @ttyname(i32 2)
store i8* %17, i8** %4
br label %18
18: ; preds = %16, %13
%19 = load i8*, i8** %4
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %22
21: ; preds = %18
store i32 -1, i32* %2
br label %77
22: ; preds = %18
%23 = load i8*, i8** %4
%24 = load i32, i32* @O_RDWR
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %5
%26 = load i32, i32* %5
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %22
store i32 -1, i32* %2
br label %77
29: ; preds = %22
%30 = call i32 @system(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%31 = call i32 @write(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 8)
br label %32
32: ; preds = %29, %66
%33 = load i32, i32* %5
%34 = call i32 @read(i32 %33, i8* %7, i32 1)
%35 = load i8, i8* %7
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 10
br i1 %37, label %42, label %38
38: ; preds = %32
%39 = load i8, i8* %7
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 13
br i1 %41, label %42, label %43
42: ; preds = %38, %32
br label %67
43: ; preds = %38
%44 = load i8, i8* %7
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 127
br i1 %46, label %47, label %54
47: ; preds = %43
%48 = load i32, i32* %6
%49 = icmp sgt i32 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %47
%51 = load i32, i32* %6
%52 = add nsw i32 %51, -1
store i32 %52, i32* %6
%53 = call i32 @write(i32 1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
br label %66
54: ; preds = %47, %43
%55 = load i32, i32* %6
%56 = icmp slt i32 %55, 15
br i1 %56, label %57, label %65
57: ; preds = %54
%58 = call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%59 = load i8, i8* %7
%60 = load i8*, i8** %3
%61 = load i32, i32* %6
%62 = add nsw i32 %61, 1
store i32 %62, i32* %6
%63 = sext i32 %61 to i64
%64 = getelementptr inbounds i8, i8* %60, i64 %63
store i8 %59, i8* %64
br label %65
65: ; preds = %57, %54
br label %66
66: ; preds = %65, %50
br label %32
67: ; preds = %42
%68 = load i8*, i8** %3
%69 = load i32, i32* %6
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %68, i64 %70
store i8 0, i8* %71
%72 = call i32 @write(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 2)
%73 = load i32, i32* %5
%74 = call i32 @close(i32 %73)
%75 = call i32 @system(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%76 = load i32, i32* %6
store i32 %76, i32* %2
br label %77
77: ; preds = %67, %28, %21
%78 = load i32, i32* %2
ret i32 %78
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str = [15 x i8] c"stty raw -echo\00"
@.str.1 = [9 x i8] c"passwd: \00"
@.str.2 = [4 x i8] c"\08 \08\00"
@.str.3 = [2 x i8] c"X\00"
@.str.4 = [3 x i8] c"\0A\0D\00"
@.str.5 = [15 x i8] c"stty -raw echo\00"
define i32 @readpasswd(i8* nocapture writeonly %0) {
%2 = alloca i8
%3 = tail call i8* @ttyname(i32 0)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %11
5: ; preds = %1
%6 = tail call i8* @ttyname(i32 1)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = tail call i8* @ttyname(i32 2)
%10 = icmp eq i8* %9, null
br i1 %10, label %45, label %11
11: ; preds = %1, %5, %8
%12 = phi i8* [ %9, %8 ], [ %6, %5 ], [ %3, %1 ]
%13 = load i32, i32* @O_RDWR
%14 = tail call i32 @open(i8* %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %45, label %16
16: ; preds = %11
%17 = tail call i32 @system(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%18 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 8)
br label %19
19: ; preds = %37, %16
%20 = phi i32 [ undef, %16 ], [ %38, %37 ]
%21 = call i32 @read(i32 %14, i8* %2, i32 1)
%22 = load i8, i8* %2
switch i8 %22, label %23 [
i8 13, label %39
i8 10, label %39
]
23: ; preds = %19
%24 = icmp eq i8 %22, 127
%25 = icmp sgt i32 %20, 0
%26 = select i1 %24, i1 %25, i1 false
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = add nsw i32 %20, -1
%29 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
br label %37
30: ; preds = %23
%31 = icmp slt i32 %20, 15
br i1 %31, label %32, label %37
32: ; preds = %30
%33 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%34 = add nsw i32 %20, 1
%35 = sext i32 %20 to i64
%36 = getelementptr inbounds i8, i8* %0, i64 %35
store i8 %22, i8* %36
br label %37
37: ; preds = %27, %32, %30
%38 = phi i32 [ %28, %27 ], [ %34, %32 ], [ %20, %30 ]
br label %19
39: ; preds = %19, %19
%40 = sext i32 %20 to i64
%41 = getelementptr inbounds i8, i8* %0, i64 %40
store i8 0, i8* %41
%42 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 2)
%43 = tail call i32 @close(i32 %14)
%44 = tail call i32 @system(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %45
45: ; preds = %39, %11, %8
%46 = phi i32 [ -1, %8 ], [ %20, %39 ], [ -1, %11 ]
ret i32 %46
}