#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_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *, unsigned int, int, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, int);
void write(unsigned char *_src, int _size, char *_prefix) {
  FILE *fh;
  unsigned int offset;
  unsigned short songno;
  int size, remain;
  char name[0x20];
  int count = 0;
  songno = 0;
  offset = 0;
  remain = _size;
  do {
    if (!(_src[offset] == 0x0c || _src[offset] == 0xfe) ||
        _src[offset + 1] != 0x00) {
      break;
    }
    size = 512;
    remain -= 512;
    while (remain > 0) {
      if ((_src[offset + size + 1] == 0x00 && _src[offset + size] == 0x0c) ||
          (_src[offset + size + 1] == 0x00 && _src[offset + size] == 0xfe)) {
        break;
      }
      remain -= 512;
      size += 512;
      continue;
    }
    sprintf(name, "%s%02X.BGM", _prefix, count);
    printf("%s ofs:0x%04x size:0x%04x no:%02x\n", name, offset, size, count);
    fh = fopen(name, "wb");
    fseek(fh, SEEK_SET, 0);
    fwrite(_src + offset, 1, size, fh);
    fclose(fh);
    offset += size;
    count++;
  } while (offset < _size);
}


x86 assembly (clang, -O0):
<write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x54(%rbp)
movw   $0x0,-0x26(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
movzbl (%rax,%rcx,1),%eax
cmp    $0xc,%eax
je     11b7 <write+0x57>
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
movzbl (%rax,%rcx,1),%eax
cmp    $0xfe,%eax
jne    11d0 <write+0x70>
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,%ecx
movzbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     11d5 <write+0x75>
jmp    1318 <write+0x1b8>
movl   $0x200,-0x2c(%rbp)
mov    -0x30(%rbp),%eax
sub    $0x200,%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jle    127d <write+0x11d>
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    -0x2c(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,%ecx
movzbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    1226 <write+0xc6>
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    -0x2c(%rbp),%ecx
mov    %ecx,%ecx
movzbl (%rax,%rcx,1),%eax
cmp    $0xc,%eax
je     125d <write+0xfd>
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    -0x2c(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,%ecx
movzbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    1262 <write+0x102>
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    -0x2c(%rbp),%ecx
mov    %ecx,%ecx
movzbl (%rax,%rcx,1),%eax
cmp    $0xfe,%eax
jne    1262 <write+0x102>
jmp    127d <write+0x11d>
mov    -0x30(%rbp),%eax
sub    $0x200,%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x200,%eax
mov    %eax,-0x2c(%rbp)
jmp    11e7 <write+0x87>
lea    -0x50(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x54(%rbp),%ecx
lea    0xd71(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x50(%rbp),%rsi
mov    -0x24(%rbp),%edx
mov    -0x2c(%rbp),%ecx
mov    -0x54(%rbp),%r8d
lea    0xd62(%rip),%rdi
call   1040 <printf@plt>
lea    -0x50(%rbp),%rdi
lea    0xd75(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2d13(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <fseek@plt>
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%eax
add    %rax,%rdi
mov    -0x2c(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x2c(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jb     118d <write+0x2d>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rdx,0x10(%rsp)
mov    %rdi,%rbx
xor    %r14d,%r14d
mov    %esi,0xc(%rsp)
mov    %esi,%r12d
xor    %r15d,%r15d
jmp    1228 <write+0xc8>
nopl   0x0(%rax,%rax,1)
add    $0xfffffe00,%r12d
mov    $0x200,%r13d
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
lea    0xe54(%rip),%rsi
mov    0x10(%rsp),%rdx
mov    %r15d,%ecx
call   1070 <sprintf@plt>
lea    0xe4b(%rip),%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
mov    %r13d,%ecx
mov    %r15d,%r8d
call   1040 <printf@plt>
mov    %rbp,%rdi
lea    0xe53(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbp
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
xor    %edx,%edx
call   1050 <fseek@plt>
mov    0x18(%rsp),%rdi
mov    $0x1,%esi
mov    %r13d,%edx
mov    %rbp,%rcx
call   1080 <fwrite@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
add    %r14d,%r13d
add    $0x1,%r15d
mov    %r13d,%r14d
cmp    0xc(%rsp),%r13d
jae    12db <write+0x17b>
mov    %r14d,%eax
mov    (%rbx,%rax,1),%cl
cmp    $0xc,%cl
je     123c <write+0xdc>
cmp    $0xfe,%cl
jne    12db <write+0x17b>
mov    %rax,%rcx
or     $0x1,%rcx
cmpb   $0x0,(%rbx,%rcx,1)
jne    12db <write+0x17b>
lea    (%rbx,%rax,1),%rdi
cmp    $0x201,%r12d
mov    %rdi,0x18(%rsp)
jl     1190 <write+0x30>
lea    0x1ff(%r12),%eax
and    $0xfffffe00,%eax
mov    $0x200,%r13d
xor    %ecx,%ecx
jmp    129f <write+0x13f>
nopw   0x0(%rax,%rax,1)
add    $0x200,%r13d
lea    (%r12,%rcx,1),%edx
add    $0xfffffe00,%edx
add    $0xfffffe00,%ecx
cmp    $0x200,%edx
jle    12d0 <write+0x170>
lea    (%r14,%r13,1),%edx
mov    %rdx,%rsi
or     $0x1,%rsi
cmpb   $0x0,(%rbx,%rsi,1)
jne    1280 <write+0x120>
mov    %edx,%edx
movzbl (%rbx,%rdx,1),%edx
cmp    $0xfe,%dl
je     12c0 <write+0x160>
cmp    $0xc,%dl
jne    1280 <write+0x120>
sub    %r13d,%r12d
jmp    119d <write+0x3d>
nopl   0x0(%rax,%rax,1)
sub    %r13d,%r12d
mov    %eax,%r13d
jmp    119d <write+0x3d>
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"%s%02X.BGM\00"
@.str.1 = [35 x i8] c"%s ofs:0x%04x size:0x%04x no:%02x\0A\00"
@.str.2 = [3 x i8] c"wb\00"
@SEEK_SET = global i32 0
define void @write(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i16
%10 = alloca i32
%11 = alloca i32
%12 = alloca [32 x i8]
%13 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
store i32 0, i32* %13
store i16 0, i16* %9
store i32 0, i32* %8
%14 = load i32, i32* %5
store i32 %14, i32* %11
br label %15
15: ; preds = %124, %3
%16 = load i8*, i8** %4
%17 = load i32, i32* %8
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = icmp eq i32 %21, 12
br i1 %22, label %31, label %23
23: ; preds = %15
%24 = load i8*, i8** %4
%25 = load i32, i32* %8
%26 = zext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = icmp eq i32 %29, 254
br i1 %30, label %31, label %40
31: ; preds = %23, %15
%32 = load i8*, i8** %4
%33 = load i32, i32* %8
%34 = add i32 %33, 1
%35 = zext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %32, i64 %35
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %31, %23
br label %128
41: ; preds = %31
store i32 512, i32* %10
%42 = load i32, i32* %11
%43 = sub nsw i32 %42, 512
store i32 %43, i32* %11
br label %44
44: ; preds = %90, %41
%45 = load i32, i32* %11
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %95
47: ; preds = %44
%48 = load i8*, i8** %4
%49 = load i32, i32* %8
%50 = load i32, i32* %10
%51 = add i32 %49, %50
%52 = add i32 %51, 1
%53 = zext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %48, i64 %53
%55 = load i8, i8* %54
%56 = zext i8 %55 to i32
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %68
58: ; preds = %47
%59 = load i8*, i8** %4
%60 = load i32, i32* %8
%61 = load i32, i32* %10
%62 = add i32 %60, %61
%63 = zext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %59, i64 %63
%65 = load i8, i8* %64
%66 = zext i8 %65 to i32
%67 = icmp eq i32 %66, 12
br i1 %67, label %89, label %68
68: ; preds = %58, %47
%69 = load i8*, i8** %4
%70 = load i32, i32* %8
%71 = load i32, i32* %10
%72 = add i32 %70, %71
%73 = add i32 %72, 1
%74 = zext i32 %73 to i64
%75 = getelementptr inbounds i8, i8* %69, i64 %74
%76 = load i8, i8* %75
%77 = zext i8 %76 to i32
%78 = icmp eq i32 %77, 0
br i1 %78, label %79, label %90
79: ; preds = %68
%80 = load i8*, i8** %4
%81 = load i32, i32* %8
%82 = load i32, i32* %10
%83 = add i32 %81, %82
%84 = zext i32 %83 to i64
%85 = getelementptr inbounds i8, i8* %80, i64 %84
%86 = load i8, i8* %85
%87 = zext i8 %86 to i32
%88 = icmp eq i32 %87, 254
br i1 %88, label %89, label %90
89: ; preds = %79, %58
br label %95
90: ; preds = %79, %68
%91 = load i32, i32* %11
%92 = sub nsw i32 %91, 512
store i32 %92, i32* %11
%93 = load i32, i32* %10
%94 = add nsw i32 %93, 512
store i32 %94, i32* %10
br label %44
95: ; preds = %89, %44
%96 = getelementptr inbounds [32 x i8], [32 x i8]* %12, i64 0, i64 0
%97 = load i8*, i8** %6
%98 = load i32, i32* %13
%99 = call i32 @sprintf(i8* %96, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %97, i32 %98)
%100 = getelementptr inbounds [32 x i8], [32 x i8]* %12, i64 0, i64 0
%101 = load i32, i32* %8
%102 = load i32, i32* %10
%103 = load i32, i32* %13
%104 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %100, i32 %101, i32 %102, i32 %103)
%105 = getelementptr inbounds [32 x i8], [32 x i8]* %12, i64 0, i64 0
%106 = call i32* @fopen(i8* %105, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %106, i32** %7
%107 = load i32*, i32** %7
%108 = load i32, i32* @SEEK_SET
%109 = call i32 @fseek(i32* %107, i32 %108, i32 0)
%110 = load i8*, i8** %4
%111 = load i32, i32* %8
%112 = zext i32 %111 to i64
%113 = getelementptr inbounds i8, i8* %110, i64 %112
%114 = load i32, i32* %10
%115 = load i32*, i32** %7
%116 = call i32 @fwrite(i8* %113, i32 1, i32 %114, i32* %115)
%117 = load i32*, i32** %7
%118 = call i32 @fclose(i32* %117)
%119 = load i32, i32* %10
%120 = load i32, i32* %8
%121 = add i32 %120, %119
store i32 %121, i32* %8
%122 = load i32, i32* %13
%123 = add nsw i32 %122, 1
store i32 %123, i32* %13
br label %124
124: ; preds = %95
%125 = load i32, i32* %8
%126 = load i32, i32* %5
%127 = icmp ult i32 %125, %126
br i1 %127, label %15, label %128
128: ; preds = %124, %40
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"%s%02X.BGM\00"
@.str.1 = [35 x i8] c"%s ofs:0x%04x size:0x%04x no:%02x\0A\00"
@.str.2 = [3 x i8] c"wb\00"
@SEEK_SET = global i32 0
define void @write(i8* nocapture %0, i32 %1, i8* %2) {
%4 = alloca [32 x i8]
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
br label %6
6: ; preds = %45, %3
%7 = phi i32 [ 0, %3 ], [ %55, %45 ]
%8 = phi i32 [ %1, %3 ], [ %47, %45 ]
%9 = phi i32 [ 0, %3 ], [ %56, %45 ]
%10 = zext i32 %7 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = load i8, i8* %11
switch i8 %12, label %58 [
i8 12, label %13
i8 -2, label %13
]
13: ; preds = %6, %6
%14 = or i32 %7, 1
%15 = zext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 0
br i1 %18, label %19, label %58
19: ; preds = %13
%20 = add nsw i32 %8, -512
%21 = icmp sgt i32 %8, 512
br i1 %21, label %22, label %45
22: ; preds = %19
%23 = add nuw i32 %8, 511
%24 = and i32 %23, -512
br label %25
25: ; preds = %22, %39
%26 = phi i64 [ 512, %22 ], [ %40, %39 ]
%27 = phi i32 [ %20, %22 ], [ %41, %39 ]
%28 = trunc i64 %26 to i32
%29 = add i32 %7, %28
%30 = or i32 %29, 1
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %0, i64 %31
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %25
%36 = zext i32 %29 to i64
%37 = getelementptr inbounds i8, i8* %0, i64 %36
%38 = load i8, i8* %37
switch i8 %38, label %39 [
i8 -2, label %43
i8 12, label %43
]
39: ; preds = %35, %25
%40 = add nuw nsw i64 %26, 512
%41 = add nsw i32 %27, -512
%42 = icmp sgt i32 %27, 512
br i1 %42, label %25, label %45
43: ; preds = %35, %35
%44 = trunc i64 %26 to i32
br label %45
45: ; preds = %39, %43, %19
%46 = phi i32 [ 512, %19 ], [ %44, %43 ], [ %24, %39 ]
%47 = phi i32 [ %20, %19 ], [ %27, %43 ], [ %41, %39 ]
%48 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %2, i32 %9)
%49 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %5, i32 %7, i32 %46, i32 %9)
%50 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%51 = load i32, i32* @SEEK_SET
%52 = call i32 @fseek(i32* %50, i32 %51, i32 0)
%53 = call i32 @fwrite(i8* %11, i32 1, i32 %46, i32* %50)
%54 = call i32 @fclose(i32* %50)
%55 = add i32 %46, %7
%56 = add nuw nsw i32 %9, 1
%57 = icmp ult i32 %55, %1
br i1 %57, label %6, label %58
58: ; preds = %6, %13, %45
ret void
}