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

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
char *readfile(char *, size_t *);
int /*<<< orphan*/ stdout;
int main(void) {
  size_t size;
  printf("=== Makefile ===\n");
  char *bytes = readfile("Makefile", &size);
  if (bytes) {
    fwrite(bytes, 1, size, stdout);
    free(bytes);
  }
  printf("\n\n=== readfile.c ===\n");
  bytes = readfile("fileio-read-full-file.c", &size);
  if (bytes) {
    fwrite(bytes, 1, size, stdout);
    free(bytes);
  }
  printf("\n\n=== Try reading a directory: /tmp ===\n");
  bytes = readfile("/tmp", &size);
  if (bytes) {
    fwrite(bytes, 1, size, stdout);
    free(bytes);
  }
  printf("\n\n=== Try reading something that doesn't exist ===\n");
  bytes = readfile("/does/not/exist", &size);
  if (bytes) {
    fwrite(bytes, 1, size, stdout);
    free(bytes);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
call   1040 <printf@plt>
lea    0xeb0(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <readfile@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     119e <main+0x5e>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    0x2e57(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1050 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
lea    0xe76(%rip),%rdi
call   1040 <printf@plt>
lea    0xe80(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <readfile@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11ed <main+0xad>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    0x2e08(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1050 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
lea    0xe55(%rip),%rdi
call   1040 <printf@plt>
lea    0xe72(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <readfile@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     123c <main+0xfc>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    0x2db9(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1050 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
lea    0xe34(%rip),%rdi
call   1040 <printf@plt>
lea    0xe5c(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <readfile@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     128b <main+0x14b>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    0x2d6a(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1050 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xeea(%rip),%rdi
call   1040 <puts@plt>
lea    0xea8(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1060 <readfile@plt>
test   %rax,%rax
je     118d <main+0x4d>
mov    %rax,%rbx
mov    0x8(%rsp),%rdx
mov    0x2e6a(%rip),%rax
mov    (%rax),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
call   1050 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xeb3(%rip),%rdi
call   1040 <puts@plt>
lea    0xe69(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1060 <readfile@plt>
test   %rax,%rax
je     11d5 <main+0x95>
mov    %rax,%rbx
mov    0x8(%rsp),%rdx
mov    0x2e22(%rip),%rax
mov    (%rax),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
call   1050 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe80(%rip),%rdi
call   1040 <puts@plt>
lea    0xe39(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1060 <readfile@plt>
test   %rax,%rax
je     121d <main+0xdd>
mov    %rax,%rbx
mov    0x8(%rsp),%rdx
mov    0x2dda(%rip),%rax
mov    (%rax),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
call   1050 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe60(%rip),%rdi
call   1040 <puts@plt>
lea    0xdf6(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1060 <readfile@plt>
test   %rax,%rax
je     1265 <main+0x125>
mov    %rax,%rbx
mov    0x8(%rsp),%rdx
mov    0x2d92(%rip),%rax
mov    (%rax),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
call   1050 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [18 x i8] c"=== Makefile ===\0A\00"
@.str.1 = [9 x i8] c"Makefile\00"
@stdout = global i32 0
@.str.2 = [22 x i8] c"\0A\0A=== readfile.c ===\0A\00"
@.str.3 = [24 x i8] c"fileio-read-full-file.c\00"
@.str.4 = [41 x i8] c"\0A\0A=== Try reading a directory: /tmp ===\0A\00"
@.str.5 = [5 x i8] c"/tmp\00"
@.str.6 = [52 x i8] c"\0A\0A=== Try reading something that doesn't exist ===\0A\00"
@.str.7 = [16 x i8] c"/does/not/exist\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i8*
store i32 0, i32* %1
%4 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%5 = call i8* @readfile(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i64* %2)
store i8* %5, i8** %3
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %15
8: ; preds = %0
%9 = load i8*, i8** %3
%10 = load i64, i64* %2
%11 = load i32, i32* @stdout
%12 = call i32 @fwrite(i8* %9, i32 1, i64 %10, i32 %11)
%13 = load i8*, i8** %3
%14 = call i32 @free(i8* %13)
br label %15
15: ; preds = %8, %0
%16 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%17 = call i8* @readfile(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i64* %2)
store i8* %17, i8** %3
%18 = load i8*, i8** %3
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %27
20: ; preds = %15
%21 = load i8*, i8** %3
%22 = load i64, i64* %2
%23 = load i32, i32* @stdout
%24 = call i32 @fwrite(i8* %21, i32 1, i64 %22, i32 %23)
%25 = load i8*, i8** %3
%26 = call i32 @free(i8* %25)
br label %27
27: ; preds = %20, %15
%28 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
%29 = call i8* @readfile(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i64* %2)
store i8* %29, i8** %3
%30 = load i8*, i8** %3
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %39
32: ; preds = %27
%33 = load i8*, i8** %3
%34 = load i64, i64* %2
%35 = load i32, i32* @stdout
%36 = call i32 @fwrite(i8* %33, i32 1, i64 %34, i32 %35)
%37 = load i8*, i8** %3
%38 = call i32 @free(i8* %37)
br label %39
39: ; preds = %32, %27
%40 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.6, i64 0, i64 0))
%41 = call i8* @readfile(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i64* %2)
store i8* %41, i8** %3
%42 = load i8*, i8** %3
%43 = icmp ne i8* %42, null
br i1 %43, label %44, label %51
44: ; preds = %39
%45 = load i8*, i8** %3
%46 = load i64, i64* %2
%47 = load i32, i32* @stdout
%48 = call i32 @fwrite(i8* %45, i32 1, i64 %46, i32 %47)
%49 = load i8*, i8** %3
%50 = call i32 @free(i8* %49)
br label %51
51: ; preds = %44, %39
%52 = load i32, i32* %1
ret i32 %52
}

LLVM IR (-O2):
@.str.1 = [9 x i8] c"Makefile\00"
@stdout = global i32 0
@.str.3 = [24 x i8] c"fileio-read-full-file.c\00"
@.str.5 = [5 x i8] c"/tmp\00"
@.str.7 = [16 x i8] c"/does/not/exist\00"
@str = [17 x i8] c"=== Makefile ===\00"
@str.8 = [21 x i8] c"\0A\0A=== readfile.c ===\00"
@str.9 = [40 x i8] c"\0A\0A=== Try reading a directory: /tmp ===\00"
@str.10 = [51 x i8] c"\0A\0A=== Try reading something that doesn't exist ===\00"
define i32 @main() {
%1 = alloca i64
%2 = bitcast i64* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%4 = call i8* @readfile(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i64* %1)
%5 = icmp eq i8* %4, null
br i1 %5, label %11, label %6
6: ; preds = %0
%7 = load i64, i64* %1
%8 = load i32, i32* @stdout
%9 = call i32 @fwrite(i8* %4, i32 1, i64 %7, i32 %8)
%10 = call i32 @free(i8* %4)
br label %11
11: ; preds = %6, %0
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.8, i64 0, i64 0))
%13 = call i8* @readfile(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i64* %1)
%14 = icmp eq i8* %13, null
br i1 %14, label %20, label %15
15: ; preds = %11
%16 = load i64, i64* %1
%17 = load i32, i32* @stdout
%18 = call i32 @fwrite(i8* %13, i32 1, i64 %16, i32 %17)
%19 = call i32 @free(i8* %13)
br label %20
20: ; preds = %15, %11
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.9, i64 0, i64 0))
%22 = call i8* @readfile(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i64* %1)
%23 = icmp eq i8* %22, null
br i1 %23, label %29, label %24
24: ; preds = %20
%25 = load i64, i64* %1
%26 = load i32, i32* @stdout
%27 = call i32 @fwrite(i8* %22, i32 1, i64 %25, i32 %26)
%28 = call i32 @free(i8* %22)
br label %29
29: ; preds = %24, %20
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str.10, i64 0, i64 0))
%31 = call i8* @readfile(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i64* %1)
%32 = icmp eq i8* %31, null
br i1 %32, label %38, label %33
33: ; preds = %29
%34 = load i64, i64* %1
%35 = load i32, i32* @stdout
%36 = call i32 @fwrite(i8* %31, i32 1, i64 %34, i32 %35)
%37 = call i32 @free(i8* %31)
br label %38
38: ; preds = %33, %29
ret i32 0
}