#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 EOF;
int /*<<< orphan*/ SEEK_END;
int feof(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ print_errno(char *);
int /*<<< orphan*/ printf(char *, ...);
void fopen_directory(const char *path) {
  FILE *f;
  printf("testing '%s'...\n", path);
  print_errno("fopen");
  f = fopen(path, "rb");
  if (f == NULL) {
    return;
  }
  int seek_res = fseek(f, 0, SEEK_END);
  print_errno("fseek");
  printf("fseek result: %d\n", seek_res);
  long tell_res = ftell(f);
  print_errno("ftell");
  printf("ftell result: %ld\n", tell_res);
  int feof_res = feof(f);
  print_errno("feof");
  printf("feof result: %d (EOF=%s)\n", feof_res,
         feof_res == EOF ? "yes" : "no");
  int fgetc_res = fgetc(f);
  print_errno("fgetc");
  printf("fgetc result: %d (EOF=%s)\n", fgetc_res,
         fgetc_res == EOF ? "yes" : "no");
  char fread_buffer[1024];
  int fread_result = fread(fread_buffer, 1024, 1, f);
  print_errno("fread");
  printf("fread result: %d\n", fread_result);
}


x86 assembly (clang, -O0):
<fopen_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe69(%rip),%rdi
call   1030 <print_errno@plt>
mov    -0x8(%rbp),%rdi
lea    0xe5f(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d1 <fopen_directory+0x51>
jmp    1302 <fopen_directory+0x182>
mov    -0x10(%rbp),%rdi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %eax,-0x14(%rbp)
lea    0xe2b(%rip),%rdi
call   1030 <print_errno@plt>
mov    -0x14(%rbp),%esi
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x20(%rbp)
lea    0xe19(%rip),%rdi
call   1030 <print_errno@plt>
mov    -0x20(%rbp),%rsi
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1080 <feof@plt>
mov    %eax,-0x24(%rbp)
lea    0xe08(%rip),%rdi
call   1030 <print_errno@plt>
mov    -0x24(%rbp),%esi
mov    -0x24(%rbp),%ecx
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rdx
lea    0xe05(%rip),%rax
cmp    %edi,%ecx
cmove  %rax,%rdx
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x28(%rbp)
lea    0xde5(%rip),%rdi
call   1030 <print_errno@plt>
mov    -0x28(%rbp),%esi
mov    -0x28(%rbp),%ecx
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
lea    0xdc7(%rip),%rdx
lea    0xdbc(%rip),%rax
cmp    %edi,%ecx
cmove  %rax,%rdx
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x400,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
mov    %eax,-0x434(%rbp)
lea    0xda9(%rip),%rdi
call   1030 <print_errno@plt>
mov    -0x434(%rbp),%esi
lea    0xd9d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fopen_directory>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rdi,%rbx
lea    0xe67(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe67(%rip),%rdi
call   1030 <print_errno@plt>
lea    0xe61(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12cb <fopen_directory+0x14b>
mov    %rax,%r14
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %eax,%ebp
lea    0xe34(%rip),%rdi
call   1030 <print_errno@plt>
lea    0xe2e(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1070 <ftell@plt>
mov    %rax,%rbp
lea    0xe25(%rip),%rdi
call   1030 <print_errno@plt>
lea    0xe1f(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1080 <feof@plt>
mov    %eax,%ebp
lea    0xe17(%rip),%rdi
call   1030 <print_errno@plt>
mov    0x2da0(%rip),%r15
cmp    (%r15),%ebp
lea    0xe20(%rip),%r12
lea    0xe1d(%rip),%rbx
mov    %rbx,%rdx
cmove  %r12,%rdx
lea    0xdf1(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1060 <fgetc@plt>
mov    %eax,%ebp
lea    0xdf8(%rip),%rdi
call   1030 <print_errno@plt>
cmp    (%r15),%ebp
cmove  %r12,%rbx
lea    0xdeb(%rip),%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rsp,%rdi
mov    $0x400,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
mov    %eax,%ebx
lea    0xddc(%rip),%rdi
call   1030 <print_errno@plt>
lea    0xdd6(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"testing '%s'...\0A\00"
@.str.1 = [6 x i8] c"fopen\00"
@.str.2 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@.str.3 = [6 x i8] c"fseek\00"
@.str.4 = [18 x i8] c"fseek result: %d\0A\00"
@.str.5 = [6 x i8] c"ftell\00"
@.str.6 = [19 x i8] c"ftell result: %ld\0A\00"
@.str.7 = [5 x i8] c"feof\00"
@.str.8 = [26 x i8] c"feof result: %d (EOF=%s)\0A\00"
@EOF = global i32 0
@.str.9 = [4 x i8] c"yes\00"
@.str.10 = [3 x i8] c"no\00"
@.str.11 = [6 x i8] c"fgetc\00"
@.str.12 = [27 x i8] c"fgetc result: %d (EOF=%s)\0A\00"
@.str.13 = [6 x i8] c"fread\00"
@.str.14 = [18 x i8] c"fread result: %d\0A\00"
define void @fopen_directory(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca [1024 x i8]
%9 = alloca i32
store i8* %0, i8** %2
%10 = load i8*, i8** %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = call i32 @print_errno(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%13 = load i8*, i8** %2
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %14, i32** %3
%15 = load i32*, i32** %3
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %18
17: ; preds = %1
br label %56
18: ; preds = %1
%19 = load i32*, i32** %3
%20 = load i32, i32* @SEEK_END
%21 = call i32 @fseek(i32* %19, i32 0, i32 %20)
store i32 %21, i32* %4
%22 = call i32 @print_errno(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%23 = load i32, i32* %4
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %23)
%25 = load i32*, i32** %3
%26 = call i64 @ftell(i32* %25)
store i64 %26, i64* %5
%27 = call i32 @print_errno(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%28 = load i64, i64* %5
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i64 %28)
%30 = load i32*, i32** %3
%31 = call i32 @feof(i32* %30)
store i32 %31, i32* %6
%32 = call i32 @print_errno(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%33 = load i32, i32* %6
%34 = load i32, i32* %6
%35 = load i32, i32* @EOF
%36 = icmp eq i32 %34, %35
%37 = zext i1 %36 to i64
%38 = select i1 %36, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0), i32 %33, i8* %38)
%40 = load i32*, i32** %3
%41 = call i32 @fgetc(i32* %40)
store i32 %41, i32* %7
%42 = call i32 @print_errno(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.11, i64 0, i64 0))
%43 = load i32, i32* %7
%44 = load i32, i32* %7
%45 = load i32, i32* @EOF
%46 = icmp eq i32 %44, %45
%47 = zext i1 %46 to i64
%48 = select i1 %46, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0)
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.12, i64 0, i64 0), i32 %43, i8* %48)
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%51 = load i32*, i32** %3
%52 = call i32 @fread(i8* %50, i32 1024, i32 1, i32* %51)
store i32 %52, i32* %9
%53 = call i32 @print_errno(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.13, i64 0, i64 0))
%54 = load i32, i32* %9
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.14, i64 0, i64 0), i32 %54)
br label %56
56: ; preds = %18, %17
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"testing '%s'...\0A\00"
@.str.1 = [6 x i8] c"fopen\00"
@.str.2 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@.str.3 = [6 x i8] c"fseek\00"
@.str.4 = [18 x i8] c"fseek result: %d\0A\00"
@.str.5 = [6 x i8] c"ftell\00"
@.str.6 = [19 x i8] c"ftell result: %ld\0A\00"
@.str.7 = [5 x i8] c"feof\00"
@.str.8 = [26 x i8] c"feof result: %d (EOF=%s)\0A\00"
@EOF = global i32 0
@.str.9 = [4 x i8] c"yes\00"
@.str.10 = [3 x i8] c"no\00"
@.str.11 = [6 x i8] c"fgetc\00"
@.str.12 = [27 x i8] c"fgetc result: %d (EOF=%s)\0A\00"
@.str.13 = [6 x i8] c"fread\00"
@.str.14 = [18 x i8] c"fread result: %d\0A\00"
define void @fopen_directory(i8* %0) {
%2 = alloca [1024 x i8]
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %0)
%4 = tail call i32 @print_errno(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %31, label %7
7: ; preds = %1
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %5, i32 0, i32 %8)
%10 = tail call i32 @print_errno(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %9)
%12 = tail call i64 @ftell(i32* %5)
%13 = tail call i32 @print_errno(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i64 %12)
%15 = tail call i32 @feof(i32* %5)
%16 = tail call i32 @print_errno(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%17 = load i32, i32* @EOF
%18 = icmp eq i32 %15, %17
%19 = select i1 %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0)
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0), i32 %15, i8* %19)
%21 = tail call i32 @fgetc(i32* %5)
%22 = tail call i32 @print_errno(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.11, i64 0, i64 0))
%23 = load i32, i32* @EOF
%24 = icmp eq i32 %21, %23
%25 = select i1 %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0)
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.12, i64 0, i64 0), i32 %21, i8* %25)
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%28 = call i32 @fread(i8* %27, i32 1024, i32 1, i32* %5)
%29 = tail call i32 @print_errno(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.13, i64 0, i64 0))
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.14, i64 0, i64 0), i32 %28)
br label %31
31: ; preds = %1, %7
ret void
}