#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 timespec {
  int dummy;
};
struct spwd {
  int /*<<< orphan*/ sp_pwdp;
};

/* Variables and functions */
scalar_t__ asprintf(char **, char *, char const *);
char *crypt(char *, int /*<<< orphan*/);
int /*<<< orphan*/ endspent();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
struct spwd *getspnam(char const *);
int /*<<< orphan*/ perror(char *);
char *prompt_echo_off(char *, struct timespec *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, int /*<<< orphan*/);
bool auth(const char *user, struct timespec *timeout) {
  char *pwd;
  char *cryptpw;
  char *msg;
  struct spwd *spw;
  int result = false;
  if (asprintf(&msg, "%s's Password: ", user) < 0)
    return false;
  if ((pwd = prompt_echo_off(msg, timeout)) == NULL)
    goto out_pwd;
  if ((spw = getspnam(user)) == NULL)
    goto out_shadow;
  if ((cryptpw = crypt(pwd, spw->sp_pwdp)) == NULL) {
    perror("vlock: crypt()");
    goto out_shadow;
  }
  result = (strcmp(cryptpw, spw->sp_pwdp) == 0);
  if (!result) {
    sleep(1);
    fprintf(stderr, "vlock: Authentication error\n");
  }
out_shadow:
  endspent();
  free(pwd);
out_pwd:
  free(msg);
  return result;
}


