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

/* Variables and functions */
int /*<<< orphan*/ ECHO;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ *fdopen(int, char *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int isatty(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ *stdin;
int strlen(char *);
scalar_t__ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int my_getpass(const char *prompt, char *buf, int size) {
  FILE *in = NULL;
  int is_tty = isatty(0);
  if (is_tty) {
    int fd = open("/dev/tty", O_RDONLY);
    if (fd >= 0)
      in = fdopen(fd, "r");
  } else {
    in = stdin;
  }
  struct termios termios;
  tcflag_t saved_flags = 0;
  int restore = 1;
  if (is_tty) {
    printf("%s", prompt);
    if (tcgetattr(0, &termios)) {
      restore = 0;
    } else {
      saved_flags = termios.c_lflag;
      termios.c_lflag &= ~ECHO;
      tcsetattr(0, TCSAFLUSH, &termios);
    }
  }
  char *result = fgets(buf, size, in);
  int len = 0;
  if (result) {
    len = strlen(buf);
    if (len && buf[len - 1] == '\n')
      --len;
  }
  if (is_tty && restore) {
    termios.c_lflag = saved_flags;
    puts("");
    tcsetattr(0, TCSAFLUSH, &termios);
  }
  return len;
}


x86 assembly (clang, -O0):
<my_getpass>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
xor    %edi,%edi
call   1040 <isatty@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11f9 <my_getpass+0x69>
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
lea    0xe31(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jl     11f4 <my_getpass+0x64>
mov    -0x28(%rbp),%edi
lea    0xe1e(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x20(%rbp)
jmp    1207 <my_getpass+0x77>
mov    0x2dd8(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x1,-0x38(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1281 <my_getpass+0xf1>
mov    -0x8(%rbp),%rsi
lea    0xde1(%rip),%rdi
call   1060 <printf@plt>
xor    %edi,%edi
lea    -0x30(%rbp),%rsi
call   1090 <tcgetattr@plt>
cmp    $0x0,%rax
je     1250 <my_getpass+0xc0>
movl   $0x0,-0x38(%rbp)
jmp    127c <my_getpass+0xec>
mov    -0x30(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d73(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x30(%rbp),%rdx
call   10a0 <tcsetattr@plt>
jmp    1281 <my_getpass+0xf1>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1070 <fgets@plt>
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     12e5 <my_getpass+0x155>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     12e0 <my_getpass+0x150>
mov    -0x10(%rbp),%rax
mov    -0x44(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    12e0 <my_getpass+0x150>
mov    -0x44(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x44(%rbp)
jmp    12e5 <my_getpass+0x155>
cmpl   $0x0,-0x24(%rbp)
je     131f <my_getpass+0x18f>
cmpl   $0x0,-0x38(%rbp)
je     131f <my_getpass+0x18f>
mov    -0x34(%rbp),%eax
mov    %eax,-0x30(%rbp)
lea    0xd04(%rip),%rdi
call   1030 <puts@plt>
mov    0x2cd6(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x30(%rbp),%rdx
call   10a0 <tcsetattr@plt>
mov    -0x44(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_getpass>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r12
mov    %rdi,%rbx
xor    %r13d,%r13d
xor    %edi,%edi
call   1040 <isatty@plt>
mov    %eax,%r15d
test   %eax,%eax
je     11e4 <my_getpass+0x54>
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
lea    0xe3b(%rip),%rdi
call   10b0 <open@plt>
xor    %r13d,%r13d
test   %eax,%eax
js     11f0 <my_getpass+0x60>
lea    0xe31(%rip),%rsi
mov    %eax,%edi
call   1080 <fdopen@plt>
mov    %rax,%rbp
jmp    11f2 <my_getpass+0x62>
mov    0x2ded(%rip),%rax
mov    (%rax),%rbp
jmp    123c <my_getpass+0xac>
xor    %ebp,%ebp
lea    0xe12(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    %rsp,%rsi
xor    %edi,%edi
call   1090 <tcgetattr@plt>
test   %rax,%rax
je     1214 <my_getpass+0x84>
xor    %ebx,%ebx
jmp    123e <my_getpass+0xae>
mov    (%rsp),%r13d
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %r13d,%eax
mov    %eax,(%rsp)
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
xor    %edi,%edi
call   10a0 <tcsetattr@plt>
mov    $0x1,%bl
mov    %r12,%rdi
mov    %r14d,%esi
mov    %rbp,%rdx
call   1070 <fgets@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     126f <my_getpass+0xdf>
mov    %r12,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
je     126f <my_getpass+0xdf>
movslq %eax,%rcx
lea    -0x1(%rcx),%rbp
cmpb   $0xa,-0x1(%r12,%rcx,1)
cmovne %eax,%ebp
test   %r15d,%r15d
sete   %al
xor    $0x1,%bl
or     %al,%bl
jne    129d <my_getpass+0x10d>
mov    %r13d,(%rsp)
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
xor    %edi,%edi
call   10a0 <tcsetattr@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [9 x i8] c"/dev/tty\00"
@O_RDONLY = global i32 0
@.str.1 = [2 x i8] c"r\00"
@stdin = global i32* null
@.str.2 = [3 x i8] c"%s\00"
@ECHO = global i32 0
@TCSAFLUSH = global i32 0
define i32 @my_getpass(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.termios
%5 = tail call i32 @isatty(i32 0)
%6 = icmp ne i32 %5, 0
br i1 %6, label %7, label %13
7: ; preds = %3
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp sgt i32 %9, -1
br i1 %10, label %11, label %16
11: ; preds = %7
%12 = tail call i32* @fdopen(i32 %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %16
13: ; preds = %3
%14 = load i32*, i32** @stdin
%15 = bitcast %struct.termios* %4 to i8*
br label %30
16: ; preds = %7, %11
%17 = phi i32* [ null, %7 ], [ %12, %11 ]
%18 = bitcast %struct.termios* %4 to i8*
%19 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%20 = call i64 @tcgetattr(i32 0, %struct.termios* %4)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %30
22: ; preds = %16
%23 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%24 = load i32, i32* %23
%25 = load i32, i32* @ECHO
%26 = xor i32 %25, -1
%27 = and i32 %24, %26
store i32 %27, i32* %23
%28 = load i32, i32* @TCSAFLUSH
%29 = call i32 @tcsetattr(i32 0, i32 %28, %struct.termios* %4)
br label %30
30: ; preds = %13, %16, %22
%31 = phi i32* [ %17, %22 ], [ %14, %13 ], [ %17, %16 ]
%32 = phi i32 [ %24, %22 ], [ 0, %13 ], [ 0, %16 ]
%33 = phi i1 [ true, %22 ], [ true, %13 ], [ false, %16 ]
%34 = call i8* @fgets(i8* %1, i32 %2, i32* %31)
%35 = icmp eq i8* %34, null
br i1 %35, label %46, label %36
36: ; preds = %30
%37 = call i32 @strlen(i8* %1)
%38 = icmp eq i32 %37, 0
br i1 %38, label %46, label %39
39: ; preds = %36
%40 = add nsw i32 %37, -1
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %1, i64 %41
%43 = load i8, i8* %42
%44 = icmp eq i8 %43, 10
%45 = select i1 %44, i32 %40, i32 %37
br label %46
46: ; preds = %39, %36, %30
%47 = phi i32 [ 0, %36 ], [ 0, %30 ], [ %45, %39 ]
%48 = and i1 %6, %33
br i1 %48, label %49, label %54
49: ; preds = %46
%50 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
store i32 %32, i32* %50
%51 = call i32 @putchar(i32 10)
%52 = load i32, i32* @TCSAFLUSH
%53 = call i32 @tcsetattr(i32 0, i32 %52, %struct.termios* %4)
br label %54
54: ; preds = %49, %46
%55 = bitcast %struct.termios* %4 to i8*
ret i32 %47
}