#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;
};

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
scalar_t__ lseek(int, int, int /*<<< orphan*/);
void *malloc(size_t);
size_t read(int, void *, size_t);
int /*<<< orphan*/ stat(char *, struct stat *);
int load_data_from_file(void *ptr, size_t len, int index, int file_desc,
                        char *filename) {
  struct stat st;
  stat(filename, &st);
  int size = st.st_size;
  if (index >= size)
    return 1;
  if (lseek(file_desc, index, SEEK_SET) < 0)
    return 1;
  ptr = malloc(len);
  if (read(file_desc, ptr, len) != len)
    return 1;
  if (lseek(file_desc, 0, SEEK_SET) < 0)
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<load_data_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <stat@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jl     1185 <load_data_from_file+0x45>
movl   $0x1,-0x4(%rbp)
jmp    1212 <load_data_from_file+0xd2>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
cmp    $0x0,%rax
jge    11af <load_data_from_file+0x6f>
movl   $0x1,-0x4(%rbp)
jmp    1212 <load_data_from_file+0xd2>
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
cmp    -0x18(%rbp),%rax
je     11e2 <load_data_from_file+0xa2>
movl   $0x1,-0x4(%rbp)
jmp    1212 <load_data_from_file+0xd2>
mov    -0x20(%rbp),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
cmp    $0x0,%rax
jge    120b <load_data_from_file+0xcb>
movl   $0x1,-0x4(%rbp)
jmp    1212 <load_data_from_file+0xd2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_data_from_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %r8,%rdi
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %rsi,%r15
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
mov    $0x1,%ebx
cmp    %ebp,0x8(%rsp)
jle    11b6 <load_data_from_file+0x76>
mov    0x2e6d(%rip),%r12
mov    (%r12),%edx
mov    %r14d,%edi
mov    %ebp,%esi
call   1030 <lseek@plt>
test   %rax,%rax
js     11b6 <load_data_from_file+0x76>
mov    %r15,%rdi
call   1060 <malloc@plt>
mov    %r14d,%edi
mov    %rax,%rsi
mov    %r15,%rdx
call   1040 <read@plt>
cmp    %r15,%rax
jne    11b6 <load_data_from_file+0x76>
mov    (%r12),%edx
mov    %r14d,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %rax,%rbx
shr    $0x3f,%rbx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@SEEK_SET = global i32 0
define i32 @load_data_from_file(i8* %0, i64 %1, i32 %2, i32 %3, i8* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca %struct.stat
%13 = alloca i32
store i8* %0, i8** %7
store i64 %1, i64* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i8* %4, i8** %11
%14 = load i8*, i8** %11
%15 = call i32 @stat(i8* %14, %struct.stat* %12)
%16 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%17 = load i32, i32* %16
store i32 %17, i32* %13
%18 = load i32, i32* %9
%19 = load i32, i32* %13
%20 = icmp sge i32 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %5
store i32 1, i32* %6
br label %46
22: ; preds = %5
%23 = load i32, i32* %10
%24 = load i32, i32* %9
%25 = load i32, i32* @SEEK_SET
%26 = call i64 @lseek(i32 %23, i32 %24, i32 %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %22
store i32 1, i32* %6
br label %46
29: ; preds = %22
%30 = load i64, i64* %8
%31 = call i8* @malloc(i64 %30)
store i8* %31, i8** %7
%32 = load i32, i32* %10
%33 = load i8*, i8** %7
%34 = load i64, i64* %8
%35 = call i64 @read(i32 %32, i8* %33, i64 %34)
%36 = load i64, i64* %8
%37 = icmp ne i64 %35, %36
br i1 %37, label %38, label %39
38: ; preds = %29
store i32 1, i32* %6
br label %46
39: ; preds = %29
%40 = load i32, i32* %10
%41 = load i32, i32* @SEEK_SET
%42 = call i64 @lseek(i32 %40, i32 0, i32 %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %39
store i32 1, i32* %6
br label %46
45: ; preds = %39
store i32 0, i32* %6
br label %46
46: ; preds = %45, %44, %38, %28, %21
%47 = load i32, i32* %6
ret i32 %47
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@SEEK_SET = global i32 0
define i32 @load_data_from_file(i8* nocapture readnone %0, i64 %1, i32 %2, i32 %3, i8* nocapture %4) {
%6 = alloca %struct.stat
%7 = bitcast %struct.stat* %6 to i8*
%8 = call i32 @stat(i8* %4, %struct.stat* %6)
%9 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
%10 = load i32, i32* %9
%11 = icmp sgt i32 %10, %2
br i1 %11, label %12, label %25
12: ; preds = %5
%13 = load i32, i32* @SEEK_SET
%14 = tail call i64 @lseek(i32 %3, i32 %2, i32 %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %25, label %16
16: ; preds = %12
%17 = tail call i8* @malloc(i64 %1)
%18 = tail call i64 @read(i32 %3, i8* %17, i64 %1)
%19 = icmp eq i64 %18, %1
br i1 %19, label %20, label %25
20: ; preds = %16
%21 = load i32, i32* @SEEK_SET
%22 = tail call i64 @lseek(i32 %3, i32 0, i32 %21)
%23 = lshr i64 %22, 63
%24 = trunc i64 %23 to i32
br label %25
25: ; preds = %20, %16, %12, %5
%26 = phi i32 [ 1, %5 ], [ 1, %12 ], [ 1, %16 ], [ %24, %20 ]
ret i32 %26
}