x86 assembly (clang, -O0):
<auth>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x10(%rbp),%rdx
lea    -0x30(%rbp),%rdi
lea    0xe3a(%rip),%rsi
call   1080 <asprintf@plt>
cmp    $0x0,%rax
jge    11e1 <auth+0x41>
movl   $0x0,-0x4(%rbp)
jmp    12b6 <auth+0x116>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10a0 <prompt_echo_off@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1201 <auth+0x61>
jmp    12a7 <auth+0x107>
mov    -0x10(%rbp),%rdi
call   1090 <getspnam@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    121d <auth+0x7d>
jmp    1297 <auth+0xf7>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    (%rax),%esi
call   1050 <crypt@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    124b <auth+0xab>
lea    0xdcf(%rip),%rdi
call   10b0 <perror@plt>
jmp    1297 <auth+0xf7>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    (%rax),%esi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    1292 <auth+0xf2>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
lea    0xd92(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1297 <auth+0xf7>
mov    $0x0,%al
call   1040 <endspent@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<auth>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0xe4b(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %rbx,%rdx
call   1080 <asprintf@plt>
test   %rax,%rax
js     122d <auth+0x8d>
mov    0x8(%rsp),%rdi
mov    %rbp,%rsi
call   10a0 <prompt_echo_off@plt>
test   %rax,%rax
je     1231 <auth+0x91>
mov    %rax,%r14
mov    %rbx,%rdi
call   1090 <getspnam@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     123a <auth+0x9a>
mov    %rax,%rbx
mov    (%rax),%esi
mov    %r14,%rdi
call   1050 <crypt@plt>
test   %rax,%rax
je     125e <auth+0xbe>
mov    (%rbx),%esi
mov    %rax,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1235 <auth+0x95>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
call   1070 <fprintf@plt>
jmp    123a <auth+0x9a>
xor    %ebp,%ebp
jmp    1253 <auth+0xb3>
xor    %ebp,%ebp
jmp    1249 <auth+0xa9>
mov    $0x1,%ebp
xor    %eax,%eax
call   1040 <endspent@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdab(%rip),%rdi
call   10b0 <perror@plt>
jmp    123a <auth+0x9a>

LLVM IR (-O0):
%struct.timespec = type { i32 }
%struct.spwd = type { i32 }
@.str = [16 x i8] c"%s's Password: \00"
@.str.1 = [15 x i8] c"vlock: crypt()\00"
@stderr = global i32 0
@.str.2 = [29 x i8] c"vlock: Authentication error\0A\00"
define i32 @auth(i8* %0, %struct.timespec* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.timespec*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca %struct.spwd*
%10 = alloca i32
store i8* %0, i8** %4
store %struct.timespec* %1, %struct.timespec** %5
store i32 0, i32* %10
%11 = load i8*, i8** %4
%12 = call i64 @asprintf(i8** %8, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 0, i32* %3
br label %58
15: ; preds = %2
%16 = load i8*, i8** %8
%17 = load %struct.timespec*, %struct.timespec** %5
%18 = call i8* @prompt_echo_off(i8* %16, %struct.timespec* %17)
store i8* %18, i8** %6
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %15
br label %54
21: ; preds = %15
%22 = load i8*, i8** %4
%23 = call %struct.spwd* @getspnam(i8* %22)
store %struct.spwd* %23, %struct.spwd** %9
%24 = icmp eq %struct.spwd* %23, null
br i1 %24, label %25, label %26
25: ; preds = %21
br label %50
26: ; preds = %21
%27 = load i8*, i8** %6
%28 = load %struct.spwd*, %struct.spwd** %9
%29 = getelementptr inbounds %struct.spwd, %struct.spwd* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i8* @crypt(i8* %27, i32 %30)
store i8* %31, i8** %7
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %35
33: ; preds = %26
%34 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %50
35: ; preds = %26
%36 = load i8*, i8** %7
%37 = load %struct.spwd*, %struct.spwd** %9
%38 = getelementptr inbounds %struct.spwd, %struct.spwd* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i64 @strcmp(i8* %36, i32 %39)
%41 = icmp eq i64 %40, 0
%42 = zext i1 %41 to i32
store i32 %42, i32* %10
%43 = load i32, i32* %10
%44 = icmp ne i32 %43, 0
br i1 %44, label %49, label %45
45: ; preds = %35
%46 = call i32 @sleep(i32 1)
%47 = load i32, i32* @stderr
%48 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %49
49: ; preds = %45, %35
br label %50
50: ; preds = %49, %33, %25
%51 = call i32 (...) @endspent()
%52 = load i8*, i8** %6
%53 = call i32 @free(i8* %52)
br label %54
54: ; preds = %50, %20
%55 = load i8*, i8** %8
%56 = call i32 @free(i8* %55)
%57 = load i32, i32* %10
store i32 %57, i32* %3
br label %58
58: ; preds = %54, %14
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
%struct.spwd = type { i32 }
@.str = [16 x i8] c"%s's Password: \00"
@.str.1 = [15 x i8] c"vlock: crypt()\00"
@stderr = global i32 0
@.str.2 = [29 x i8] c"vlock: Authentication error\0A\00"
define i32 @auth(i8* %0, %struct.timespec* %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = call i64 @asprintf(i8** %3, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %0)
%6 = icmp slt i64 %5, 0
br i1 %6, label %37, label %7
7: ; preds = %2
%8 = load i8*, i8** %3
%9 = call i8* @prompt_echo_off(i8* %8, %struct.timespec* %1)
%10 = icmp eq i8* %9, null
br i1 %10, label %33, label %11
11: ; preds = %7
%12 = call %struct.spwd* @getspnam(i8* %0)
%13 = icmp eq %struct.spwd* %12, null
br i1 %13, label %29, label %14
14: ; preds = %11
%15 = getelementptr inbounds %struct.spwd, %struct.spwd* %12, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i8* @crypt(i8* %9, i32 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %29
21: ; preds = %14
%22 = load i32, i32* %15
%23 = call i64 @strcmp(i8* %17, i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %21
%26 = call i32 @sleep(i32 1)
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %21, %25, %11, %19
%30 = phi i32 [ 0, %11 ], [ 0, %19 ], [ 1, %21 ], [ 0, %25 ]
%31 = call i32 (...) @endspent()
%32 = call i32 @free(i8* %9)
br label %33
33: ; preds = %7, %29
%34 = phi i32 [ 0, %7 ], [ %30, %29 ]
%35 = load i8*, i8** %3
%36 = call i32 @free(i8* %35)
br label %37
37: ; preds = %2, %33
%38 = phi i32 [ %34, %33 ], [ 0, %2 ]
ret i32 %38
}