#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;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int ECHO;
int ISIG;
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ _IONBF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ putc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ setvbuf(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                           int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ *stderr;
int /*<<< orphan*/ *stdin;
int strlen(char *);
scalar_t__ tcgetattr(int /*<<< orphan*/, struct termios *);
scalar_t__ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
char *getpasswd(const char *prompt) {
  static char *buf;
  FILE *in, *out;
  struct termios s, t;
  int tty_changed;
  int nread;
  free(buf);
  buf = malloc(256);
  in = fopen("/dev/tty", "r+");
  if (in == NULL) {
    in = stdin;
    out = stderr;
  } else
    out = in;
  if (tcgetattr(fileno(in), &t) == 0) {
    s = t;
    t.c_lflag &= ~(ECHO | ISIG);
    tty_changed = (tcsetattr(fileno(in), TCSAFLUSH, &t) == 0);
    if (in != stdin) {
      setvbuf(in, NULL, _IONBF, 0);
    }
  } else
    tty_changed = 0;
  fputs(prompt, out);
  fflush(out);
  fgets(buf, 256 - 1, in);
  if (buf != NULL) {
    nread = strlen(buf);
    if (nread < 0)
      buf[0] = '\0';
    else if (buf[nread - 1] == '\n') {
      buf[nread - 1] = '\0';
      if (tty_changed)
        putc('\n', out);
    }
  }
  if (tty_changed) {
    (void)tcsetattr(fileno(in), TCSAFLUSH, &s);
  }
  if (in != stdin)
    fclose(in);
  return buf;
}


x86 assembly (clang, -O0):
<getpasswd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2ead(%rip),%rdi
call   1030 <free@plt>
mov    $0x100,%edi
call   10a0 <malloc@plt>
mov    %rax,0x2e97(%rip)
lea    0xe00(%rip),%rdi
lea    0xe02(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    123c <getpasswd+0x6c>
mov    0x2dbe(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
mov    0x2d88(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    1244 <getpasswd+0x74>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <fileno@plt>
mov    %eax,%edi
lea    -0x28(%rbp),%rsi
call   10d0 <tcgetattr@plt>
cmp    $0x0,%rax
jne    12e0 <getpasswd+0x110>
mov    -0x28(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d69(%rip),%rax
mov    (%rax),%eax
mov    0x2d58(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <fileno@plt>
mov    %eax,%edi
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   10e0 <tcsetattr@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2d26(%rip),%rcx
cmp    (%rcx),%rax
je     12db <getpasswd+0x10b>
mov    -0x10(%rbp),%rdi
mov    0x2cfa(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
xor    %ecx,%ecx
call   10c0 <setvbuf@plt>
jmp    12e7 <getpasswd+0x117>
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <fputs@plt>
mov    -0x18(%rbp),%rdi
call   10b0 <fflush@plt>
mov    0x2d8c(%rip),%rdi
mov    -0x10(%rbp),%rdx
mov    $0xff,%esi
call   1070 <fgets@plt>
cmpq   $0x0,0x2d76(%rip)
je     13a0 <getpasswd+0x1d0>
mov    0x2d69(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    1348 <getpasswd+0x178>
mov    0x2d50(%rip),%rax
movb   $0x0,(%rax)
jmp    139b <getpasswd+0x1cb>
mov    0x2d41(%rip),%rax
mov    -0x30(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1396 <getpasswd+0x1c6>
mov    0x2d24(%rip),%rax
mov    -0x30(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0x0,-0x2c(%rbp)
je     1391 <getpasswd+0x1c1>
mov    -0x18(%rbp),%rsi
mov    $0xa,%edi
call   1080 <putc@plt>
jmp    1396 <getpasswd+0x1c6>
jmp    139b <getpasswd+0x1cb>
jmp    13a0 <getpasswd+0x1d0>
cmpl   $0x0,-0x2c(%rbp)
je     13c7 <getpasswd+0x1f7>
mov    -0x10(%rbp),%rdi
call   1090 <fileno@plt>
mov    %eax,%edi
mov    0x2c2c(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   10e0 <tcsetattr@plt>
mov    -0x10(%rbp),%rax
mov    0x2c0e(%rip),%rcx
cmp    (%rcx),%rax
je     13e4 <getpasswd+0x214>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2ca5(%rip),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getpasswd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r15
mov    0x2eaa(%rip),%rdi
call   1030 <free@plt>
mov    $0x100,%edi
call   10a0 <malloc@plt>
mov    %rax,0x2e94(%rip)
lea    0xdfd(%rip),%rdi
lea    0xdff(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,%r14
mov    0x2dc7(%rip),%r12
mov    %rax,%rbx
test   %rax,%rax
jne    122f <getpasswd+0x5f>
mov    0x2d90(%rip),%rax
mov    (%rax),%r14
mov    (%r12),%rbx
mov    %rbx,%rdi
call   1090 <fileno@plt>
mov    %rsp,%rsi
mov    %eax,%edi
call   10d0 <tcgetattr@plt>
test   %rax,%rax
je     124a <getpasswd+0x7a>
xor    %ebp,%ebp
jmp    12a7 <getpasswd+0xd7>
mov    (%rsp),%eax
mov    0x2d84(%rip),%rcx
mov    0x2d75(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    %eax,0x8(%rsp)
not    %edx
and    %eax,%edx
mov    %edx,(%rsp)
mov    %rbx,%rdi
call   1090 <fileno@plt>
mov    0x2d6f(%rip),%rcx
mov    (%rcx),%esi
mov    %rsp,%rdx
mov    %eax,%edi
call   10e0 <tcsetattr@plt>
test   %rax,%rax
sete   %bpl
cmp    (%r12),%rbx
je     12a7 <getpasswd+0xd7>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
xor    %ecx,%ecx
call   10c0 <setvbuf@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <fputs@plt>
mov    %r14,%rdi
call   10b0 <fflush@plt>
mov    0x2dcf(%rip),%rdi
mov    $0xff,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
mov    0x2dbb(%rip),%rdi
test   %rdi,%rdi
je     130f <getpasswd+0x13f>
call   1050 <strlen@plt>
mov    0x2daa(%rip),%rcx
test   %eax,%eax
js     130c <getpasswd+0x13c>
cltq
cmpb   $0xa,-0x1(%rax,%rcx,1)
jne    130f <getpasswd+0x13f>
movb   $0x0,-0x1(%rcx,%rax,1)
test   %bpl,%bpl
je     1331 <getpasswd+0x161>
mov    $0xa,%edi
mov    %r14,%rsi
call   1080 <putc@plt>
jmp    1314 <getpasswd+0x144>
movb   $0x0,(%rcx)
test   %bpl,%bpl
je     1331 <getpasswd+0x161>
mov    %rbx,%rdi
call   1090 <fileno@plt>
mov    0x2cc5(%rip),%rcx
mov    (%rcx),%esi
lea    0x8(%rsp),%rdx
mov    %eax,%edi
call   10e0 <tcsetattr@plt>
cmp    (%r12),%rbx
je     133f <getpasswd+0x16f>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2d4a(%rip),%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@getpasswd.buf = internal global i8* null
@.str = [9 x i8] c"/dev/tty\00"
@.str.1 = [3 x i8] c"r+\00"
@stdin = global i32* null
@stderr = global i32* null
@ECHO = global i32 0
@ISIG = global i32 0
@TCSAFLUSH = global i32 0
@_IONBF = global i32 0
define i8* @getpasswd(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca %struct.termios
%6 = alloca %struct.termios
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %2
%9 = load i8*, i8** @getpasswd.buf
%10 = call i32 @free(i8* %9)
%11 = call i8* @malloc(i32 256)
store i8* %11, i8** @getpasswd.buf
%12 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %3
%13 = load i32*, i32** %3
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = load i32*, i32** @stdin
store i32* %16, i32** %3
%17 = load i32*, i32** @stderr
store i32* %17, i32** %4
br label %20
18: ; preds = %1
%19 = load i32*, i32** %3
store i32* %19, i32** %4
br label %20
20: ; preds = %18, %15
%21 = load i32*, i32** %3
%22 = call i32 @fileno(i32* %21)
%23 = call i64 @tcgetattr(i32 %22, %struct.termios* %6)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %49
25: ; preds = %20
%26 = bitcast %struct.termios* %5 to i8*
%27 = bitcast %struct.termios* %6 to i8*
%28 = load i32, i32* @ECHO
%29 = load i32, i32* @ISIG
%30 = or i32 %28, %29
%31 = xor i32 %30, -1
%32 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%33 = load i32, i32* %32
%34 = and i32 %33, %31
store i32 %34, i32* %32
%35 = load i32*, i32** %3
%36 = call i32 @fileno(i32* %35)
%37 = load i32, i32* @TCSAFLUSH
%38 = call i64 @tcsetattr(i32 %36, i32 %37, %struct.termios* %6)
%39 = icmp eq i64 %38, 0
%40 = zext i1 %39 to i32
store i32 %40, i32* %7
%41 = load i32*, i32** %3
%42 = load i32*, i32** @stdin
%43 = icmp ne i32* %41, %42
br i1 %43, label %44, label %48
44: ; preds = %25
%45 = load i32*, i32** %3
%46 = load i32, i32* @_IONBF
%47 = call i32 @setvbuf(i32* %45, i32* null, i32 %46, i32 0)
br label %48
48: ; preds = %44, %25
br label %50
49: ; preds = %20
store i32 0, i32* %7
br label %50
50: ; preds = %49, %48
%51 = load i8*, i8** %2
%52 = load i32*, i32** %4
%53 = call i32 @fputs(i8* %51, i32* %52)
%54 = load i32*, i32** %4
%55 = call i32 @fflush(i32* %54)
%56 = load i8*, i8** @getpasswd.buf
%57 = load i32*, i32** %3
%58 = call i32 @fgets(i8* %56, i32 255, i32* %57)
%59 = load i8*, i8** @getpasswd.buf
%60 = icmp ne i8* %59, null
br i1 %60, label %61, label %92
61: ; preds = %50
%62 = load i8*, i8** @getpasswd.buf
%63 = call i32 @strlen(i8* %62)
store i32 %63, i32* %8
%64 = load i32, i32* %8
%65 = icmp slt i32 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %61
%67 = load i8*, i8** @getpasswd.buf
%68 = getelementptr inbounds i8, i8* %67, i64 0
store i8 0, i8* %68
br label %91
69: ; preds = %61
%70 = load i8*, i8** @getpasswd.buf
%71 = load i32, i32* %8
%72 = sub nsw i32 %71, 1
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i8, i8* %70, i64 %73
%75 = load i8, i8* %74
%76 = sext i8 %75 to i32
%77 = icmp eq i32 %76, 10
br i1 %77, label %78, label %90
78: ; preds = %69
%79 = load i8*, i8** @getpasswd.buf
%80 = load i32, i32* %8
%81 = sub nsw i32 %80, 1
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i8, i8* %79, i64 %82
store i8 0, i8* %83
%84 = load i32, i32* %7
%85 = icmp ne i32 %84, 0
br i1 %85, label %86, label %89
86: ; preds = %78
%87 = load i32*, i32** %4
%88 = call i32 @putc(i8 signext 10, i32* %87)
br label %89
89: ; preds = %86, %78
br label %90
90: ; preds = %89, %69
br label %91
91: ; preds = %90, %66
br label %92
92: ; preds = %91, %50
%93 = load i32, i32* %7
%94 = icmp ne i32 %93, 0
br i1 %94, label %95, label %100
95: ; preds = %92
%96 = load i32*, i32** %3
%97 = call i32 @fileno(i32* %96)
%98 = load i32, i32* @TCSAFLUSH
%99 = call i64 @tcsetattr(i32 %97, i32 %98, %struct.termios* %5)
br label %100
100: ; preds = %95, %92
%101 = load i32*, i32** %3
%102 = load i32*, i32** @stdin
%103 = icmp ne i32* %101, %102
br i1 %103, label %104, label %107
104: ; preds = %100
%105 = load i32*, i32** %3
%106 = call i32 @fclose(i32* %105)
br label %107
107: ; preds = %104, %100
%108 = load i8*, i8** @getpasswd.buf
ret i8* %108
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@getpasswd.buf = internal unnamed_addr global i8* null
@.str = [9 x i8] c"/dev/tty\00"
@.str.1 = [3 x i8] c"r+\00"
@stdin = global i32* null
@stderr = global i32* null
@ECHO = global i32 0
@ISIG = global i32 0
@TCSAFLUSH = global i32 0
@_IONBF = global i32 0
define i8* @getpasswd(i8* nocapture %0) {
%2 = alloca %struct.termios
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %2 to i8*
%5 = bitcast %struct.termios* %3 to i8*
%6 = load i8*, i8** @getpasswd.buf
%7 = tail call i32 @free(i8* %6)
%8 = tail call dereferenceable_or_null(256) i8* @malloc(i32 256)
store i8* %8, i8** @getpasswd.buf
%9 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
%11 = load i32*, i32** @stdin
%12 = load i32*, i32** @stderr
%13 = select i1 %10, i32* %12, i32* %9
%14 = select i1 %10, i32* %11, i32* %9
%15 = tail call i32 @fileno(i32* %14)
%16 = call i64 @tcgetattr(i32 %15, %struct.termios* %3)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %36
18: ; preds = %1
%19 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%20 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%21 = load i32, i32* %19
store i32 %21, i32* %20
%22 = load i32, i32* @ECHO
%23 = load i32, i32* @ISIG
%24 = or i32 %23, %22
%25 = xor i32 %24, -1
%26 = and i32 %21, %25
store i32 %26, i32* %19
%27 = call i32 @fileno(i32* %14)
%28 = load i32, i32* @TCSAFLUSH
%29 = call i64 @tcsetattr(i32 %27, i32 %28, %struct.termios* %3)
%30 = icmp eq i64 %29, 0
%31 = load i32*, i32** @stdin
%32 = icmp eq i32* %14, %31
br i1 %32, label %36, label %33
33: ; preds = %18
%34 = load i32, i32* @_IONBF
%35 = call i32 @setvbuf(i32* %14, i32* null, i32 %34, i32 0)
br label %36
36: ; preds = %1, %18, %33
%37 = phi i1 [ %30, %33 ], [ %30, %18 ], [ false, %1 ]
%38 = call i32 @fputs(i8* %0, i32* %13)
%39 = call i32 @fflush(i32* %13)
%40 = load i8*, i8** @getpasswd.buf
%41 = call i32 @fgets(i8* %40, i32 255, i32* %14)
%42 = load i8*, i8** @getpasswd.buf
%43 = icmp eq i8* %42, null
br i1 %43, label %58, label %44
44: ; preds = %36
%45 = call i32 @strlen(i8* %42)
%46 = icmp slt i32 %45, 0
%47 = load i8*, i8** @getpasswd.buf
br i1 %46, label %48, label %49
48: ; preds = %44
store i8 0, i8* %47
br label %58
49: ; preds = %44
%50 = add nsw i32 %45, -1
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %47, i64 %51
%53 = load i8, i8* %52
%54 = icmp eq i8 %53, 10
br i1 %54, label %55, label %58
55: ; preds = %49
store i8 0, i8* %52
br i1 %37, label %56, label %63
56: ; preds = %55
%57 = call i32 @putc(i8 signext 10, i32* %13)
br label %59
58: ; preds = %48, %49, %36
br i1 %37, label %59, label %63
59: ; preds = %56, %58
%60 = call i32 @fileno(i32* %14)
%61 = load i32, i32* @TCSAFLUSH
%62 = call i64 @tcsetattr(i32 %60, i32 %61, %struct.termios* %2)
br label %63
63: ; preds = %55, %59, %58
%64 = load i32*, i32** @stdin
%65 = icmp eq i32* %14, %64
br i1 %65, label %68, label %66
66: ; preds = %63
%67 = call i32 @fclose(i32* %14)
br label %68
68: ; preds = %66, %63
%69 = load i8*, i8** @getpasswd.buf
ret i8* %69
}