// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
movq $0x0, 0x8(%rax)
cmpq $0x0, -0x18(%rbp)
jne 0x4526
movl $0x2, -0x4(%rbp)
jmp 0x48da
movq -0x18(%rbp), %rdi
leaq 0x19bdb(%rip), %rsi     # 0x1e10c
callq 0x32f0
movq %rax, -0x60(%rbp)
jmp 0x453c
movq -0x58(%rbp), %rax
movq -0x60(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq 0x8(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x32e0
jmp 0x455c
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x3030
movq %rax, -0x68(%rbp)
jmp 0x456f
movq -0x58(%rbp), %rax
movq -0x68(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq 0x8(%rax), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x32e0
jmp 0x458a
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rdi
incq %rdi
callq 0x3040
movq %rax, -0x70(%rbp)
jmp 0x45a0
movq -0x58(%rbp), %rax
movq -0x70(%rbp), %rcx
movq %rcx, 0x18(%rax)
movq 0x18(%rax), %rcx
movq %rcx, 0x20(%rax)
movq 0x20(%rax), %rdi
movq 0x8(%rax), %rcx
movq 0x10(%rax), %rdx
movl $0x1, %esi
callq 0x33a0
movq %rax, -0x78(%rbp)
jmp 0x45d0
movq -0x78(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0x46bb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x48e6
movq -0x28(%rbp), %rdi
callq 0x30e0
movq %rax, -0x40(%rbp)
movq 0x1f9e3(%rip), %rdi     # 0x23ff0
leaq 0x19afb(%rip), %rsi     # 0x1e10f
callq 0x3250
movq %rax, -0x80(%rbp)
jmp 0x461f
movq -0x80(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x3250
movq %rax, -0x88(%rbp)
jmp 0x4635
movq -0x88(%rbp), %rdi
movq 0x1f975(%rip), %rsi     # 0x23fb8
callq 0x3290
jmp 0x464a
movq 0x1f99f(%rip), %rdi     # 0x23ff0
leaq 0x19ac6(%rip), %rsi     # 0x1e11e
callq 0x3250
movq %rax, -0x90(%rbp)
jmp 0x4666
movq -0x90(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x3250
movq %rax, -0x98(%rbp)
jmp 0x467f
movq -0x98(%rbp), %rdi
movq 0x1f92b(%rip), %rsi     # 0x23fb8
callq 0x3290
jmp 0x4694
movl $0x1, -0x4(%rbp)
callq 0x33e0
jmp 0x48da
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
callq 0x33e0
jmp 0x48e6
movq -0x58(%rbp), %rax
movq 0x20(%rax), %rdi
leaq 0x19a64(%rip), %rsi     # 0x1e12e
movl $0x5, %edx
callq 0x3110
cmpl $0x0, %eax
je 0x46e5
movl $0x2, -0x4(%rbp)
jmp 0x48da
movq -0x58(%rbp), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq %rax, %rdi
callq 0x3100
movq -0x58(%rbp), %rdi
movq %rax, -0xa8(%rbp)
movq (%rdi), %rax
callq *0x8(%rax)
movq %rax, %rdi
callq 0x3100
movq -0x58(%rbp), %rdi
movq %rax, %rcx
movq -0xa8(%rbp), %rax
addq %rcx, %rax
movq %rax, -0xa0(%rbp)
movq (%rdi), %rax
callq *(%rax)
movq %rax, %rdi
callq 0x3100
movq -0xa0(%rbp), %rcx
movq %rax, %rdx
movq -0x58(%rbp), %rax
addq %rdx, %rcx
addq $0x3, %rcx
addl $0x9, %ecx
movl %ecx, 0x28(%rax)
movq 0x20(%rax), %rax
movzbl 0x8(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x476d
movq -0x58(%rbp), %rax
movl 0x28(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x28(%rax)
movq -0x58(%rbp), %rax
movq 0x20(%rax), %rax
movzbl 0x8(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x478e
movq -0x58(%rbp), %rax
movl 0x28(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x28(%rax)
movq -0x58(%rbp), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movl %eax, %ecx
movq -0x58(%rbp), %rax
addl $0x1, %ecx
movl %ecx, -0x44(%rbp)
movl 0x28(%rax), %ecx
movl %ecx, -0x48(%rbp)
movl -0x44(%rbp), %ecx
addl 0x28(%rax), %ecx
movl %ecx, 0x28(%rax)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x44(%rbp), %eax
jae 0x47f8
movq -0x58(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x48(%rbp), %ecx
addl -0x1c(%rbp), %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x47eb
movq -0x58(%rbp), %rax
movl 0x28(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x28(%rax)
jmp 0x47ed
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x47ba
movq -0x58(%rbp), %rax
movq 0x20(%rax), %rcx
movl 0x28(%rax), %edx
movzbl (%rcx,%rdx), %ecx
movl %ecx, -0x4c(%rbp)
movl 0x28(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x28(%rax)
movq 0x20(%rax), %rcx
movl 0x28(%rax), %edx
movzbl (%rcx,%rdx), %ecx
movl %ecx, -0x50(%rbp)
movl 0x28(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x28(%rax)
movq 0x20(%rax), %rcx
movl 0x28(%rax), %edx
movzwl (%rcx,%rdx), %ecx
movl %ecx, 0x38(%rax)
movl 0x28(%rax), %ecx
addl $0x2, %ecx
movl %ecx, 0x28(%rax)
movl -0x50(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x4869
movq -0x58(%rbp), %rax
movq 0x20(%rax), %rcx
movl 0x28(%rax), %edx
movzwl (%rcx,%rdx), %ecx
movl %ecx, 0x2c(%rax)
movl 0x28(%rax), %ecx
addl $0x2, %ecx
movl %ecx, 0x28(%rax)
jmp 0x4874
movq -0x58(%rbp), %rax
movl $0xe2ea, 0x2c(%rax)     # imm = 0xE2EA
movl -0x50(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x489c
movq -0x58(%rbp), %rax
movq 0x20(%rax), %rcx
movl 0x28(%rax), %edx
movzwl (%rcx,%rdx), %ecx
movl %ecx, 0x30(%rax)
movl 0x28(%rax), %ecx
addl $0x2, %ecx
movl %ecx, 0x28(%rax)
jmp 0x48a7
movq -0x58(%rbp), %rax
movl $0x0, 0x30(%rax)
movq -0x58(%rbp), %rax
movq 0x20(%rax), %rcx
movl 0x28(%rax), %edx
movzwl (%rcx,%rdx), %ecx
movl %ecx, 0x34(%rax)
movl 0x28(%rax), %ecx
addl $0x2, %ecx
movl %ecx, 0x28(%rax)
cmpl $0x0, 0x30(%rax)
jne 0x48d3
movq -0x58(%rbp), %rax
movl $0x0, 0x30(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
movq -0x28(%rbp), %rdi
callq 0x3410
nop