#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*/ assert(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int strlen(char *);
int main(int argc, char **argv) {
  FILE *f;
  int sz;
  char *buf, *p;
  assert(f = fopen(argv[1], "rb"));
  fseek(f, 0, SEEK_END);
  sz = ftell(f);
  rewind(f);
  printf("Unpacking %s, %d bytes:\n", argv[1], sz);
  buf = p = malloc(sz);
  assert(fread(p, 1, sz, f) == sz);
  fclose(f);
  f = NULL;
  assert(sizeof(int) == 4);
  int off, prevoff = 0;
  while (1) {
    off = *((int *)p);
    if (!off)
      off = sz;
    p += 4;
    if (f) {
      int n = off - prevoff;
      printf(".. offset %d, len %d\n", prevoff, n);
      fwrite(buf + prevoff, 1, n, f);
      fclose(f);
    }
    if (off == sz)
      break;
    f = fopen(p, "wb+");
    printf(" -> %s ", p);
    p = p + strlen(p) + 1;
    prevoff = off;
  }
  free(buf);
  return 0;
}


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    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe1b(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    %eax,%edi
call   1090 <assert@plt>
mov    -0x18(%rbp),%rdi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <ftell@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <rewind@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x1c(%rbp),%edx
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x1c(%rbp),%edi
call   10b0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x1c(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x18(%rbp)
mov    $0x1,%edi
call   1090 <assert@plt>
movl   $0x0,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    12a7 <main+0xe7>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1300 <main+0x140>
mov    -0x34(%rbp),%eax
sub    -0x38(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%esi
mov    -0x3c(%rbp),%edx
lea    0xd48(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x28(%rbp),%rdi
movslq -0x38(%rbp),%rax
add    %rax,%rdi
mov    -0x3c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   10e0 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    1311 <main+0x151>
jmp    1367 <main+0x1a7>
mov    -0x30(%rbp),%rdi
lea    0xd16(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%rsi
lea    0xd06(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
jmp    128e <main+0xce>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
lea    0xe27(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,%r15
mov    %r15d,%edi
call   1090 <assert@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    %r15,%rdi
call   10a0 <ftell@plt>
mov    %eax,%r12d
mov    %r15,%rdi
call   1080 <rewind@plt>
mov    0x8(%rbp),%rsi
lea    0xde9(%rip),%rdi
mov    %r12d,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r12d,%edi
call   10b0 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
xor    %edi,%edi
cmp    %r12d,%eax
sete   %dil
call   1090 <assert@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    $0x1,%edi
call   1090 <assert@plt>
mov    %r14,(%rsp)
xor    %ebx,%ebx
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    (%r14),%r13d
test   %r13d,%r13d
cmove  %r12d,%r13d
test   %rbp,%rbp
je     12b7 <main+0xf7>
mov    %r13d,%r15d
sub    %ebx,%r15d
lea    0xd90(%rip),%rdi
mov    %ebx,%esi
mov    %r15d,%edx
xor    %eax,%eax
call   1070 <printf@plt>
movslq %ebx,%rdi
add    (%rsp),%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rbp,%rcx
call   10e0 <fwrite@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
cmp    %r12d,%r13d
je     12fc <main+0x13c>
lea    0x4(%r14),%rbx
mov    %rbx,%rdi
lea    0xd68(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,%rbp
lea    0xd5d(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
add    %rax,%r14
add    $0x5,%r14
mov    %r13d,%ebx
jmp    1270 <main+0xb0>
mov    (%rsp),%rdi
call   1030 <free@plt>
xor    %eax,%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"
@SEEK_END = global i32 0
@.str.1 = [25 x i8] c"Unpacking %s, %d bytes:\0A\00"
@.str.2 = [22 x i8] c".. offset %d, len %d\0A\00"
@.str.3 = [4 x i8] c"wb+\00"
@.str.4 = [8 x i8] c" -> %s \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %6
%17 = ptrtoint i32* %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i32*, i32** %6
%20 = load i32, i32* @SEEK_END
%21 = call i32 @fseek(i32* %19, i32 0, i32 %20)
%22 = load i32*, i32** %6
%23 = call i32 @ftell(i32* %22)
store i32 %23, i32* %7
%24 = load i32*, i32** %6
%25 = call i32 @rewind(i32* %24)
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = load i32, i32* %7
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %28, i32 %29)
%31 = load i32, i32* %7
%32 = call i8* @malloc(i32 %31)
store i8* %32, i8** %9
store i8* %32, i8** %8
%33 = load i8*, i8** %9
%34 = load i32, i32* %7
%35 = load i32*, i32** %6
%36 = call i32 @fread(i8* %33, i32 1, i32 %34, i32* %35)
%37 = load i32, i32* %7
%38 = icmp eq i32 %36, %37
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
%41 = load i32*, i32** %6
%42 = call i32 @fclose(i32* %41)
store i32* null, i32** %6
%43 = call i32 @assert(i32 1)
store i32 0, i32* %11
br label %44
44: ; preds = %2, %78
%45 = load i8*, i8** %9
%46 = bitcast i8* %45 to i32*
%47 = load i32, i32* %46
store i32 %47, i32* %10
%48 = load i32, i32* %10
%49 = icmp ne i32 %48, 0
br i1 %49, label %52, label %50
50: ; preds = %44
%51 = load i32, i32* %7
store i32 %51, i32* %10
br label %52
52: ; preds = %50, %44
%53 = load i8*, i8** %9
%54 = getelementptr inbounds i8, i8* %53, i64 4
store i8* %54, i8** %9
%55 = load i32*, i32** %6
%56 = icmp ne i32* %55, null
br i1 %56, label %57, label %73
57: ; preds = %52
%58 = load i32, i32* %10
%59 = load i32, i32* %11
%60 = sub nsw i32 %58, %59
store i32 %60, i32* %12
%61 = load i32, i32* %11
%62 = load i32, i32* %12
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %61, i32 %62)
%64 = load i8*, i8** %8
%65 = load i32, i32* %11
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %64, i64 %66
%68 = load i32, i32* %12
%69 = load i32*, i32** %6
%70 = call i32 @fwrite(i8* %67, i32 1, i32 %68, i32* %69)
%71 = load i32*, i32** %6
%72 = call i32 @fclose(i32* %71)
br label %73
73: ; preds = %57, %52
%74 = load i32, i32* %10
%75 = load i32, i32* %7
%76 = icmp eq i32 %74, %75
br i1 %76, label %77, label %78
77: ; preds = %73
br label %90
78: ; preds = %73
%79 = load i8*, i8** %9
%80 = call i32* @fopen(i8* %79, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
store i32* %80, i32** %6
%81 = load i8*, i8** %9
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %81)
%83 = load i8*, i8** %9
%84 = load i8*, i8** %9
%85 = call i32 @strlen(i8* %84)
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds i8, i8* %83, i64 %86
%88 = getelementptr inbounds i8, i8* %87, i64 1
store i8* %88, i8** %9
%89 = load i32, i32* %10
store i32 %89, i32* %11
br label %44
90: ; preds = %77
%91 = load i8*, i8** %8
%92 = call i32 @free(i8* %91)
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@.str.1 = [25 x i8] c"Unpacking %s, %d bytes:\0A\00"
@.str.2 = [22 x i8] c".. offset %d, len %d\0A\00"
@.str.3 = [4 x i8] c"wb+\00"
@.str.4 = [8 x i8] c" -> %s \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = ptrtoint i32* %5 to i64
%7 = trunc i64 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %5, i32 0, i32 %9)
%11 = tail call i32 @ftell(i32* %5)
%12 = tail call i32 @rewind(i32* %5)
%13 = load i8*, i8** %3
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %13, i32 %11)
%15 = tail call i8* @malloc(i32 %11)
%16 = tail call i32 @fread(i8* %15, i32 1, i32 %11, i32* %5)
%17 = icmp eq i32 %16, %11
%18 = zext i1 %17 to i32
%19 = tail call i32 @assert(i32 %18)
%20 = tail call i32 @fclose(i32* %5)
%21 = tail call i32 @assert(i32 1)
br label %22
22: ; preds = %41, %2
%23 = phi i8* [ %15, %2 ], [ %47, %41 ]
%24 = phi i32 [ 0, %2 ], [ %29, %41 ]
%25 = phi i32* [ null, %2 ], [ %42, %41 ]
%26 = bitcast i8* %23 to i32*
%27 = load i32, i32* %26
%28 = icmp eq i32 %27, 0
%29 = select i1 %28, i32 %11, i32 %27
%30 = getelementptr inbounds i8, i8* %23, i64 4
%31 = icmp eq i32* %25, null
br i1 %31, label %39, label %32
32: ; preds = %22
%33 = sub nsw i32 %29, %24
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %24, i32 %33)
%35 = sext i32 %24 to i64
%36 = getelementptr inbounds i8, i8* %15, i64 %35
%37 = tail call i32 @fwrite(i8* %36, i32 1, i32 %33, i32* %25)
%38 = tail call i32 @fclose(i32* %25)
br label %39
39: ; preds = %32, %22
%40 = icmp eq i32 %29, %11
br i1 %40, label %48, label %41
41: ; preds = %39
%42 = tail call i32* @fopen(i8* %30, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %30)
%44 = tail call i32 @strlen(i8* %30)
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %23, i64 5
%47 = getelementptr inbounds i8, i8* %46, i64 %45
br label %22
48: ; preds = %39
%49 = tail call i32 @free(i8* %15)
ret i32 0
}