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

/* Variables and functions */
int ECHO;
int ECHOE;
int ECHOK;
int ECHONL;
int ICANON;
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
char getc(int /*<<< orphan*/);
char *malloc(int);
char *realloc(char *, int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
scalar_t__ tcgetattr(int /*<<< orphan*/, struct termios *);
scalar_t__ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
char *get_passwd(const char *prompt) {
  struct termios old, new;
  int size = 0;
  char c;
  char *pass = malloc(size + 1);
  if (tcgetattr(fileno(stdin), &old) != 0)
    return NULL;
  new = old;
  new.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL | ICANON);
  if (tcsetattr(fileno(stdin), TCSAFLUSH, &new) != 0)
    return NULL;
  if (prompt)
    fprintf(stdout, "%s", prompt);
  while ((c = getc(stdin)) != '\n') {
    if (c == 127) {
      if (size > 0)
        size--;
    } else {
      size++;
      pass = realloc(pass, size + 1);
      *(pass + size - 1) = c;
    }
  }
  *(pass + size) = '\0';
  tcsetattr(fileno(stdin), TCSAFLUSH, &old);
  return pass;
}


x86 assembly (clang, -O0):
<get_passwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
call   1040 <fileno@plt>
mov    %eax,%edi
lea    -0x18(%rbp),%rsi
call   1070 <tcgetattr@plt>
cmp    $0x0,%rax
je     11c2 <get_passwd+0x52>
movq   $0x0,-0x8(%rbp)
jmp    1308 <get_passwd+0x198>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    0x2df8(%rip),%rcx
or     (%rcx),%eax
mov    0x2ddf(%rip),%rcx
or     (%rcx),%eax
mov    0x2dce(%rip),%rcx
or     (%rcx),%eax
mov    0x2db5(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1040 <fileno@plt>
mov    %eax,%edi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1080 <tcsetattr@plt>
cmp    $0x0,%rax
je     1237 <get_passwd+0xc7>
movq   $0x0,-0x8(%rbp)
jmp    1308 <get_passwd+0x198>
cmpq   $0x0,-0x10(%rbp)
je     125b <get_passwd+0xeb>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdaa(%rip),%rsi
call   1030 <fprintf@plt>
jmp    1260 <get_passwd+0xf0>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   1090 <getc@plt>
mov    %al,-0x25(%rbp)
movsbl %al,%eax
cmp    $0xa,%eax
je     12d2 <get_passwd+0x162>
movsbl -0x25(%rbp),%eax
cmp    $0x7f,%eax
jne    12a2 <get_passwd+0x132>
cmpl   $0x0,-0x24(%rbp)
jle    129d <get_passwd+0x12d>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    12cd <get_passwd+0x15d>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
add    $0x1,%esi
call   1060 <realloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x25(%rbp),%dl
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %dl,-0x1(%rax,%rcx,1)
jmp    1260 <get_passwd+0xf0>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
call   1040 <fileno@plt>
mov    %eax,%edi
mov    0x2cf3(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1080 <tcsetattr@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_passwd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    0x2e4b(%rip),%r12
mov    (%r12),%edi
call   1040 <fileno@plt>
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1070 <tcgetattr@plt>
test   %rax,%rax
jne    1209 <get_passwd+0x99>
mov    0x2e22(%rip),%rax
mov    0x2e13(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2df8(%rip),%rax
or     (%rax),%ecx
mov    0x2de7(%rip),%rax
or     (%rax),%ecx
mov    0x2dce(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    0x8(%rsp),%ecx
mov    %ecx,0x10(%rsp)
mov    (%r12),%edi
call   1040 <fileno@plt>
mov    0x2df2(%rip),%rcx
mov    (%rcx),%esi
lea    0x10(%rsp),%rdx
mov    %eax,%edi
call   1080 <tcsetattr@plt>
test   %rax,%rax
je     121e <get_passwd+0xae>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
test   %r14,%r14
je     123b <get_passwd+0xcb>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
lea    0xdcd(%rip),%rsi
mov    %r14,%rdx
call   1030 <fprintf@plt>
xor    %ebp,%ebp
jmp    1249 <get_passwd+0xd9>
nop
xor    %eax,%eax
test   %ebp,%ebp
setg   %al
sub    %eax,%ebp
mov    (%r12),%edi
call   1090 <getc@plt>
cmp    $0x7f,%al
je     1240 <get_passwd+0xd0>
mov    %eax,%ebx
cmp    $0xa,%al
je     127c <get_passwd+0x10c>
movslq %ebp,%r14
lea    0x1(%r14),%r13
add    $0x2,%ebp
mov    %r15,%rdi
mov    %ebp,%esi
call   1060 <realloc@plt>
mov    %rax,%r15
mov    %bl,(%rax,%r14,1)
mov    %r13d,%ebp
jmp    1249 <get_passwd+0xd9>
movslq %ebp,%rax
movb   $0x0,(%r15,%rax,1)
mov    (%r12),%edi
call   1040 <fileno@plt>
mov    0x2d54(%rip),%rcx
mov    (%rcx),%esi
lea    0x8(%rsp),%rdx
mov    %eax,%edi
call   1080 <tcsetattr@plt>
jmp    120c <get_passwd+0x9c>

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

LLVM IR (-O2):
%struct.termios = type { i32 }
@stdin = global i32 0
@ECHO = global i32 0
@ECHOE = global i32 0
@ECHOK = global i32 0
@ECHONL = global i32 0
@ICANON = global i32 0
@TCSAFLUSH = global i32 0
@stdout = global i32 0
@.str = [3 x i8] c"%s\00"
define i8* @get_passwd(i8* %0) {
%2 = alloca %struct.termios
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %2 to i8*
%5 = bitcast %struct.termios* %3 to i8*
%6 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%7 = load i32, i32* @stdin
%8 = tail call i32 @fileno(i32 %7)
%9 = call i64 @tcgetattr(i32 %8, %struct.termios* %2)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %62
11: ; preds = %1
%12 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%13 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%14 = load i32, i32* %12
%15 = load i32, i32* @ECHO
%16 = load i32, i32* @ECHOE
%17 = or i32 %16, %15
%18 = load i32, i32* @ECHOK
%19 = or i32 %17, %18
%20 = load i32, i32* @ECHONL
%21 = or i32 %19, %20
%22 = load i32, i32* @ICANON
%23 = or i32 %21, %22
%24 = xor i32 %23, -1
%25 = and i32 %14, %24
store i32 %25, i32* %13
%26 = load i32, i32* @stdin
%27 = call i32 @fileno(i32 %26)
%28 = load i32, i32* @TCSAFLUSH
%29 = call i64 @tcsetattr(i32 %27, i32 %28, %struct.termios* %3)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %62
31: ; preds = %11
%32 = icmp eq i8* %0, null
br i1 %32, label %36, label %33
33: ; preds = %31
%34 = load i32, i32* @stdout
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %36
36: ; preds = %33, %31
br label %37
37: ; preds = %36, %48
%38 = phi i32 [ 0, %36 ], [ %49, %48 ]
%39 = phi i8* [ %6, %36 ], [ %51, %48 ]
br label %40
40: ; preds = %37, %44
%41 = phi i32 [ %47, %44 ], [ %38, %37 ]
%42 = load i32, i32* @stdin
%43 = call signext i8 @getc(i32 %42)
switch i8 %43, label %48 [
i8 10, label %55
i8 127, label %44
]
44: ; preds = %40
%45 = icmp sgt i32 %41, 0
%46 = sext i1 %45 to i32
%47 = add nsw i32 %41, %46
br label %40
48: ; preds = %40
%49 = add nsw i32 %41, 1
%50 = add nsw i32 %41, 2
%51 = call i8* @realloc(i8* %39, i32 %50)
%52 = sext i32 %49 to i64
%53 = getelementptr inbounds i8, i8* %51, i64 -1
%54 = getelementptr inbounds i8, i8* %53, i64 %52
store i8 %43, i8* %54
br label %37
55: ; preds = %40
%56 = sext i32 %41 to i64
%57 = getelementptr inbounds i8, i8* %39, i64 %56
store i8 0, i8* %57
%58 = load i32, i32* @stdin
%59 = call i32 @fileno(i32 %58)
%60 = load i32, i32* @TCSAFLUSH
%61 = call i64 @tcsetattr(i32 %59, i32 %60, %struct.termios* %2)
br label %62
62: ; preds = %11, %1, %55
%63 = phi i8* [ %39, %55 ], [ null, %1 ], [ null, %11 ]
ret i8* %63
}