#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*/ table;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, short, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int);
int /*<<< orphan*/ fwrite(unsigned char *, int, short, int /*<<< orphan*/ *);
scalar_t__ malloc(short);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, int);
int main(int argc, char *argv[]) {
  int i = 0;
  short flen = 0;
  char fname[12];
  FILE *rfp = NULL, *wfp = NULL;
  unsigned char table[0x40], *buf;
  memset(table, 0, sizeof(table));
  printf("Twilight Zone 3 (MSX) cutter\n");
  {
    rfp = fopen("TZ3.3", "rb");
    if (rfp == NULL) {
      printf("Can't open %s\n", "TZ3.3");
      return -1;
    }
    fseek(rfp, 0x218, SEEK_SET);
    fread(table, 1, 0x3a, rfp);
    fclose(rfp);
  }
  rfp = fopen("BGM.DAT", "rb");
  if (rfp == NULL) {
    printf("Can't open %s\n", "BGM.DAT");
    return -1;
  }
  fseek(rfp, SEEK_SET, 0);
  do {
    flen = table[i * 2 + 2] + (table[i * 2 + 3] * 0x100);
    if (flen == 0) {
      break;
    }
    buf = (unsigned char *)malloc(flen);
    fread(buf, 1, flen, rfp);
    {
      sprintf(fname, "BGM.%03d", i);
      printf("%s size : 0x%04x\n", fname, flen);
      wfp = fopen(fname, "wb");
      fwrite(buf, 1, flen, wfp);
    }
    free(buf);
    i++;
  } while (flen != 0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movw   $0x0,-0x16(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1070 <memset@plt>
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe23(%rip),%rdi
lea    0xe22(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1237 <main+0x97>
lea    0xe0a(%rip),%rdi
lea    0xdfa(%rip),%rsi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13af <main+0x20f>
mov    -0x30(%rbp),%rdi
mov    0x2d9e(%rip),%rax
mov    (%rax),%edx
mov    $0x218,%esi
call   1090 <fseek@plt>
lea    -0x80(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3a,%edx
call   1040 <fread@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xdc1(%rip),%rdi
lea    0xda8(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12b1 <main+0x111>
lea    0xd90(%rip),%rdi
lea    0xd98(%rip),%rsi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13af <main+0x20f>
mov    -0x30(%rbp),%rdi
mov    0x2d24(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <fseek@plt>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
add    $0x2,%eax
cltq
movzbl -0x80(%rbp,%rax,1),%eax
mov    -0x14(%rbp),%ecx
shl    $0x1,%ecx
add    $0x3,%ecx
movslq %ecx,%rcx
movzbl -0x80(%rbp,%rcx,1),%ecx
shl    $0x8,%ecx
add    %ecx,%eax
mov    %ax,-0x16(%rbp)
movswl -0x16(%rbp),%eax
cmp    $0x0,%eax
jne    1301 <main+0x161>
jmp    13a8 <main+0x208>
movswl -0x16(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x88(%rbp)
mov    -0x88(%rbp),%rdi
mov    -0x16(%rbp),%ax
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
movswl %ax,%edx
call   1040 <fread@plt>
lea    -0x22(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xd03(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x22(%rbp),%rsi
movswl -0x16(%rbp),%edx
lea    0xcf8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x22(%rbp),%rdi
lea    0xcf8(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x88(%rbp),%rdi
mov    -0x16(%rbp),%ax
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
movswl %ax,%edx
call   10c0 <fwrite@plt>
mov    -0x88(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
movswl -0x16(%rbp),%eax
cmp    $0x0,%eax
jne    12c5 <main+0x125>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1080 <memset@plt>
lea    0xe68(%rip),%rdi
call   1040 <puts@plt>
lea    0xe1e(%rip),%rbp
lea    0xe1d(%rip),%rsi
mov    %rbp,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     131b <main+0x16b>
mov    %rax,%rbx
mov    0x2ddc(%rip),%r14
mov    (%r14),%edx
mov    %rax,%rdi
mov    $0x218,%esi
call   10a0 <fseek@plt>
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x3a,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    0xdde(%rip),%rbp
lea    0xdc5(%rip),%rsi
mov    %rbp,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     131b <main+0x16b>
mov    %rax,%rbx
mov    (%r14),%esi
xor    %ebp,%ebp
mov    %rax,%rdi
xor    %edx,%edx
call   10a0 <fseek@plt>
movzwl 0x12(%rsp),%eax
test   %ax,%ax
je     1331 <main+0x181>
mov    $0x4,%r13d
lea    0x4(%rsp),%rbp
xor    %r14d,%r14d
movswl %ax,%r15d
mov    %r15d,%edi
call   1090 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
mov    %rbp,%rdi
lea    0xd74(%rip),%rsi
mov    %r14d,%edx
call   10c0 <sprintf@plt>
lea    0xd6e(%rip),%rdi
mov    %rbp,%rsi
mov    %r15d,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbp,%rdi
lea    0xd69(%rip),%rsi
call   10b0 <fopen@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rax,%rcx
call   10d0 <fwrite@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r13d,%eax
movzbl 0x10(%rsp,%rax,1),%ecx
lea    0x1(%r13),%eax
movzbl 0x10(%rsp,%rax,1),%eax
shl    $0x8,%eax
add    $0x1,%r14d
add    $0x2,%r13d
or     %cx,%ax
jne    1280 <main+0xd0>
xor    %ebp,%ebp
jmp    1331 <main+0x181>
lea    0xce7(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"Twilight Zone 3 (MSX) cutter\0A\00"
@.str.1 = [6 x i8] c"TZ3.3\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [15 x i8] c"Can't open %s\0A\00"
@SEEK_SET = global i32 0
@.str.4 = [8 x i8] c"BGM.DAT\00"
@.str.5 = [9 x i8] c"BGM.%03d\00"
@.str.6 = [18 x i8] c"%s size : 0x%04x\0A\00"
@.str.7 = [3 x i8] c"wb\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i16
%8 = alloca [12 x i8]
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca [64 x i8]
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i16 0, i16* %7
store i32* null, i32** %9
store i32* null, i32** %10
%13 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%14 = call i32 @memset(i8* %13, i32 0, i32 64)
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%16 = call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %16, i32** %9
%17 = load i32*, i32** %9
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %91
21: ; preds = %2
%22 = load i32*, i32** %9
%23 = load i32, i32* @SEEK_SET
%24 = call i32 @fseek(i32* %22, i32 536, i32 %23)
%25 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%26 = load i32*, i32** %9
%27 = call i32 @fread(i8* %25, i32 1, i16 signext 58, i32* %26)
%28 = load i32*, i32** %9
%29 = call i32 @fclose(i32* %28)
%30 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %30, i32** %9
%31 = load i32*, i32** %9
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %35
33: ; preds = %21
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %91
35: ; preds = %21
%36 = load i32*, i32** %9
%37 = load i32, i32* @SEEK_SET
%38 = call i32 @fseek(i32* %36, i32 %37, i32 0)
br label %39
39: ; preds = %86, %35
%40 = load i32, i32* %6
%41 = mul nsw i32 %40, 2
%42 = add nsw i32 %41, 2
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 %43
%45 = load i8, i8* %44
%46 = zext i8 %45 to i32
%47 = load i32, i32* %6
%48 = mul nsw i32 %47, 2
%49 = add nsw i32 %48, 3
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 %50
%52 = load i8, i8* %51
%53 = zext i8 %52 to i32
%54 = mul nsw i32 %53, 256
%55 = add nsw i32 %46, %54
%56 = trunc i32 %55 to i16
store i16 %56, i16* %7
%57 = load i16, i16* %7
%58 = sext i16 %57 to i32
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %39
br label %90
61: ; preds = %39
%62 = load i16, i16* %7
%63 = call i64 @malloc(i16 signext %62)
%64 = inttoptr i64 %63 to i8*
store i8* %64, i8** %12
%65 = load i8*, i8** %12
%66 = load i16, i16* %7
%67 = load i32*, i32** %9
%68 = call i32 @fread(i8* %65, i32 1, i16 signext %66, i32* %67)
%69 = getelementptr inbounds [12 x i8], [12 x i8]* %8, i64 0, i64 0
%70 = load i32, i32* %6
%71 = call i32 @sprintf(i8* %69, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %70)
%72 = getelementptr inbounds [12 x i8], [12 x i8]* %8, i64 0, i64 0
%73 = load i16, i16* %7
%74 = sext i16 %73 to i32
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i8* %72, i32 %74)
%76 = getelementptr inbounds [12 x i8], [12 x i8]* %8, i64 0, i64 0
%77 = call i32* @fopen(i8* %76, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
store i32* %77, i32** %10
%78 = load i8*, i8** %12
%79 = load i16, i16* %7
%80 = load i32*, i32** %10
%81 = call i32 @fwrite(i8* %78, i32 1, i16 signext %79, i32* %80)
%82 = load i8*, i8** %12
%83 = call i32 @free(i8* %82)
%84 = load i32, i32* %6
%85 = add nsw i32 %84, 1
store i32 %85, i32* %6
br label %86
86: ; preds = %61
%87 = load i16, i16* %7
%88 = sext i16 %87 to i32
%89 = icmp ne i32 %88, 0
br i1 %89, label %39, label %90
90: ; preds = %86, %60
store i32 0, i32* %3
br label %91
91: ; preds = %90, %33, %19
%92 = load i32, i32* %3
ret i32 %92
}

LLVM IR (-O2):
@.str.1 = [6 x i8] c"TZ3.3\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [15 x i8] c"Can't open %s\0A\00"
@SEEK_SET = global i32 0
@.str.4 = [8 x i8] c"BGM.DAT\00"
@.str.5 = [9 x i8] c"BGM.%03d\00"
@.str.6 = [18 x i8] c"%s size : 0x%04x\0A\00"
@.str.7 = [3 x i8] c"wb\00"
@str = [29 x i8] c"Twilight Zone 3 (MSX) cutter\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [12 x i8]
%4 = alloca [64 x i8]
%5 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%7 = call i32 @memset(i8* %6, i32 0, i32 64)
%8 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%9 = call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %59, label %11
11: ; preds = %2
%12 = load i32, i32* @SEEK_SET
%13 = call i32 @fseek(i32* %9, i32 536, i32 %12)
%14 = call i32 @fread(i8* %6, i32 1, i16 signext 58, i32* %9)
%15 = call i32 @fclose(i32* %9)
%16 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %59, label %18
18: ; preds = %11
%19 = load i32, i32* @SEEK_SET
%20 = call i32 @fseek(i32* %16, i32 %19, i32 0)
%21 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 2
%22 = load i8, i8* %21
%23 = zext i8 %22 to i16
%24 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 3
%25 = load i8, i8* %24
%26 = zext i8 %25 to i16
%27 = shl nuw i16 %26, 8
%28 = or i16 %27, %23
%29 = icmp eq i16 %28, 0
br i1 %29, label %62, label %30
30: ; preds = %18, %30
%31 = phi i64 [ %43, %30 ], [ 0, %18 ]
%32 = phi i16 [ %57, %30 ], [ %28, %18 ]
%33 = sext i16 %32 to i32
%34 = call i64 @malloc(i16 signext %32)
%35 = inttoptr i64 %34 to i8*
%36 = call i32 @fread(i8* %35, i32 1, i16 signext %32, i32* %16)
%37 = trunc i64 %31 to i32
%38 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %37)
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i8* %5, i32 %33)
%40 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%41 = call i32 @fwrite(i8* %35, i32 1, i16 signext %32, i32* %40)
%42 = call i32 @free(i8* %35)
%43 = add nuw i64 %31, 1
%44 = trunc i64 %43 to i32
%45 = shl nuw nsw i32 %44, 1
%46 = add nuw nsw i32 %45, 2
%47 = zext i32 %46 to i64
%48 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 %47
%49 = load i8, i8* %48
%50 = zext i8 %49 to i16
%51 = add nuw nsw i32 %45, 3
%52 = zext i32 %51 to i64
%53 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 %52
%54 = load i8, i8* %53
%55 = zext i8 %54 to i16
%56 = shl nuw i16 %55, 8
%57 = or i16 %56, %50
%58 = icmp eq i16 %57, 0
br i1 %58, label %62, label %30
59: ; preds = %11, %2
%60 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %2 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), %11 ]
%61 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %60)
br label %62
62: ; preds = %30, %59, %18
%63 = phi i32 [ 0, %18 ], [ -1, %59 ], [ 0, %30 ]
ret i32 %63
}