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

/* Variables and functions */
int fileno(int /*<<< orphan*/);
int gp_load_term(int, struct termios *);
int gp_save_term(int, struct termios *);
int /*<<< orphan*/ gp_set_password_flags(struct termios *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stdin;
int palla_getpass(char *dest, size_t len) {
  unsigned int i;
  int ttyfd = fileno(stdin);
  struct termios orig, new;
  if (!len || !dest)
    return -1;
  if (gp_save_term(ttyfd, &orig) == -1)
    return -1;
  new = orig;
  gp_set_password_flags(&new);
  if (gp_load_term(ttyfd, &new) == -1)
    return -1;
  i = 0;
  while (i < (len - 1)) {
    char c;
    if (read(ttyfd, &c, 1) <= 0)
      break;
    if (c == '\n')
      break;
    dest[i] = c;
    i++;
  }
  dest[i] = '\0';
  if (gp_load_term(ttyfd, &orig) == -1)
    return -1;
  return i;
}


x86 assembly (clang, -O0):
<palla_getpass>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
call   1050 <fileno@plt>
mov    %eax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1187 <palla_getpass+0x37>
cmpq   $0x0,-0x10(%rbp)
jne    1193 <palla_getpass+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <palla_getpass+0x12e>
mov    -0x20(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1060 <gp_save_term@plt>
cmp    $0xffffffff,%eax
jne    11b4 <palla_getpass+0x64>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <palla_getpass+0x12e>
mov    -0x28(%rbp),%eax
mov    %eax,-0x30(%rbp)
lea    -0x30(%rbp),%rdi
call   1070 <gp_set_password_flags@plt>
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1030 <gp_load_term@plt>
cmp    $0xffffffff,%eax
jne    11e4 <palla_getpass+0x94>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <palla_getpass+0x12e>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jae    124c <palla_getpass+0xfc>
mov    -0x20(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
jg     121f <palla_getpass+0xcf>
jmp    124c <palla_getpass+0xfc>
movsbl -0x31(%rbp),%eax
cmp    $0xa,%eax
jne    1231 <palla_getpass+0xe1>
jmp    124c <palla_getpass+0xfc>
mov    -0x31(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11eb <palla_getpass+0x9b>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1030 <gp_load_term@plt>
cmp    $0xffffffff,%eax
jne    1278 <palla_getpass+0x128>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <palla_getpass+0x12e>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<palla_getpass>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbp
mov    %rdi,%r14
mov    0x2e7f(%rip),%rax
mov    (%rax),%edi
call   1050 <fileno@plt>
mov    $0xffffffff,%ebx
test   %r14,%r14
je     1224 <palla_getpass+0xd4>
test   %rbp,%rbp
je     1224 <palla_getpass+0xd4>
mov    %eax,%r15d
lea    0x10(%rsp),%rsi
mov    %eax,%edi
call   1060 <gp_save_term@plt>
cmp    $0xffffffff,%eax
je     1224 <palla_getpass+0xd4>
mov    0x10(%rsp),%eax
mov    %eax,0x18(%rsp)
lea    0x18(%rsp),%r12
mov    %r12,%rdi
call   1070 <gp_set_password_flags@plt>
mov    %r15d,%edi
mov    %r12,%rsi
call   1030 <gp_load_term@plt>
cmp    $0xffffffff,%eax
je     1224 <palla_getpass+0xd4>
xor    %ebx,%ebx
add    $0xffffffffffffffff,%rbp
je     120a <palla_getpass+0xba>
lea    0xf(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %rax,%rax
jle    120a <palla_getpass+0xba>
movzbl 0xf(%rsp),%eax
cmp    $0xa,%al
je     120a <palla_getpass+0xba>
mov    %al,(%r14,%rbx,1)
add    $0x1,%ebx
cmp    %rbx,%rbp
ja     11e0 <palla_getpass+0x90>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
call   1030 <gp_load_term@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@stdin = global i32 0
define i32 @palla_getpass(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.termios
%9 = alloca %struct.termios
%10 = alloca i8
store i8* %0, i8** %4
store i64 %1, i64* %5
%11 = load i32, i32* @stdin
%12 = call i32 @fileno(i32 %11)
store i32 %12, i32* %7
%13 = load i64, i64* %5
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = icmp ne i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %15, %2
store i32 -1, i32* %3
br label %68
19: ; preds = %15
%20 = load i32, i32* %7
%21 = call i32 @gp_save_term(i32 %20, %struct.termios* %8)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %19
store i32 -1, i32* %3
br label %68
24: ; preds = %19
%25 = bitcast %struct.termios* %9 to i8*
%26 = bitcast %struct.termios* %8 to i8*
%27 = call i32 @gp_set_password_flags(%struct.termios* %9)
%28 = load i32, i32* %7
%29 = call i32 @gp_load_term(i32 %28, %struct.termios* %9)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %24
store i32 -1, i32* %3
br label %68
32: ; preds = %24
store i32 0, i32* %6
br label %33
33: ; preds = %49, %32
%34 = load i32, i32* %6
%35 = zext i32 %34 to i64
%36 = load i64, i64* %5
%37 = sub i64 %36, 1
%38 = icmp ult i64 %35, %37
br i1 %38, label %39, label %57
39: ; preds = %33
%40 = load i32, i32* %7
%41 = call i64 @read(i32 %40, i8* %10, i32 1)
%42 = icmp sle i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %39
br label %57
44: ; preds = %39
%45 = load i8, i8* %10
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 10
br i1 %47, label %48, label %49
48: ; preds = %44
br label %57
49: ; preds = %44
%50 = load i8, i8* %10
%51 = load i8*, i8** %4
%52 = load i32, i32* %6
%53 = zext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
store i8 %50, i8* %54
%55 = load i32, i32* %6
%56 = add i32 %55, 1
store i32 %56, i32* %6
br label %33
57: ; preds = %48, %43, %33
%58 = load i8*, i8** %4
%59 = load i32, i32* %6
%60 = zext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %58, i64 %60
store i8 0, i8* %61
%62 = load i32, i32* %7
%63 = call i32 @gp_load_term(i32 %62, %struct.termios* %8)
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %66
65: ; preds = %57
store i32 -1, i32* %3
br label %68
66: ; preds = %57
%67 = load i32, i32* %6
store i32 %67, i32* %3
br label %68
68: ; preds = %66, %65, %31, %23, %18
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@stdin = global i32 0
define i32 @palla_getpass(i8* writeonly %0, i64 %1) {
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = alloca i8
%6 = load i32, i32* @stdin
%7 = tail call i32 @fileno(i32 %6)
%8 = bitcast %struct.termios* %3 to i8*
%9 = bitcast %struct.termios* %4 to i8*
%10 = icmp ne i64 %1, 0
%11 = icmp ne i8* %0, null
%12 = and i1 %11, %10
br i1 %12, label %13, label %48
13: ; preds = %2
%14 = call i32 @gp_save_term(i32 %7, %struct.termios* %3)
%15 = icmp eq i32 %14, -1
br i1 %15, label %48, label %16
16: ; preds = %13
%17 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%18 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%19 = load i32, i32* %17
store i32 %19, i32* %18
%20 = call i32 @gp_set_password_flags(%struct.termios* %4)
%21 = call i32 @gp_load_term(i32 %7, %struct.termios* %4)
%22 = icmp eq i32 %21, -1
br i1 %22, label %48, label %23
23: ; preds = %16
%24 = add i64 %1, -1
%25 = icmp eq i64 %24, 0
br i1 %25, label %41, label %26
26: ; preds = %23, %36
%27 = phi i64 [ %39, %36 ], [ 0, %23 ]
%28 = trunc i64 %27 to i32
%29 = call i64 @read(i32 %7, i8* %5, i32 1)
%30 = icmp slt i64 %29, 1
br i1 %30, label %34, label %31
31: ; preds = %26
%32 = load i8, i8* %5
%33 = icmp eq i8 %32, 10
br i1 %33, label %34, label %36
34: ; preds = %26, %31
%35 = trunc i64 %27 to i32
br label %41
36: ; preds = %31
%37 = getelementptr inbounds i8, i8* %0, i64 %27
store i8 %32, i8* %37
%38 = add i32 %28, 1
%39 = zext i32 %38 to i64
%40 = icmp ugt i64 %24, %39
br i1 %40, label %26, label %41
41: ; preds = %36, %23, %34
%42 = phi i32 [ %35, %34 ], [ 0, %23 ], [ %38, %36 ]
%43 = zext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %0, i64 %43
store i8 0, i8* %44
%45 = call i32 @gp_load_term(i32 %7, %struct.termios* %3)
%46 = icmp eq i32 %45, -1
%47 = select i1 %46, i32 -1, i32 %42
br label %48
48: ; preds = %41, %16, %13, %2
%49 = phi i32 [ -1, %2 ], [ -1, %13 ], [ -1, %16 ], [ %47, %41 ]
ret i32 %49
}