// File: /workspace/github_c_makefile_star2_O1/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 0x3f95
movq %rsi, %r14
movq %rdi, %rbx
leaq 0xb2bf(%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 0xb273(%rip), %rsi      # 0xf12e
movl $0x5, %edx
callq 0x3100
testl %eax, %eax
jne 0x3fe8
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), %rcx
testb $0x1, 0x8(%rcx)
je 0x3f27
addl %eax, %r14d
addl $0xd, %r14d
movl %r14d, 0x28(%rbx)
testb $0x2, 0x8(%rcx)
je 0x3f30
incl 0x28(%rbx)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x18(%rax)
movl 0x28(%rbx), %ecx
incl %eax
leal (%rcx,%rax), %edx
movl %edx, 0x28(%rbx)
je 0x3f63
movl 0x28(%rbx), %edx
movq 0x20(%rbx), %rsi
movl %eax, %eax
movl %ecx, %edi
testb $0x2, (%rsi,%rdi)
je 0x3f5c
incl %edx
movl %edx, 0x28(%rbx)
incl %ecx
decq %rax
jne 0x3f4f
movl 0x28(%rbx), %edx
leal 0x1(%rdx), %ecx
movl %ecx, 0x28(%rbx)
movq 0x20(%rbx), %rax
movzbl (%rax,%rcx), %ecx
leal 0x2(%rdx), %esi
movl %esi, 0x28(%rbx)
movzwl (%rax,%rsi), %esi
movl %esi, 0x38(%rbx)
leal 0x4(%rdx), %esi
movl %esi, 0x28(%rbx)
testb $0x1, %cl
jne 0x3f9c
movl $0xe2ea, 0x2c(%rbx)     # imm = 0xE2EA
jmp 0x3fab
movl $0x2, %ebp
jmp 0x3fe8
movl %esi, %esi
movzwl (%rax,%rsi), %esi
movl %esi, 0x2c(%rbx)
addl $0x6, %edx
movl %edx, 0x28(%rbx)
testb $0x2, %cl
jne 0x3fb9
movl $0x0, 0x30(%rbx)
jmp 0x3fc9
movl 0x28(%rbx), %ecx
movzwl (%rax,%rcx), %edx
movl %edx, 0x30(%rbx)
addl $0x2, %ecx
movl %ecx, 0x28(%rbx)
movl 0x28(%rbx), %ecx
movzwl (%rax,%rcx), %eax
movl %eax, 0x34(%rbx)
leal 0x2(%rcx), %eax
movl %eax, 0x28(%rbx)
xorl %ebp, %ebp
cmpl $0x0, 0x30(%rbx)
jne 0x3fe8
movl $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 0x4090
movq %rbx, %rdi
callq 0x30e0
movq %rax, %rbx
movq 0xffdd(%rip), %rdi      # 0x13ff0
leaq 0xb0f5(%rip), %rsi      # 0xf10f
movl $0xe, %edx
callq 0x3260
movq %r14, %rdi
callq 0x30f0
movq 0xffbd(%rip), %rdi      # 0x13ff0
movq %r14, %rsi
movq %rax, %rdx
callq 0x3260
movq 0xffab(%rip), %rdi      # 0x13ff0
callq 0x3110
movq 0xff9f(%rip), %rdi      # 0x13ff0
leaq 0xb0c6(%rip), %rsi      # 0xf11e
movl $0xf, %edx
callq 0x3260
movq 0xff87(%rip), %rdi      # 0x13ff0
movq %rbx, %rsi
callq 0x3210
movq %rax, %rdi
callq 0x3110
callq 0x3330
movl $0x1, %ebp
jmp 0x3fe8
movq %rax, %rbx
callq 0x3330
movq %rbx, %rdi
callq 0x3360