#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 */
struct stat {
  int st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fstat(int /*<<< orphan*/, struct stat *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
char *memchr(char *, char, int);
int /*<<< orphan*/ mkdir(char *, int);
char *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strncmp(char *, char const *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strrchr(char *, char);
char *write_xpm(char *, int);
int main(int argc, char *argv[]) {
  FILE *lvl_file;
  FILE *unknown_file;
  char buf[100];
  char *data;
  char *last_ptr;
  char *save_ptr;
  char *off_ptr;
  struct stat sb;
  int data_size;
  int counter = 0;
  int unknown = 0;
  const char entry_id[] = "TRPS";
  if (argc < 2) {
    printf("Usage: %s input.lvl [destination]\n", argv[0]);
    exit(1);
  }
  lvl_file = fopen(argv[1], "r");
  if (lvl_file == NULL) {
    perror("Error opening file");
    exit(1);
  }
  fread(buf, 1, 12, lvl_file);
  if ((buf[0] != 'D') && (buf[1] != 'A') && (buf[2] != 'T') &&
      (buf[3] != 'A')) {
    printf("Invalid file\n");
    fclose(lvl_file);
    exit(1);
  }
  if (argc > 2) {
    mkdir(argv[2], 0777);
    chdir(argv[2]);
    printf("Directory: %s\n", argv[2]);
  } else {
    if (strrchr(argv[1], '/'))
      strncpy(buf, strrchr(argv[1], '/') + 1, 16);
    else
      strncpy(buf, argv[1], 16);
    if (strrchr(buf, '.')) {
      char *loc = strrchr(buf, '.');
      *loc = 0;
    }
    mkdir(buf, 0777);
    chdir(buf);
    printf("Directory: %s\n", buf);
  }
  fstat(fileno(lvl_file), &sb);
  data_size = sb.st_size;
  data = mmap(NULL, data_size, PROT_READ, MAP_PRIVATE, fileno(lvl_file), 0);
  last_ptr = data;
  save_ptr = data;
  while (off_ptr = memchr(last_ptr, 'T', (data_size - (last_ptr - data)))) {
    unknown += (off_ptr - last_ptr);
    if (!strncmp(off_ptr, entry_id, 4)) {
      counter++;
      if ((off_ptr - save_ptr > 0)) {
        snprintf(buf, 100, "%08d.bin", counter);
        unknown_file = fopen(buf, "wb");
        fwrite(save_ptr, 1, (off_ptr - save_ptr), unknown_file);
        fclose(unknown_file);
      }
      last_ptr = write_xpm(off_ptr, counter);
      save_ptr = last_ptr;
    } else {
      last_ptr = off_ptr + 1;
    }
  }
  printf("Contains %d extracted images, unknown content: %d bytes\n", counter,
         unknown);
  munmap(data, data_size);
  fclose(lvl_file);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0xc0(%rbp)
movl   $0x0,-0xc4(%rbp)
mov    0xd9d(%rip),%eax
mov    %eax,-0xc9(%rbp)
mov    0xd95(%rip),%al
mov    %al,-0xc5(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    129e <main+0x6e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xd78(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd7b(%rip),%rsi
call   1110 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12d7 <main+0xa7>
lea    0xd62(%rip),%rdi
call   1120 <perror@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
lea    -0x90(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0xc,%edx
call   1060 <fread@plt>
movsbl -0x90(%rbp),%eax
cmp    $0x44,%eax
je     1352 <main+0x122>
movsbl -0x8f(%rbp),%eax
cmp    $0x41,%eax
je     1352 <main+0x122>
movsbl -0x8e(%rbp),%eax
cmp    $0x54,%eax
je     1352 <main+0x122>
movsbl -0x8d(%rbp),%eax
cmp    $0x41,%eax
je     1352 <main+0x122>
lea    0xd05(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1070 <fclose@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
cmpl   $0x2,-0x8(%rbp)
jle    1396 <main+0x166>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    $0x1ff,%esi
call   1050 <mkdir@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <chdir@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xcc1(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
jmp    1477 <main+0x247>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x2f,%esi
call   10c0 <strrchr@plt>
cmp    $0x0,%rax
je     13ef <main+0x1bf>
lea    -0x90(%rbp),%rax
mov    %rax,-0xe0(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x2f,%esi
call   10c0 <strrchr@plt>
mov    -0xe0(%rbp),%rdi
mov    %rax,%rsi
add    $0x1,%rsi
mov    $0x10,%edx
call   1030 <strncpy@plt>
jmp    1408 <main+0x1d8>
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    $0x10,%edx
call   1030 <strncpy@plt>
lea    -0x90(%rbp),%rdi
mov    $0x2e,%esi
call   10c0 <strrchr@plt>
cmp    $0x0,%rax
je     1445 <main+0x215>
lea    -0x90(%rbp),%rdi
mov    $0x2e,%esi
call   10c0 <strrchr@plt>
mov    %rax,-0xd8(%rbp)
mov    -0xd8(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x90(%rbp),%rdi
mov    $0x1ff,%esi
call   1050 <mkdir@plt>
lea    -0x90(%rbp),%rdi
call   1080 <chdir@plt>
lea    -0x90(%rbp),%rsi
lea    0xbdb(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
mov    -0x18(%rbp),%rdi
call   10f0 <fileno@plt>
mov    %eax,%edi
lea    -0xb8(%rbp),%rsi
call   1150 <fstat@plt>
mov    -0xb8(%rbp),%eax
mov    %eax,-0xbc(%rbp)
mov    -0xbc(%rbp),%eax
mov    %eax,-0xec(%rbp)
mov    0x2b4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xe8(%rbp)
mov    0x2b1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xe4(%rbp)
mov    -0x18(%rbp),%rdi
call   10f0 <fileno@plt>
mov    -0xec(%rbp),%esi
mov    -0xe8(%rbp),%edx
mov    -0xe4(%rbp),%ecx
mov    %eax,%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1090 <mmap@plt>
mov    %rax,-0x98(%rbp)
mov    -0x98(%rbp),%rax
mov    %rax,-0xa0(%rbp)
mov    -0x98(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    -0xa0(%rbp),%rdi
movslq -0xbc(%rbp),%rax
mov    -0xa0(%rbp),%rcx
mov    -0x98(%rbp),%rdx
sub    %rdx,%rcx
sub    %rcx,%rax
mov    %eax,%edx
mov    $0x54,%esi
call   10e0 <memchr@plt>
mov    %rax,-0xb0(%rbp)
cmp    $0x0,%rax
je     1665 <main+0x435>
mov    -0xb0(%rbp),%rcx
mov    -0xa0(%rbp),%rax
sub    %rax,%rcx
movslq -0xc4(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0xc4(%rbp)
mov    -0xb0(%rbp),%rdi
lea    -0xc9(%rbp),%rsi
mov    $0x4,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%eax
jne    164e <main+0x41e>
mov    -0xc0(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc0(%rbp)
mov    -0xb0(%rbp),%rax
mov    -0xa8(%rbp),%rcx
sub    %rcx,%rax
cmp    $0x0,%rax
jle    1622 <main+0x3f2>
lea    -0x90(%rbp),%rdi
mov    -0xc0(%rbp),%ecx
mov    $0x64,%esi
lea    0xa85(%rip),%rdx
call   10b0 <snprintf@plt>
lea    -0x90(%rbp),%rdi
lea    0xa7b(%rip),%rsi
call   1110 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0xa8(%rbp),%rdi
mov    -0xb0(%rbp),%rax
mov    -0xa8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1140 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1070 <fclose@plt>
mov    -0xb0(%rbp),%rdi
mov    -0xc0(%rbp),%esi
call   10d0 <write_xpm@plt>
mov    %rax,-0xa0(%rbp)
mov    -0xa0(%rbp),%rax
mov    %rax,-0xa8(%rbp)
jmp    1660 <main+0x430>
mov    -0xb0(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0xa0(%rbp)
jmp    1511 <main+0x2e1>
mov    -0xc0(%rbp),%esi
mov    -0xc4(%rbp),%edx
lea    0x9ee(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
mov    -0x98(%rbp),%rdi
mov    -0xbc(%rbp),%esi
call   1100 <munmap@plt>
mov    -0x18(%rbp),%rdi
call   1070 <fclose@plt>
xor    %eax,%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %rsi,%rbx
movb   $0x0,0x14(%rsp)
movl   $0x53505254,0x10(%rsp)
53
cmp    $0x1,%edi
jle    14f8 <main+0x2b8>
mov    %edi,%ebp
mov    0x8(%rbx),%rdi
lea    0xdc1(%rip),%rsi
call   1120 <fopen@plt>
test   %rax,%rax
je     1513 <main+0x2d3>
mov    %rax,%r14
lea    0x30(%rsp),%rdi
mov    $0x1,%esi
mov    $0xc,%edx
mov    %rax,%rcx
call   1070 <fread@plt>
movdqa 0x30(%rsp),%xmm0
pcmpeqb 0xd53(%rip),%xmm0
punpcklbw %xmm0,%xmm0
punpcklwd %xmm0,%xmm0
movmskps %xmm0,%eax
test   %eax,%eax
je     1529 <main+0x2e9>
cmp    $0x3,%ebp
jb     12e9 <main+0xa9>
mov    0x10(%rbx),%rdi
mov    $0x1ff,%esi
call   1050 <mkdir@plt>
mov    0x10(%rbx),%rdi
call   1090 <chdir@plt>
mov    0x10(%rbx),%rsi
lea    0xd66(%rip),%rdi
jmp    135a <main+0x11a>
mov    0x8(%rbx),%rbx
mov    %rbx,%rdi
mov    $0x2f,%esi
call   10d0 <strrchr@plt>
test   %rax,%rax
je     130d <main+0xcd>
add    $0x1,%rax
lea    0x30(%rsp),%rdi
mov    %rax,%rsi
jmp    1315 <main+0xd5>
lea    0x30(%rsp),%rdi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1030 <strncpy@plt>
lea    0x30(%rsp),%rdi
mov    $0x2e,%esi
call   10d0 <strrchr@plt>
test   %rax,%rax
je     1336 <main+0xf6>
movb   $0x0,(%rax)
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x1ff,%esi
call   1050 <mkdir@plt>
mov    %rbx,%rdi
call   1090 <chdir@plt>
lea    0xcf6(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   10b0 <printf@plt>
mov    %r14,%rdi
call   1100 <fileno@plt>
lea    0x20(%rsp),%rsi
mov    %eax,%edi
call   1160 <fstat@plt>
mov    0x20(%rsp),%ebp
mov    0x2c78(%rip),%rax
mov    (%rax),%r15d
mov    0x2c4e(%rip),%rax
mov    (%rax),%ebx
mov    %r14,0x28(%rsp)
mov    %r14,%rdi
call   1100 <fileno@plt>
xor    %ecx,%ecx
mov    %rcx,0x8(%rsp)
xor    %edi,%edi
mov    %ebp,%esi
mov    %r15d,%edx
mov    %ebx,%ecx
mov    %eax,%r8d
xor    %r9d,%r9d
call   10a0 <mmap@plt>
mov    %rax,0x18(%rsp)
mov    %rax,%rdi
mov    $0x54,%esi
mov    %ebp,%edx
call   10f0 <memchr@plt>
mov    $0x0,%ebx
test   %rax,%rax
je     14b9 <main+0x279>
mov    %rax,%r14
xor    %ebx,%ebx
mov    0x18(%rsp),%r13
mov    %r13,%r12
xor    %eax,%eax
mov    %rax,0x8(%rsp)
mov    %ebp,0x24(%rsp)
jmp    141f <main+0x1df>
add    $0x1,%r14
mov    %r14,%r12
add    %r15d,%ebx
mov    0x18(%rsp),%rax
mov    %eax,%edx
sub    %r12d,%edx
add    %ebp,%edx
mov    %r12,%rdi
mov    $0x54,%esi
call   10f0 <memchr@plt>
mov    %rax,%r14
test   %rax,%rax
je     14b9 <main+0x279>
mov    %r14d,%r15d
sub    %r12d,%r15d
mov    %r14,%rdi
lea    0x10(%rsp),%rsi
mov    $0x4,%edx
call   1040 <strncmp@plt>
test   %eax,%eax
jne    13f0 <main+0x1b0>
mov    0x8(%rsp),%rsi
add    $0x1,%esi
mov    %r14,%rbp
sub    %r13,%rbp
test   %rbp,%rbp
mov    %rsi,0x8(%rsp)
jle    14a2 <main+0x262>
lea    0x30(%rsp),%r12
mov    %r12,%rdi
mov    $0x64,%esi
lea    0xbf5(%rip),%rdx
mov    0x8(%rsp),%rcx
call   10c0 <snprintf@plt>
mov    %r12,%rdi
lea    0xbea(%rip),%rsi
call   1120 <fopen@plt>
mov    %rax,%r12
mov    %r13,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1150 <fwrite@plt>
mov    %r12,%rdi
call   1080 <fclose@plt>
mov    0x8(%rsp),%rsi
mov    %r14,%rdi
call   10e0 <write_xpm@plt>
mov    %rax,%r13
mov    %rax,%r12
mov    0x24(%rsp),%ebp
jmp    13f7 <main+0x1b7>
lea    0xba8(%rip),%rdi
mov    0x8(%rsp),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   10b0 <printf@plt>
mov    0x18(%rsp),%rdi
mov    %ebp,%esi
call   1110 <munmap@plt>
mov    0x28(%rsp),%rdi
call   1080 <fclose@plt>
xor    %eax,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%rsi
lea    0xb13(%rip),%rdi
xor    %eax,%eax
call   10b0 <printf@plt>
mov    $0x1,%edi
call   1140 <exit@plt>
lea    0xb20(%rip),%rdi
call   1130 <perror@plt>
mov    $0x1,%edi
call   1140 <exit@plt>
lea    0xb71(%rip),%rdi
call   1060 <puts@plt>
mov    %r14,%rdi
call   1080 <fclose@plt>
mov    $0x1,%edi
call   1140 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@__const.main.entry_id = [5 x i8] c"TRPS\00"
@.str = [35 x i8] c"Usage: %s input.lvl [destination]\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c"Error opening file\00"
@.str.3 = [14 x i8] c"Invalid file\0A\00"
@.str.4 = [15 x i8] c"Directory: %s\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.5 = [9 x i8] c"%08d.bin\00"
@.str.6 = [3 x i8] c"wb\00"
@.str.7 = [57 x i8] c"Contains %d extracted images, unknown content: %d bytes\0A\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 [100 x i8]
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca %struct.stat
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca [5 x i8]
%18 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %15
store i32 0, i32* %16
%19 = bitcast [5 x i8]* %17 to i8*
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 2
br i1 %21, label %22, label %28
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 0
%25 = load i8*, i8** %24
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %2
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %32, i32** %6
%33 = load i32*, i32** %6
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %38
35: ; preds = %28
%36 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %28
%39 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%40 = load i32*, i32** %6
%41 = call i32 @fread(i8* %39, i32 1, i32 12, i32* %40)
%42 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp ne i32 %44, 68
br i1 %45, label %46, label %66
46: ; preds = %38
%47 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 1
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = icmp ne i32 %49, 65
br i1 %50, label %51, label %66
51: ; preds = %46
%52 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 2
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp ne i32 %54, 84
br i1 %55, label %56, label %66
56: ; preds = %51
%57 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 3
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = icmp ne i32 %59, 65
br i1 %60, label %61, label %66
61: ; preds = %56
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%63 = load i32*, i32** %6
%64 = call i32 @fclose(i32* %63)
%65 = call i32 @exit(i32 1)
unreachable
66: ; preds = %56, %51, %46, %38
%67 = load i32, i32* %4
%68 = icmp sgt i32 %67, 2
br i1 %68, label %69, label %82
69: ; preds = %66
%70 = load i8**, i8*** %5
%71 = getelementptr inbounds i8*, i8** %70, i64 2
%72 = load i8*, i8** %71
%73 = call i32 @mkdir(i8* %72, i32 511)
%74 = load i8**, i8*** %5
%75 = getelementptr inbounds i8*, i8** %74, i64 2
%76 = load i8*, i8** %75
%77 = call i32 @chdir(i8* %76)
%78 = load i8**, i8*** %5
%79 = getelementptr inbounds i8*, i8** %78, i64 2
%80 = load i8*, i8** %79
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %80)
br label %117
82: ; preds = %66
%83 = load i8**, i8*** %5
%84 = getelementptr inbounds i8*, i8** %83, i64 1
%85 = load i8*, i8** %84
%86 = call i8* @strrchr(i8* %85, i8 signext 47)
%87 = icmp ne i8* %86, null
br i1 %87, label %88, label %96
88: ; preds = %82
%89 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%90 = load i8**, i8*** %5
%91 = getelementptr inbounds i8*, i8** %90, i64 1
%92 = load i8*, i8** %91
%93 = call i8* @strrchr(i8* %92, i8 signext 47)
%94 = getelementptr inbounds i8, i8* %93, i64 1
%95 = call i32 @strncpy(i8* %89, i8* %94, i32 16)
br label %102
96: ; preds = %82
%97 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%98 = load i8**, i8*** %5
%99 = getelementptr inbounds i8*, i8** %98, i64 1
%100 = load i8*, i8** %99
%101 = call i32 @strncpy(i8* %97, i8* %100, i32 16)
br label %102
102: ; preds = %96, %88
%103 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%104 = call i8* @strrchr(i8* %103, i8 signext 46)
%105 = icmp ne i8* %104, null
br i1 %105, label %106, label %110
106: ; preds = %102
%107 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%108 = call i8* @strrchr(i8* %107, i8 signext 46)
store i8* %108, i8** %18
%109 = load i8*, i8** %18
store i8 0, i8* %109
br label %110
110: ; preds = %106, %102
%111 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%112 = call i32 @mkdir(i8* %111, i32 511)
%113 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%114 = call i32 @chdir(i8* %113)
%115 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%116 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %115)
br label %117
117: ; preds = %110, %69
%118 = load i32*, i32** %6
%119 = call i32 @fileno(i32* %118)
%120 = call i32 @fstat(i32 %119, %struct.stat* %13)
%121 = getelementptr inbounds %struct.stat, %struct.stat* %13, i32 0, i32 0
%122 = load i32, i32* %121
store i32 %122, i32* %14
%123 = load i32, i32* %14
%124 = load i32, i32* @PROT_READ
%125 = load i32, i32* @MAP_PRIVATE
%126 = load i32*, i32** %6
%127 = call i32 @fileno(i32* %126)
%128 = call i8* @mmap(i32* null, i32 %123, i32 %124, i32 %125, i32 %127, i32 0)
store i8* %128, i8** %9
%129 = load i8*, i8** %9
store i8* %129, i8** %10
%130 = load i8*, i8** %9
store i8* %130, i8** %11
br label %131
131: ; preds = %192, %117
%132 = load i8*, i8** %10
%133 = load i32, i32* %14
%134 = sext i32 %133 to i64
%135 = load i8*, i8** %10
%136 = load i8*, i8** %9
%137 = ptrtoint i8* %135 to i64
%138 = ptrtoint i8* %136 to i64
%139 = sub i64 %137, %138
%140 = sub nsw i64 %134, %139
%141 = trunc i64 %140 to i32
%142 = call i8* @memchr(i8* %132, i8 signext 84, i32 %141)
store i8* %142, i8** %12
%143 = icmp ne i8* %142, null
br i1 %143, label %144, label %193
144: ; preds = %131
%145 = load i8*, i8** %12
%146 = load i8*, i8** %10
%147 = ptrtoint i8* %145 to i64
%148 = ptrtoint i8* %146 to i64
%149 = sub i64 %147, %148
%150 = load i32, i32* %16
%151 = sext i32 %150 to i64
%152 = add nsw i64 %151, %149
%153 = trunc i64 %152 to i32
store i32 %153, i32* %16
%154 = load i8*, i8** %12
%155 = getelementptr inbounds [5 x i8], [5 x i8]* %17, i64 0, i64 0
%156 = call i32 @strncmp(i8* %154, i8* %155, i32 4)
%157 = icmp ne i32 %156, 0
br i1 %157, label %189, label %158
158: ; preds = %144
%159 = load i32, i32* %15
%160 = add nsw i32 %159, 1
store i32 %160, i32* %15
%161 = load i8*, i8** %12
%162 = load i8*, i8** %11
%163 = ptrtoint i8* %161 to i64
%164 = ptrtoint i8* %162 to i64
%165 = sub i64 %163, %164
%166 = icmp sgt i64 %165, 0
br i1 %166, label %167, label %184
167: ; preds = %158
%168 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%169 = load i32, i32* %15
%170 = call i32 @snprintf(i8* %168, i32 100, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %169)
%171 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%172 = call i32* @fopen(i8* %171, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
store i32* %172, i32** %7
%173 = load i8*, i8** %11
%174 = load i8*, i8** %12
%175 = load i8*, i8** %11
%176 = ptrtoint i8* %174 to i64
%177 = ptrtoint i8* %175 to i64
%178 = sub i64 %176, %177
%179 = trunc i64 %178 to i32
%180 = load i32*, i32** %7
%181 = call i32 @fwrite(i8* %173, i32 1, i32 %179, i32* %180)
%182 = load i32*, i32** %7
%183 = call i32 @fclose(i32* %182)
br label %184
184: ; preds = %167, %158
%185 = load i8*, i8** %12
%186 = load i32, i32* %15
%187 = call i8* @write_xpm(i8* %185, i32 %186)
store i8* %187, i8** %10
%188 = load i8*, i8** %10
store i8* %188, i8** %11
br label %192
189: ; preds = %144
%190 = load i8*, i8** %12
%191 = getelementptr inbounds i8, i8* %190, i64 1
store i8* %191, i8** %10
br label %192
192: ; preds = %189, %184
br label %131
193: ; preds = %131
%194 = load i32, i32* %15
%195 = load i32, i32* %16
%196 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.7, i64 0, i64 0), i32 %194, i32 %195)
%197 = load i8*, i8** %9
%198 = load i32, i32* %14
%199 = call i32 @munmap(i8* %197, i32 %198)
%200 = load i32*, i32** %6
%201 = call i32 @fclose(i32* %200)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@__const.main.entry_id = [5 x i8] c"TRPS\00"
@.str = [35 x i8] c"Usage: %s input.lvl [destination]\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c"Error opening file\00"
@.str.4 = [15 x i8] c"Directory: %s\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.5 = [9 x i8] c"%08d.bin\00"
@.str.6 = [3 x i8] c"wb\00"
@.str.7 = [57 x i8] c"Contains %d extracted images, unknown content: %d bytes\0A\00"
@str = [13 x i8] c"Invalid file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [100 x i8]
%4 = alloca %struct.stat
%5 = alloca [5 x i8]
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.stat* %4 to i8*
%8 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%9 = icmp slt i32 %0, 2
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i8*, i8** %1
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = tail call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %14
%23 = call i32 @fread(i8* %6, i32 1, i32 12, i32* %17)
%24 = bitcast [100 x i8]* %3 to <4 x i8>*
%25 = load <4 x i8>, <4 x i8>* %24
%26 = freeze <4 x i8> %25
%27 = icmp eq <4 x i8> %26, <i8 68, i8 65, i8 84, i8 65>
%28 = bitcast <4 x i1> %27 to i4
%29 = icmp eq i4 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %22
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%32 = tail call i32 @fclose(i32* %17)
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %22
%35 = icmp ugt i32 %0, 2
br i1 %35, label %36, label %44
36: ; preds = %34
%37 = getelementptr inbounds i8*, i8** %1, i64 2
%38 = load i8*, i8** %37
%39 = tail call i32 @mkdir(i8* %38, i32 511)
%40 = load i8*, i8** %37
%41 = tail call i32 @chdir(i8* %40)
%42 = load i8*, i8** %37
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %42)
br label %61
44: ; preds = %34
%45 = load i8*, i8** %15
%46 = tail call i8* @strrchr(i8* dereferenceable(1) %45, i8 signext 47)
%47 = icmp eq i8* %46, null
br i1 %47, label %51, label %48
48: ; preds = %44
%49 = getelementptr inbounds i8, i8* %46, i64 1
%50 = call i32 @strncpy(i8* %6, i8* %49, i32 16)
br label %53
51: ; preds = %44
%52 = call i32 @strncpy(i8* %6, i8* %45, i32 16)
br label %53
53: ; preds = %51, %48
%54 = call i8* @strrchr(i8* %6, i8 signext 46)
%55 = icmp eq i8* %54, null
br i1 %55, label %57, label %56
56: ; preds = %53
store i8 0, i8* %54
br label %57
57: ; preds = %56, %53
%58 = call i32 @mkdir(i8* %6, i32 511)
%59 = call i32 @chdir(i8* %6)
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %6)
br label %61
61: ; preds = %57, %36
%62 = call i32 @fileno(i32* %17)
%63 = call i32 @fstat(i32 %62, %struct.stat* %4)
%64 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%65 = load i32, i32* %64
%66 = load i32, i32* @PROT_READ
%67 = load i32, i32* @MAP_PRIVATE
%68 = call i32 @fileno(i32* %17)
%69 = call i8* @mmap(i32* null, i32 %65, i32 %66, i32 %67, i32 %68, i32 0)
%70 = ptrtoint i8* %69 to i64
%71 = call i8* @memchr(i8* %69, i8 signext 84, i32 %65)
%72 = icmp eq i8* %71, null
br i1 %72, label %110, label %73
73: ; preds = %61, %100
%74 = phi i8* [ %108, %100 ], [ %71, %61 ]
%75 = phi i64 [ %104, %100 ], [ %70, %61 ]
%76 = phi i32 [ %82, %100 ], [ 0, %61 ]
%77 = phi i32 [ %102, %100 ], [ 0, %61 ]
%78 = phi i8* [ %101, %100 ], [ %69, %61 ]
%79 = ptrtoint i8* %74 to i64
%80 = sub i64 %79, %75
%81 = trunc i64 %80 to i32
%82 = add i32 %76, %81
%83 = call i32 @strncmp(i8* %74, i8* %8, i32 4)
%84 = icmp eq i32 %83, 0
br i1 %84, label %85, label %98
85: ; preds = %73
%86 = add nsw i32 %77, 1
%87 = ptrtoint i8* %78 to i64
%88 = sub i64 %79, %87
%89 = icmp sgt i64 %88, 0
br i1 %89, label %90, label %96
90: ; preds = %85
%91 = call i32 @snprintf(i8* %6, i32 100, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %86)
%92 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%93 = trunc i64 %88 to i32
%94 = call i32 @fwrite(i8* %78, i32 1, i32 %93, i32* %92)
%95 = call i32 @fclose(i32* %92)
br label %96
96: ; preds = %90, %85
%97 = call i8* @write_xpm(i8* %74, i32 %86)
br label %100
98: ; preds = %73
%99 = getelementptr inbounds i8, i8* %74, i64 1
br label %100
100: ; preds = %98, %96
%101 = phi i8* [ %78, %98 ], [ %97, %96 ]
%102 = phi i32 [ %77, %98 ], [ %86, %96 ]
%103 = phi i8* [ %99, %98 ], [ %97, %96 ]
%104 = ptrtoint i8* %103 to i64
%105 = sub i64 %70, %104
%106 = trunc i64 %105 to i32
%107 = add i32 %65, %106
%108 = call i8* @memchr(i8* %103, i8 signext 84, i32 %107)
%109 = icmp eq i8* %108, null
br i1 %109, label %110, label %73
110: ; preds = %100, %61
%111 = phi i32 [ 0, %61 ], [ %102, %100 ]
%112 = phi i32 [ 0, %61 ], [ %82, %100 ]
%113 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @.str.7, i64 0, i64 0), i32 %111, i32 %112)
%114 = call i32 @munmap(i8* %69, i32 %65)
%115 = call i32 @fclose(i32* %17)
ret i32 0
}