#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*/ STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, size_t, int /*<<< orphan*/);
scalar_t__ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
size_t strlen(char *);
int tcgetattr(int /*<<< orphan*/, struct termios *);
int tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
char *getpass_r(const char *prompt, char *buf, size_t buflen) {
  size_t pass_len;
  struct termios t;
  if (0 > fprintf(stdout, "%s", prompt)) {
    return NULL;
  }
  fflush(stdout);
  if (-1 == tcgetattr(STDIN_FILENO, &t)) {
    return NULL;
  }
  t.c_lflag &= ~ECHO;
  if (-1 == tcsetattr(STDIN_FILENO, TCSANOW, &t)) {
    return NULL;
  }
  t.c_lflag |= ECHO;
  if (NULL == fgets(buf, buflen, stdin)) {
    tcsetattr(STDIN_FILENO, TCSANOW, &t);
    return NULL;
  }
  if (-1 == tcsetattr(STDIN_FILENO, TCSANOW, &t)) {
    return NULL;
  }
  pass_len = strlen(buf);
  if (buf[pass_len - 1] != '\n') {
    return NULL;
  }
  buf[pass_len - 1] = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<getpass_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe78(%rip),%rsi
call   1050 <fprintf@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    11a8 <getpass_r+0x48>
movq   $0x0,-0x8(%rbp)
jmp    130a <getpass_r+0x1aa>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1070 <tcgetattr@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11e4 <getpass_r+0x84>
movq   $0x0,-0x8(%rbp)
jmp    130a <getpass_r+0x1aa>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1080 <tcsetattr@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    122d <getpass_r+0xcd>
movq   $0x0,-0x8(%rbp)
jmp    130a <getpass_r+0x1aa>
mov    0x2da4(%rip),%rax
mov    (%rax),%eax
or     -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2d9d(%rip),%rax
mov    (%rax),%edx
call   1040 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1288 <getpass_r+0x128>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
mov    0x2d70(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1080 <tcsetattr@plt>
movq   $0x0,-0x8(%rbp)
jmp    130a <getpass_r+0x1aa>
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
mov    0x2d48(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1080 <tcsetattr@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12bf <getpass_r+0x15f>
movq   $0x0,-0x8(%rbp)
jmp    130a <getpass_r+0x1aa>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     12f2 <getpass_r+0x192>
movq   $0x0,-0x8(%rbp)
jmp    130a <getpass_r+0x1aa>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getpass_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbp
mov    %rsi,%r14
mov    %rdi,%rdx
mov    0x2e4d(%rip),%rbx
mov    (%rbx),%edi
lea    0xe7c(%rip),%rsi
call   1050 <fprintf@plt>
test   %rax,%rax
js     1235 <getpass_r+0xd5>
mov    (%rbx),%edi
call   1060 <fflush@plt>
mov    0x2e20(%rip),%r15
mov    (%r15),%edi
mov    %rsp,%rsi
call   1070 <tcgetattr@plt>
cmp    $0xffffffff,%eax
je     1235 <getpass_r+0xd5>
mov    0x2e1d(%rip),%r13
mov    0x0(%r13),%eax
not    %eax
and    %eax,(%rsp)
mov    (%r15),%edi
mov    0x2e12(%rip),%r12
mov    (%r12),%esi
mov    %rsp,%rdx
call   1080 <tcsetattr@plt>
cmp    $0xffffffff,%eax
je     1235 <getpass_r+0xd5>
mov    0x0(%r13),%eax
or     %eax,(%rsp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    %rbp,%rsi
call   1040 <fgets@plt>
mov    %rax,%rbx
mov    (%r15),%edi
mov    (%r12),%esi
mov    %rsp,%rdx
call   1080 <tcsetattr@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %rbx,%rbx
je     1237 <getpass_r+0xd7>
cmp    $0xffffffff,%ecx
je     1237 <getpass_r+0xd7>
mov    %r14,%rdi
call   1030 <strlen@plt>
cmpb   $0xa,-0x1(%rax,%r14,1)
jne    1235 <getpass_r+0xd5>
movb   $0x0,-0x1(%r14,%rax,1)
mov    %r14,%rax
jmp    1237 <getpass_r+0xd7>
xor    %eax,%eax
add    $0x8,%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
@.str = [3 x i8] c"%s\00"
@STDIN_FILENO = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
@stdin = global i32 0
define i8* @getpass_r(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca %struct.termios
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i32, i32* @stdout
%11 = load i8*, i8** %5
%12 = call i64 @fprintf(i32 %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = icmp sgt i64 0, %12
br i1 %13, label %14, label %15
14: ; preds = %3
store i8* null, i8** %4
br label %70
15: ; preds = %3
%16 = load i32, i32* @stdout
%17 = call i32 @fflush(i32 %16)
%18 = load i32, i32* @STDIN_FILENO
%19 = call i32 @tcgetattr(i32 %18, %struct.termios* %9)
%20 = icmp eq i32 -1, %19
br i1 %20, label %21, label %22
21: ; preds = %15
store i8* null, i8** %4
br label %70
22: ; preds = %15
%23 = load i32, i32* @ECHO
%24 = xor i32 %23, -1
%25 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 0
%26 = load i32, i32* %25
%27 = and i32 %26, %24
store i32 %27, i32* %25
%28 = load i32, i32* @STDIN_FILENO
%29 = load i32, i32* @TCSANOW
%30 = call i32 @tcsetattr(i32 %28, i32 %29, %struct.termios* %9)
%31 = icmp eq i32 -1, %30
br i1 %31, label %32, label %33
32: ; preds = %22
store i8* null, i8** %4
br label %70
33: ; preds = %22
%34 = load i32, i32* @ECHO
%35 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 0
%36 = load i32, i32* %35
%37 = or i32 %36, %34
store i32 %37, i32* %35
%38 = load i8*, i8** %6
%39 = load i64, i64* %7
%40 = load i32, i32* @stdin
%41 = call i32* @fgets(i8* %38, i64 %39, i32 %40)
%42 = icmp eq i32* null, %41
br i1 %42, label %43, label %47
43: ; preds = %33
%44 = load i32, i32* @STDIN_FILENO
%45 = load i32, i32* @TCSANOW
%46 = call i32 @tcsetattr(i32 %44, i32 %45, %struct.termios* %9)
store i8* null, i8** %4
br label %70
47: ; preds = %33
%48 = load i32, i32* @STDIN_FILENO
%49 = load i32, i32* @TCSANOW
%50 = call i32 @tcsetattr(i32 %48, i32 %49, %struct.termios* %9)
%51 = icmp eq i32 -1, %50
br i1 %51, label %52, label %53
52: ; preds = %47
store i8* null, i8** %4
br label %70
53: ; preds = %47
%54 = load i8*, i8** %6
%55 = call i64 @strlen(i8* %54)
store i64 %55, i64* %8
%56 = load i8*, i8** %6
%57 = load i64, i64* %8
%58 = sub i64 %57, 1
%59 = getelementptr inbounds i8, i8* %56, i64 %58
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = icmp ne i32 %61, 10
br i1 %62, label %63, label %64
63: ; preds = %53
store i8* null, i8** %4
br label %70
64: ; preds = %53
%65 = load i8*, i8** %6
%66 = load i64, i64* %8
%67 = sub i64 %66, 1
%68 = getelementptr inbounds i8, i8* %65, i64 %67
store i8 0, i8* %68
%69 = load i8*, i8** %6
store i8* %69, i8** %4
br label %70
70: ; preds = %64, %63, %52, %43, %32, %21, %14
%71 = load i8*, i8** %4
ret i8* %71
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@stdout = global i32 0
@.str = [3 x i8] c"%s\00"
@STDIN_FILENO = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
@stdin = global i32 0
define i8* @getpass_r(i8* %0, i8* %1, i64 %2) {
%4 = alloca %struct.termios
%5 = bitcast %struct.termios* %4 to i8*
%6 = load i32, i32* @stdout
%7 = tail call i64 @fprintf(i32 %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%8 = icmp slt i64 %7, 0
br i1 %8, label %44, label %9
9: ; preds = %3
%10 = load i32, i32* @stdout
%11 = tail call i32 @fflush(i32 %10)
%12 = load i32, i32* @STDIN_FILENO
%13 = call i32 @tcgetattr(i32 %12, %struct.termios* %4)
%14 = icmp eq i32 %13, -1
br i1 %14, label %44, label %15
15: ; preds = %9
%16 = load i32, i32* @ECHO
%17 = xor i32 %16, -1
%18 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%19 = load i32, i32* %18
%20 = and i32 %19, %17
store i32 %20, i32* %18
%21 = load i32, i32* @STDIN_FILENO
%22 = load i32, i32* @TCSANOW
%23 = call i32 @tcsetattr(i32 %21, i32 %22, %struct.termios* %4)
%24 = icmp eq i32 %23, -1
br i1 %24, label %44, label %25
25: ; preds = %15
%26 = load i32, i32* @ECHO
%27 = load i32, i32* %18
%28 = or i32 %27, %26
store i32 %28, i32* %18
%29 = load i32, i32* @stdin
%30 = call i32* @fgets(i8* %1, i64 %2, i32 %29)
%31 = icmp eq i32* %30, null
%32 = load i32, i32* @STDIN_FILENO
%33 = load i32, i32* @TCSANOW
%34 = call i32 @tcsetattr(i32 %32, i32 %33, %struct.termios* %4)
%35 = icmp eq i32 %34, -1
%36 = select i1 %31, i1 true, i1 %35
br i1 %36, label %44, label %37
37: ; preds = %25
%38 = call i64 @strlen(i8* dereferenceable(1) %1)
%39 = add i64 %38, -1
%40 = getelementptr inbounds i8, i8* %1, i64 %39
%41 = load i8, i8* %40
%42 = icmp eq i8 %41, 10
br i1 %42, label %43, label %44
43: ; preds = %37
store i8 0, i8* %40
br label %44
44: ; preds = %25, %37, %15, %9, %3, %43
%45 = phi i8* [ %1, %43 ], [ null, %3 ], [ null, %9 ], [ null, %15 ], [ null, %37 ], [ null, %25 ]
ret i8* %45
}