#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 spwd {
  int dummy;
};
typedef int /*<<< orphan*/ password_buf;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char *getlogin();
struct spwd *getspnam(char *);
int pass_correct(struct spwd *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ readpass(char *, int, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(void) {
  static char password_buf[1024];
  if (readpass(password_buf, sizeof(password_buf), "Password: ") < 0) {
    perror("Couldn't read password from terminal");
    exit(EXIT_FAILURE);
  }
  char *username = getlogin();
  if (username == NULL) {
    perror("Couldn't get login username");
    exit(EXIT_FAILURE);
  }
  struct spwd *spentry = getspnam(username);
  if (spentry == NULL) {
    fprintf(stderr, "Couldn't get password information for %s: %s\n", username,
            strerror(errno));
    exit(EXIT_FAILURE);
  }
  int res = pass_correct(spentry, password_buf);
  if (res == -1) {
    fprintf(stderr, "Internal error. Can't validate password.\n");
  } else if (res == 1) {
    printf("Password accepted.\n");
  } else if (res == 0) {
    printf("Access denied.\n");
  } else {
    assert(0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0x2eca(%rip),%rdi
mov    $0x400,%esi
lea    0xe3e(%rip),%rdx
call   1070 <readpass@plt>
cmp    $0x0,%rax
jge    11eb <main+0x4b>
lea    0xe33(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   1080 <getlogin@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    121b <main+0x7b>
lea    0xe28(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1090 <getspnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    127b <main+0xdb>
mov    0x2da6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xde6(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
lea    0x2dfa(%rip),%rsi
call   1030 <pass_correct@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12b4 <main+0x114>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
lea    0xdd2(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12fb <main+0x15b>
cmpl   $0x1,-0x1c(%rbp)
jne    12cf <main+0x12f>
lea    0xddf(%rip),%rdi
call   1040 <printf@plt>
jmp    12f6 <main+0x156>
cmpl   $0x0,-0x1c(%rbp)
jne    12ea <main+0x14a>
lea    0xdd8(%rip),%rdi
call   1040 <printf@plt>
jmp    12f1 <main+0x151>
xor    %edi,%edi
call   1050 <assert@plt>
jmp    12f6 <main+0x156>
jmp    12fb <main+0x15b>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0x2ed6(%rip),%rdi
lea    0xe4f(%rip),%rdx
mov    $0x400,%esi
call   1070 <readpass@plt>
test   %rax,%rax
js     123e <main+0x9e>
xor    %eax,%eax
call   1080 <getlogin@plt>
test   %rax,%rax
je     1247 <main+0xa7>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <getspnam@plt>
test   %rax,%rax
je     1255 <main+0xb5>
lea    0x2e9d(%rip),%rsi
mov    %rax,%rdi
call   1040 <pass_correct@plt>
cmp    $0x1,%eax
je     1220 <main+0x80>
test   %eax,%eax
je     1212 <main+0x72>
cmp    $0xffffffff,%eax
jne    122e <main+0x8e>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
lea    0xe71(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1235 <main+0x95>
lea    0xe8b(%rip),%rdi
call   1030 <puts@plt>
jmp    1235 <main+0x95>
lea    0xe8c(%rip),%rdi
call   1030 <puts@plt>
jmp    1235 <main+0x95>
xor    %edi,%edi
call   1050 <assert@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdc6(%rip),%rdi
jmp    124e <main+0xae>
lea    0xde2(%rip),%rdi
call   10a0 <perror@plt>
jmp    1282 <main+0xe2>
mov    0x2d84(%rip),%rax
mov    (%rax),%ebp
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xdd9(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.spwd = type { i32 }
@main.password_buf = internal global [1024 x i8] zeroinitializer
@.str = [11 x i8] c"Password: \00"
@.str.1 = [37 x i8] c"Couldn't read password from terminal\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [28 x i8] c"Couldn't get login username\00"
@stderr = global i32 0
@.str.3 = [46 x i8] c"Couldn't get password information for %s: %s\0A\00"
@errno = global i32 0
@.str.4 = [42 x i8] c"Internal error. Can't validate password.\0A\00"
@.str.5 = [20 x i8] c"Password accepted.\0A\00"
@.str.6 = [16 x i8] c"Access denied.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca %struct.spwd*
%4 = alloca i32
store i32 0, i32* %1
%5 = call i64 @readpass(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @main.password_buf, i64 0, i64 0), i32 1024, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = call i8* (...) @getlogin()
store i8* %12, i8** %2
%13 = load i8*, i8** %2
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %11
%20 = load i8*, i8** %2
%21 = call %struct.spwd* @getspnam(i8* %20)
store %struct.spwd* %21, %struct.spwd** %3
%22 = load %struct.spwd*, %struct.spwd** %3
%23 = icmp eq %struct.spwd* %22, null
br i1 %23, label %24, label %32
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %2
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i8* %26, i8* %28)
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %19
%33 = load %struct.spwd*, %struct.spwd** %3
%34 = call i32 @pass_correct(%struct.spwd* %33, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @main.password_buf, i64 0, i64 0))
store i32 %34, i32* %4
%35 = load i32, i32* %4
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
br label %54
40: ; preds = %32
%41 = load i32, i32* %4
%42 = icmp eq i32 %41, 1
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %53
45: ; preds = %40
%46 = load i32, i32* %4
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
br label %52
50: ; preds = %45
%51 = call i32 @assert(i32 0)
br label %52
52: ; preds = %50, %48
br label %53
53: ; preds = %52, %43
br label %54
54: ; preds = %53, %37
ret i32 0
}

LLVM IR (-O2):
%struct.spwd = type { i32 }
@main.password_buf = internal global [1024 x i8] zeroinitializer
@.str = [11 x i8] c"Password: \00"
@.str.1 = [37 x i8] c"Couldn't read password from terminal\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [28 x i8] c"Couldn't get login username\00"
@stderr = global i32 0
@.str.3 = [46 x i8] c"Couldn't get password information for %s: %s\0A\00"
@errno = global i32 0
@.str.4 = [42 x i8] c"Internal error. Can't validate password.\0A\00"
@str = [15 x i8] c"Access denied.\00"
@str.7 = [19 x i8] c"Password accepted.\00"
define i32 @main() {
%1 = tail call i64 @readpass(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @main.password_buf, i64 0, i64 0), i32 1024, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %7
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%5 = load i32, i32* @EXIT_FAILURE
%6 = tail call i32 @exit(i32 %5)
unreachable
7: ; preds = %0
%8 = tail call i8* (...) @getlogin()
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %14
10: ; preds = %7
%11 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %7
%15 = tail call %struct.spwd* @getspnam(i8* %8)
%16 = icmp eq %struct.spwd* %15, null
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = tail call i8* @strerror(i32 %19)
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i8* %8, i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %14
%25 = tail call i32 @pass_correct(%struct.spwd* %15, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @main.password_buf, i64 0, i64 0))
switch i32 %25, label %33 [
i32 -1, label %26
i32 1, label %29
i32 0, label %31
]
26: ; preds = %24
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
br label %35
29: ; preds = %24
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.7, i64 0, i64 0))
br label %35
31: ; preds = %24
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %35
33: ; preds = %24
%34 = tail call i32 @assert(i32 0)
br label %35
35: ; preds = %29, %33, %31, %26
ret i32 0
}