// File: /workspace/github_c_makefile_star2_O3/calmopyrin[P]tedplay/CbmTune.cpp
// Function: CbmTune::parse(char*)
int CbmTune::parse(char *fName)
{
	unsigned int i;
	file = (FILE *) 0;

	if (!fName)
		return 2;

	try {
		file = fopen(fName, "rb");
		fseek(file, 0, SEEK_END);
		flen = ftell(file);
		fseek(file, 0, SEEK_SET);
		buffer = new unsigned char[flen + 1];
		bufferPtr = buffer;
		size_t r = fread(bufferPtr, 1, flen, file);
	} catch(char *str) {
		cerr << "Error opening " << fName << endl;
		cerr << "    exception: " << str << endl;
		return 1;
	}
	// check for magic
	if (strncmp((const char *)(bufferPtr), "CBM8M", 5))
		return 2;
	//
	dataIndex = CBM8M_HDR_STRINGS
		 + (unsigned int) (strlen(getReleaseDate()) + strlen(getAuthor()) + strlen(getName()) + 3);
	if (bufferPtr[CBM8M_HDR_FIELDS] & CBM8M_FIELD_SUBTUNES)
		dataIndex += 1;
	if (bufferPtr[CBM8M_HDR_FIELDS] & CBM8M_FIELD_DEFAULT)
		dataIndex += 1;
	unsigned int stunes = getNrOfSubtunes() + 1;
	unsigned int subTuneFieldOffset = dataIndex;
	dataIndex += stunes;
	// subtune field enum
	for (i = 0; i < stunes; i++) {
		if (bufferPtr[subTuneFieldOffset + i] & 2) {
			dataIndex += 1;
		}
	}
	// read binary config
	unsigned int config = bufferPtr[dataIndex];
	dataIndex += 1;
	// read binary header desc flag
	unsigned int binField = bufferPtr[dataIndex];
	dataIndex += 1;
	// get and store binary dump length
	dumpLength = *((unsigned short *) (bufferPtr + dataIndex));
	dataIndex += 2;
	// optional init address is provided
	if (binField & 1) {
		initAddress = *((unsigned short *) (bufferPtr + dataIndex));
		dataIndex += 2;
	} else {
		initAddress = 0xe2ea;
	}
	// optional play address is provided
	if (binField & 2) {
		playAddress = *((unsigned short *) (bufferPtr + dataIndex));
		dataIndex += 2;
	} else {
		playAddress = 0;
	}
	loadAddress = *((unsigned short *) (bufferPtr + dataIndex));
	dataIndex += 2;
	if (!playAddress) playAddress = 0; //loadAddress;
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq $0x0, 0x8(%rdi)
testq %rsi, %rsi
je 0x3f99
movq %rsi, %r14
movq %rdi, %rbx
leaq 0xb2eb(%rip), %rsi      # 0xf10c
movq %r14, %rdi
callq 0x3270
movq %rax, 0x8(%rbx)
movl $0x2, %ebp
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x3250
movq 0x8(%rbx), %rdi
callq 0x3030
movq %rax, 0x10(%rbx)
movq 0x8(%rbx), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3250
movq 0x10(%rbx), %r15
leaq 0x1(%r15), %rdi
callq 0x3040
movq %rax, 0x18(%rbx)
movq %rax, 0x20(%rbx)
movq 0x8(%rbx), %rcx
movl $0x1, %esi
movq %rax, %rdi
movq %r15, %rdx
callq 0x32e0
movq 0x20(%rbx), %rdi
leaq 0xb29f(%rip), %rsi      # 0xf12e
movl $0x5, %edx
callq 0x3100
testl %eax, %eax
jne 0x3fce
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq %rax, %rdi
callq 0x30f0
movq %rax, %r15
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
movq %rax, %rdi
callq 0x30f0
movq %rax, %r14
addl %r15d, %r14d
movq (%rbx), %rax
movq %rbx, %rdi
callq *(%rax)
movq %rax, %rdi
callq 0x30f0
leal (%rax,%r14), %ecx
addl $0xc, %ecx
movl %ecx, 0x28(%rbx)
movq 0x20(%rbx), %rdx
movb 0x8(%rdx), %sil
testb $0x1, %sil
je 0x3f06
addl %eax, %r14d
addl $0xd, %r14d
movl %r14d, 0x28(%rbx)
movb 0x8(%rdx), %sil
movl %r14d, %ecx
testb $0x2, %sil
je 0x3f11
incl %ecx
movl %ecx, 0x28(%rbx)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x18(%rax)
movl 0x28(%rbx), %esi
incl %eax
leal (%rsi,%rax), %edx
movl %edx, 0x28(%rbx)
movq 0x20(%rbx), %rcx
je 0x3f41
movl %eax, %eax
movl %esi, %edi
testb $0x2, (%rcx,%rdi)
je 0x3f3a
incl %edx
movl %edx, 0x28(%rbx)
incl %esi
decq %rax
jne 0x3f2d
leal 0x1(%rdx), %eax
movl %eax, 0x28(%rbx)
movzbl (%rcx,%rax), %esi
leal 0x2(%rdx), %eax
movzwl (%rcx,%rax), %eax
movl %eax, 0x38(%rbx)
leal 0x4(%rdx), %eax
movl %eax, 0x28(%rbx)
movl $0xe2ea, %edi           # imm = 0xE2EA
testb $0x1, %sil
je 0x3f74
movl %eax, %eax
movzwl (%rcx,%rax), %edi
addl $0x6, %edx
movl %edx, 0x28(%rbx)
movl %edx, %eax
movl %edi, 0x2c(%rbx)
testb $0x2, %sil
jne 0x3fa0
leaq 0x30(%rbx), %rdx
movl $0x0, 0x30(%rbx)
movl %eax, %esi
movzwl (%rcx,%rsi), %ecx
movl %ecx, 0x34(%rbx)
addl $0x2, %eax
movl %eax, 0x28(%rbx)
jmp 0x3fc6
movl $0x2, %ebp
jmp 0x3fce
movl %eax, %edx
movzwl (%rcx,%rdx), %edx
leal 0x2(%rax), %esi
addl $0x4, %eax
xorl %ebp, %ebp
movl %edx, 0x30(%rbx)
movzwl (%rcx,%rsi), %ecx
movl %ecx, 0x34(%rbx)
movl %eax, 0x28(%rbx)
testl %edx, %edx
jne 0x3fce
addq $0x30, %rbx
movq %rbx, %rdx
movl $0x0, (%rdx)
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x4076
movq %rbx, %rdi
callq 0x30e0
movq %rax, %rbx
movq 0x10ff7(%rip), %rdi     # 0x14ff0
leaq 0xb10f(%rip), %rsi      # 0xf10f
movl $0xe, %edx
callq 0x3260
movq %r14, %rdi
callq 0x30f0
movq 0x10fd7(%rip), %rdi     # 0x14ff0
movq %r14, %rsi
movq %rax, %rdx
callq 0x3260
movq 0x10fc5(%rip), %rdi     # 0x14ff0
callq 0x3110
movq 0x10fb9(%rip), %rdi     # 0x14ff0
leaq 0xb0e0(%rip), %rsi      # 0xf11e
movl $0xf, %edx
callq 0x3260
movq 0x10fa1(%rip), %rdi     # 0x14ff0
movq %rbx, %rsi
callq 0x3210
movq %rax, %rdi
callq 0x3110
callq 0x3330
movl $0x1, %ebp
jmp 0x3fce
movq %rax, %rbx
callq 0x3330
movq %rbx, %rdi
callq 0x3360