#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*/ ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ cleanup();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ isdir(char *);
int mkdir(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  if (mkdir("fileio-fopen-directory.temp", 0700) == -1) {
    perror("mkdir");
    exit(EXIT_FAILURE);
  }
  if (isdir("fileio-fopen-directory.temp"))
    printf("%s is a directory\n", "fileio-fopen-directory.temp");
  else
    printf("%s is NOT a directory\n", "fileio-fopen-directory.temp");
  FILE *fs = fopen("fileio-fopen-directory.temp", "r");
  if (fs == NULL) {
    perror("fopen");
    cleanup();
    exit(EXIT_FAILURE);
  }
  printf("We fopen()'d a directory?!\n");
  char buf[256];
  ssize_t bytesRead = fread(buf, 1, 256, fs);
  printf("We fread() %zu bytes from the directory.\n", bytesRead);
  if (bytesRead == 0) {
    if (feof(fs))
      printf("We reached the end of a file.");
    else if (ferror(fs))
      printf("An error occurred!\n");
  }
  fclose(fs);
  cleanup();
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe37(%rip),%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
cmp    $0xffffffff,%eax
jne    11f6 <main+0x46>
lea    0xe39(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    0xe03(%rip),%rdi
call   1090 <isdir@plt>
cmp    $0x0,%rax
je     1226 <main+0x76>
lea    0xe0f(%rip),%rdi
lea    0xde6(%rip),%rsi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    123b <main+0x8b>
lea    0xe08(%rip),%rdi
lea    0xdcc(%rip),%rsi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xdbe(%rip),%rdi
lea    0xdd7(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    127e <main+0xce>
lea    0xde8(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x0,%al
call   10a0 <cleanup@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x100,%edx
call   1050 <fread@plt>
mov    %eax,-0x114(%rbp)
mov    -0x114(%rbp),%esi
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
cmpl   $0x0,-0x114(%rbp)
jne    131e <main+0x16e>
mov    -0x10(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%rax
je     12f3 <main+0x143>
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1319 <main+0x169>
mov    -0x10(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     1314 <main+0x164>
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1319 <main+0x169>
jmp    131e <main+0x16e>
mov    -0x10(%rbp),%rdi
call   1060 <fclose@plt>
mov    $0x0,%al
call   10a0 <cleanup@plt>
mov    0x2ca3(%rip),%rax
mov    (%rax),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x108,%rsp
lea    0xe30(%rip),%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
cmp    $0xffffffff,%eax
je     12bb <main+0xfb>
lea    0xe16(%rip),%rbx
mov    %rbx,%rdi
call   10a0 <isdir@plt>
test   %rax,%rax
lea    0xe39(%rip),%rax
lea    0xe1f(%rip),%rdi
cmove  %rax,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0xe08(%rip),%rsi
mov    %rbx,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     12c9 <main+0x109>
mov    %rax,%rbx
lea    0xe67(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x100,%edx
mov    %rbx,%rcx
call   1060 <fread@plt>
mov    %eax,%ebp
lea    0xdfc(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
test   %ebp,%ebp
jne    1299 <main+0xd9>
mov    %rbx,%rdi
call   1090 <feof@plt>
test   %rax,%rax
je     1280 <main+0xc0>
lea    0xe05(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
jmp    1299 <main+0xd9>
mov    %rbx,%rdi
call   1050 <ferror@plt>
test   %rax,%rax
je     1299 <main+0xd9>
lea    0xe21(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1070 <fclose@plt>
xor    %eax,%eax
call   10b0 <cleanup@plt>
mov    0x2d29(%rip),%rax
mov    (%rax),%eax
add    $0x108,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd5a(%rip),%rdi
call   10d0 <perror@plt>
jmp    12dc <main+0x11c>
lea    0xd7c(%rip),%rdi
call   10d0 <perror@plt>
xor    %eax,%eax
call   10b0 <cleanup@plt>
mov    0x2cfd(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [28 x i8] c"fileio-fopen-directory.temp\00"
@.str.1 = [6 x i8] c"mkdir\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [19 x i8] c"%s is a directory\0A\00"
@.str.3 = [23 x i8] c"%s is NOT a directory\0A\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [6 x i8] c"fopen\00"
@.str.6 = [28 x i8] c"We fopen()'d a directory?!\0A\00"
@.str.7 = [42 x i8] c"We fread() %zu bytes from the directory.\0A\00"
@.str.8 = [30 x i8] c"We reached the end of a file.\00"
@.str.9 = [20 x i8] c"An error occurred!\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [256 x i8]
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 @mkdir(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 448)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = call i64 @isdir(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %18
16: ; preds = %11
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %14
%19 = call i32* @fopen(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %19, i32** %2
%20 = load i32*, i32** %2
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %27
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%24 = call i32 (...) @cleanup()
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %18
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%30 = load i32*, i32** %2
%31 = call i32 @fread(i8* %29, i32 1, i32 256, i32* %30)
store i32 %31, i32* %4
%32 = load i32, i32* %4
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.7, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %4
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %50
36: ; preds = %27
%37 = load i32*, i32** %2
%38 = call i64 @feof(i32* %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.8, i64 0, i64 0))
br label %49
42: ; preds = %36
%43 = load i32*, i32** %2
%44 = call i64 @ferror(i32* %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0))
br label %48
48: ; preds = %46, %42
br label %49
49: ; preds = %48, %40
br label %50
50: ; preds = %49, %27
%51 = load i32*, i32** %2
%52 = call i32 @fclose(i32* %51)
%53 = call i32 (...) @cleanup()
%54 = load i32, i32* @EXIT_SUCCESS
ret i32 %54
}

LLVM IR (-O2):
@.str = [28 x i8] c"fileio-fopen-directory.temp\00"
@.str.1 = [6 x i8] c"mkdir\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [19 x i8] c"%s is a directory\0A\00"
@.str.3 = [23 x i8] c"%s is NOT a directory\0A\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [6 x i8] c"fopen\00"
@.str.7 = [42 x i8] c"We fread() %zu bytes from the directory.\0A\00"
@.str.8 = [30 x i8] c"We reached the end of a file.\00"
@EXIT_SUCCESS = global i32 0
@str = [27 x i8] c"We fopen()'d a directory?!\00"
@str.10 = [19 x i8] c"An error occurred!\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = tail call i32 @mkdir(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 448)
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %8
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %0
%9 = tail call i64 @isdir(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i64 %9, 0
%11 = select i1 %10, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %11, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32* @fopen(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %20
15: ; preds = %8
%16 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%17 = tail call i32 (...) @cleanup()
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %8
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%23 = call i32 @fread(i8* %22, i32 1, i32 256, i32* %13)
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.7, i64 0, i64 0), i32 %23)
%25 = icmp eq i32 %23, 0
br i1 %25, label %26, label %36
26: ; preds = %20
%27 = tail call i64 @feof(i32* %13)
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %26
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.8, i64 0, i64 0))
br label %36
31: ; preds = %26
%32 = tail call i64 @ferror(i32* %13)
%33 = icmp eq i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %31
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.10, i64 0, i64 0))
br label %36
36: ; preds = %29, %34, %31, %20
%37 = tail call i32 @fclose(i32* %13)
%38 = tail call i32 (...) @cleanup()
%39 = load i32, i32* @EXIT_SUCCESS
ret i32 %39
}