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

/* Variables and functions */
int /*<<< orphan*/ ECHO;
int /*<<< orphan*/ ECHONL;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
int /*<<< orphan*/ halt_os_error(char *);
int /*<<< orphan*/ memcpy(struct termios *, struct termios *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ stdin;
int strlen(char *);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
scalar_t__ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
int getpasswd(const char *prompt, char *passwd, int size) {
  struct termios old_flags;
  struct termios new_flags;
  if (size < 0) {
    return -1;
  }
  memset(passwd, 0, (size_t)size);
  tcgetattr(fileno(stdin), &old_flags);
  memcpy(&new_flags, &old_flags, sizeof(old_flags));
  new_flags.c_lflag &= ~ECHO;
  new_flags.c_lflag |= ECHONL;
  if (tcsetattr(fileno(stdin), TCSANOW, &new_flags) != 0) {
    halt_os_error("Could not disable echo");
    return -1;
  }
  printf("%s", prompt);
  fgets(passwd, size, stdin);
  if (passwd[strlen(passwd) - 1] == '\n') {
    passwd[strlen(passwd) - 1] = '\0';
  }
  if (tcsetattr(fileno(stdin), TCSANOW, &old_flags) != 0) {
    halt_os_error("Could not restore terminal attributes");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<getpasswd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11b9 <getpasswd+0x29>
movl   $0xffffffff,-0x4(%rbp)
jmp    1313 <getpasswd+0x183>
mov    -0x18(%rbp),%rdi
movslq -0x1c(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1090 <fileno@plt>
mov    %eax,%edi
lea    -0x20(%rbp),%rsi
call   10a0 <tcgetattr@plt>
lea    -0x28(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   1090 <fileno@plt>
mov    %eax,%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   10b0 <tcsetattr@plt>
cmp    $0x0,%rax
je     1258 <getpasswd+0xc8>
lea    0xdb9(%rip),%rdi
call   1080 <halt_os_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1313 <getpasswd+0x183>
mov    -0x10(%rbp),%rsi
lea    0xdb4(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    0x2d72(%rip),%rax
mov    (%rax),%edx
call   1060 <fgets@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    12c8 <getpasswd+0x138>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
call   1090 <fileno@plt>
mov    %eax,%edi
mov    0x2d01(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   10b0 <tcsetattr@plt>
cmp    $0x0,%rax
je     130c <getpasswd+0x17c>
lea    0xd1f(%rip),%rdi
call   1080 <halt_os_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1313 <getpasswd+0x183>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getpasswd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0xffffffff,%eax
test   %edx,%edx
js     12ab <getpasswd+0x11b>
mov    %edx,%ebp
mov    %rsi,%r12
mov    %rdi,%r15
mov    %edx,%edx
mov    %rsi,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    0x2e22(%rip),%r14
mov    (%r14),%edi
call   1090 <fileno@plt>
lea    0x10(%rsp),%rbx
mov    %eax,%edi
mov    %rbx,%rsi
call   10a0 <tcgetattr@plt>
lea    0x8(%rsp),%r13
mov    %r13,%rdi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0x8(%rsp),%eax
mov    0x2dc0(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x8(%rsp)
mov    (%r14),%edi
call   1090 <fileno@plt>
mov    0x2dc3(%rip),%rbx
mov    (%rbx),%esi
mov    %eax,%edi
mov    %r13,%rdx
call   10b0 <tcsetattr@plt>
test   %rax,%rax
je     1237 <getpasswd+0xa7>
lea    0xdcb(%rip),%rdi
jmp    12a1 <getpasswd+0x111>
lea    0xdd9(%rip),%rdi
mov    %r15,%rsi
call   1040 <printf@plt>
mov    0x2d9b(%rip),%r15
mov    (%r15),%edx
mov    %r12,%rdi
mov    %ebp,%esi
call   1060 <fgets@plt>
mov    %r12,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%r12,1)
mov    %r15,%rbp
jne    127f <getpasswd+0xef>
mov    %r12,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%r12,1)
mov    0x0(%rbp),%edi
call   1090 <fileno@plt>
mov    (%rbx),%esi
lea    0x10(%rsp),%rdx
mov    %eax,%edi
call   10b0 <tcsetattr@plt>
test   %rax,%rax
je     12ba <getpasswd+0x12a>
lea    0xd79(%rip),%rdi
call   1080 <halt_os_error@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
jmp    12ab <getpasswd+0x11b>

LLVM IR (-O0):
%struct.termios = type { i32 }
@stdin = global i32 0
@ECHO = global i32 0
@ECHONL = global i32 0
@TCSANOW = global i32 0
@.str = [23 x i8] c"Could not disable echo\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [38 x i8] c"Could not restore terminal attributes\00"
define i32 @getpasswd(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.termios
%9 = alloca %struct.termios
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i32, i32* %7
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %3
store i32 -1, i32* %4
br label %70
13: ; preds = %3
%14 = load i8*, i8** %6
%15 = load i32, i32* %7
%16 = sext i32 %15 to i64
%17 = call i32 @memset(i8* %14, i32 0, i64 %16)
%18 = load i32, i32* @stdin
%19 = call i32 @fileno(i32 %18)
%20 = call i32 @tcgetattr(i32 %19, %struct.termios* %8)
%21 = call i32 @memcpy(%struct.termios* %9, %struct.termios* %8, i32 4)
%22 = load i32, i32* @ECHO
%23 = xor i32 %22, -1
%24 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 0
%25 = load i32, i32* %24
%26 = and i32 %25, %23
store i32 %26, i32* %24
%27 = load i32, i32* @ECHONL
%28 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 0
%29 = load i32, i32* %28
%30 = or i32 %29, %27
store i32 %30, i32* %28
%31 = load i32, i32* @stdin
%32 = call i32 @fileno(i32 %31)
%33 = load i32, i32* @TCSANOW
%34 = call i64 @tcsetattr(i32 %32, i32 %33, %struct.termios* %9)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %13
%37 = call i32 @halt_os_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %70
38: ; preds = %13
%39 = load i8*, i8** %5
%40 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %39)
%41 = load i8*, i8** %6
%42 = load i32, i32* %7
%43 = load i32, i32* @stdin
%44 = call i32 @fgets(i8* %41, i32 %42, i32 %43)
%45 = load i8*, i8** %6
%46 = load i8*, i8** %6
%47 = call i32 @strlen(i8* %46)
%48 = sub nsw i32 %47, 1
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %45, i64 %49
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = icmp eq i32 %52, 10
br i1 %53, label %54, label %61
54: ; preds = %38
%55 = load i8*, i8** %6
%56 = load i8*, i8** %6
%57 = call i32 @strlen(i8* %56)
%58 = sub nsw i32 %57, 1
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %55, i64 %59
store i8 0, i8* %60
br label %61
61: ; preds = %54, %38
%62 = load i32, i32* @stdin
%63 = call i32 @fileno(i32 %62)
%64 = load i32, i32* @TCSANOW
%65 = call i64 @tcsetattr(i32 %63, i32 %64, %struct.termios* %8)
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %61
%68 = call i32 @halt_os_error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %70
69: ; preds = %61
store i32 0, i32* %4
br label %70
70: ; preds = %69, %67, %36, %12
%71 = load i32, i32* %4
ret i32 %71
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@stdin = global i32 0
@ECHO = global i32 0
@ECHONL = global i32 0
@TCSANOW = global i32 0
@.str = [23 x i8] c"Could not disable echo\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [38 x i8] c"Could not restore terminal attributes\00"
define i32 @getpasswd(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 slt i32 %2, 0
br i1 %8, label %52, label %9
9: ; preds = %3
%10 = zext i32 %2 to i64
%11 = tail call i32 @memset(i8* %1, i32 0, i64 %10)
%12 = load i32, i32* @stdin
%13 = tail call i32 @fileno(i32 %12)
%14 = call i32 @tcgetattr(i32 %13, %struct.termios* %4)
%15 = call i32 @memcpy(%struct.termios* %5, %struct.termios* %4, i32 4)
%16 = load i32, i32* @ECHO
%17 = xor i32 %16, -1
%18 = getelementptr inbounds %struct.termios, %struct.termios* %5, i64 0, i32 0
%19 = load i32, i32* %18
%20 = and i32 %19, %17
%21 = load i32, i32* @ECHONL
%22 = or i32 %21, %20
store i32 %22, i32* %18
%23 = load i32, i32* @stdin
%24 = call i32 @fileno(i32 %23)
%25 = load i32, i32* @TCSANOW
%26 = call i64 @tcsetattr(i32 %24, i32 %25, %struct.termios* %5)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %49
28: ; preds = %9
%29 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%30 = load i32, i32* @stdin
%31 = call i32 @fgets(i8* %1, i32 %2, i32 %30)
%32 = call i32 @strlen(i8* %1)
%33 = add nsw i32 %32, -1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %1, i64 %34
%36 = load i8, i8* %35
%37 = icmp eq i8 %36, 10
br i1 %37, label %38, label %43
38: ; preds = %28
%39 = call i32 @strlen(i8* %1)
%40 = add nsw i32 %39, -1
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %1, i64 %41
store i8 0, i8* %42
br label %43
43: ; preds = %38, %28
%44 = load i32, i32* @stdin
%45 = call i32 @fileno(i32 %44)
%46 = load i32, i32* @TCSANOW
%47 = call i64 @tcsetattr(i32 %45, i32 %46, %struct.termios* %4)
%48 = icmp eq i64 %47, 0
br i1 %48, label %52, label %49
49: ; preds = %43, %9
%50 = phi i8* [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), %9 ], [ getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), %43 ]
%51 = call i32 @halt_os_error(i8* %50)
br label %52
52: ; preds = %49, %43, %3
%53 = phi i32 [ -1, %3 ], [ 0, %43 ], [ -1, %49 ]
ret i32 %53
}