// File: /workspace/github_c_makefile_star2_O2/salextpuru[P]speccy-toochain/src/spgbld/func.cxx
// Function: unpack_spg(char*)
void unpack_spg(char* name)
{
	char fu[1024];
	char fa[1024];
	char fa1[1024];
	sprintf(fa, "%s", name);
	// Read input SPG
	FILE* f = fopen(name, "r");

	if (!f) error(FERR);

	struct stat st;

	if(stat(name, &st)<0) {
		error(FNFD);
	}

	void *fbuf = malloc(st.st_size);

	if (!fbuf)
		error(MALC);

	fread(fbuf, 1, st.st_size, f);
	fclose(f);
	// Create INI
	sprintf(fu, "%s.ini", name);
	f = fopen(fu, "w+");

	if (!f) error(FERR);

	HDR *hdr = (HDR*)fbuf;
	fprintf(f, "Desc = %s\n", hdr->desc);
	fprintf(f, "Start = 0x%0X\n", hdr->start);
	fprintf(f, "Stack = 0x%0X\n", hdr->sp);
	fprintf(f, "Resident = 0x%0X\n", hdr->resid);
	fprintf(f, "Page3 = %u\n", hdr->page3);
	fprintf(f, "Clock = %u\n", hdr->clk);
	fprintf(f, "INT = %u\n", hdr->ei);
	fprintf(f, "Pager = 0x%0X\n", hdr->pager);
	int nblk;

	for (int i = 0; i < hdr->num_blk; i++) {
		fprintf(f, "Block = 0x%0X, %u, %s.%03u\n", hdr->blk[i].addr * 512 + 0xC000, hdr->blk[i].page, fa, i);
		nblk = i + 1;
		// if (hdr->blk[i].last) break;
	}

	fclose(f);
	// Save files
	u8 *data = (u8*)fbuf + sizeof(HDR);

	for (int i = 0; i < nblk; i++) {
		int size = (hdr->blk[i].size + 1) * 512;
		void *addr = data;
		data += size;

		if (hdr->blk[i].comp == PM_NONE) {
			sprintf(fu, "%s.%03u", name, i);
			f = fopen(fu, "w");

		} else {
			rand_name(fa);
			sprintf(fa1, "%s.%03u", name, i);
			f = fopen(fa, "w");
		}

		if (!f) error(FERR);

		fwrite(addr, 1, size, f);
		fclose(f);

		if (hdr->blk[i].comp != PM_NONE) {
			switch (hdr->blk[i].comp) {
			case PM_MLZ: {
				std::string cmd("mhmt -d -mlz ");
				cmd=cmd+fa+" "+fa1;

				if (system(cmd.c_str()) <0) error(MHMT);
				break;
			}

			case PM_HST: {
				std::string cmd("mhmt -d -hst ");
				cmd=cmd+fa+" "+fa1;

				if (system(cmd.c_str()) <0) error(MHMT);
				break;
			}
			}

			remove(fa);
		}
	}

	free(fbuf);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd58, %rsp            # imm = 0xD58
movq %rdi, %rbp
leaq 0x150(%rsp), %rdi
movq %rbp, %rsi
callq 0x2100
leaq 0xe3e(%rip), %rsi       # 0x40e7
movq %rbp, %rdi
callq 0x2150
testq %rax, %rax
je 0x36e3
movq %rax, %rbx
leaq 0xc0(%rsp), %rsi
movq %rbp, %rdi
callq 0x20e0
testl %eax, %eax
js 0x3723
movq 0xf0(%rsp), %r15
movq %r15, %rdi
callq 0x21a0
testq %rax, %rax
je 0x3741
movq %rax, %r12
pushq $0x1
popq %rsi
movq %rax, %rdi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x2200
movq %rbx, %rdi
callq 0x2120
leaq 0xf91(%rip), %rsi       # 0x42a2
leaq 0x950(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2040
leaq 0xf0e(%rip), %rsi       # 0x423b
movq %rbx, %rdi
callq 0x2150
testq %rax, %rax
je 0x36e3
movq %rax, %rbx
leaq 0xf61(%rip), %rsi       # 0x42a9
movq %rax, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x21e0
movzwl 0x30(%r12), %edx
leaq 0xf52(%rip), %rsi       # 0x42b4
movq %rbx, %rdi
xorl %eax, %eax
callq 0x21e0
movzwl 0x32(%r12), %edx
leaq 0xf4a(%rip), %rsi       # 0x42c3
movq %rbx, %rdi
xorl %eax, %eax
callq 0x21e0
movzwl 0x38(%r12), %edx
leaq 0xf42(%rip), %rsi       # 0x42d2
movq %rbx, %rdi
xorl %eax, %eax
callq 0x21e0
movzbl 0x34(%r12), %edx
leaq 0xf3d(%rip), %rsi       # 0x42e4
movq %rbx, %rdi
xorl %eax, %eax
callq 0x21e0
movzbl 0x35(%r12), %edx
andl $0x3, %edx
leaq 0xf2f(%rip), %rsi       # 0x42f0
movq %rbx, %rdi
xorl %eax, %eax
callq 0x21e0
movzbl 0x35(%r12), %edx
shrl $0x2, %edx
andl $0x1, %edx
leaq 0xf1e(%rip), %rsi       # 0x42fc
movq %rbx, %rdi
xorl %eax, %eax
callq 0x21e0
movzwl 0x36(%r12), %edx
leaq 0xf11(%rip), %rsi       # 0x4306
movq %rbx, %rdi
xorl %eax, %eax
callq 0x21e0
leaq 0x100(%r12), %rax
movq %rax, 0x18(%rsp)
movq %r12, 0x8(%rsp)
addq $0x102, %r12            # imm = 0x102
leaq 0xef6(%rip), %r15       # 0x4315
xorl %r13d, %r13d
movq 0x8(%rsp), %rax
movzwl 0x3a(%rax), %eax
cmpq %rax, %r13
jae 0x3465
movzwl -0x2(%r12), %eax
orl $0x60, %eax
shll $0x9, %eax
movzwl %ax, %edx
movzbl (%r12), %ecx
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x150(%rsp), %r8
movl %r13d, %r9d
xorl %eax, %eax
callq 0x21e0
incq %r13
addq $0x3, %r12
jmp 0x3422
movq %rbp, 0x30(%rsp)
movq %rbx, %rdi
callq 0x2120
movq 0x8(%rsp), %rax
addq $0x400, %rax            # imm = 0x400
movq %rax, 0x10(%rsp)
xorl %eax, %eax
testl %r13d, %r13d
cmovlel %eax, %r13d
leaq 0x40(%rsp), %r15
leaq 0xa0(%rsp), %rbp
leaq 0x60(%rsp), %r14
leaq 0x80(%rsp), %rbx
xorl %eax, %eax
movq %r13, 0x28(%rsp)
cmpq %rax, %r13
je 0x36c7
movq %rax, 0x20(%rsp)
movq %rbx, %r13
movq %r14, %rbx
movq %rbp, %r14
movq %r15, %rbp
movq 0x18(%rsp), %rax
movzwl (%rax), %eax
leaq 0x950(%rsp), %r15
movq %r15, %rdi
movq %rax, %r12
cmpw $0x4000, %ax            # imm = 0x4000
jb 0x34fa
leaq 0x150(%rsp), %r15
movq %r15, %rdi
callq 0x2c6f
leaq 0x550(%rsp), %rdi
leaq 0xe30(%rip), %rsi       # 0x4331
movq 0x30(%rsp), %rdx
movq 0x20(%rsp), %rcx
xorl %eax, %eax
callq 0x2040
movq %r15, %rdi
leaq 0xe1d(%rip), %rsi       # 0x4339
callq 0x2150
testq %rax, %rax
je 0x36e3
movq %rax, %r15
andl $0x1f00, %r12d          # imm = 0x1F00
movq %r12, 0x38(%rsp)
leaq 0x200(,%r12,2), %rdx
movq 0x10(%rsp), %rdi
pushq $0x1
popq %rsi
movq %rax, %rcx
callq 0x2290
movq %r15, %rdi
callq 0x2120
movq 0x18(%rsp), %rax
movzwl (%rax), %eax
cmpl $0x4000, %eax           # imm = 0x4000
movq %rbp, %r15
movq %r14, %rbp
movq %rbx, %r14
movq %r13, %rbx
movq 0x28(%rsp), %r13
jb 0x369b
shrl $0xe, %eax
cmpl $0x2, %eax
je 0x360b
cmpl $0x1, %eax
jne 0x368e
movq %r15, %rdi
leaq 0xd9f(%rip), %rsi       # 0x433b
movq %rbp, %rdx
callq 0x37a6
movq %r14, %rdi
movq %r15, %rsi
leaq 0x150(%rsp), %rdx
callq 0x3800
movq %rbx, %rdi
movq %r14, %rsi
leaq 0xc2f(%rip), %rdx       # 0x41f3
callq 0x37e1
movq %rbp, %rdi
movq %rbx, %rsi
leaq 0x550(%rsp), %rdx
callq 0x37e1
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2180
movq %rbp, %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x2070
movq %r14, %rdi
callq 0x2070
movq 0x40(%rsp), %rdi
callq 0x20d0
jmp 0x3682
movq %r15, %rdi
leaq 0xd34(%rip), %rsi       # 0x4349
movq %rbp, %rdx
callq 0x37a6
movq %r14, %rdi
movq %r15, %rsi
leaq 0x150(%rsp), %rdx
callq 0x3800
movq %rbx, %rdi
movq %r14, %rsi
leaq 0xbb6(%rip), %rdx       # 0x41f3
callq 0x37e1
movq %rbp, %rdi
movq %rbx, %rsi
leaq 0x550(%rsp), %rdx
callq 0x37e1
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2180
movq %rbp, %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x2070
movq %r14, %rdi
callq 0x2070
movq 0x40(%rsp), %rdi
callq 0x20d0
testl %eax, %eax
js 0x3701
movq %r15, %rdi
callq 0x2070
leaq 0x150(%rsp), %rdi
callq 0x2260
movq 0x10(%rsp), %rax
movq 0x38(%rsp), %rcx
leaq (%rax,%rcx,2), %rax
addq $0x200, %rax            # imm = 0x200
movq %rax, 0x10(%rsp)
movq 0x20(%rsp), %rax
incq %rax
addq $0x3, 0x18(%rsp)
jmp 0x34ac
movq 0x8(%rsp), %rdi
callq 0x2160
addq $0xd58, %rsp            # imm = 0xD58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xb9e(%rip), %rdi       # 0x4288
xorl %eax, %eax
callq 0x2030
leaq 0xbd8(%rip), %rdi       # 0x42d0
callq 0x2210
pushq $0x6
jmp 0x371d
leaq 0xb41(%rip), %rdi       # 0x4249
xorl %eax, %eax
callq 0x2030
leaq 0xbba(%rip), %rdi       # 0x42d0
callq 0x2210
pushq $0xb
popq %rdi
callq 0x2170
leaq 0xacb(%rip), %rdi       # 0x41f5
xorl %eax, %eax
callq 0x2030
leaq 0xb98(%rip), %rdi       # 0x42d0
callq 0x2210
pushq $0x5
jmp 0x371d
leaq 0xb4c(%rip), %rdi       # 0x4294
xorl %eax, %eax
callq 0x2030
leaq 0xb7a(%rip), %rdi       # 0x42d0
callq 0x2210
pushq $0xe
jmp 0x371d
jmp 0x3769
jmp 0x377b
jmp 0x3791
jmp 0x378c
jmp 0x3791
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x2070
jmp 0x377e
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x2070
jmp 0x3794
jmp 0x3791
movq %rax, %rbx
jmp 0x379e
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x2240