#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, fsize, remain;
  char name[0x20];
  int count = 0;
  songno = 0;
  offset = 0;
  remain = _size;
  size = 512;
  do {
    while (remain > 0) {
      if ((_src[offset] == 0x00 && _src[offset + 1] == 0x42)) {
        break;
      }
      offset += 512;
      remain -= 512;
      continue;
    }
    if (remain <= 0)
      return;
    fsize = (_src[offset + 3] << 8) + _src[offset + 2];
    size = ((fsize / 512) + 1) * 512;
    sprintf(name, "%s%02d.BIN", _prefix, count);
    printf("%s | ofs:0x%05x size:0x%04x no:%02d\n", name, offset, fsize, count);
    fh = fopen(name, "wb");
    fseek(fh, SEEK_SET, 0);
    fwrite(_src + offset, 1, fsize, fh);
    fclose(fh);
    offset += size;
    remain -= size;
    count++;
  } while (offset < _size);
}


x86 assembly (clang, -O0):
<write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x64(%rbp)
movw   $0x0,-0x26(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x34(%rbp)
movl   $0x200,-0x2c(%rbp)
jmp    1199 <write+0x39>
cmpl   $0x0,-0x34(%rbp)
jle    11f0 <write+0x90>
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
movzbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    11d5 <write+0x75>
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,%ecx
movzbl (%rax,%rcx,1),%eax
cmp    $0x42,%eax
jne    11d5 <write+0x75>
jmp    11f0 <write+0x90>
mov    -0x24(%rbp),%eax
add    $0x200,%eax
mov    %eax,-0x24(%rbp)
mov    -0x34(%rbp),%eax
sub    $0x200,%eax
mov    %eax,-0x34(%rbp)
jmp    1199 <write+0x39>
cmpl   $0x0,-0x34(%rbp)
jg     11ff <write+0x9f>
jmp    12e1 <write+0x181>
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x3,%ecx
mov    %ecx,%ecx
movzbl (%rax,%rcx,1),%eax
shl    $0x8,%eax
mov    -0x8(%rbp),%rcx
mov    -0x24(%rbp),%edx
add    $0x2,%edx
mov    %edx,%edx
movzbl (%rcx,%rdx,1),%ecx
add    %ecx,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    $0x200,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
shl    $0x9,%eax
mov    %eax,-0x2c(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x64(%rbp),%ecx
lea    0xdb3(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x60(%rbp),%rsi
mov    -0x24(%rbp),%edx
mov    -0x30(%rbp),%ecx
mov    -0x64(%rbp),%r8d
lea    0xda4(%rip),%rdi
call   1040 <printf@plt>
lea    -0x60(%rbp),%rdi
lea    0xdb9(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2d55(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <fseek@plt>
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%eax
add    %rax,%rdi
mov    -0x30(%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    -0x2c(%rbp),%ecx
mov    -0x34(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x34(%rbp)
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jb     1194 <write+0x34>
add    $0x70,%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    %rdi,%rbx
xor    %r15d,%r15d
lea    0xe85(%rip),%r8
mov    %esi,%r13d
xor    %ecx,%ecx
test   %r13d,%r13d
jle    12b8 <write+0x158>
lea    0x200(%r13),%eax
xor    %r14d,%r14d
jmp    11b7 <write+0x57>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x200,%r14d
add    $0xfffffe00,%eax
cmp    $0x200,%eax
jle    12b8 <write+0x158>
lea    (%r15,%r14,1),%r12d
cmpb   $0x0,(%rbx,%r12,1)
jne    11a0 <write+0x40>
lea    (%r15,%r14,1),%edi
add    $0x1,%edi
cmpb   $0x42,(%rbx,%rdi,1)
jne    11a0 <write+0x40>
lea    (%rbx,%r12,1),%rax
mov    %rax,0x18(%rsp)
lea    (%r15,%r14,1),%eax
add    $0x3,%eax
movzbl (%rbx,%rax,1),%ebp
shl    $0x8,%ebp
lea    (%r15,%r14,1),%eax
add    $0x2,%eax
movzbl (%rbx,%rax,1),%eax
or     %ebp,%eax
mov    %eax,0x4(%rsp)
and    $0xfe00,%ebp
lea    0x20(%rsp),%rdi
mov    %esi,0xc(%rsp)
mov    %r8,%rsi
mov    %rdx,0x10(%rsp)
mov    0x10(%rsp),%rdx
mov    %ecx,0x8(%rsp)
call   1070 <sprintf@plt>
lea    0xde8(%rip),%rdi
lea    0x20(%rsp),%rsi
mov    %r12d,%edx
mov    0x4(%rsp),%ecx
mov    0x8(%rsp),%r8d
call   1040 <printf@plt>
lea    0x20(%rsp),%rdi
lea    0xdeb(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r12
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
xor    %edx,%edx
call   1050 <fseek@plt>
mov    0x18(%rsp),%rdi
mov    $0x1,%esi
mov    0x4(%rsp),%edx
mov    %r12,%rcx
call   1080 <fwrite@plt>
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    0x8(%rsp),%ecx
lea    0xd77(%rip),%r8
mov    0xc(%rsp),%esi
mov    0x10(%rsp),%rdx
add    %ebp,%r15d
add    %r14d,%r15d
add    $0x200,%r15d
sub    %ebp,%r13d
sub    %r14d,%r13d
add    $0xfffffe00,%r13d
add    $0x1,%ecx
cmp    %esi,%r15d
jb     1180 <write+0x20>
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%02d.BIN\00"
@.str.1 = [37 x i8] c"%s | ofs:0x%05x size:0x%04x no:%02d\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 i32
%13 = alloca [32 x i8]
%14 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
store i32 0, i32* %14
store i16 0, i16* %9
store i32 0, i32* %8
%15 = load i32, i32* %5
store i32 %15, i32* %12
store i32 512, i32* %10
br label %16
16: ; preds = %99, %3
br label %17
17: ; preds = %38, %16
%18 = load i32, i32* %12
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %43
20: ; preds = %17
%21 = load i8*, i8** %4
%22 = load i32, i32* %8
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i8, i8* %24
%26 = zext i8 %25 to i32
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %38
28: ; preds = %20
%29 = load i8*, i8** %4
%30 = load i32, i32* %8
%31 = add i32 %30, 1
%32 = zext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = icmp eq i32 %35, 66
br i1 %36, label %37, label %38
37: ; preds = %28
br label %43
38: ; preds = %28, %20
%39 = load i32, i32* %8
%40 = add i32 %39, 512
store i32 %40, i32* %8
%41 = load i32, i32* %12
%42 = sub nsw i32 %41, 512
store i32 %42, i32* %12
br label %17
43: ; preds = %37, %17
%44 = load i32, i32* %12
%45 = icmp sle i32 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %43
br label %103
47: ; preds = %43
%48 = load i8*, i8** %4
%49 = load i32, i32* %8
%50 = add i32 %49, 3
%51 = zext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %48, i64 %51
%53 = load i8, i8* %52
%54 = zext i8 %53 to i32
%55 = shl i32 %54, 8
%56 = load i8*, i8** %4
%57 = load i32, i32* %8
%58 = add i32 %57, 2
%59 = zext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %56, i64 %59
%61 = load i8, i8* %60
%62 = zext i8 %61 to i32
%63 = add nsw i32 %55, %62
store i32 %63, i32* %11
%64 = load i32, i32* %11
%65 = sdiv i32 %64, 512
%66 = add nsw i32 %65, 1
%67 = mul nsw i32 %66, 512
store i32 %67, i32* %10
%68 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%69 = load i8*, i8** %6
%70 = load i32, i32* %14
%71 = call i32 @sprintf(i8* %68, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %69, i32 %70)
%72 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%73 = load i32, i32* %8
%74 = load i32, i32* %11
%75 = load i32, i32* %14
%76 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %72, i32 %73, i32 %74, i32 %75)
%77 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%78 = call i32* @fopen(i8* %77, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %78, i32** %7
%79 = load i32*, i32** %7
%80 = load i32, i32* @SEEK_SET
%81 = call i32 @fseek(i32* %79, i32 %80, i32 0)
%82 = load i8*, i8** %4
%83 = load i32, i32* %8
%84 = zext i32 %83 to i64
%85 = getelementptr inbounds i8, i8* %82, i64 %84
%86 = load i32, i32* %11
%87 = load i32*, i32** %7
%88 = call i32 @fwrite(i8* %85, i32 1, i32 %86, i32* %87)
%89 = load i32*, i32** %7
%90 = call i32 @fclose(i32* %89)
%91 = load i32, i32* %10
%92 = load i32, i32* %8
%93 = add i32 %92, %91
store i32 %93, i32* %8
%94 = load i32, i32* %10
%95 = load i32, i32* %12
%96 = sub nsw i32 %95, %94
store i32 %96, i32* %12
%97 = load i32, i32* %14
%98 = add nsw i32 %97, 1
store i32 %98, i32* %14
br label %99
99: ; preds = %47
%100 = load i32, i32* %8
%101 = load i32, i32* %5
%102 = icmp ult i32 %100, %101
br i1 %102, label %16, label %103
103: ; preds = %46, %99
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"%s%02d.BIN\00"
@.str.1 = [37 x i8] c"%s | ofs:0x%05x size:0x%04x no:%02d\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 = %28, %3
%7 = phi i32 [ 0, %3 ], [ %52, %28 ]
%8 = phi i32 [ %1, %3 ], [ %53, %28 ]
%9 = phi i32 [ 0, %3 ], [ %54, %28 ]
%10 = icmp sgt i32 %8, 0
br i1 %10, label %11, label %56
11: ; preds = %6, %24
%12 = phi i32 [ %26, %24 ], [ %8, %6 ]
%13 = phi i32 [ %25, %24 ], [ %7, %6 ]
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %0, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %11
%19 = add i32 %13, 1
%20 = zext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %0, i64 %20
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 66
br i1 %23, label %28, label %24
24: ; preds = %18, %11
%25 = add i32 %13, 512
%26 = add nsw i32 %12, -512
%27 = icmp sgt i32 %12, 512
br i1 %27, label %11, label %56
28: ; preds = %18
%29 = zext i32 %13 to i64
%30 = getelementptr inbounds i8, i8* %0, i64 %29
%31 = add i32 %13, 3
%32 = zext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %0, i64 %32
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = shl nuw nsw i32 %35, 8
%37 = add i32 %13, 2
%38 = zext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %0, i64 %38
%40 = load i8, i8* %39
%41 = zext i8 %40 to i32
%42 = or i32 %36, %41
%43 = and i32 %36, 65024
%44 = add nuw nsw i32 %43, 512
%45 = 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)
%46 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %5, i32 %13, i32 %42, i32 %9)
%47 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%48 = load i32, i32* @SEEK_SET
%49 = call i32 @fseek(i32* %47, i32 %48, i32 0)
%50 = call i32 @fwrite(i8* %30, i32 1, i32 %42, i32* %47)
%51 = call i32 @fclose(i32* %47)
%52 = add i32 %44, %13
%53 = sub nsw i32 %12, %44
%54 = add nuw nsw i32 %9, 1
%55 = icmp ult i32 %52, %1
br i1 %55, label %6, label %56
56: ; preds = %28, %6, %24
ret void
}