#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;
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ tcgetattr(int /*<<< orphan*/, struct termios *);
scalar_t__ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
size_t my_getpass(char *prompt, char **lineptr, size_t *n, FILE *stream) {
  struct termios old, new;
  int nread;
  if (tcgetattr(fileno(stream), &old) != 0)
    return -1;
  new = old;
  new.c_lflag &= ~ECHO;
  if (tcsetattr(fileno(stream), TCSAFLUSH, &new) != 0)
    return -1;
  if (prompt)
    printf("%s", prompt);
  nread = getline(lineptr, n, stream);
  if (nread >= 1 && (*lineptr)[nread - 1] == '\n') {
    (*lineptr)[nread - 1] = 0;
    nread--;
  }
  printf("\n");
  tcsetattr(fileno(stream), TCSAFLUSH, &old);
  return nread;
}


x86 assembly (clang, -O0):
<my_getpass>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,%edi
lea    -0x30(%rbp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0x0,%rax
je     1193 <my_getpass+0x43>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1287 <my_getpass+0x137>
mov    -0x30(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e40(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
je     11df <my_getpass+0x8f>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1287 <my_getpass+0x137>
cmpq   $0x0,-0x10(%rbp)
je     11fc <my_getpass+0xac>
mov    -0x10(%rbp),%rsi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1070 <getline@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x1,-0x3c(%rbp)
jl     1254 <my_getpass+0x104>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x3c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1254 <my_getpass+0x104>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x3c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x3c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x3c(%rbp)
lea    0xda8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,%edi
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1060 <tcsetattr@plt>
movslq -0x3c(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_getpass>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
mov    %rcx,%rdi
call   1050 <fileno@plt>
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1060 <tcgetattr@plt>
mov    $0xffffffffffffffff,%rbp
test   %rax,%rax
jne    1238 <my_getpass+0xd8>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0x8(%rsp),%eax
mov    %eax,0x10(%rsp)
mov    %rbx,%rdi
call   1050 <fileno@plt>
mov    0x2e28(%rip),%r13
mov    0x0(%r13),%esi
lea    0x10(%rsp),%rdx
mov    %eax,%edi
call   1070 <tcsetattr@plt>
test   %rax,%rax
jne    1238 <my_getpass+0xd8>
test   %r12,%r12
je     11eb <my_getpass+0x8b>
lea    0xe1f(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1080 <getline@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1213 <my_getpass+0xb3>
mov    (%r14),%rax
mov    %ebp,%ecx
add    $0xffffffff,%ecx
cmpb   $0xa,(%rax,%rcx,1)
jne    1213 <my_getpass+0xb3>
movb   $0x0,(%rax,%rcx,1)
mov    %ecx,%ebp
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   1050 <fileno@plt>
mov    0x0(%r13),%esi
lea    0x8(%rsp),%rdx
mov    %eax,%edi
call   1070 <tcsetattr@plt>
movslq %ebp,%rbp
mov    %rbp,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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