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

/* Variables and functions */
int /*<<< orphan*/ ECHO;
int ENOTTY;
int /*<<< orphan*/ TCSAFLUSH;
int errno;
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
size_t strlen(char *);
scalar_t__ tcgetattr(int /*<<< orphan*/, struct termios *);
scalar_t__ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
int get_pass(char *prompt, char *pwbuf, int buf_len, FILE *stream) {
  struct termios old, new;
  int rc, term_set, err;
  size_t br;
  rc = -1;
  term_set = 0;
  err = 0;
  if (tcgetattr(fileno(stream), &old) != 0) {
    err = errno;
    if (err != ENOTTY)
      goto out;
  }
  if (err == 0) {
    new = old;
    new.c_lflag &= ~ECHO;
    if (tcsetattr(fileno(stream), TCSAFLUSH, &new) != 0)
      goto out;
    term_set = 1;
  }
  printf("%s", prompt);
  if (!fgets(pwbuf, buf_len - 1, stream)) {
    printf("\n");
    goto out;
  }
  printf("\n");
  br = strlen(pwbuf);
  if (pwbuf[br - 1] != '\n') {
    fprintf(stderr, "too many characters in password\n");
    goto out;
  }
  pwbuf[br - 1] = '\0';
  rc = 0;
out:
  if (term_set)
    (void)tcsetattr(fileno(stream), TCSAFLUSH, &old);
  return rc;
}


