#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*/ fclose(int /*<<< orphan*/ *);
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) {
    fclose(fp);
    return NULL;
  }
  if (fread(result, 1, len, fp) != len) {
    fclose(fp);
    free(result);
    return NULL;
  }
  fclose(fp);
  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    0xe83(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    119d <getMemInFile+0x3d>
movq   $0x0,-0x8(%rbp)
jmp    1234 <getMemInFile+0xd4>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
movslq -0x18(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e6 <getMemInFile+0x86>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1234 <getMemInFile+0xd4>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x18(%rbp),%eax
je     1223 <getMemInFile+0xc3>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1234 <getMemInFile+0xd4>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
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   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%ebx
lea    0xe8d(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11d8 <getMemInFile+0x78>
mov    %rax,%rbp
mov    0x2e59(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    %ebx,%esi
call   1070 <fseek@plt>
mov    %r14d,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11d0 <getMemInFile+0x70>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rbp,%rcx
call   1040 <fread@plt>
mov    %eax,%r15d
mov    %rbp,%rdi
call   1050 <fclose@plt>
cmp    %r14d,%r15d
je     11da <getMemInFile+0x7a>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11d8 <getMemInFile+0x78>
mov    %rbp,%rdi
call   1050 <fclose@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
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 %45
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 %29, label %26
26: ; preds = %14
%27 = load i32*, i32** %9
%28 = call i32 @fclose(i32* %27)
store i8* null, i8** %4
br label %45
29: ; preds = %14
%30 = load i8*, i8** %8
%31 = load i32, i32* %7
%32 = load i32*, i32** %9
%33 = call i32 @fread(i8* %30, i32 1, i32 %31, i32* %32)
%34 = load i32, i32* %7
%35 = icmp ne i32 %33, %34
br i1 %35, label %36, label %41
36: ; preds = %29
%37 = load i32*, i32** %9
%38 = call i32 @fclose(i32* %37)
%39 = load i8*, i8** %8
%40 = call i32 @free(i8* %39)
store i8* null, i8** %4
br label %45
41: ; preds = %29
%42 = load i32*, i32** %9
%43 = call i32 @fclose(i32* %42)
%44 = load i8*, i8** %8
store i8* %44, i8** %4
br label %45
45: ; preds = %41, %36, %26, %13
%46 = load i8*, i8** %4
ret i8* %46
}

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 %19, 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 %11, label %13
11: ; preds = %6
%12 = tail call i32 @fclose(i32* %4)
br label %19
13: ; preds = %6
%14 = tail call i32 @fread(i8* %9, i32 1, i32 %2, i32* %4)
%15 = icmp eq i32 %14, %2
%16 = tail call i32 @fclose(i32* %4)
br i1 %15, label %19, label %17
17: ; preds = %13
%18 = tail call i32 @free(i8* %9)
br label %19
19: ; preds = %13, %3, %17, %11
%20 = phi i8* [ null, %17 ], [ null, %11 ], [ null, %3 ], [ %9, %13 ]
ret i8* %20
}