#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 *);
int /*<<< orphan*/ printf(char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncat(char *, char const *, int);
scalar_t__ strncmp(char *, char const *, int);
char *strtok(char *, char *);
int doesFileExist(const char *path, const char *filename) {
  if (path == NULL || filename == NULL) {
    printf("ERROR: Could not read filesystem\n");
    return -1;
  }
  char filePath[255];
  filePath[0] = 0;
  strncat(filePath, path, 255);
  strncat(filePath, ".list", 6);
  FILE *file = fopen(filePath, "r");
  if (file == NULL) {
    printf("ERROR: failed to read filesystem\n");
    fclose(file);
    return -1;
  }
  char line[255];
  while (fgets(line, 255, file) != NULL) {
    if (strlen(line) > 0) {
      char *l = strtok(line, "\n");
      if (strncmp(l, filename, 255) == 0) {
        fclose(file);
        return 1;
      }
    }
  }
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<doesFileExist>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a9 <doesFileExist+0x29>
cmpq   $0x0,-0x18(%rbp)
jne    11c1 <doesFileExist+0x41>
lea    0xe50(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ec <doesFileExist+0x16c>
movb   $0x0,-0x120(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0xff,%edx
call   1070 <strncat@plt>
lea    -0x120(%rbp),%rdi
lea    0xe37(%rip),%rsi
mov    $0x6,%edx
call   1070 <strncat@plt>
lea    -0x120(%rbp),%rdi
lea    0xe25(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
jne    1241 <doesFileExist+0xc1>
lea    0xe06(%rip),%rdi
call   1060 <printf@plt>
mov    -0x128(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ec <doesFileExist+0x16c>
jmp    1246 <doesFileExist+0xc6>
lea    -0x230(%rbp),%rdi
mov    -0x128(%rbp),%rdx
mov    $0xff,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     12d9 <doesFileExist+0x159>
lea    -0x230(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%rax
jle    12d4 <doesFileExist+0x154>
lea    -0x230(%rbp),%rdi
lea    0xdbe(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x238(%rbp)
mov    -0x238(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0xff,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    12cf <doesFileExist+0x14f>
mov    -0x128(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ec <doesFileExist+0x16c>
jmp    12d4 <doesFileExist+0x154>
jmp    1246 <doesFileExist+0xc6>
mov    -0x128(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doesFileExist>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x200,%rsp
test   %rdi,%rdi
je     1272 <doesFileExist+0xf2>
mov    %rsi,%r14
test   %rsi,%rsi
je     1272 <doesFileExist+0xf2>
mov    %rdi,%rsi
movb   $0x0,0x100(%rsp)
lea    0x100(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xff,%edx
call   1070 <strncat@plt>
lea    0xe35(%rip),%rsi
mov    %rbx,%rdi
mov    $0x6,%edx
call   1070 <strncat@plt>
lea    0xe27(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1280 <doesFileExist+0x100>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0xff,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
xor    %r15d,%r15d
test   %rax,%rax
je     1268 <doesFileExist+0xe8>
mov    %rsp,%rbp
lea    0xdf3(%rip),%r12
jmp    1235 <doesFileExist+0xb5>
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    $0xff,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     1268 <doesFileExist+0xe8>
mov    %rbp,%rdi
call   1060 <strlen@plt>
test   %rax,%rax
jle    1220 <doesFileExist+0xa0>
mov    %rbp,%rdi
mov    %r12,%rsi
call   10a0 <strtok@plt>
mov    %rax,%rdi
mov    %r14,%rsi
mov    $0xff,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1220 <doesFileExist+0xa0>
mov    $0x1,%r15d
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    1299 <doesFileExist+0x119>
lea    0xdb2(%rip),%rdi
call   1040 <puts@plt>
jmp    1293 <doesFileExist+0x113>
lea    0xd83(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1050 <fclose@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x200,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [34 x i8] c"ERROR: Could not read filesystem\0A\00"
@.str.1 = [6 x i8] c".list\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [34 x i8] c"ERROR: failed to read filesystem\0A\00"
@.str.4 = [2 x i8] c"\0A\00"
define i32 @doesFileExist(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [255 x i8]
%7 = alloca i32*
%8 = alloca [255 x i8]
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = icmp eq i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %12, %2
%16 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %57
17: ; preds = %12
%18 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
store i8 0, i8* %18
%19 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%20 = load i8*, i8** %4
%21 = call i32 @strncat(i8* %19, i8* %20, i32 255)
%22 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%23 = call i32 @strncat(i8* %22, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 6)
%24 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %25, i32** %7
%26 = load i32*, i32** %7
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %32
28: ; preds = %17
%29 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%30 = load i32*, i32** %7
%31 = call i32 @fclose(i32* %30)
store i32 -1, i32* %3
br label %57
32: ; preds = %17
br label %33
33: ; preds = %53, %32
%34 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%35 = load i32*, i32** %7
%36 = call i32* @fgets(i8* %34, i32 255, i32* %35)
%37 = icmp ne i32* %36, null
br i1 %37, label %38, label %54
38: ; preds = %33
%39 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%40 = call i64 @strlen(i8* %39)
%41 = icmp sgt i64 %40, 0
br i1 %41, label %42, label %53
42: ; preds = %38
%43 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%44 = call i8* @strtok(i8* %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i8* %44, i8** %9
%45 = load i8*, i8** %9
%46 = load i8*, i8** %5
%47 = call i64 @strncmp(i8* %45, i8* %46, i32 255)
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %42
%50 = load i32*, i32** %7
%51 = call i32 @fclose(i32* %50)
store i32 1, i32* %3
br label %57
52: ; preds = %42
br label %53
53: ; preds = %52, %38
br label %33
54: ; preds = %33
%55 = load i32*, i32** %7
%56 = call i32 @fclose(i32* %55)
store i32 0, i32* %3
br label %57
57: ; preds = %54, %49, %28, %15
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
@.str.1 = [6 x i8] c".list\00"
@.str.2 = [2 x i8] c"r\00"
@.str.4 = [2 x i8] c"\0A\00"
@str = [33 x i8] c"ERROR: failed to read filesystem\00"
@str.5 = [33 x i8] c"ERROR: Could not read filesystem\00"
define i32 @doesFileExist(i8* %0, i8* %1) {
%3 = alloca [255 x i8]
%4 = alloca [255 x i8]
%5 = icmp eq i8* %0, null
%6 = icmp eq i8* %1, null
%7 = or i1 %5, %6
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.5, i64 0, i64 0))
br label %38
10: ; preds = %2
%11 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
store i8 0, i8* %11
%12 = call i32 @strncat(i8* %11, i8* %0, i32 255)
%13 = call i32 @strncat(i8* %11, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 6)
%14 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
%18 = call i32 @fclose(i32* null)
br label %36
19: ; preds = %10
%20 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%21 = call i32* @fgets(i8* %20, i32 255, i32* %14)
%22 = icmp eq i32* %21, null
br i1 %22, label %33, label %23
23: ; preds = %19, %30
%24 = call i64 @strlen(i8* %20)
%25 = icmp sgt i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = call i8* @strtok(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%28 = call i64 @strncmp(i8* %27, i8* %1, i32 255)
%29 = icmp eq i64 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %26, %23
%31 = call i32* @fgets(i8* %20, i32 255, i32* %14)
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %23
33: ; preds = %30, %26, %19
%34 = phi i32 [ 0, %19 ], [ 1, %26 ], [ 0, %30 ]
%35 = call i32 @fclose(i32* %14)
br label %36
36: ; preds = %33, %16
%37 = phi i32 [ -1, %16 ], [ %34, %33 ]
br label %38
38: ; preds = %36, %8
%39 = phi i32 [ -1, %8 ], [ %37, %36 ]
ret i32 %39
}