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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *strchr(char *, char);
scalar_t__ strncmp(char *, char const *, int);
char *strstr(char *, char *);
int authenticate(const char *username, const char *password) {
  FILE *f;
  char input[1024];
  char *end_username;
  char *end_password;
  int username_matches;
  int password_matches;
  f = fopen("users.txt", "r");
  if (f == NULL)
    return 0;
  while (fgets(input, 1024, f) != NULL) {
    end_username = strchr(input, ' ');
    if (end_username == NULL) {
      fclose(f);
      return 0;
    }
    end_password = strstr(input, "\n");
    if (end_password == NULL) {
      fclose(f);
      return 0;
    }
    username_matches = (strncmp(input, username, end_username - input) == 0);
    password_matches = (strncmp(end_username + 1, password,
                                end_password - end_username - 1) == 0);
    if (username_matches && password_matches) {
      break;
    }
  }
  fclose(f);
  return username_matches && password_matches;
}


x86 assembly (clang, -O0):
<authenticate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe86(%rip),%rdi
lea    0xe89(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a1 <authenticate+0x41>
movl   $0x0,-0x4(%rbp)
jmp    130f <authenticate+0x1af>
jmp    11a6 <authenticate+0x46>
lea    -0x420(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     12d3 <authenticate+0x173>
lea    -0x420(%rbp),%rdi
mov    $0x20,%esi
call   1050 <strchr@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    1200 <authenticate+0xa0>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    130f <authenticate+0x1af>
lea    -0x420(%rbp),%rdi
lea    0xdfe(%rip),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x430(%rbp)
jne    123d <authenticate+0xdd>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    130f <authenticate+0x1af>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x428(%rbp),%rax
lea    -0x420(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x434(%rbp)
mov    -0x428(%rbp),%rdi
add    $0x1,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x430(%rbp),%rax
mov    -0x428(%rbp),%rcx
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x438(%rbp)
cmpl   $0x0,-0x434(%rbp)
je     12ce <authenticate+0x16e>
cmpl   $0x0,-0x438(%rbp)
je     12ce <authenticate+0x16e>
jmp    12d3 <authenticate+0x173>
jmp    11a6 <authenticate+0x46>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
cmpl   $0x0,-0x434(%rbp)
mov    %al,-0x439(%rbp)
je     1301 <authenticate+0x1a1>
cmpl   $0x0,-0x438(%rbp)
setne  %al
mov    %al,-0x439(%rbp)
mov    -0x439(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<authenticate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rsi,0x8(%rsp)
mov    %rdi,(%rsp)
lea    0xe8f(%rip),%rdi
lea    0xe92(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1235 <authenticate+0xe5>
mov    %rax,%r13
lea    0x10(%rsp),%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     123d <authenticate+0xed>
lea    0x10(%rsp),%r12
nopl   0x0(%rax)
mov    %r12,%rdi
mov    $0x20,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     123a <authenticate+0xea>
mov    %rax,%r14
mov    %r12,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     123a <authenticate+0xea>
mov    %rax,%r15
mov    %r14d,%edx
sub    %r12d,%edx
mov    %r12,%rdi
mov    (%rsp),%rsi
call   1030 <strncmp@plt>
mov    %rax,%rbx
mov    %r14,%rdi
add    $0x1,%rdi
not    %r14d
add    %r14d,%r15d
mov    0x8(%rsp),%rsi
mov    %r15d,%edx
call   1030 <strncmp@plt>
mov    %rax,%rbp
mov    %rbx,%rax
or     %rbp,%rax
je     1229 <authenticate+0xd9>
mov    %r12,%rdi
mov    $0x400,%esi
mov    %r13,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    11b0 <authenticate+0x60>
xor    %r14d,%r14d
or     %rbp,%rbx
sete   %r14b
jmp    123d <authenticate+0xed>
xor    %r14d,%r14d
jmp    1245 <authenticate+0xf5>
xor    %r14d,%r14d
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    %r14d,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"users.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @authenticate(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca [1024 x i8]
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%12 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 0, i32* %3
br label %81
16: ; preds = %2
br label %17
17: ; preds = %69, %16
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%19 = load i32*, i32** %6
%20 = call i32* @fgets(i8* %18, i32 1024, i32* %19)
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %70
22: ; preds = %17
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%24 = call i8* @strchr(i8* %23, i8 signext 32)
store i8* %24, i8** %8
%25 = load i8*, i8** %8
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i32*, i32** %6
%29 = call i32 @fclose(i32* %28)
store i32 0, i32* %3
br label %81
30: ; preds = %22
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%32 = call i8* @strstr(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %32, i8** %9
%33 = load i8*, i8** %9
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = load i32*, i32** %6
%37 = call i32 @fclose(i32* %36)
store i32 0, i32* %3
br label %81
38: ; preds = %30
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%40 = load i8*, i8** %4
%41 = load i8*, i8** %8
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%43 = ptrtoint i8* %41 to i64
%44 = ptrtoint i8* %42 to i64
%45 = sub i64 %43, %44
%46 = trunc i64 %45 to i32
%47 = call i64 @strncmp(i8* %39, i8* %40, i32 %46)
%48 = icmp eq i64 %47, 0
%49 = zext i1 %48 to i32
store i32 %49, i32* %10
%50 = load i8*, i8** %8
%51 = getelementptr inbounds i8, i8* %50, i64 1
%52 = load i8*, i8** %5
%53 = load i8*, i8** %9
%54 = load i8*, i8** %8
%55 = ptrtoint i8* %53 to i64
%56 = ptrtoint i8* %54 to i64
%57 = sub i64 %55, %56
%58 = sub nsw i64 %57, 1
%59 = trunc i64 %58 to i32
%60 = call i64 @strncmp(i8* %51, i8* %52, i32 %59)
%61 = icmp eq i64 %60, 0
%62 = zext i1 %61 to i32
store i32 %62, i32* %11
%63 = load i32, i32* %10
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %69
65: ; preds = %38
%66 = load i32, i32* %11
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %69
68: ; preds = %65
br label %70
69: ; preds = %65, %38
br label %17
70: ; preds = %68, %17
%71 = load i32*, i32** %6
%72 = call i32 @fclose(i32* %71)
%73 = load i32, i32* %10
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %78
75: ; preds = %70
%76 = load i32, i32* %11
%77 = icmp ne i32 %76, 0
br label %78
78: ; preds = %75, %70
%79 = phi i1 [ false, %70 ], [ %77, %75 ]
%80 = zext i1 %79 to i32
store i32 %80, i32* %3
br label %81
81: ; preds = %78, %35, %27, %15
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
@.str = [10 x i8] c"users.txt\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @authenticate(i8* %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %41, label %7
7: ; preds = %2
%8 = call i32* @fgets(i8* %4, i32 1024, i32* %5)
%9 = icmp eq i32* %8, null
br i1 %9, label %38, label %10
10: ; preds = %7
%11 = ptrtoint [1024 x i8]* %3 to i64
br label %15
12: ; preds = %21
%13 = call i32* @fgets(i8* %4, i32 1024, i32* %5)
%14 = icmp eq i32* %13, null
br i1 %14, label %35, label %15
15: ; preds = %10, %12
%16 = call i8* @strchr(i8* %4, i8 signext 32)
%17 = icmp eq i8* %16, null
br i1 %17, label %38, label %18
18: ; preds = %15
%19 = call i8* bitcast (i8* (i8*, i8)* @strchr to i8* (i8*, i32)*)(i8* %4, i32 10)
%20 = icmp eq i8* %19, null
br i1 %20, label %38, label %21
21: ; preds = %18
%22 = ptrtoint i8* %16 to i64
%23 = sub i64 %22, %11
%24 = trunc i64 %23 to i32
%25 = call i64 @strncmp(i8* %4, i8* %0, i32 %24)
%26 = icmp eq i64 %25, 0
%27 = getelementptr inbounds i8, i8* %16, i64 1
%28 = ptrtoint i8* %19 to i64
%29 = xor i64 %22, -1
%30 = add i64 %28, %29
%31 = trunc i64 %30 to i32
%32 = call i64 @strncmp(i8* %27, i8* %1, i32 %31)
%33 = icmp eq i64 %32, 0
%34 = select i1 %26, i1 %33, i1 false
br i1 %34, label %35, label %12
35: ; preds = %21, %12
%36 = select i1 %26, i1 %33, i1 false
%37 = zext i1 %36 to i32
br label %38
38: ; preds = %18, %15, %7, %35
%39 = phi i32 [ 0, %7 ], [ %37, %35 ], [ 0, %15 ], [ 0, %18 ]
%40 = call i32 @fclose(i32* %5)
br label %41
41: ; preds = %38, %2
%42 = phi i32 [ 0, %2 ], [ %39, %38 ]
ret i32 %42
}