#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 dirent {
  int /*<<< orphan*/ d_name;
};
typedef int /*<<< orphan*/ lines;
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
char *concat(char const *, char const *);
scalar_t__ errno;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcmp(int /*<<< orphan*/, char const *);
int main(void) {
  const char *path = "/home/abondar/";
  const char *file = "npm-debug.log";
  struct dirent *entry;
  char lines[8192];
  int ret = 1;
  DIR *dir;
  FILE *fl;
  int c;
  dir = opendir(path);
  errno = 0;
  while ((entry = readdir(dir)) != NULL) {
    if (!strcmp(entry->d_name, file)) {
      ret = 0;
      break;
    }
  }
  if (errno && !entry) {
    perror("readdir");
  }
  closedir(dir);
  if (ret == 0) {
    char *abs_path = concat(path, file);
    fl = fopen(abs_path, "r");
    if (!fl) {
      perror("fopen");
      return 1;
    }
    if (!fread(&lines, sizeof(lines), 1, fl)) {
      perror("fread");
      return 1;
    }
    printf("%s\n", lines);
    if (fclose(fl)) {
      perror("fclose");
      return 1;
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2050,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe47(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xe4b(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x2024(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x2030(%rbp)
mov    0x2df7(%rip),%rax
movq   $0x0,(%rax)
mov    -0x2030(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1236 <main+0x96>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    1231 <main+0x91>
movl   $0x0,-0x2024(%rbp)
jmp    1236 <main+0x96>
jmp    11f0 <main+0x50>
mov    0x2da3(%rip),%rax
cmpq   $0x0,(%rax)
je     125e <main+0xbe>
cmpq   $0x0,-0x20(%rbp)
jne    125e <main+0xbe>
lea    0xdc4(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x2030(%rbp),%rdi
call   1080 <closedir@plt>
cmpl   $0x0,-0x2024(%rbp)
jne    134f <main+0x1af>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <concat@plt>
mov    %rax,-0x2048(%rbp)
mov    -0x2048(%rbp),%rdi
lea    0xd8a(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x2038(%rbp)
cmpq   $0x0,-0x2038(%rbp)
jne    12cb <main+0x12b>
lea    0xd6b(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    134f <main+0x1af>
lea    -0x2020(%rbp),%rdi
mov    -0x2038(%rbp),%rcx
mov    $0x2000,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x0,%eax
jne    1309 <main+0x169>
lea    0xd33(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    134f <main+0x1af>
lea    -0x2020(%rbp),%rsi
lea    0xd1a(%rip),%rdi
call   1060 <printf@plt>
mov    -0x2038(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     134a <main+0x1aa>
lea    0xcfc(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    134f <main+0x1af>
jmp    134f <main+0x1af>
mov    -0x4(%rbp),%eax
add    $0x2050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x2000,%rsp
lea    0xe4d(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,%rbx
mov    0x2e1e(%rip),%r15
movq   $0x0,(%r15)
lea    0xe3f(%rip),%r14
mov    %rbx,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1266 <main+0xc6>
mov    (%rax),%edi
mov    %r14,%rsi
call   1090 <strcmp@plt>
test   %eax,%eax
jne    11d0 <main+0x30>
mov    %rbx,%rdi
call   1080 <closedir@plt>
lea    0xe02(%rip),%rdi
lea    0xe0a(%rip),%rsi
call   1070 <concat@plt>
lea    0xe12(%rip),%rsi
mov    %rax,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1285 <main+0xe5>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x1,%ebx
mov    $0x2000,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
test   %eax,%eax
je     12a6 <main+0x106>
lea    0xdeb(%rip),%rdi
mov    %rsp,%rsi
call   1060 <printf@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     1276 <main+0xd6>
lea    0xdd1(%rip),%rdi
jmp    128c <main+0xec>
cmpq   $0x0,(%r15)
jne    1298 <main+0xf8>
mov    %rbx,%rdi
call   1080 <closedir@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x2000,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd99(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%ebx
jmp    1276 <main+0xd6>
lea    0xd7e(%rip),%rdi
call   10c0 <perror@plt>
jmp    126c <main+0xcc>
lea    0xd7e(%rip),%rdi
call   10c0 <perror@plt>
jmp    1276 <main+0xd6>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [15 x i8] c"/home/abondar/\00"
@.str.1 = [14 x i8] c"npm-debug.log\00"
@errno = global i64 0
@.str.2 = [8 x i8] c"readdir\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [6 x i8] c"fopen\00"
@.str.5 = [6 x i8] c"fread\00"
@.str.6 = [4 x i8] c"%s\0A\00"
@.str.7 = [7 x i8] c"fclose\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.dirent*
%5 = alloca [8192 x i8]
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i8*
store i32 0, i32* %1
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8** %2
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8** %3
store i32 1, i32* %6
%11 = load i8*, i8** %2
%12 = call i32* @opendir(i8* %11)
store i32* %12, i32** %7
store i64 0, i64* @errno
br label %13
13: ; preds = %25, %0
%14 = load i32*, i32** %7
%15 = call %struct.dirent* @readdir(i32* %14)
store %struct.dirent* %15, %struct.dirent** %4
%16 = icmp ne %struct.dirent* %15, null
br i1 %16, label %17, label %26
17: ; preds = %13
%18 = load %struct.dirent*, %struct.dirent** %4
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i32 0, i32 0
%20 = load i32, i32* %19
%21 = load i8*, i8** %3
%22 = call i32 @strcmp(i32 %20, i8* %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %25, label %24
24: ; preds = %17
store i32 0, i32* %6
br label %26
25: ; preds = %17
br label %13
26: ; preds = %24, %13
%27 = load i64, i64* @errno
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = load %struct.dirent*, %struct.dirent** %4
%31 = icmp ne %struct.dirent* %30, null
br i1 %31, label %34, label %32
32: ; preds = %29
%33 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %29, %26
%35 = load i32*, i32** %7
%36 = call i32 @closedir(i32* %35)
%37 = load i32, i32* %6
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %65
39: ; preds = %34
%40 = load i8*, i8** %2
%41 = load i8*, i8** %3
%42 = call i8* @concat(i8* %40, i8* %41)
store i8* %42, i8** %10
%43 = load i8*, i8** %10
%44 = call i32* @fopen(i8* %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %44, i32** %8
%45 = load i32*, i32** %8
%46 = icmp ne i32* %45, null
br i1 %46, label %49, label %47
47: ; preds = %39
%48 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %65
49: ; preds = %39
%50 = bitcast [8192 x i8]* %5 to i8**
%51 = load i32*, i32** %8
%52 = call i32 @fread(i8** %50, i32 8192, i32 1, i32* %51)
%53 = icmp ne i32 %52, 0
br i1 %53, label %56, label %54
54: ; preds = %49
%55 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %65
56: ; preds = %49
%57 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 0
%58 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %57)
%59 = load i32*, i32** %8
%60 = call i64 @fclose(i32* %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %56
%63 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %1
br label %65
64: ; preds = %56
br label %65
65: ; preds = %47, %54, %62, %64, %34
%66 = load i32, i32* %1
ret i32 %66
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [15 x i8] c"/home/abondar/\00"
@.str.1 = [14 x i8] c"npm-debug.log\00"
@errno = global i64 0
@.str.2 = [8 x i8] c"readdir\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [6 x i8] c"fopen\00"
@.str.5 = [6 x i8] c"fread\00"
@.str.6 = [4 x i8] c"%s\0A\00"
@.str.7 = [7 x i8] c"fclose\00"
define i32 @main() {
%1 = alloca [8192 x i8]
%2 = getelementptr inbounds [8192 x i8], [8192 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @opendir(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* @errno
br label %4
4: ; preds = %7, %0
%5 = tail call %struct.dirent* @readdir(i32* %3)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %12, label %7
7: ; preds = %4
%8 = getelementptr inbounds %struct.dirent, %struct.dirent* %5, i64 0, i32 0
%9 = load i32, i32* %8
%10 = tail call i32 @strcmp(i32 %9, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32 %10, 0
br i1 %11, label %19, label %4
12: ; preds = %4
%13 = load i64, i64* @errno
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = tail call i32 @closedir(i32* %3)
br label %38
19: ; preds = %7
%20 = tail call i32 @closedir(i32* %3)
%21 = tail call i8* @concat(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%22 = tail call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %38
26: ; preds = %19
%27 = bitcast [8192 x i8]* %1 to i8**
%28 = call i32 @fread(i8** %27, i32 8192, i32 1, i32* %22)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %38
32: ; preds = %26
%33 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %2)
%34 = call i64 @fclose(i32* %22)
%35 = icmp eq i64 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
br label %38
38: ; preds = %17, %24, %30, %36, %32
%39 = phi i32 [ 1, %36 ], [ 1, %30 ], [ 1, %24 ], [ 0, %32 ], [ 0, %17 ]
ret i32 %39
}