#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ file_load(char *, char **, int *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int strtoul(char *, int /*<<< orphan*/ *, int);
int main(int argc, char **argv) {
  char *filename = NULL;
  char *image = NULL;
  char *ptr = NULL;
  FILE *out = NULL;
  int image_size = 0;
  int size = 0;
  int offset = 0;
  int i = 0;
  if (argc != 5) {
    fprintf(stderr, "syntax:: get_data file_name offset size output_file\n");
    return 1;
  }
  filename = argv[1];
  offset = strtoul(argv[2], NULL, 16);
  size = strtoul(argv[3], NULL, 16);
  if (!file_load(filename, &image, &image_size)) {
    printf("execve:: error loading file\n");
    return -1;
  }
  out = fopen(argv[4], "w");
  if (!out) {
    fprintf(stderr, "get_data:: error opening file [%s]\n", argv[4]);
    return 2;
  }
  ptr = &image[offset];
  for (i = 0; i < size; ++i) {
    fputc(ptr[i], out);
  }
  fclose(out);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
cmpl   $0x5,-0x8(%rbp)
je     11ef <main+0x7f>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
lea    0xe24(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fd <main+0x18d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1080 <strtoul@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1080 <strtoul@plt>
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   1090 <file_load@plt>
cmp    $0x0,%eax
jne    125f <main+0xef>
lea    0xde7(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fd <main+0x18d>
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
lea    0xde4(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12ad <main+0x13d>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdx
lea    0xdba(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12fd <main+0x18d>
mov    -0x20(%rbp),%rax
movslq -0x3c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x38(%rbp),%eax
jge    12f4 <main+0x184>
mov    -0x28(%rbp),%rax
movslq -0x40(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    -0x30(%rbp),%rsi
movsbl %al,%edi
call   1050 <fputc@plt>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    12c3 <main+0x153>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movq   $0x0,0x8(%rsp)
movl   $0x0,0x4(%rsp)
cmp    $0x5,%edi
jne    1232 <main+0xc2>
mov    %rsi,%rbx
mov    0x8(%rsi),%rbp
mov    0x10(%rsi),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <strtoul@plt>
mov    %eax,%r14d
mov    0x18(%rbx),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <strtoul@plt>
mov    %eax,%r15d
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %rbp,%rdi
call   1090 <file_load@plt>
test   %eax,%eax
je     1250 <main+0xe0>
mov    0x20(%rbx),%rdi
lea    0xe51(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1263 <main+0xf3>
mov    %rax,%r12
test   %r15d,%r15d
jle    1226 <main+0xb6>
movslq %r14d,%r14
add    0x8(%rsp),%r14
mov    %r15d,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl (%r14,%rbx,1),%edi
mov    %r12,%rsi
call   1050 <fputc@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1210 <main+0xa0>
mov    %r12,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    1283 <main+0x113>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
lea    0xdbe(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%eax
jmp    1283 <main+0x113>
lea    0xe04(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    1283 <main+0x113>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
mov    0x20(%rbx),%rdx
lea    0xdc0(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x2,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [53 x i8] c"syntax:: get_data file_name offset size output_file\0A\00"
@.str.1 = [29 x i8] c"execve:: error loading file\0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [36 x i8] c"get_data:: error opening file [%s]\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
store i8* null, i8** %7
store i8* null, i8** %8
store i32* null, i32** %9
store i32 0, i32* %10
store i32 0, i32* %11
store i32 0, i32* %12
store i32 0, i32* %13
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 5
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %72
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
store i8* %22, i8** %6
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
%26 = call i32 @strtoul(i8* %25, i32* null, i32 16)
store i32 %26, i32* %12
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 3
%29 = load i8*, i8** %28
%30 = call i32 @strtoul(i8* %29, i32* null, i32 16)
store i32 %30, i32* %11
%31 = load i8*, i8** %6
%32 = call i32 @file_load(i8* %31, i8** %7, i32* %10)
%33 = icmp ne i32 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %19
%35 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %72
36: ; preds = %19
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 4
%39 = load i8*, i8** %38
%40 = call i32* @fopen(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %40, i32** %9
%41 = load i32*, i32** %9
%42 = icmp ne i32* %41, null
br i1 %42, label %49, label %43
43: ; preds = %36
%44 = load i32, i32* @stderr
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 4
%47 = load i8*, i8** %46
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %47)
store i32 2, i32* %3
br label %72
49: ; preds = %36
%50 = load i8*, i8** %7
%51 = load i32, i32* %12
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
store i8* %53, i8** %8
store i32 0, i32* %13
br label %54
54: ; preds = %66, %49
%55 = load i32, i32* %13
%56 = load i32, i32* %11
%57 = icmp slt i32 %55, %56
br i1 %57, label %58, label %69
58: ; preds = %54
%59 = load i8*, i8** %8
%60 = load i32, i32* %13
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %59, i64 %61
%63 = load i8, i8* %62
%64 = load i32*, i32** %9
%65 = call i32 @fputc(i8 signext %63, i32* %64)
br label %66
66: ; preds = %58
%67 = load i32, i32* %13
%68 = add nsw i32 %67, 1
store i32 %68, i32* %13
br label %54
69: ; preds = %54
%70 = load i32*, i32** %9
%71 = call i32 @fclose(i32* %70)
br label %72
72: ; preds = %69, %43, %34, %16
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [53 x i8] c"syntax:: get_data file_name offset size output_file\0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [36 x i8] c"get_data:: error opening file [%s]\0A\00"
@str = [28 x i8] c"execve:: error loading file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = icmp eq i32 %0, 5
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
br label %49
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call i32 @strtoul(i8* nocapture %15, i32* null, i32 16)
%17 = getelementptr inbounds i8*, i8** %1, i64 3
%18 = load i8*, i8** %17
%19 = tail call i32 @strtoul(i8* nocapture %18, i32* null, i32 16)
%20 = call i32 @file_load(i8* %13, i8** %3, i32* %4)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %11
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %49
24: ; preds = %11
%25 = getelementptr inbounds i8*, i8** %1, i64 4
%26 = load i8*, i8** %25
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = load i8*, i8** %25
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %31)
br label %49
33: ; preds = %24
%34 = load i8*, i8** %3
%35 = sext i32 %16 to i64
%36 = getelementptr inbounds i8, i8* %34, i64 %35
%37 = icmp sgt i32 %19, 0
br i1 %37, label %38, label %47
38: ; preds = %33
%39 = zext i32 %19 to i64
br label %40
40: ; preds = %38, %40
%41 = phi i64 [ 0, %38 ], [ %45, %40 ]
%42 = getelementptr inbounds i8, i8* %36, i64 %41
%43 = load i8, i8* %42
%44 = call i32 @fputc(i8 signext %43, i32* %27)
%45 = add nuw nsw i64 %41, 1
%46 = icmp eq i64 %45, %39
br i1 %46, label %47, label %40
47: ; preds = %40, %33
%48 = call i32 @fclose(i32* %27)
br label %49
49: ; preds = %47, %29, %22, %8
%50 = phi i32 [ 1, %8 ], [ 0, %47 ], [ 2, %29 ], [ -1, %22 ]
ret i32 %50
}