#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*/ password;

/* Variables and functions */
int /*<<< orphan*/ ECHO;
int /*<<< orphan*/ TCSADRAIN;
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stdin;
char *strchr(char *, char);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
const char *read_password(void) {
  static char password[1024];
  struct termios saved, no_echo;
  printf("Password: ");
  tcgetattr(0, &saved);
  no_echo = saved;
  no_echo.c_lflag &= ~ECHO;
  tcsetattr(0, TCSADRAIN, &no_echo);
  fgets(password, sizeof(password), stdin);
  tcsetattr(0, TCSADRAIN, &saved);
  puts("");
  if (strchr(password, '\n'))
    *strchr(password, '\n') = '\0';
  return password;
}


x86 assembly (clang, -O0):
<read_password>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe91(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
lea    -0x8(%rbp),%rsi
call   1070 <tcgetattr@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x10(%rbp),%rdx
call   1080 <tcsetattr@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
lea    0x2ea5(%rip),%rdi
mov    $0x400,%esi
call   1060 <fgets@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x8(%rbp),%rdx
call   1080 <tcsetattr@plt>
lea    0xe2a(%rip),%rdi
call   1030 <puts@plt>
lea    0x2e74(%rip),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
je     1214 <read_password+0xb4>
lea    0x2e59(%rip),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
movb   $0x0,(%rax)
lea    0x2e45(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_password>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe90(%rip),%rdi
call   1050 <printf@plt>
lea    0x8(%rsp),%r14
xor    %edi,%edi
mov    %r14,%rsi
call   1070 <tcgetattr@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0x8(%rsp),%eax
mov    %eax,(%rsp)
mov    0x2e33(%rip),%rbx
mov    (%rbx),%esi
mov    %rsp,%rdx
xor    %edi,%edi
call   1080 <tcsetattr@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
lea    0x2ea7(%rip),%r15
mov    %r15,%rdi
mov    $0x400,%esi
call   1060 <fgets@plt>
mov    (%rbx),%esi
xor    %edi,%edi
mov    %r14,%rdx
call   1080 <tcsetattr@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r15,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11f1 <read_password+0x91>
movb   $0x0,(%rax)
lea    0x2e68(%rip),%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@read_password.password = internal global [1024 x i8] zeroinitializer
@.str = [11 x i8] c"Password: \00"
@ECHO = global i32 0
@TCSADRAIN = global i32 0
@stdin = global i32 0
@.str.1 = [1 x i8] zeroinitializer
define i8* @read_password() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @tcgetattr(i32 0, %struct.termios* %1)
%5 = bitcast %struct.termios* %2 to i8*
%6 = bitcast %struct.termios* %1 to i8*
%7 = load i32, i32* @ECHO
%8 = xor i32 %7, -1
%9 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 0
%10 = load i32, i32* %9
%11 = and i32 %10, %8
store i32 %11, i32* %9
%12 = load i32, i32* @TCSADRAIN
%13 = call i32 @tcsetattr(i32 0, i32 %12, %struct.termios* %2)
%14 = load i32, i32* @stdin
%15 = call i32 @fgets(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @read_password.password, i64 0, i64 0), i32 1024, i32 %14)
%16 = load i32, i32* @TCSADRAIN
%17 = call i32 @tcsetattr(i32 0, i32 %16, %struct.termios* %1)
%18 = call i32 @puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
%19 = call i8* @strchr(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @read_password.password, i64 0, i64 0), i8 signext 10)
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %0
%22 = call i8* @strchr(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @read_password.password, i64 0, i64 0), i8 signext 10)
store i8 0, i8* %22
br label %23
23: ; preds = %21, %0
ret i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @read_password.password, i64 0, i64 0)
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@read_password.password = internal global [1024 x i8] zeroinitializer
@.str = [11 x i8] c"Password: \00"
@ECHO = global i32 0
@TCSADRAIN = global i32 0
@stdin = global i32 0
define i8* @read_password() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %1 to i8*
%4 = bitcast %struct.termios* %2 to i8*
%5 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @tcgetattr(i32 0, %struct.termios* %1)
%7 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%8 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%9 = load i32, i32* %7
%10 = load i32, i32* @ECHO
%11 = xor i32 %10, -1
%12 = and i32 %9, %11
store i32 %12, i32* %8
%13 = load i32, i32* @TCSADRAIN
%14 = call i32 @tcsetattr(i32 0, i32 %13, %struct.termios* %2)
%15 = load i32, i32* @stdin
%16 = call i32 @fgets(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @read_password.password, i64 0, i64 0), i32 1024, i32 %15)
%17 = load i32, i32* @TCSADRAIN
%18 = call i32 @tcsetattr(i32 0, i32 %17, %struct.termios* %1)
%19 = call i32 @putchar(i32 10)
%20 = call i8* @strchr(i8* dereferenceable(1) getelementptr inbounds ([1024 x i8], [1024 x i8]* @read_password.password, i64 0, i64 0), i8 signext 10)
%21 = icmp eq i8* %20, null
br i1 %21, label %23, label %22
22: ; preds = %0
store i8 0, i8* %20
br label %23
23: ; preds = %22, %0
ret i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @read_password.password, i64 0, i64 0)
}