#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 */
typedef int /*<<< orphan*/ uid_t;
struct spwd {
  int /*<<< orphan*/ sp_pwdp;
};
struct passwd {
  int /*<<< orphan*/ pw_gid;
  int /*<<< orphan*/ pw_uid;
  int /*<<< orphan*/ pw_passwd;
};
typedef int /*<<< orphan*/ gid_t;

/* Variables and functions */
scalar_t__ EACCES;
char *crypt(char *, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ fatal(char *);
char *getpass(char *);
struct passwd *getpwnam(char const *);
struct spwd *getspnam(char const *);
int /*<<< orphan*/ pexit(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
scalar_t__ strncmp(char *, int /*<<< orphan*/, int);
bool authenticate(const char *username, uid_t *uid, gid_t *gid) {
  char *password, *encrypted, *p;
  struct passwd *pwd;
  struct spwd *spwd;
  char message[(1024)];
  pwd = getpwnam(username);
  if (pwd == NULL)
    fatal("Could not read password record. Double check that the username was "
          "entered correctly.");
  spwd = getspnam(username);
  if (spwd == NULL && errno == EACCES)
    fatal("No permission to read the shadow password file.");
  if (spwd != NULL)
    pwd->pw_passwd = spwd->sp_pwdp;
  snprintf(message, (1024), "[douser] password for %s: ", username);
  password = getpass(message);
  encrypted = crypt(password, pwd->pw_passwd);
  for (p = password; *p != '\0'; ++p) {
    *p = '\0';
  }
  if (encrypted == NULL)
    pexit("crypt");
  if (strncmp(encrypted, pwd->pw_passwd, (1024)) == 0) {
    *uid = pwd->pw_uid;
    *gid = pwd->pw_gid;
    return true;
  }
  return false;
}


x86 assembly (clang, -O0):
<authenticate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <getpwnam@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11bb <authenticate+0x3b>
lea    0xe4a(%rip),%rdi
call   10a0 <fatal@plt>
mov    -0x10(%rbp),%rdi
call   1080 <getspnam@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    11f9 <authenticate+0x79>
mov    0x2dfe(%rip),%rax
mov    (%rax),%rax
mov    0x2e04(%rip),%rcx
cmp    (%rcx),%rax
jne    11f9 <authenticate+0x79>
lea    0xe62(%rip),%rdi
call   10a0 <fatal@plt>
cmpq   $0x0,-0x48(%rbp)
je     1211 <authenticate+0x91>
mov    -0x48(%rbp),%rax
mov    (%rax),%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,0x8(%rax)
lea    -0x450(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x400,%esi
lea    0xe5e(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x450(%rbp),%rdi
call   1070 <getpass@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%esi
call   1040 <crypt@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1281 <authenticate+0x101>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1259 <authenticate+0xd9>
cmpq   $0x0,-0x30(%rbp)
jne    1298 <authenticate+0x118>
lea    0xe0e(%rip),%rdi
call   1090 <pexit@plt>
mov    -0x30(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%esi
mov    $0x400,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    12dc <authenticate+0x15c>
mov    -0x40(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    12e3 <authenticate+0x163>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<authenticate>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1060 <getpwnam@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11b2 <authenticate+0x32>
lea    0xe53(%rip),%rdi
call   10a0 <fatal@plt>
mov    %rbx,%rdi
call   1080 <getspnam@plt>
test   %rax,%rax
je     11c8 <authenticate+0x48>
mov    (%rax),%eax
mov    %eax,0x8(%r12)
jmp    11ea <authenticate+0x6a>
mov    0x2e09(%rip),%rax
mov    (%rax),%rax
mov    0x2e0f(%rip),%rcx
cmp    (%rcx),%rax
jne    11ea <authenticate+0x6a>
lea    0xe71(%rip),%rdi
call   10a0 <fatal@plt>
lea    0xe95(%rip),%rdx
mov    %rsp,%r13
mov    %r13,%rdi
mov    $0x400,%esi
mov    %rbx,%rcx
call   1050 <snprintf@plt>
mov    %r13,%rdi
call   1070 <getpass@plt>
mov    %rax,%rbx
mov    0x8(%r12),%esi
mov    %rax,%rdi
call   1040 <crypt@plt>
mov    %rax,%r13
cmpb   $0x0,(%rbx)
je     123d <authenticate+0xbd>
add    $0x1,%rbx
nopl   0x0(%rax,%rax,1)
movb   $0x0,-0x1(%rbx)
cmpb   $0x0,(%rbx)
lea    0x1(%rbx),%rbx
jne    1230 <authenticate+0xb0>
test   %r13,%r13
jne    124e <authenticate+0xce>
lea    0xe58(%rip),%rdi
call   1090 <pexit@plt>
mov    0x8(%r12),%esi
mov    %r13,%rdi
mov    $0x400,%edx
call   1030 <strncmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    127e <authenticate+0xfe>
mov    0x4(%r12),%eax
mov    %eax,(%r15)
mov    (%r12),%eax
mov    %eax,(%r14)
mov    $0x1,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.passwd = type { i32, i32, i32 }
%struct.spwd = type { i32 }
@.str = [86 x i8] c"Could not read password record. Double check that the username was entered correctly.\00"
@errno = global i64 0
@EACCES = global i64 0
@.str.1 = [48 x i8] c"No permission to read the shadow password file.\00"
@.str.2 = [27 x i8] c"[douser] password for %s: \00"
@.str.3 = [6 x i8] c"crypt\00"
define i32 @authenticate(i8* %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca %struct.passwd*
%12 = alloca %struct.spwd*
%13 = alloca [1024 x i8]
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
%14 = load i8*, i8** %5
%15 = call %struct.passwd* @getpwnam(i8* %14)
store %struct.passwd* %15, %struct.passwd** %11
%16 = load %struct.passwd*, %struct.passwd** %11
%17 = icmp eq %struct.passwd* %16, null
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = call i32 @fatal(i8* getelementptr inbounds ([86 x i8], [86 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %3
%21 = load i8*, i8** %5
%22 = call %struct.spwd* @getspnam(i8* %21)
store %struct.spwd* %22, %struct.spwd** %12
%23 = load %struct.spwd*, %struct.spwd** %12
%24 = icmp eq %struct.spwd* %23, null
br i1 %24, label %25, label %31
25: ; preds = %20
%26 = load i64, i64* @errno
%27 = load i64, i64* @EACCES
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @fatal(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %29, %25, %20
%32 = load %struct.spwd*, %struct.spwd** %12
%33 = icmp ne %struct.spwd* %32, null
br i1 %33, label %34, label %40
34: ; preds = %31
%35 = load %struct.spwd*, %struct.spwd** %12
%36 = getelementptr inbounds %struct.spwd, %struct.spwd* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = load %struct.passwd*, %struct.passwd** %11
%39 = getelementptr inbounds %struct.passwd, %struct.passwd* %38, i32 0, i32 2
store i32 %37, i32* %39
br label %40
40: ; preds = %34, %31
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%42 = load i8*, i8** %5
%43 = call i32 @snprintf(i8* %41, i32 1024, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %42)
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%45 = call i8* @getpass(i8* %44)
store i8* %45, i8** %8
%46 = load i8*, i8** %8
%47 = load %struct.passwd*, %struct.passwd** %11
%48 = getelementptr inbounds %struct.passwd, %struct.passwd* %47, i32 0, i32 2
%49 = load i32, i32* %48
%50 = call i8* @crypt(i8* %46, i32 %49)
store i8* %50, i8** %9
%51 = load i8*, i8** %8
store i8* %51, i8** %10
br label %52
52: ; preds = %59, %40
%53 = load i8*, i8** %10
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %62
57: ; preds = %52
%58 = load i8*, i8** %10
store i8 0, i8* %58
br label %59
59: ; preds = %57
%60 = load i8*, i8** %10
%61 = getelementptr inbounds i8, i8* %60, i32 1
store i8* %61, i8** %10
br label %52
62: ; preds = %52
%63 = load i8*, i8** %9
%64 = icmp eq i8* %63, null
br i1 %64, label %65, label %67
65: ; preds = %62
%66 = call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %67
67: ; preds = %65, %62
%68 = load i8*, i8** %9
%69 = load %struct.passwd*, %struct.passwd** %11
%70 = getelementptr inbounds %struct.passwd, %struct.passwd* %69, i32 0, i32 2
%71 = load i32, i32* %70
%72 = call i64 @strncmp(i8* %68, i32 %71, i32 1024)
%73 = icmp eq i64 %72, 0
br i1 %73, label %74, label %83
74: ; preds = %67
%75 = load %struct.passwd*, %struct.passwd** %11
%76 = getelementptr inbounds %struct.passwd, %struct.passwd* %75, i32 0, i32 1
%77 = load i32, i32* %76
%78 = load i32*, i32** %6
store i32 %77, i32* %78
%79 = load %struct.passwd*, %struct.passwd** %11
%80 = getelementptr inbounds %struct.passwd, %struct.passwd* %79, i32 0, i32 0
%81 = load i32, i32* %80
%82 = load i32*, i32** %7
store i32 %81, i32* %82
store i32 1, i32* %4
br label %84
83: ; preds = %67
store i32 0, i32* %4
br label %84
84: ; preds = %83, %74
%85 = load i32, i32* %4
ret i32 %85
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32, i32 }
%struct.spwd = type { i32 }
@.str = [86 x i8] c"Could not read password record. Double check that the username was entered correctly.\00"
@errno = global i64 0
@EACCES = global i64 0
@.str.1 = [48 x i8] c"No permission to read the shadow password file.\00"
@.str.2 = [27 x i8] c"[douser] password for %s: \00"
@.str.3 = [6 x i8] c"crypt\00"
define i32 @authenticate(i8* %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = tail call %struct.passwd* @getpwnam(i8* %0)
%7 = icmp eq %struct.passwd* %6, null
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @fatal(i8* getelementptr inbounds ([86 x i8], [86 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %3
%11 = tail call %struct.spwd* @getspnam(i8* %0)
%12 = icmp eq %struct.spwd* %11, null
br i1 %12, label %13, label %19
13: ; preds = %10
%14 = load i64, i64* @errno
%15 = load i64, i64* @EACCES
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %23
17: ; preds = %13
%18 = tail call i32 @fatal(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
br label %23
19: ; preds = %10
%20 = getelementptr inbounds %struct.spwd, %struct.spwd* %11, i64 0, i32 0
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.passwd, %struct.passwd* %6, i64 0, i32 2
store i32 %21, i32* %22
br label %23
23: ; preds = %13, %17, %19
%24 = call i32 @snprintf(i8* %5, i32 1024, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%25 = call i8* @getpass(i8* %5)
%26 = getelementptr inbounds %struct.passwd, %struct.passwd* %6, i64 0, i32 2
%27 = load i32, i32* %26
%28 = call i8* @crypt(i8* %25, i32 %27)
%29 = load i8, i8* %25
%30 = icmp eq i8 %29, 0
br i1 %30, label %36, label %31
31: ; preds = %23, %31
%32 = phi i8* [ %33, %31 ], [ %25, %23 ]
store i8 0, i8* %32
%33 = getelementptr inbounds i8, i8* %32, i64 1
%34 = load i8, i8* %33
%35 = icmp eq i8 %34, 0
br i1 %35, label %36, label %31
36: ; preds = %31, %23
%37 = icmp eq i8* %28, null
br i1 %37, label %38, label %40
38: ; preds = %36
%39 = call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %38, %36
%41 = load i32, i32* %26
%42 = call i64 @strncmp(i8* %28, i32 %41, i32 1024)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %40
%45 = getelementptr inbounds %struct.passwd, %struct.passwd* %6, i64 0, i32 1
%46 = load i32, i32* %45
store i32 %46, i32* %1
%47 = getelementptr inbounds %struct.passwd, %struct.passwd* %6, i64 0, i32 0
%48 = load i32, i32* %47
store i32 %48, i32* %2
br label %49
49: ; preds = %40, %44
%50 = phi i32 [ 1, %44 ], [ 0, %40 ]
ret i32 %50
}