#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 */

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
int /*<<< orphan*/ printf(char *, ...);
unsigned char *readFiletoBuffer(size_t *, char *);
int /*<<< orphan*/ writeBuffertoFile(unsigned char *, size_t, unsigned char *);
int main(int argc, char *argv[]) {
  unsigned char *buffer;
  unsigned char seg_title[16] = "XXXX.dat";
  size_t size, seg_size, fptr = 0;
  if (argc < 2) {
    printf("Usage:%s <C02-File> <Out-Directory>\n", argv[0]);
    printf("This tool can help you unpack the c02 file\n");
    exit(0);
  }
  buffer = readFiletoBuffer(&size, argv[1]);
  printf("File %s, Length %d\n", argv[1], size);
  while (fptr < size) {
    memcpy(seg_title, &buffer[fptr], 4);
    seg_size = buffer[fptr + 4] * 0x1000000 + buffer[fptr + 5] * 0x10000 +
               buffer[fptr + 6] * 0x100 + buffer[fptr + 7];
    printf("Found Segment %s. size %d\n", seg_title, seg_size);
    writeBuffertoFile(&buffer[fptr], seg_size + 8, seg_title);
    fptr += seg_size + 8;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe93(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe90(%rip),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x48(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b8 <main+0x68>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x38(%rbp),%rdi
call   1030 <readFiletoBuffer@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x38(%rbp),%rdx
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x48(%rbp),%rax
cmp    -0x38(%rbp),%rax
jae    1298 <main+0x148>
lea    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    -0x48(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x48(%rbp),%rcx
movzbl 0x4(%rax,%rcx,1),%eax
shl    $0x18,%eax
mov    -0x18(%rbp),%rcx
mov    -0x48(%rbp),%rdx
movzbl 0x5(%rcx,%rdx,1),%ecx
shl    $0x10,%ecx
add    %ecx,%eax
mov    -0x18(%rbp),%rcx
mov    -0x48(%rbp),%rdx
movzbl 0x6(%rcx,%rdx,1),%ecx
shl    $0x8,%ecx
add    %ecx,%eax
mov    -0x18(%rbp),%rcx
mov    -0x48(%rbp),%rdx
movzbl 0x7(%rcx,%rdx,1),%ecx
add    %ecx,%eax
cltq
mov    %rax,-0x40(%rbp)
lea    -0x30(%rbp),%rsi
mov    -0x40(%rbp),%rdx
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
add    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
add    $0x8,%rsi
lea    -0x30(%rbp),%rdx
call   1050 <writeBuffertoFile@plt>
mov    -0x40(%rbp),%rax
add    $0x8,%rax
add    -0x48(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    11e7 <main+0x97>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
movaps 0xe88(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
cmp    $0x1,%edi
jle    122e <main+0xce>
mov    0x8(%r14),%rsi
lea    0x8(%rsp),%rdi
call   1040 <readFiletoBuffer@plt>
mov    %rax,%r12
mov    0x8(%r14),%rsi
mov    0x8(%rsp),%rdx
lea    0xe8e(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
cmpq   $0x0,0x8(%rsp)
je     121d <main+0xbd>
lea    0x10(%rsp),%r14
lea    0xe87(%rip),%r15
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    (%r12,%rbp,1),%r13
mov    %r14,%rdi
mov    %r13,%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    0x4(%r12,%rbp,1),%ebx
shl    $0x20,%rbx
bswap  %rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x8(%rbx),%rsi
mov    %r13,%rdi
mov    %r14,%rdx
call   1060 <writeBuffertoFile@plt>
add    %rbx,%rbp
add    $0x8,%rbp
cmp    0x8(%rsp),%rbp
jb     11d0 <main+0x70>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r14),%rsi
lea    0xdd8(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe1e(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@__const.main.seg_title = [16 x i8] c"XXXX.dat\00\00\00\00\00\00\00\00"
@.str = [37 x i8] c"Usage:%s <C02-File> <Out-Directory>\0A\00"
@.str.1 = [44 x i8] c"This tool can help you unpack the c02 file\0A\00"
@.str.2 = [20 x i8] c"File %s, Length %d\0A\00"
@.str.3 = [27 x i8] c"Found Segment %s. size %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca [16 x i8]
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = bitcast [16 x i8]* %7 to i8*
store i64 0, i64* %10
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %21
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i8* @readFiletoBuffer(i64* %8, i8* %24)
store i8* %25, i8** %6
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = load i64, i64* %8
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %28, i64 %29)
br label %31
31: ; preds = %35, %21
%32 = load i64, i64* %10
%33 = load i64, i64* %8
%34 = icmp ult i64 %32, %33
br i1 %34, label %35, label %86
35: ; preds = %31
%36 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%37 = load i8*, i8** %6
%38 = load i64, i64* %10
%39 = getelementptr inbounds i8, i8* %37, i64 %38
%40 = call i32 @memcpy(i8* %36, i8* %39, i32 4)
%41 = load i8*, i8** %6
%42 = load i64, i64* %10
%43 = add i64 %42, 4
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = load i8, i8* %44
%46 = zext i8 %45 to i32
%47 = mul nsw i32 %46, 16777216
%48 = load i8*, i8** %6
%49 = load i64, i64* %10
%50 = add i64 %49, 5
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = load i8, i8* %51
%53 = zext i8 %52 to i32
%54 = mul nsw i32 %53, 65536
%55 = add nsw i32 %47, %54
%56 = load i8*, i8** %6
%57 = load i64, i64* %10
%58 = add i64 %57, 6
%59 = getelementptr inbounds i8, i8* %56, i64 %58
%60 = load i8, i8* %59
%61 = zext i8 %60 to i32
%62 = mul nsw i32 %61, 256
%63 = add nsw i32 %55, %62
%64 = load i8*, i8** %6
%65 = load i64, i64* %10
%66 = add i64 %65, 7
%67 = getelementptr inbounds i8, i8* %64, i64 %66
%68 = load i8, i8* %67
%69 = zext i8 %68 to i32
%70 = add nsw i32 %63, %69
%71 = sext i32 %70 to i64
store i64 %71, i64* %9
%72 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%73 = load i64, i64* %9
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %72, i64 %73)
%75 = load i8*, i8** %6
%76 = load i64, i64* %10
%77 = getelementptr inbounds i8, i8* %75, i64 %76
%78 = load i64, i64* %9
%79 = add i64 %78, 8
%80 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%81 = call i32 @writeBuffertoFile(i8* %77, i64 %79, i8* %80)
%82 = load i64, i64* %9
%83 = add i64 %82, 8
%84 = load i64, i64* %10
%85 = add i64 %84, %83
store i64 %85, i64* %10
br label %31
86: ; preds = %31
%87 = load i32, i32* %3
ret i32 %87
}

LLVM IR (-O2):
@__const.main.seg_title = [16 x i8] c"XXXX.dat\00\00\00\00\00\00\00\00"
@.str = [37 x i8] c"Usage:%s <C02-File> <Out-Directory>\0A\00"
@.str.2 = [20 x i8] c"File %s, Length %d\0A\00"
@.str.3 = [27 x i8] c"Found Segment %s. size %d\0A\00"
@str = [43 x i8] c"This tool can help you unpack the c02 file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [16 x i8]
%4 = alloca i64
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%6 = bitcast i64* %4 to i8*
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 0)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = call i8* @readFiletoBuffer(i64* %4, i8* %15)
%17 = load i8*, i8** %14
%18 = load i64, i64* %4
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %17, i64 %18)
%20 = load i64, i64* %4
%21 = icmp eq i64 %20, 0
br i1 %21, label %54, label %22
22: ; preds = %13, %22
%23 = phi i64 [ %51, %22 ], [ 0, %13 ]
%24 = getelementptr inbounds i8, i8* %16, i64 %23
%25 = call i32 @memcpy(i8* %5, i8* %24, i32 4)
%26 = add i64 %23, 4
%27 = getelementptr inbounds i8, i8* %16, i64 %26
%28 = load i8, i8* %27
%29 = zext i8 %28 to i64
%30 = shl nuw nsw i64 %29, 24
%31 = add i64 %23, 5
%32 = getelementptr inbounds i8, i8* %16, i64 %31
%33 = load i8, i8* %32
%34 = zext i8 %33 to i64
%35 = shl nuw nsw i64 %34, 16
%36 = or i64 %35, %30
%37 = add i64 %23, 6
%38 = getelementptr inbounds i8, i8* %16, i64 %37
%39 = load i8, i8* %38
%40 = zext i8 %39 to i64
%41 = shl nuw nsw i64 %40, 8
%42 = or i64 %36, %41
%43 = add i64 %23, 7
%44 = getelementptr inbounds i8, i8* %16, i64 %43
%45 = load i8, i8* %44
%46 = zext i8 %45 to i64
%47 = or i64 %42, %46
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %5, i64 %47)
%49 = add nuw nsw i64 %47, 8
%50 = call i32 @writeBuffertoFile(i8* %24, i64 %49, i8* %5)
%51 = add i64 %49, %23
%52 = load i64, i64* %4
%53 = icmp ult i64 %51, %52
br i1 %53, label %22, label %54
54: ; preds = %22, %13
ret i32 0
}