#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*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ read_by_char(int, char **, int *, int *);
char *read_file(const char *filename) {
  int c, charCount = 0, bytes = 1;
  FILE *alpha;
  char *f = malloc(sizeof(char) * bytes);
  if ((alpha = fopen(filename, "r")) == NULL) {
    printf("Couldn't read file: %s\n", filename);
    fclose(alpha);
    return NULL;
  } else {
    while ((c = fgetc(alpha)) != EOF)
      read_by_char(c, &f, &charCount, &bytes);
  }
  fclose(alpha);
  return f;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe68(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11d1 <read_file+0x71>
mov    -0x10(%rbp),%rsi
lea    0xe4c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1220 <read_file+0xc0>
jmp    11d6 <read_file+0x76>
mov    -0x28(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dff(%rip),%rcx
cmp    (%rcx),%eax
je     120a <read_file+0xaa>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x1c(%rbp),%rcx
call   1070 <read_by_char@plt>
jmp    11d6 <read_file+0x76>
jmp    120f <read_file+0xaf>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
movl   $0x0,0x4(%rsp)
movl   $0x1,(%rsp)
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,0x8(%rsp)
lea    0xe6b(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     11fd <read_file+0x9d>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <fgetc@plt>
mov    0x2e34(%rip),%r13
cmp    0x0(%r13),%eax
je     11ee <read_file+0x8e>
lea    0x8(%rsp),%r14
lea    0x4(%rsp),%r15
mov    %rsp,%r12
nopw   0x0(%rax,%rax,1)
mov    %eax,%edi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %r12,%rcx
call   1070 <read_by_char@plt>
mov    %rbx,%rdi
call   1050 <fgetc@plt>
cmp    0x0(%r13),%eax
jne    11d0 <read_file+0x70>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x8(%rsp),%rbx
jmp    1215 <read_file+0xb5>
lea    0xdfe(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
xor    %ebx,%ebx
xor    %edi,%edi
call   1030 <fclose@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [24 x i8] c"Couldn't read file: %s\0A\00"
@EOF = global i32 0
define i8* @read_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i8*
store i8* %0, i8** %3
store i32 0, i32* %5
store i32 1, i32* %6
%9 = load i32, i32* %6
%10 = sext i32 %9 to i64
%11 = mul i64 1, %10
%12 = trunc i64 %11 to i32
%13 = call i8* @malloc(i32 %12)
store i8* %13, i8** %8
%14 = load i8*, i8** %3
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %7
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %22
17: ; preds = %1
%18 = load i8*, i8** %3
%19 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = load i32*, i32** %7
%21 = call i32 @fclose(i32* %20)
store i8* null, i8** %2
br label %36
22: ; preds = %1
br label %23
23: ; preds = %28, %22
%24 = load i32*, i32** %7
%25 = call i32 @fgetc(i32* %24)
store i32 %25, i32* %4
%26 = load i32, i32* @EOF
%27 = icmp ne i32 %25, %26
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i32, i32* %4
%30 = call i32 @read_by_char(i32 %29, i8** %8, i32* %5, i32* %6)
br label %23
31: ; preds = %23
br label %32
32: ; preds = %31
%33 = load i32*, i32** %7
%34 = call i32 @fclose(i32* %33)
%35 = load i8*, i8** %8
store i8* %35, i8** %2
br label %36
36: ; preds = %32, %17
%37 = load i8*, i8** %2
ret i8* %37
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [24 x i8] c"Couldn't read file: %s\0A\00"
@EOF = global i32 0
define i8* @read_file(i8* %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%6 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%7 = bitcast i8** %4 to i8*
%8 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
store i8* %8, i8** %4
%9 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %15, label %11
11: ; preds = %1
%12 = call i32 @fgetc(i32* %9)
%13 = load i32, i32* @EOF
%14 = icmp eq i32 %12, %13
br i1 %14, label %24, label %18
15: ; preds = %1
%16 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%17 = tail call i32 @fclose(i32* null)
br label %27
18: ; preds = %11, %18
%19 = phi i32 [ %21, %18 ], [ %12, %11 ]
%20 = call i32 @read_by_char(i32 %19, i8** %4, i32* %2, i32* %3)
%21 = call i32 @fgetc(i32* %9)
%22 = load i32, i32* @EOF
%23 = icmp eq i32 %21, %22
br i1 %23, label %24, label %18
24: ; preds = %18, %11
%25 = call i32 @fclose(i32* %9)
%26 = load i8*, i8** %4
br label %27
27: ; preds = %24, %15
%28 = phi i8* [ null, %15 ], [ %26, %24 ]
ret i8* %28
}