// File: /workspace/github_c_makefile_star2_O2/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
andq $0x0, 0x8(%rdi)
testq %rsi, %rsi
je 0x3e9a
movq %rsi, %r14
movq %rdi, %rbx
leaq 0xa38c(%rip), %rsi      # 0xe10c
movq %r14, %rdi
callq 0x3260
movq %rax, 0x8(%rbx)
pushq $0x2
popq %rbp
movq %rax, %rdi
xorl %esi, %esi
movl %ebp, %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
pushq $0x1
popq %rsi
movq %rax, %rdi
movq %r15, %rdx
callq 0x32d0
movq 0x20(%rbx), %rdi
leaq 0xa347(%rip), %rsi      # 0xe12e
pushq $0x5
popq %rdx
callq 0x3100
testl %eax, %eax
jne 0x3f07
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq %rax, %rdi
callq 0x30e0
movq %rax, %r15
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
movq %rax, %rdi
callq 0x30e0
movq %rax, %r14
addl %r15d, %r14d
movq (%rbx), %rax
movq %rbx, %rdi
callq *(%rax)
movq %rax, %rdi
callq 0x30e0
leal (%rax,%r14), %ecx
addl $0xc, %ecx
movl %ecx, 0x28(%rbx)
movq 0x20(%rbx), %rdx
movb 0x8(%rdx), %sil
testb $0x1, %sil
je 0x3e5c
addl %eax, %r14d
addl $0xd, %r14d
movl %r14d, 0x28(%rbx)
movb 0x8(%rdx), %sil
movl %r14d, %ecx
testb $0x2, %sil
je 0x3e67
incl %ecx
movl %ecx, 0x28(%rbx)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x18(%rax)
movl 0x28(%rbx), %edx
leal (%rdx,%rax), %ecx
incl %ecx
movl %eax, %esi
incl %esi
movl %ecx, 0x28(%rbx)
movq 0x20(%rbx), %rax
subq $0x1, %rsi
jb 0x3e9f
movl %edx, %edi
testb $0x2, (%rax,%rdi)
je 0x3e96
incl %ecx
movl %ecx, 0x28(%rbx)
incl %edx
jmp 0x3e83
pushq $0x2
popq %rbp
jmp 0x3f07
leal 0x1(%rcx), %edx
movl %edx, 0x28(%rbx)
movzbl (%rax,%rdx), %esi
leal 0x2(%rcx), %edx
movzwl (%rax,%rdx), %edx
movl %edx, 0x38(%rbx)
leal 0x4(%rcx), %edx
movl %edx, 0x28(%rbx)
movl $0xe2ea, %edi           # imm = 0xE2EA
testb $0x1, %sil
je 0x3ed2
movl %edx, %edx
movzwl (%rax,%rdx), %edi
addl $0x6, %ecx
movl %ecx, 0x28(%rbx)
movl %ecx, %edx
movl %edi, 0x2c(%rbx)
xorl %ebp, %ebp
testb $0x2, %sil
jne 0x3ee1
xorl %ecx, %ecx
jmp 0x3eed
movl %edx, %ecx
movzwl (%rax,%rcx), %ecx
addl $0x2, %edx
movl %edx, 0x28(%rbx)
movl %ecx, 0x30(%rbx)
movl %edx, %esi
movzwl (%rax,%rsi), %eax
movl %eax, 0x34(%rbx)
addl $0x2, %edx
movl %edx, 0x28(%rbx)
testl %ecx, %ecx
jne 0x3f07
andl $0x0, 0x30(%rbx)
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x3f87
movq %rbx, %rdi
callq 0x30d0
movq %rax, %rbx
movq 0x100c2(%rip), %rdi     # 0x13ff0
leaq 0xa1da(%rip), %rsi      # 0xe10f
callq 0x3200
movq %rax, %rdi
movq %r14, %rsi
callq 0x3200
movq %rax, %rdi
callq 0x3110
movq 0x1009c(%rip), %rdi     # 0x13ff0
leaq 0xa1c3(%rip), %rsi      # 0xe11e
callq 0x3200
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3200
movq %rax, %rdi
callq 0x3110
callq 0x3310
pushq $0x1
jmp 0x3e9c
movq %rax, %rbx
callq 0x3310
movq %rbx, %rdi
callq 0x3340
nop