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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/);
scalar_t__ malloc(size_t);
int stat(char *, struct stat *);
char *qfits_memory_falloc(char *name, size_t offs, size_t *size,
                          const char *srcname, int srclin) {
  char *t;
  struct stat sta;
  FILE *thefile;
  if (stat(name, &sta) == -1) {
    return NULL;
  }
  if ((t = (char *)malloc((sta.st_size - offs + 1) * sizeof(char)))) {
    if ((thefile = fopen(name, "r"))) {
      fseek(thefile, offs, SEEK_SET);
      fread(t, sizeof(char), sta.st_size - offs + 1, thefile);
      fclose(thefile);
      if (size != NULL) {
        (*size) = sta.st_size;
      }
    }
  }
  return t;
}


x86 assembly (clang, -O0):
<qfits_memory_falloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
jne    119f <qfits_memory_falloc+0x3f>
movq   $0x0,-0x8(%rbp)
jmp    1245 <qfits_memory_falloc+0xe5>
mov    -0x40(%rbp),%rdi
sub    -0x18(%rbp),%rdi
add    $0x1,%rdi
shl    $0x0,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     123d <qfits_memory_falloc+0xdd>
mov    -0x10(%rbp),%rdi
lea    0xe33(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
je     1238 <qfits_memory_falloc+0xd8>
mov    -0x48(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rdx
sub    -0x18(%rbp),%rdx
add    $0x1,%rdx
mov    -0x48(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x48(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x20(%rbp)
je     1233 <qfits_memory_falloc+0xd3>
mov    -0x40(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
jmp    1238 <qfits_memory_falloc+0xd8>
jmp    123d <qfits_memory_falloc+0xdd>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qfits_memory_falloc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
mov    %rsp,%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
je     11ef <qfits_memory_falloc+0x8f>
mov    (%rsp),%rbp
mov    %rbp,%rbx
sub    %r15,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
call   1060 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
je     11f2 <qfits_memory_falloc+0x92>
lea    0xe5a(%rip),%rsi
mov    %r13,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     11f2 <qfits_memory_falloc+0x92>
mov    %rax,%r13
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
mov    %r15,%rsi
call   1070 <fseek@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r13,%rcx
call   1030 <fread@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
test   %r14,%r14
je     11f2 <qfits_memory_falloc+0x92>
mov    %rbp,(%r14)
jmp    11f2 <qfits_memory_falloc+0x92>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [2 x i8] c"r\00"
@SEEK_SET = global i32 0
define i8* @qfits_memory_falloc(i8* nocapture %0, i64 %1, i64* writeonly %2, i8* nocapture readnone %3, i32 %4) {
%6 = alloca %struct.stat
%7 = bitcast %struct.stat* %6 to i8*
%8 = call i32 @stat(i8* %0, %struct.stat* %6)
%9 = icmp eq i32 %8, -1
br i1 %9, label %28, label %10
10: ; preds = %5
%11 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
%12 = load i64, i64* %11
%13 = sub i64 %12, %1
%14 = add i64 %13, 1
%15 = tail call i64 @malloc(i64 %14)
%16 = inttoptr i64 %15 to i8*
%17 = icmp eq i64 %15, 0
br i1 %17, label %28, label %18
18: ; preds = %10
%19 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %28, label %21
21: ; preds = %18
%22 = load i32, i32* @SEEK_SET
%23 = tail call i32 @fseek(i32* %19, i64 %1, i32 %22)
%24 = tail call i32 @fread(i8* %16, i32 1, i64 %14, i32* %19)
%25 = tail call i32 @fclose(i32* %19)
%26 = icmp eq i64* %2, null
br i1 %26, label %28, label %27
27: ; preds = %21
store i64 %12, i64* %2
br label %28
28: ; preds = %10, %21, %27, %18, %5
%29 = phi i8* [ null, %5 ], [ %16, %18 ], [ %16, %27 ], [ %16, %21 ], [ %16, %10 ]
ret i8* %29
}