#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 EINVAL;
int ENOMEM;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
unsigned char *malloc(size_t);
int /*<<< orphan*/ pr_err(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int load_pkt(char *filename, unsigned char **result, size_t *result_len) {
  unsigned char *pkt;
  size_t pkt_len;
  FILE *file;
  int bytes_read;
  file = fopen(filename, "rb");
  if (!file) {
    pr_err("Could not open the file %s.", filename);
    return -EINVAL;
  }
  fseek(file, 0, SEEK_END);
  pkt_len = ftell(file);
  rewind(file);
  pkt = malloc(pkt_len);
  if (!pkt) {
    pr_err("Could not allocate the packet.");
    fclose(file);
    return -ENOMEM;
  }
  bytes_read = fread(pkt, 1, pkt_len, file);
  fclose(file);
  if (bytes_read != pkt_len) {
    pr_err("Reading error.");
    free(pkt);
    return -EINVAL;
  }
  *result = pkt;
  *result_len = pkt_len;
  return 0;
}


x86 assembly (clang, -O0):
<load_pkt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11e8 <load_pkt+0x58>
mov    -0x10(%rbp),%rsi
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1080 <pr_err@plt>
xor    %eax,%eax
mov    0x2df2(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12cb <load_pkt+0x13b>
mov    -0x38(%rbp),%rdi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x38(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x30(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1254 <load_pkt+0xc4>
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1080 <pr_err@plt>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
mov    0x2d8e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12cb <load_pkt+0x13b>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
movslq -0x3c(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     12ae <load_pkt+0x11e>
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1080 <pr_err@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
mov    0x2d2c(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12cb <load_pkt+0x13b>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_pkt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbp
lea    0xe55(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1229 <load_pkt+0x99>
mov    %rax,%rbx
mov    %r14,(%rsp)
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,%r13
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    %r13,%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     123f <load_pkt+0xaf>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r13,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <fclose@plt>
movslq %ebp,%rax
cmp    %rax,%r13
jne    125e <load_pkt+0xce>
mov    %r12,(%r15)
mov    (%rsp),%rax
mov    %r13,(%rax)
xor    %r14d,%r14d
jmp    1281 <load_pkt+0xf1>
lea    0xdd3(%rip),%rdi
xor    %r14d,%r14d
mov    %rbp,%rsi
xor    %eax,%eax
call   1080 <pr_err@plt>
jmp    1277 <load_pkt+0xe7>
lea    0xdd9(%rip),%rdi
xor    %eax,%eax
call   1080 <pr_err@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x2d7c(%rip),%rax
jmp    127e <load_pkt+0xee>
lea    0xdd9(%rip),%rdi
xor    %r14d,%r14d
xor    %eax,%eax
call   1080 <pr_err@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    0x2d52(%rip),%rax
sub    (%rax),%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [28 x i8] c"Could not open the file %s.\00"
@EINVAL = global i32 0
@SEEK_END = global i32 0
@.str.2 = [31 x i8] c"Could not allocate the packet.\00"
@ENOMEM = global i32 0
@.str.3 = [15 x i8] c"Reading error.\00"
define i32 @load_pkt(i8* %0, i8** %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i64*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32*
%11 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i64* %2, i64** %7
%12 = load i8*, i8** %5
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %10
%14 = load i32*, i32** %10
%15 = icmp ne i32* %14, null
br i1 %15, label %21, label %16
16: ; preds = %3
%17 = load i8*, i8** %5
%18 = call i32 (i8*, ...) @pr_err(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @EINVAL
%20 = sub nsw i32 0, %19
store i32 %20, i32* %4
br label %61
21: ; preds = %3
%22 = load i32*, i32** %10
%23 = load i32, i32* @SEEK_END
%24 = call i32 @fseek(i32* %22, i32 0, i32 %23)
%25 = load i32*, i32** %10
%26 = call i64 @ftell(i32* %25)
store i64 %26, i64* %9
%27 = load i32*, i32** %10
%28 = call i32 @rewind(i32* %27)
%29 = load i64, i64* %9
%30 = call i8* @malloc(i64 %29)
store i8* %30, i8** %8
%31 = load i8*, i8** %8
%32 = icmp ne i8* %31, null
br i1 %32, label %39, label %33
33: ; preds = %21
%34 = call i32 (i8*, ...) @pr_err(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32*, i32** %10
%36 = call i32 @fclose(i32* %35)
%37 = load i32, i32* @ENOMEM
%38 = sub nsw i32 0, %37
store i32 %38, i32* %4
br label %61
39: ; preds = %21
%40 = load i8*, i8** %8
%41 = load i64, i64* %9
%42 = load i32*, i32** %10
%43 = call i32 @fread(i8* %40, i32 1, i64 %41, i32* %42)
store i32 %43, i32* %11
%44 = load i32*, i32** %10
%45 = call i32 @fclose(i32* %44)
%46 = load i32, i32* %11
%47 = sext i32 %46 to i64
%48 = load i64, i64* %9
%49 = icmp ne i64 %47, %48
br i1 %49, label %50, label %56
50: ; preds = %39
%51 = call i32 (i8*, ...) @pr_err(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%52 = load i8*, i8** %8
%53 = call i32 @free(i8* %52)
%54 = load i32, i32* @EINVAL
%55 = sub nsw i32 0, %54
store i32 %55, i32* %4
br label %61
56: ; preds = %39
%57 = load i8*, i8** %8
%58 = load i8**, i8*** %6
store i8* %57, i8** %58
%59 = load i64, i64* %9
%60 = load i64*, i64** %7
store i64 %59, i64* %60
store i32 0, i32* %4
br label %61
61: ; preds = %56, %50, %33, %16
%62 = load i32, i32* %4
ret i32 %62
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [28 x i8] c"Could not open the file %s.\00"
@EINVAL = global i32 0
@SEEK_END = global i32 0
@.str.2 = [31 x i8] c"Could not allocate the packet.\00"
@ENOMEM = global i32 0
@.str.3 = [15 x i8] c"Reading error.\00"
define i32 @load_pkt(i8* %0, i8** nocapture writeonly %1, i64* nocapture writeonly %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = tail call i32 (i8*, ...) @pr_err(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%8 = load i32, i32* @EINVAL
%9 = sub nsw i32 0, %8
br label %33
10: ; preds = %3
%11 = load i32, i32* @SEEK_END
%12 = tail call i32 @fseek(i32* %4, i32 0, i32 %11)
%13 = tail call i64 @ftell(i32* %4)
%14 = tail call i32 @rewind(i32* %4)
%15 = tail call i8* @malloc(i64 %13)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %22
17: ; preds = %10
%18 = tail call i32 (i8*, ...) @pr_err(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @fclose(i32* %4)
%20 = load i32, i32* @ENOMEM
%21 = sub nsw i32 0, %20
br label %33
22: ; preds = %10
%23 = tail call i32 @fread(i8* %15, i32 1, i64 %13, i32* %4)
%24 = tail call i32 @fclose(i32* %4)
%25 = sext i32 %23 to i64
%26 = icmp eq i64 %13, %25
br i1 %26, label %32, label %27
27: ; preds = %22
%28 = tail call i32 (i8*, ...) @pr_err(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%29 = tail call i32 @free(i8* %15)
%30 = load i32, i32* @EINVAL
%31 = sub nsw i32 0, %30
br label %33
32: ; preds = %22
store i8* %15, i8** %1
store i64 %13, i64* %2
br label %33
33: ; preds = %32, %27, %17, %6
%34 = phi i32 [ %31, %27 ], [ 0, %32 ], [ %21, %17 ], [ %9, %6 ]
ret i32 %34
}