#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 */
struct stat {
  int st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fstat(int /*<<< orphan*/, struct stat *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
char *read_file(void) {
  FILE *fp;
  struct stat fs;
  char *buffer;
  int pos;
  fp = fopen("scroll.c", "r");
  if (!fp) {
    printf("scroll.c was not found\n");
    return NULL;
  }
  fstat(fileno(fp), &fs);
  buffer = (char *)malloc(fs.st_size + 1);
  if (buffer == NULL) {
    fclose(fp);
    return NULL;
  }
  pos = 0;
  for (;;) {
    int c;
    c = fgetc(fp);
    if (c == EOF)
      break;
    buffer[pos++] = (char)c;
    if (pos >= fs.st_size)
      break;
  }
  buffer[pos] = 0;
  fclose(fp);
  return buffer;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe81(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b3 <read_file+0x43>
lea    0xe6a(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    126e <read_file+0xfe>
mov    -0x10(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
lea    -0x18(%rbp),%rsi
call   1090 <fstat@plt>
mov    -0x18(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f7 <read_file+0x87>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    126e <read_file+0xfe>
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    0x2dd4(%rip),%rcx
cmp    (%rcx),%eax
jne    1221 <read_file+0xb1>
jmp    1251 <read_file+0xe1>
mov    -0x28(%rbp),%eax
mov    %al,%dl
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
cmp    -0x18(%rbp),%eax
jl     124c <read_file+0xdc>
jmp    1251 <read_file+0xe1>
jmp    11fe <read_file+0x8e>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%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
lea    0xe7c(%rip),%rdi
lea    0xe7e(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11fd <read_file+0x8d>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <fileno@plt>
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
mov    0x8(%rsp),%ebx
lea    0x1(%rbx),%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     120e <read_file+0x9e>
mov    %rax,%r15
cmp    $0x2,%ebx
mov    $0x1,%r12d
cmovge %ebx,%r12d
xor    %ebx,%ebx
mov    0x2e12(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1050 <fgetc@plt>
cmp    0x0(%r13),%eax
je     121b <read_file+0xab>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    11e0 <read_file+0x70>
jmp    121e <read_file+0xae>
lea    0xe07(%rip),%rdi
call   1030 <puts@plt>
xor    %r15d,%r15d
jmp    122e <read_file+0xbe>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %r15d,%r15d
jmp    122e <read_file+0xbe>
mov    %ebx,%r12d
movslq %r12d,%rax
movb   $0x0,(%r15,%rax,1)
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [9 x i8] c"scroll.c\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [24 x i8] c"scroll.c was not found\0A\00"
@EOF = global i32 0
define i8* @read_file() {
%1 = alloca i8*
%2 = alloca i32*
%3 = alloca %struct.stat
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %2
%8 = load i32*, i32** %2
%9 = icmp ne i32* %8, null
br i1 %9, label %12, label %10
10: ; preds = %0
%11 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %1
br label %56
12: ; preds = %0
%13 = load i32*, i32** %2
%14 = call i32 @fileno(i32* %13)
%15 = call i32 @fstat(i32 %14, %struct.stat* %3)
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%17 = load i32, i32* %16
%18 = add nsw i32 %17, 1
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %4
%21 = load i8*, i8** %4
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %12
%24 = load i32*, i32** %2
%25 = call i32 @fclose(i32* %24)
store i8* null, i8** %1
br label %56
26: ; preds = %12
store i32 0, i32* %5
br label %27
27: ; preds = %47, %26
%28 = load i32*, i32** %2
%29 = call i32 @fgetc(i32* %28)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = load i32, i32* @EOF
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %27
br label %48
34: ; preds = %27
%35 = load i32, i32* %6
%36 = trunc i32 %35 to i8
%37 = load i8*, i8** %4
%38 = load i32, i32* %5
%39 = add nsw i32 %38, 1
store i32 %39, i32* %5
%40 = sext i32 %38 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
store i8 %36, i8* %41
%42 = load i32, i32* %5
%43 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%44 = load i32, i32* %43
%45 = icmp sge i32 %42, %44
br i1 %45, label %46, label %47
46: ; preds = %34
br label %48
47: ; preds = %34
br label %27
48: ; preds = %46, %33
%49 = load i8*, i8** %4
%50 = load i32, i32* %5
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
store i8 0, i8* %52
%53 = load i32*, i32** %2
%54 = call i32 @fclose(i32* %53)
%55 = load i8*, i8** %4
store i8* %55, i8** %1
br label %56
56: ; preds = %48, %23, %10
%57 = load i8*, i8** %1
ret i8* %57
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [9 x i8] c"scroll.c\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i32 0
@str = [23 x i8] c"scroll.c was not found\00"
define i8* @read_file() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %38
7: ; preds = %0
%8 = tail call i32 @fileno(i32* %3)
%9 = call i32 @fstat(i32 %8, %struct.stat* %1)
%10 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%11 = load i32, i32* %10
%12 = add nsw i32 %11, 1
%13 = tail call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
%15 = icmp eq i64 %13, 0
br i1 %15, label %19, label %16
16: ; preds = %7
%17 = call i32 @llvm.smax.i32(i32 %11, i32 1)
%18 = zext i32 %17 to i64
br label %21
19: ; preds = %7
%20 = tail call i32 @fclose(i32* %3)
br label %38
21: ; preds = %16, %26
%22 = phi i64 [ 0, %16 ], [ %28, %26 ]
%23 = tail call i32 @fgetc(i32* %3)
%24 = load i32, i32* @EOF
%25 = icmp eq i32 %23, %24
br i1 %25, label %31, label %26
26: ; preds = %21
%27 = trunc i32 %23 to i8
%28 = add nuw nsw i64 %22, 1
%29 = getelementptr inbounds i8, i8* %14, i64 %22
store i8 %27, i8* %29
%30 = icmp eq i64 %28, %18
br i1 %30, label %33, label %21
31: ; preds = %21
%32 = trunc i64 %22 to i32
br label %33
33: ; preds = %26, %31
%34 = phi i32 [ %32, %31 ], [ %17, %26 ]
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %14, i64 %35
store i8 0, i8* %36
%37 = tail call i32 @fclose(i32* %3)
br label %38
38: ; preds = %33, %19, %5
%39 = phi i8* [ null, %19 ], [ %14, %33 ], [ null, %5 ]
ret i8* %39
}