#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 /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(long);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
const char *read_file(const char *file) {
  char *buffer = 0;
  char fp[1024];
  long length;
  FILE *f;
  strcpy(fp, ".");
  strcat(fp, "/share/cpomf/");
  strcat(fp, file);
  f = fopen(fp, "r");
  if (f == NULL) {
    fprintf(stderr, "The file '%s' doesn't exist.\n", fp);
    return "";
  }
  fseek(f, 0, SEEK_END);
  length = ftell(f);
  fseek(f, 0, SEEK_SET);
  buffer = malloc(length);
  if (buffer) {
    fread(buffer, 1, length, f);
  }
  fclose(f);
  return buffer;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
lea    0xe4b(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x420(%rbp),%rdi
lea    0xe3a(%rip),%rsi
call   10b0 <strcat@plt>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10b0 <strcat@plt>
lea    -0x420(%rbp),%rdi
lea    0xe25(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x430(%rbp)
jne    1231 <read_file+0xa1>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rdx
lea    0xdf6(%rip),%rsi
call   1060 <fprintf@plt>
lea    0xe07(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12bd <read_file+0x12d>
mov    -0x430(%rbp),%rdi
mov    0x2da9(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x430(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x428(%rbp)
mov    -0x430(%rbp),%rdi
mov    0x2d67(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x428(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12a9 <read_file+0x119>
mov    -0x18(%rbp),%rdi
mov    -0x428(%rbp),%rdx
mov    -0x430(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x430(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rdi,%r14
lea    0xe5a(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xe4a(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10b0 <strcat@plt>
lea    0xe3e(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1240 <read_file+0xb0>
mov    %rax,%rbx
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,%r15
mov    0x2dc9(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r15,%rdi
call   1080 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     1236 <read_file+0xa6>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    125f <read_file+0xcf>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    0xdc2(%rip),%rsi
mov    %rsp,%rdx
call   1060 <fprintf@plt>
lea    0xdd0(%rip),%r14
mov    %r14,%rax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@.str.1 = [14 x i8] c"/share/cpomf/\00"
@.str.2 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.3 = [30 x i8] c"The file '%s' doesn't exist.\0A\00"
@.str.4 = [1 x i8] zeroinitializer
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @read_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [1024 x i8]
%6 = alloca i64
%7 = alloca i32*
store i8* %0, i8** %3
store i8* null, i8** %4
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%9 = call i32 @strcpy(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%11 = call i32 @strcat(i8* %10, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%13 = load i8*, i8** %3
%14 = call i32 @strcat(i8* %12, i8* %13)
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %16, i32** %7
%17 = load i32*, i32** %7
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %23
19: ; preds = %1
%20 = load i32, i32* @stderr
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %21)
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0), i8** %2
br label %45
23: ; preds = %1
%24 = load i32*, i32** %7
%25 = load i32, i32* @SEEK_END
%26 = call i32 @fseek(i32* %24, i32 0, i32 %25)
%27 = load i32*, i32** %7
%28 = call i64 @ftell(i32* %27)
store i64 %28, i64* %6
%29 = load i32*, i32** %7
%30 = load i32, i32* @SEEK_SET
%31 = call i32 @fseek(i32* %29, i32 0, i32 %30)
%32 = load i64, i64* %6
%33 = call i8* @malloc(i64 %32)
store i8* %33, i8** %4
%34 = load i8*, i8** %4
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %41
36: ; preds = %23
%37 = load i8*, i8** %4
%38 = load i64, i64* %6
%39 = load i32*, i32** %7
%40 = call i32 @fread(i8* %37, i32 1, i64 %38, i32* %39)
br label %41
41: ; preds = %36, %23
%42 = load i32*, i32** %7
%43 = call i32 @fclose(i32* %42)
%44 = load i8*, i8** %4
store i8* %44, i8** %2
br label %45
45: ; preds = %41, %19
%46 = load i8*, i8** %2
ret i8* %46
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@.str.1 = [14 x i8] c"/share/cpomf/\00"
@.str.2 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.3 = [30 x i8] c"The file '%s' doesn't exist.\0A\00"
@.str.4 = [1 x i8] zeroinitializer
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @read_file(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i32 @strcpy(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = call i32 @strcat(i8* %3, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%6 = call i32 @strcat(i8* %3, i8* %0)
%7 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %3)
br label %24
12: ; preds = %1
%13 = load i32, i32* @SEEK_END
%14 = call i32 @fseek(i32* %7, i32 0, i32 %13)
%15 = call i64 @ftell(i32* %7)
%16 = load i32, i32* @SEEK_SET
%17 = call i32 @fseek(i32* %7, i32 0, i32 %16)
%18 = call i8* @malloc(i64 %15)
%19 = icmp eq i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %12
%21 = call i32 @fread(i8* %18, i32 1, i64 %15, i32* %7)
br label %22
22: ; preds = %20, %12
%23 = call i32 @fclose(i32* %7)
br label %24
24: ; preds = %22, %9
%25 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0), %9 ], [ %18, %22 ]
ret i8* %25
}