#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_SET;
int /*<<< orphan*/ *fopen(char *, char *);
int fread(void *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
void *malloc(int);
void *getMemInFile(char *filename, int offset, int len) {
  void *result;
  FILE *fp;
  if ((fp = fopen(filename, "r")) == NULL) {
    return NULL;
  }
  fseek(fp, offset, SEEK_SET);
  result = malloc(sizeof(unsigned char) * len);
  if (!result)
    return NULL;
  if (fread(result, 1, len, fp) != len) {
    free(result);
    return NULL;
  }
  return result;
}


x86 assembly (clang, -O0):
<getMemInFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe93(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    118d <getMemInFile+0x3d>
movq   $0x0,-0x8(%rbp)
jmp    1209 <getMemInFile+0xb9>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
call   1060 <fseek@plt>
movslq -0x18(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11cd <getMemInFile+0x7d>
movq   $0x0,-0x8(%rbp)
jmp    1209 <getMemInFile+0xb9>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x18(%rbp),%eax
je     1201 <getMemInFile+0xb1>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1209 <getMemInFile+0xb9>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getMemInFile>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %esi,%ebx
lea    0xea0(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11b0 <getMemInFile+0x60>
mov    %rax,%rbp
mov    0x2e6c(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    %ebx,%esi
call   1060 <fseek@plt>
mov    %r14d,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11b0 <getMemInFile+0x60>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rbp,%rcx
call   1040 <fread@plt>
cmp    %r14d,%eax
je     11b2 <getMemInFile+0x62>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@SEEK_SET = global i32 0
define i8* @getMemInFile(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %9
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %3
store i8* null, i8** %4
br label %39
14: ; preds = %3
%15 = load i32*, i32** %9
%16 = load i32, i32* %6
%17 = load i32, i32* @SEEK_SET
%18 = call i32 @fseek(i32* %15, i32 %16, i32 %17)
%19 = load i32, i32* %7
%20 = sext i32 %19 to i64
%21 = mul i64 1, %20
%22 = trunc i64 %21 to i32
%23 = call i8* @malloc(i32 %22)
store i8* %23, i8** %8
%24 = load i8*, i8** %8
%25 = icmp ne i8* %24, null
br i1 %25, label %27, label %26
26: ; preds = %14
store i8* null, i8** %4
br label %39
27: ; preds = %14
%28 = load i8*, i8** %8
%29 = load i32, i32* %7
%30 = load i32*, i32** %9
%31 = call i32 @fread(i8* %28, i32 1, i32 %29, i32* %30)
%32 = load i32, i32* %7
%33 = icmp ne i32 %31, %32
br i1 %33, label %34, label %37
34: ; preds = %27
%35 = load i8*, i8** %8
%36 = call i32 @free(i8* %35)
store i8* null, i8** %4
br label %39
37: ; preds = %27
%38 = load i8*, i8** %8
store i8* %38, i8** %4
br label %39
39: ; preds = %37, %34, %26, %13
%40 = load i8*, i8** %4
ret i8* %40
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_SET = global i32 0
define noalias i8* @getMemInFile(i8* nocapture %0, i32 %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %16, label %6
6: ; preds = %3
%7 = load i32, i32* @SEEK_SET
%8 = tail call i32 @fseek(i32* %4, i32 %1, i32 %7)
%9 = tail call i8* @malloc(i32 %2)
%10 = icmp eq i8* %9, null
br i1 %10, label %16, label %11
11: ; preds = %6
%12 = tail call i32 @fread(i8* %9, i32 1, i32 %2, i32* %4)
%13 = icmp eq i32 %12, %2
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = tail call i32 @free(i8* %9)
br label %16
16: ; preds = %11, %6, %3, %14
%17 = phi i8* [ null, %14 ], [ null, %3 ], [ null, %6 ], [ %9, %11 ]
ret i8* %17
}