x86 assembly (clang, -O0):
<get_pass>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0xffffffff,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <fileno@plt>
mov    %eax,%edi
lea    -0x28(%rbp),%rsi
call   1080 <tcgetattr@plt>
cmp    $0x0,%rax
je     11e2 <get_pass+0x72>
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    0x2e10(%rip),%rcx
cmp    (%rcx),%eax
je     11dd <get_pass+0x6d>
jmp    12dd <get_pass+0x16d>
jmp    11e2 <get_pass+0x72>
cmpl   $0x0,-0x3c(%rbp)
jne    1237 <get_pass+0xc7>
mov    -0x28(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <fileno@plt>
mov    %eax,%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1090 <tcsetattr@plt>
cmp    $0x0,%rax
je     1230 <get_pass+0xc0>
jmp    12dd <get_pass+0x16d>
movl   $0x1,-0x38(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
sub    $0x1,%esi
mov    -0x20(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%eax
jne    1278 <get_pass+0x108>
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12dd <get_pass+0x16d>
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x48(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     12c6 <get_pass+0x156>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
lea    0xd47(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12dd <get_pass+0x16d>
mov    -0x10(%rbp),%rax
mov    -0x48(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x34(%rbp)
cmpl   $0x0,-0x38(%rbp)
je     1304 <get_pass+0x194>
mov    -0x20(%rbp),%rdi
call   1070 <fileno@plt>
mov    %eax,%edi
mov    0x2cef(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1090 <tcsetattr@plt>
mov    -0x34(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pass>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r12
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rcx,%rdi
call   1080 <fileno@plt>
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <tcgetattr@plt>
test   %rax,%rax
je     11d6 <get_pass+0x56>
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    0x2e1e(%rip),%rcx
mov    $0xffffffff,%ebx
cmp    (%rcx),%eax
jne    1283 <get_pass+0x103>
mov    $0x1,%r13b
test   %eax,%eax
jne    1213 <get_pass+0x93>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0x8(%rsp),%eax
mov    %eax,0x10(%rsp)
mov    %r12,%rdi
call   1080 <fileno@plt>
mov    0x2df0(%rip),%rcx
mov    (%rcx),%esi
lea    0x10(%rsp),%rdx
mov    %eax,%edi
call   10a0 <tcsetattr@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    1283 <get_pass+0x103>
xor    %r13d,%r13d
lea    0xde6(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0xffffffff,%ebp
mov    %r14,%rdi
mov    %ebp,%esi
mov    %r12,%rdx
call   1060 <fgets@plt>
mov    %eax,%ebp
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    $0xffffffff,%ebx
test   %ebp,%ebp
je     1261 <get_pass+0xe1>
mov    %r14,%rdi
call   1040 <strlen@plt>
cmpb   $0xa,-0x1(%rax,%r14,1)
jne    1294 <get_pass+0x114>
movb   $0x0,-0x1(%r14,%rax,1)
xor    %ebx,%ebx
test   %r13b,%r13b
jne    1283 <get_pass+0x103>
mov    %r12,%rdi
call   1080 <fileno@plt>
mov    0x2d73(%rip),%rcx
mov    (%rcx),%esi
lea    0x8(%rsp),%rdx
mov    %eax,%edi
call   10a0 <tcsetattr@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
lea    0xd5f(%rip),%rsi
call   1070 <fprintf@plt>
test   %r13b,%r13b
je     1266 <get_pass+0xe6>
jmp    1283 <get_pass+0x103>

LLVM IR (-O0):
%struct.termios = type { i32 }
@errno = global i32 0
@ENOTTY = global i32 0
@ECHO = global i32 0
@TCSAFLUSH = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [2 x i8] c"\0A\00"
@stderr = global i32 0
@.str.2 = [33 x i8] c"too many characters in password\0A\00"
define i32 @get_pass(i8* %0, i8* %1, i32 %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca %struct.termios
%10 = alloca %struct.termios
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32* %3, i32** %8
store i32 -1, i32* %11
store i32 0, i32* %12
store i32 0, i32* %13
%15 = load i32*, i32** %8
%16 = call i32 @fileno(i32* %15)
%17 = call i64 @tcgetattr(i32 %16, %struct.termios* %9)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %4
%20 = load i32, i32* @errno
store i32 %20, i32* %13
%21 = load i32, i32* %13
%22 = load i32, i32* @ENOTTY
%23 = icmp ne i32 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %19
br label %74
25: ; preds = %19
br label %26
26: ; preds = %25, %4
%27 = load i32, i32* %13
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %44
29: ; preds = %26
%30 = bitcast %struct.termios* %10 to i8*
%31 = bitcast %struct.termios* %9 to i8*
%32 = load i32, i32* @ECHO
%33 = xor i32 %32, -1
%34 = getelementptr inbounds %struct.termios, %struct.termios* %10, i32 0, i32 0
%35 = load i32, i32* %34
%36 = and i32 %35, %33
store i32 %36, i32* %34
%37 = load i32*, i32** %8
%38 = call i32 @fileno(i32* %37)
%39 = load i32, i32* @TCSAFLUSH
%40 = call i64 @tcsetattr(i32 %38, i32 %39, %struct.termios* %10)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %29
br label %74
43: ; preds = %29
store i32 1, i32* %12
br label %44
44: ; preds = %43, %26
%45 = load i8*, i8** %5
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %45)
%47 = load i8*, i8** %6
%48 = load i32, i32* %7
%49 = sub nsw i32 %48, 1
%50 = load i32*, i32** %8
%51 = call i32 @fgets(i8* %47, i32 %49, i32* %50)
%52 = icmp ne i32 %51, 0
br i1 %52, label %55, label %53
53: ; preds = %44
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %74
55: ; preds = %44
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%57 = load i8*, i8** %6
%58 = call i64 @strlen(i8* %57)
store i64 %58, i64* %14
%59 = load i8*, i8** %6
%60 = load i64, i64* %14
%61 = sub i64 %60, 1
%62 = getelementptr inbounds i8, i8* %59, i64 %61
%63 = load i8, i8* %62
%64 = sext i8 %63 to i32
%65 = icmp ne i32 %64, 10
br i1 %65, label %66, label %69
66: ; preds = %55
%67 = load i32, i32* @stderr
%68 = call i32 @fprintf(i32 %67, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %74
69: ; preds = %55
%70 = load i8*, i8** %6
%71 = load i64, i64* %14
%72 = sub i64 %71, 1
%73 = getelementptr inbounds i8, i8* %70, i64 %72
store i8 0, i8* %73
store i32 0, i32* %11
br label %74
74: ; preds = %69, %66, %53, %42, %24
%75 = load i32, i32* %12
%76 = icmp ne i32 %75, 0
br i1 %76, label %77, label %82
77: ; preds = %74
%78 = load i32*, i32** %8
%79 = call i32 @fileno(i32* %78)
%80 = load i32, i32* @TCSAFLUSH
%81 = call i64 @tcsetattr(i32 %79, i32 %80, %struct.termios* %9)
br label %82
82: ; preds = %77, %74
%83 = load i32, i32* %11
ret i32 %83
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@errno = global i32 0
@ENOTTY = global i32 0
@ECHO = global i32 0
@TCSAFLUSH = global i32 0
@.str = [3 x i8] c"%s\00"
@stderr = global i32 0
@.str.2 = [33 x i8] c"too many characters in password\0A\00"
define i32 @get_pass(i8* %0, i8* %1, i32 %2, i32* nocapture %3) {
%5 = alloca %struct.termios
%6 = alloca %struct.termios
%7 = bitcast %struct.termios* %5 to i8*
%8 = bitcast %struct.termios* %6 to i8*
%9 = tail call i32 @fileno(i32* %3)
%10 = call i64 @tcgetattr(i32 %9, %struct.termios* %5)
%11 = icmp eq i64 %10, 0
br i1 %11, label %18, label %12
12: ; preds = %4
%13 = load i32, i32* @errno
%14 = load i32, i32* @ENOTTY
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %52
16: ; preds = %12
%17 = icmp eq i32 %13, 0
br i1 %17, label %18, label %29
18: ; preds = %4, %16
%19 = getelementptr inbounds %struct.termios, %struct.termios* %5, i64 0, i32 0
%20 = getelementptr inbounds %struct.termios, %struct.termios* %6, i64 0, i32 0
%21 = load i32, i32* %19
%22 = load i32, i32* @ECHO
%23 = xor i32 %22, -1
%24 = and i32 %21, %23
store i32 %24, i32* %20
%25 = call i32 @fileno(i32* %3)
%26 = load i32, i32* @TCSAFLUSH
%27 = call i64 @tcsetattr(i32 %25, i32 %26, %struct.termios* %6)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %52
29: ; preds = %18, %16
%30 = phi i1 [ false, %18 ], [ true, %16 ]
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%32 = add nsw i32 %2, -1
%33 = call i32 @fgets(i8* %1, i32 %32, i32* %3)
%34 = icmp eq i32 %33, 0
%35 = call i32 @putchar(i32 10)
br i1 %34, label %46, label %36
36: ; preds = %29
%37 = call i64 @strlen(i8* dereferenceable(1) %1)
%38 = add i64 %37, -1
%39 = getelementptr inbounds i8, i8* %1, i64 %38
%40 = load i8, i8* %39
%41 = icmp eq i8 %40, 10
br i1 %41, label %45, label %42
42: ; preds = %36
%43 = load i32, i32* @stderr
%44 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %46
45: ; preds = %36
store i8 0, i8* %39
br label %46
46: ; preds = %29, %45, %42
%47 = phi i32 [ -1, %42 ], [ 0, %45 ], [ -1, %29 ]
br i1 %30, label %52, label %48
48: ; preds = %46
%49 = call i32 @fileno(i32* %3)
%50 = load i32, i32* @TCSAFLUSH
%51 = call i64 @tcsetattr(i32 %49, i32 %50, %struct.termios* %5)
br label %52
52: ; preds = %18, %12, %48, %46
%53 = phi i32 [ %47, %48 ], [ %47, %46 ], [ -1, %12 ], [ -1, %18 ]
ret i32 %53
}