#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_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int get_sym_value(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ put_mem28array(int, unsigned char *, int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
char *strtok(int /*<<< orphan*/ *, char *);
void cmdLoad(void) {
  char *strBinFile = strtok(NULL, " ");
  if (!strBinFile) {
    printf("Missing <binfile> parameter!\n");
    return;
  }
  char *strAddr = strtok(NULL, " ");
  if (!strAddr) {
    printf("Missing <addr> parameter!\n");
    return;
  }
  int addr = get_sym_value(strAddr);
  FILE *fload = fopen(strBinFile, "rb");
  if (fload) {
    fseek(fload, 0, SEEK_END);
    int fsize = ftell(fload);
    rewind(fload);
    char *buffer = (char *)malloc(fsize * sizeof(char));
    if (buffer) {
      fread(buffer, fsize, 1, fload);
      int i = 0;
      while (i < fsize) {
        int outSize = fsize - i;
        if (outSize > 16) {
          outSize = 16;
        }
        put_mem28array(addr + i, (unsigned char *)(buffer + i), outSize);
        i += outSize;
      }
      free(buffer);
    }
    fclose(fload);
  } else {
    printf("Error opening the file '%s'!\n", strBinFile);
  }
}


x86 assembly (clang, -O0):
<cmdLoad>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
xor    %eax,%eax
mov    %eax,%edi
lea    0xe2d(%rip),%rsi
call   10e0 <strtok@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11fa <cmdLoad+0x3a>
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1330 <cmdLoad+0x170>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdfb(%rip),%rsi
call   10e0 <strtok@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    122c <cmdLoad+0x6c>
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1330 <cmdLoad+0x170>
mov    -0x10(%rbp),%rdi
call   1040 <get_sym_value@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xdf8(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     131e <cmdLoad+0x15e>
mov    -0x20(%rbp),%rdi
mov    0x2d86(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   10a0 <ftell@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <rewind@plt>
movslq -0x24(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1310 <cmdLoad+0x150>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1050 <fread@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1307 <cmdLoad+0x147>
mov    -0x24(%rbp),%eax
sub    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
cmpl   $0x10,-0x38(%rbp)
jle    12e0 <cmdLoad+0x120>
movl   $0x10,-0x38(%rbp)
mov    -0x14(%rbp),%edi
add    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
movslq -0x34(%rbp),%rax
add    %rax,%rsi
mov    -0x38(%rbp),%edx
call   1090 <put_mem28array@plt>
mov    -0x38(%rbp),%eax
add    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    12ba <cmdLoad+0xfa>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
jmp    1330 <cmdLoad+0x170>
mov    -0x8(%rbp),%rsi
lea    0xd15(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmdLoad>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe1e(%rip),%rsi
xor    %edi,%edi
call   10f0 <strtok@plt>
test   %rax,%rax
je     12d9 <cmdLoad+0x109>
mov    %rax,%rbx
lea    0xe04(%rip),%rsi
xor    %edi,%edi
call   10f0 <strtok@plt>
test   %rax,%rax
je     12e2 <cmdLoad+0x112>
mov    %rax,%rdi
call   1040 <get_sym_value@plt>
mov    %eax,%r15d
lea    0xde4(%rip),%rsi
mov    %rbx,%rdi
call   10e0 <fopen@plt>
test   %rax,%rax
je     12fc <cmdLoad+0x12c>
mov    %rax,%rbp
mov    0x2daf(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    %rbp,%rdi
call   10b0 <ftell@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1090 <rewind@plt>
mov    %ebx,%edi
call   10c0 <malloc@plt>
test   %rax,%rax
je     12c3 <cmdLoad+0xf3>
mov    %rax,%r12
mov    %rax,%rdi
mov    %ebx,%esi
mov    $0x1,%edx
mov    %rbp,(%rsp)
mov    %rbp,%rcx
call   1060 <fread@plt>
test   %ebx,%ebx
jle    12b7 <cmdLoad+0xe7>
xor    %edi,%edi
mov    $0x10,%r13d
nopl   0x0(%rax,%rax,1)
mov    %ebx,%ebp
sub    %edi,%ebp
cmp    $0x10,%ebp
cmovge %r13d,%ebp
movslq %edi,%r14
add    %r15d,%edi
lea    (%r12,%r14,1),%rsi
mov    %ebp,%edx
call   10a0 <put_mem28array@plt>
add    %ebp,%r14d
mov    %r14d,%edi
cmp    %r14d,%ebx
jg     1290 <cmdLoad+0xc0>
mov    %r12,%rdi
call   1030 <free@plt>
mov    (%rsp),%rbp
mov    %rbp,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <fclose@plt>
lea    0xd43(%rip),%rdi
jmp    12e9 <cmdLoad+0x119>
lea    0xd57(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <puts@plt>
lea    0xd02(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <printf@plt>

LLVM IR (-O0):
@.str = [2 x i8] c" \00"
@.str.1 = [30 x i8] c"Missing <binfile> parameter!\0A\00"
@.str.2 = [27 x i8] c"Missing <addr> parameter!\0A\00"
@.str.3 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@.str.4 = [30 x i8] c"Error opening the file '%s'!\0A\00"
define void @cmdLoad() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = call i8* @strtok(i32* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %9, i8** %1
%10 = load i8*, i8** %1
%11 = icmp ne i8* %10, null
br i1 %11, label %14, label %12
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %81
14: ; preds = %0
%15 = call i8* @strtok(i32* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %15, i8** %2
%16 = load i8*, i8** %2
%17 = icmp ne i8* %16, null
br i1 %17, label %20, label %18
18: ; preds = %14
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %81
20: ; preds = %14
%21 = load i8*, i8** %2
%22 = call i32 @get_sym_value(i8* %21)
store i32 %22, i32* %3
%23 = load i8*, i8** %1
%24 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %24, i32** %4
%25 = load i32*, i32** %4
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %78
27: ; preds = %20
%28 = load i32*, i32** %4
%29 = load i32, i32* @SEEK_END
%30 = call i32 @fseek(i32* %28, i32 0, i32 %29)
%31 = load i32*, i32** %4
%32 = call i32 @ftell(i32* %31)
store i32 %32, i32* %5
%33 = load i32*, i32** %4
%34 = call i32 @rewind(i32* %33)
%35 = load i32, i32* %5
%36 = sext i32 %35 to i64
%37 = mul i64 %36, 1
%38 = trunc i64 %37 to i32
%39 = call i64 @malloc(i32 %38)
%40 = inttoptr i64 %39 to i8*
store i8* %40, i8** %6
%41 = load i8*, i8** %6
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %75
43: ; preds = %27
%44 = load i8*, i8** %6
%45 = load i32, i32* %5
%46 = load i32*, i32** %4
%47 = call i32 @fread(i8* %44, i32 %45, i32 1, i32* %46)
store i32 0, i32* %7
br label %48
48: ; preds = %59, %43
%49 = load i32, i32* %7
%50 = load i32, i32* %5
%51 = icmp slt i32 %49, %50
br i1 %51, label %52, label %72
52: ; preds = %48
%53 = load i32, i32* %5
%54 = load i32, i32* %7
%55 = sub nsw i32 %53, %54
store i32 %55, i32* %8
%56 = load i32, i32* %8
%57 = icmp sgt i32 %56, 16
br i1 %57, label %58, label %59
58: ; preds = %52
store i32 16, i32* %8
br label %59
59: ; preds = %58, %52
%60 = load i32, i32* %3
%61 = load i32, i32* %7
%62 = add nsw i32 %60, %61
%63 = load i8*, i8** %6
%64 = load i32, i32* %7
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %63, i64 %65
%67 = load i32, i32* %8
%68 = call i32 @put_mem28array(i32 %62, i8* %66, i32 %67)
%69 = load i32, i32* %8
%70 = load i32, i32* %7
%71 = add nsw i32 %70, %69
store i32 %71, i32* %7
br label %48
72: ; preds = %48
%73 = load i8*, i8** %6
%74 = call i32 @free(i8* %73)
br label %75
75: ; preds = %72, %27
%76 = load i32*, i32** %4
%77 = call i32 @fclose(i32* %76)
br label %81
78: ; preds = %20
%79 = load i8*, i8** %1
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %79)
br label %81
81: ; preds = %12, %18, %78, %75
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
@.str.3 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@.str.4 = [30 x i8] c"Error opening the file '%s'!\0A\00"
@str = [29 x i8] c"Missing <binfile> parameter!\00"
@str.5 = [26 x i8] c"Missing <addr> parameter!\00"
define void @cmdLoad() {
%1 = tail call i8* @strtok(i32* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i8* %1, null
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %42
5: ; preds = %0
%6 = tail call i8* @strtok(i32* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.5, i64 0, i64 0))
br label %42
10: ; preds = %5
%11 = tail call i32 @get_sym_value(i8* %6)
%12 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %40, label %14
14: ; preds = %10
%15 = load i32, i32* @SEEK_END
%16 = tail call i32 @fseek(i32* %12, i32 0, i32 %15)
%17 = tail call i32 @ftell(i32* %12)
%18 = tail call i32 @rewind(i32* %12)
%19 = tail call i64 @malloc(i32 %17)
%20 = inttoptr i64 %19 to i8*
%21 = icmp eq i64 %19, 0
br i1 %21, label %38, label %22
22: ; preds = %14
%23 = tail call i32 @fread(i8* %20, i32 %17, i32 1, i32* %12)
%24 = icmp sgt i32 %17, 0
br i1 %24, label %25, label %36
25: ; preds = %22, %25
%26 = phi i32 [ %34, %25 ], [ 0, %22 ]
%27 = sub nsw i32 %17, %26
%28 = icmp slt i32 %27, 16
%29 = select i1 %28, i32 %27, i32 16
%30 = add nsw i32 %26, %11
%31 = sext i32 %26 to i64
%32 = getelementptr inbounds i8, i8* %20, i64 %31
%33 = tail call i32 @put_mem28array(i32 %30, i8* %32, i32 %29)
%34 = add nsw i32 %29, %26
%35 = icmp sgt i32 %17, %34
br i1 %35, label %25, label %36
36: ; preds = %25, %22
%37 = tail call i32 @free(i8* %20)
br label %38
38: ; preds = %36, %14
%39 = tail call i32 @fclose(i32* %12)
br label %42
40: ; preds = %10
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %1)
br label %42
42: ; preds = %8, %40, %38, %3
ret void
}