#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;
};

/* Variables and functions */
int /*<<< orphan*/ ECHO;
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ putchar(char);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stdout;
char *strchr(char *, char);
int strlen(char *);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int readpwd(char *buf, int bufsiz) {
  struct termios old, new;
  int i;
  char *p;
  tcgetattr(0, &old);
  new = old;
  new.c_lflag &= ~(ECHO);
  tcsetattr(0, TCSAFLUSH, &new);
  buf[0] = 0;
  i = read(0, buf, bufsiz - 1);
  if (i >= 0)
    buf[i] = 0;
  if ((p = strchr(buf, '\n')))
    *p = 0;
  tcsetattr(0, TCSAFLUSH, &old);
  putchar('\n');
  fflush(stdout);
  return strlen(buf);
}


x86 assembly (clang, -O0):
<readpwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
xor    %edi,%edi
lea    -0x10(%rbp),%rsi
call   1080 <tcgetattr@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x18(%rbp),%rdx
call   1090 <tcsetattr@plt>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
xor    %edi,%edi
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     11e7 <readpwd+0x77>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     120a <readpwd+0x9a>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x10(%rbp),%rdx
call   1090 <tcsetattr@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readpwd>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rsi
xor    %edi,%edi
call   1080 <tcgetattr@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
not    %eax
and    (%rsp),%eax
mov    %eax,0x8(%rsp)
mov    0x2e48(%rip),%r14
mov    (%r14),%esi
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   1090 <tcsetattr@plt>
movb   $0x0,(%rbx)
add    $0xffffffff,%ebp
xor    %edi,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1060 <read@plt>
test   %eax,%eax
js     11cb <readpwd+0x5b>
mov    %eax,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11e0 <readpwd+0x70>
movb   $0x0,(%rax)
mov    (%r14),%esi
mov    %rsp,%rdx
xor    %edi,%edi
call   1090 <tcsetattr@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@ECHO = global i32 0
@TCSAFLUSH = global i32 0
@stdout = global i32 0
define i32 @readpwd(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.termios
%6 = alloca %struct.termios
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = call i32 @tcgetattr(i32 0, %struct.termios* %5)
%10 = bitcast %struct.termios* %6 to i8*
%11 = bitcast %struct.termios* %5 to i8*
%12 = load i32, i32* @ECHO
%13 = xor i32 %12, -1
%14 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%15 = load i32, i32* %14
%16 = and i32 %15, %13
store i32 %16, i32* %14
%17 = load i32, i32* @TCSAFLUSH
%18 = call i32 @tcsetattr(i32 0, i32 %17, %struct.termios* %6)
%19 = load i8*, i8** %3
%20 = getelementptr inbounds i8, i8* %19, i64 0
store i8 0, i8* %20
%21 = load i8*, i8** %3
%22 = load i32, i32* %4
%23 = sub nsw i32 %22, 1
%24 = call i32 @read(i32 0, i8* %21, i32 %23)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp sge i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %2
%28 = load i8*, i8** %3
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 0, i8* %31
br label %32
32: ; preds = %27, %2
%33 = load i8*, i8** %3
%34 = call i8* @strchr(i8* %33, i8 signext 10)
store i8* %34, i8** %8
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = load i8*, i8** %8
store i8 0, i8* %37
br label %38
38: ; preds = %36, %32
%39 = load i32, i32* @TCSAFLUSH
%40 = call i32 @tcsetattr(i32 0, i32 %39, %struct.termios* %5)
%41 = call i32 @putchar(i8 signext 10)
%42 = load i32, i32* @stdout
%43 = call i32 @fflush(i32 %42)
%44 = load i8*, i8** %3
%45 = call i32 @strlen(i8* %44)
ret i32 %45
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@ECHO = global i32 0
@TCSAFLUSH = global i32 0
@stdout = global i32 0
define i32 @readpwd(i8* %0, i32 %1) {
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = bitcast %struct.termios* %3 to i8*
%6 = bitcast %struct.termios* %4 to i8*
%7 = call i32 @tcgetattr(i32 0, %struct.termios* %3)
%8 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%9 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%10 = load i32, i32* %8
%11 = load i32, i32* @ECHO
%12 = xor i32 %11, -1
%13 = and i32 %10, %12
store i32 %13, i32* %9
%14 = load i32, i32* @TCSAFLUSH
%15 = call i32 @tcsetattr(i32 0, i32 %14, %struct.termios* %4)
store i8 0, i8* %0
%16 = add nsw i32 %1, -1
%17 = call i32 @read(i32 0, i8* %0, i32 %16)
%18 = icmp sgt i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = zext i32 %17 to i64
%21 = getelementptr inbounds i8, i8* %0, i64 %20
store i8 0, i8* %21
br label %22
22: ; preds = %19, %2
%23 = call i8* @strchr(i8* %0, i8 signext 10)
%24 = icmp eq i8* %23, null
br i1 %24, label %26, label %25
25: ; preds = %22
store i8 0, i8* %23
br label %26
26: ; preds = %25, %22
%27 = load i32, i32* @TCSAFLUSH
%28 = call i32 @tcsetattr(i32 0, i32 %27, %struct.termios* %3)
%29 = call i32 @putchar(i8 signext 10)
%30 = load i32, i32* @stdout
%31 = call i32 @fflush(i32 %30)
%32 = call i32 @strlen(i8* %0)
ret i32 %32
}