#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 const *, char *);
int fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, long);
scalar_t__ pig_newseg(long);
int /*<<< orphan*/ printf(char *, char const *);
char *get_pigsty_file_data(const char *filepath) {
  char *retval = NULL;
  FILE *fp = fopen(filepath, "rb");
  long file_size = 0;
  if (fp == NULL) {
    printf("pig i/o PANIC: unable to open file \"%s\".\n", filepath);
    return NULL;
  }
  if (fseek(fp, 0L, SEEK_END) != -1) {
    file_size = ftell(fp);
    fseek(fp, 0L, SEEK_SET);
  } else {
    printf("pig i/o PANIC: unable to get some file informations from \"%s\".\n",
           filepath);
    fclose(fp);
    return NULL;
  }
  retval = (char *)pig_newseg(file_size + 1);
  memset(retval, 0, file_size + 1);
  if (fread(retval, 1, file_size, fp) == -1) {
    printf("pig i/o PANIC: unable to load data from file \"%s\".\n", filepath);
    free(retval);
    retval = NULL;
  }
  fclose(fp);
  return retval;
}


x86 assembly (clang, -O0):
<get_pigsty_file_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e8 <get_pigsty_file_data+0x58>
mov    -0x10(%rbp),%rsi
lea    0xe2d(%rip),%rdi
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ca <get_pigsty_file_data+0x13a>
mov    -0x20(%rbp),%rdi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
cmp    $0xffffffff,%eax
je     122f <get_pigsty_file_data+0x9f>
mov    -0x20(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2db9(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
jmp    1255 <get_pigsty_file_data+0xc5>
mov    -0x10(%rbp),%rsi
lea    0xdf3(%rip),%rdi
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ca <get_pigsty_file_data+0x13a>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   10a0 <pig_newseg@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    $0xffffffff,%eax
jne    12b9 <get_pigsty_file_data+0x129>
mov    -0x10(%rbp),%rsi
lea    0xdca(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pigsty_file_data>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
lea    0xe5d(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1246 <get_pigsty_file_data+0xb6>
mov    %rax,%r15
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
cmp    $0xffffffff,%eax
je     1257 <get_pigsty_file_data+0xc7>
mov    %r15,%rdi
call   1080 <ftell@plt>
mov    %rax,%r12
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
lea    0x1(%r12),%r13
mov    %r13,%rdi
call   10a0 <pig_newseg@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %r13,%rdx
call   1070 <memset@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %r15,%rcx
call   1040 <fread@plt>
cmp    $0xffffffff,%eax
jne    123c <get_pigsty_file_data+0xac>
lea    0xe43(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1050 <fclose@plt>
jmp    1270 <get_pigsty_file_data+0xe0>
lea    0xdb6(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
jmp    126e <get_pigsty_file_data+0xde>
lea    0xdcf(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [42 x i8] c"pig i/o PANIC: unable to open file \22%s\22.\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [64 x i8] c"pig i/o PANIC: unable to get some file informations from \22%s\22.\0A\00"
@.str.3 = [52 x i8] c"pig i/o PANIC: unable to load data from file \22%s\22.\0A\00"
define i8* @get_pigsty_file_data(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i64
store i8* %0, i8** %3
store i8* null, i8** %4
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %5
store i64 0, i64* %6
%9 = load i32*, i32** %5
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %12)
store i8* null, i8** %2
br label %53
14: ; preds = %1
%15 = load i32*, i32** %5
%16 = load i32, i32* @SEEK_END
%17 = call i32 @fseek(i32* %15, i64 0, i32 %16)
%18 = icmp ne i32 %17, -1
br i1 %18, label %19, label %25
19: ; preds = %14
%20 = load i32*, i32** %5
%21 = call i64 @ftell(i32* %20)
store i64 %21, i64* %6
%22 = load i32*, i32** %5
%23 = load i32, i32* @SEEK_SET
%24 = call i32 @fseek(i32* %22, i64 0, i32 %23)
br label %30
25: ; preds = %14
%26 = load i8*, i8** %3
%27 = call i32 @printf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.2, i64 0, i64 0), i8* %26)
%28 = load i32*, i32** %5
%29 = call i32 @fclose(i32* %28)
store i8* null, i8** %2
br label %53
30: ; preds = %19
%31 = load i64, i64* %6
%32 = add nsw i64 %31, 1
%33 = call i64 @pig_newseg(i64 %32)
%34 = inttoptr i64 %33 to i8*
store i8* %34, i8** %4
%35 = load i8*, i8** %4
%36 = load i64, i64* %6
%37 = add nsw i64 %36, 1
%38 = call i32 @memset(i8* %35, i32 0, i64 %37)
%39 = load i8*, i8** %4
%40 = load i64, i64* %6
%41 = load i32*, i32** %5
%42 = call i32 @fread(i8* %39, i32 1, i64 %40, i32* %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %49
44: ; preds = %30
%45 = load i8*, i8** %3
%46 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i8* %45)
%47 = load i8*, i8** %4
%48 = call i32 @free(i8* %47)
store i8* null, i8** %4
br label %49
49: ; preds = %44, %30
%50 = load i32*, i32** %5
%51 = call i32 @fclose(i32* %50)
%52 = load i8*, i8** %4
store i8* %52, i8** %2
br label %53
53: ; preds = %49, %25, %11
%54 = load i8*, i8** %2
ret i8* %54
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [42 x i8] c"pig i/o PANIC: unable to open file \22%s\22.\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [64 x i8] c"pig i/o PANIC: unable to get some file informations from \22%s\22.\0A\00"
@.str.3 = [52 x i8] c"pig i/o PANIC: unable to load data from file \22%s\22.\0A\00"
define i8* @get_pigsty_file_data(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %29
6: ; preds = %1
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %2, i64 0, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %20, label %10
10: ; preds = %6
%11 = tail call i64 @ftell(i32* %2)
%12 = load i32, i32* @SEEK_SET
%13 = tail call i32 @fseek(i32* %2, i64 0, i32 %12)
%14 = add nsw i64 %11, 1
%15 = tail call i64 @pig_newseg(i64 %14)
%16 = inttoptr i64 %15 to i8*
%17 = tail call i32 @memset(i8* %16, i32 0, i64 %14)
%18 = tail call i32 @fread(i8* %16, i32 1, i64 %11, i32* %2)
%19 = icmp eq i32 %18, -1
br i1 %19, label %23, label %26
20: ; preds = %6
%21 = tail call i32 @printf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%22 = tail call i32 @fclose(i32* %2)
br label %29
23: ; preds = %10
%24 = tail call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%25 = tail call i32 @free(i8* %16)
br label %26
26: ; preds = %23, %10
%27 = phi i8* [ null, %23 ], [ %16, %10 ]
%28 = tail call i32 @fclose(i32* %2)
br label %29
29: ; preds = %26, %20, %4
%30 = phi i8* [ null, %4 ], [ %27, %26 ], [ null, %20 ]
ret i8* %30
}