#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 /*<<< orphan*/ TCSAFLUSH;
char *chomp_crlf(int /*<<< orphan*/, size_t);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, size_t, int /*<<< orphan*/ *);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
char *fgetpass(char *buf, size_t len, FILE *stream) {
  int fno = fileno(stream);
  struct termios orig;
  int err = tcgetattr(fno, &orig);
  if (err) {
    do {
      fflush(stdout);
      fprintf(stderr, "tcgetattr returned error %d for %d?", err, fno);
      fprintf(stderr, "\n");
    } while (0);
    return NULL;
  }
  struct termios next = orig;
  next.c_lflag &= ~ECHO;
  err = tcsetattr(fno, TCSAFLUSH, &next);
  if (err) {
    do {
      fflush(stdout);
      fprintf(stderr, "tcsetattr returned error %d for %d?", err, fno);
      fprintf(stderr, "\n");
    } while (0);
    return NULL;
  }
  char *str = chomp_crlf(fgets(buf, len, stream), len);
  err = tcsetattr(fno, TCSAFLUSH, &orig);
  if (err) {
    do {
      fflush(stdout);
      fprintf(stderr, "reset tcsetattr returned error %d for %d?", err, fno);
      fprintf(stderr, "\n");
    } while (0);
    return NULL;
  }
  return str;
}


x86 assembly (clang, -O0):
<fgetpass>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1080 <tcgetattr@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     11fd <fgetpass+0x8d>
jmp    11ae <fgetpass+0x3e>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
mov    -0x2c(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xe2e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1329 <fgetpass+0x1b9>
mov    -0x28(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1090 <tcsetattr@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     128b <fgetpass+0x11b>
jmp    123c <fgetpass+0xcc>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
mov    -0x2c(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xdcc(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
lea    0xdad(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1329 <fgetpass+0x1b9>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <fgets@plt>
mov    %eax,%edi
mov    -0x18(%rbp),%rsi
call   1030 <chomp_crlf@plt>
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2d33(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1090 <tcsetattr@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1321 <fgetpass+0x1b1>
jmp    12d2 <fgetpass+0x162>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
mov    -0x2c(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xd30(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2ccc(%rip),%rax
mov    (%rax),%edi
lea    0xd17(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1329 <fgetpass+0x1b9>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgetpass>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rdx,%rdi
call   1060 <fileno@plt>
mov    %eax,%r12d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1080 <tcgetattr@plt>
test   %eax,%eax
je     11e0 <fgetpass+0x70>
mov    %eax,%ebp
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    0x2e17(%rip),%r14
mov    (%r14),%edi
lea    0xe3d(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edx
mov    %r12d,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r14),%edi
lea    0xe49(%rip),%rsi
jmp    129f <fgetpass+0x12f>
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0x8(%rsp),%eax
mov    %eax,0x10(%rsp)
mov    0x2dee(%rip),%r13
mov    0x0(%r13),%esi
lea    0x10(%rsp),%rdx
mov    %r12d,%edi
call   1090 <tcsetattr@plt>
test   %eax,%eax
je     1231 <fgetpass+0xc1>
mov    %eax,%ebp
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    0xdfd(%rip),%rsi
jmp    1281 <fgetpass+0x111>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <fgets@plt>
mov    %eax,%edi
mov    %r14,%rsi
call   1030 <chomp_crlf@plt>
mov    %rax,%rbx
mov    0x0(%r13),%esi
lea    0x8(%rsp),%rdx
mov    %r12d,%edi
call   1090 <tcsetattr@plt>
test   %eax,%eax
je     12a6 <fgetpass+0x136>
mov    %eax,%ebp
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
lea    0xda5(%rip),%rsi
mov    %ebp,%edx
mov    %r12d,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
lea    0xd87(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.termios = type { i32 }
@stdout = global i32 0
@stderr = global i32 0
@.str = [36 x i8] c"tcgetattr returned error %d for %d?\00"
@.str.1 = [2 x i8] c"\0A\00"
@ECHO = global i32 0
@TCSAFLUSH = global i32 0
@.str.2 = [36 x i8] c"tcsetattr returned error %d for %d?\00"
@.str.3 = [42 x i8] c"reset tcsetattr returned error %d for %d?\00"
define i8* @fgetpass(i8* %0, i64 %1, i32* nocapture %2) {
%4 = alloca %struct.termios
%5 = alloca %struct.termios
%6 = tail call i32 @fileno(i32* %2)
%7 = bitcast %struct.termios* %4 to i8*
%8 = call i32 @tcgetattr(i32 %6, %struct.termios* %4)
%9 = icmp eq i32 %8, 0
br i1 %9, label %17, label %10
10: ; preds = %3
%11 = load i32, i32* @stdout
%12 = call i32 @fflush(i32 %11)
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %6)
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %49
17: ; preds = %3
%18 = bitcast %struct.termios* %5 to i8*
%19 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%20 = getelementptr inbounds %struct.termios, %struct.termios* %5, 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 = load i32, i32* @TCSAFLUSH
%26 = call i32 @tcsetattr(i32 %6, i32 %25, %struct.termios* %5)
%27 = icmp eq i32 %26, 0
br i1 %27, label %33, label %28
28: ; preds = %17
%29 = load i32, i32* @stdout
%30 = call i32 @fflush(i32 %29)
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %26, i32 %6)
br label %44
33: ; preds = %17
%34 = call i32 @fgets(i8* %0, i64 %1, i32* %2)
%35 = call i8* @chomp_crlf(i32 %34, i64 %1)
%36 = load i32, i32* @TCSAFLUSH
%37 = call i32 @tcsetattr(i32 %6, i32 %36, %struct.termios* %4)
%38 = icmp eq i32 %37, 0
br i1 %38, label %47, label %39
39: ; preds = %33
%40 = load i32, i32* @stdout
%41 = call i32 @fflush(i32 %40)
%42 = load i32, i32* @stderr
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i32 %37, i32 %6)
br label %44
44: ; preds = %28, %39
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %47
47: ; preds = %44, %33
%48 = phi i8* [ %35, %33 ], [ null, %44 ]
br label %49
49: ; preds = %47, %10
%50 = phi i8* [ null, %10 ], [ %48, %47 ]
ret i8* %50
}