#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] == 0x01) &&
           _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    1204 <write+0xa4>
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
movzbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     11cb <write+0x6b>
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
movzbl (%rax,%rcx,1),%eax
cmp    $0x1,%eax
jne    11e9 <write+0x89>
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,%ecx
movzbl (%rax,%rcx,1),%eax
cmp    $0x42,%eax
jne    11e9 <write+0x89>
jmp    1204 <write+0xa4>
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     1213 <write+0xb3>
jmp    12f5 <write+0x195>
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    0xd9f(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x60(%rbp),%rsi
mov    -0x24(%rbp),%edx
mov    -0x30(%rbp),%ecx
mov    -0x64(%rbp),%r8d
lea    0xd90(%rip),%rdi
call   1040 <printf@plt>
lea    -0x60(%rbp),%rdi
lea    0xda5(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2d41(%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   $0x1,(%rbx,%r12,1)
ja     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 = %107, %3
br label %17
17: ; preds = %46, %16
%18 = load i32, i32* %12
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %51
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 %36, label %28
28: ; preds = %20
%29 = load i8*, i8** %4
%30 = load i32, i32* %8
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i8, i8* %32
%34 = zext i8 %33 to i32
%35 = icmp eq i32 %34, 1
br i1 %35, label %36, label %46
36: ; preds = %28, %20
%37 = load i8*, i8** %4
%38 = load i32, i32* %8
%39 = add i32 %38, 1
%40 = zext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
%42 = load i8, i8* %41
%43 = zext i8 %42 to i32
%44 = icmp eq i32 %43, 66
br i1 %44, label %45, label %46
45: ; preds = %36
br label %51
46: ; preds = %36, %28
%47 = load i32, i32* %8
%48 = add i32 %47, 512
store i32 %48, i32* %8
%49 = load i32, i32* %12
%50 = sub nsw i32 %49, 512
store i32 %50, i32* %12
br label %17
51: ; preds = %45, %17
%52 = load i32, i32* %12
%53 = icmp sle i32 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %51
br label %111
55: ; preds = %51
%56 = load i8*, i8** %4
%57 = load i32, i32* %8
%58 = add i32 %57, 3
%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 = shl i32 %62, 8
%64 = load i8*, i8** %4
%65 = load i32, i32* %8
%66 = add i32 %65, 2
%67 = zext i32 %66 to i64
%68 = getelementptr inbounds i8, i8* %64, i64 %67
%69 = load i8, i8* %68
%70 = zext i8 %69 to i32
%71 = add nsw i32 %63, %70
store i32 %71, i32* %11
%72 = load i32, i32* %11
%73 = sdiv i32 %72, 512
%74 = add nsw i32 %73, 1
%75 = mul nsw i32 %74, 512
store i32 %75, i32* %10
%76 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%77 = load i8*, i8** %6
%78 = load i32, i32* %14
%79 = call i32 @sprintf(i8* %76, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %77, i32 %78)
%80 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%81 = load i32, i32* %8
%82 = load i32, i32* %11
%83 = load i32, i32* %14
%84 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %80, i32 %81, i32 %82, i32 %83)
%85 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%86 = call i32* @fopen(i8* %85, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %86, i32** %7
%87 = load i32*, i32** %7
%88 = load i32, i32* @SEEK_SET
%89 = call i32 @fseek(i32* %87, i32 %88, i32 0)
%90 = load i8*, i8** %4
%91 = load i32, i32* %8
%92 = zext i32 %91 to i64
%93 = getelementptr inbounds i8, i8* %90, i64 %92
%94 = load i32, i32* %11
%95 = load i32*, i32** %7
%96 = call i32 @fwrite(i8* %93, i32 1, i32 %94, i32* %95)
%97 = load i32*, i32** %7
%98 = call i32 @fclose(i32* %97)
%99 = load i32, i32* %10
%100 = load i32, i32* %8
%101 = add i32 %100, %99
store i32 %101, i32* %8
%102 = load i32, i32* %10
%103 = load i32, i32* %12
%104 = sub nsw i32 %103, %102
store i32 %104, i32* %12
%105 = load i32, i32* %14
%106 = add nsw i32 %105, 1
store i32 %106, i32* %14
br label %107
107: ; preds = %55
%108 = load i32, i32* %8
%109 = load i32, i32* %5
%110 = icmp ult i32 %108, %109
br i1 %110, label %16, label %111
111: ; preds = %54, %107
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 ult i8 %16, 2
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 = %11, %18
%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
}