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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_cache_header(char const *, char *, int);
int read(int, char **, int);
int /*<<< orphan*/ stderr;
int list_file(const char *fname) {
  int fd;
  char buf[4096];
  int avail;
  if ((fd = open(fname, O_RDONLY)) < 0) {
    fprintf(stderr, "Could not open: %s. ", fname);
    perror("Reason");
    return 2;
  }
  if ((avail = read(fd, &buf, sizeof(buf))) < 0) {
    fprintf(stderr, "Could not read: %s. ", fname);
    perror("Reason");
    return 3;
  }
  print_cache_header(fname, buf, avail);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<list_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11be <list_file+0x5e>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe5f(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xe68(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1237 <list_file+0xd7>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,-0x1024(%rbp)
cmp    $0x0,%eax
jge    1212 <list_file+0xb2>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe27(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xe14(%rip),%rdi
call   1080 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    1237 <list_file+0xd7>
mov    -0x10(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
mov    -0x1024(%rbp),%edx
call   1060 <print_cache_header@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list_file>:
push   %rbp
push   %rbx
sub    $0x1008,%rsp
mov    %rdi,%rbx
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11b3 <list_file+0x53>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x1000,%edx
call   1040 <read@plt>
test   %eax,%eax
js     11de <list_file+0x7e>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    %eax,%edx
call   1060 <print_cache_header@plt>
mov    %ebp,%edi
call   1030 <close@plt>
xor    %eax,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe3d(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
lea    0xe43(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%eax
jmp    11a9 <list_file+0x49>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
lea    0xe18(%rip),%rdi
call   1080 <perror@plt>
mov    $0x3,%eax
jmp    11a9 <list_file+0x49>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"Could not open: %s. \00"
@.str.1 = [7 x i8] c"Reason\00"
@.str.2 = [21 x i8] c"Could not read: %s. \00"
define i32 @list_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [4096 x i8]
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %3
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %2
br label %33
16: ; preds = %1
%17 = load i32, i32* %4
%18 = bitcast [4096 x i8]* %5 to i8**
%19 = call i32 @read(i32 %17, i8** %18, i32 4096)
store i32 %19, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %3
%24 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 3, i32* %2
br label %33
26: ; preds = %16
%27 = load i8*, i8** %3
%28 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%29 = load i32, i32* %6
%30 = call i32 @print_cache_header(i8* %27, i8* %28, i32 %29)
%31 = load i32, i32* %4
%32 = call i32 @close(i32 %31)
store i32 0, i32* %2
br label %33
33: ; preds = %26, %21, %11
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"Could not open: %s. \00"
@.str.1 = [7 x i8] c"Reason\00"
@.str.2 = [21 x i8] c"Could not read: %s. \00"
define i32 @list_file(i8* %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0)
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %22
11: ; preds = %1
%12 = bitcast [4096 x i8]* %2 to i8**
%13 = call i32 @read(i32 %5, i8** %12, i32 4096)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%18 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %22
19: ; preds = %11
%20 = call i32 @print_cache_header(i8* %0, i8* %3, i32 %13)
%21 = call i32 @close(i32 %5)
br label %22
22: ; preds = %19, %15, %7
%23 = phi i32 [ 2, %7 ], [ 3, %15 ], [ 0, %19 ]
ret i32 %23
}