// File: /workspace/github_c_makefile_star2_O3/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 $0xd38, %rsp            # imm = 0xD38
movq %rdi, %r14
leaq 0x130(%rsp), %rdi
movq %r14, %rsi
callq 0x20e0
leaq 0xba3(%rip), %rsi       # 0x40e7
movq %r14, %rdi
callq 0x2130
testq %rax, %rax
je 0x3b3f
movq %rax, %r15
leaq 0xa0(%rsp), %rsi
movq %r14, %rdi
callq 0x20c0
testl %eax, %eax
js 0x3b87
movq 0xd0(%rsp), %rbx
movq %rbx, %rdi
callq 0x2170
testq %rax, %rax
je 0x3bab
movq %rax, %r12
movl $0x1, %esi
movq %rax, %rdi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x21c0
movq %r15, %rdi
callq 0x2100
leaq 0xcf4(%rip), %rsi       # 0x42a2
leaq 0x930(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2040
leaq 0xc71(%rip), %rsi       # 0x423b
movq %rbx, %rdi
callq 0x2130
testq %rax, %rax
je 0x3b3f
movq %rax, %r15
leaq 0xcc4(%rip), %rsi       # 0x42a9
movq %rax, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x21a0
movzwl 0x30(%r12), %edx
leaq 0xcb5(%rip), %rsi       # 0x42b4
movq %r15, %rdi
xorl %eax, %eax
callq 0x21a0
movzwl 0x32(%r12), %edx
leaq 0xcad(%rip), %rsi       # 0x42c3
movq %r15, %rdi
xorl %eax, %eax
callq 0x21a0
movzwl 0x38(%r12), %edx
leaq 0xca5(%rip), %rsi       # 0x42d2
movq %r15, %rdi
xorl %eax, %eax
callq 0x21a0
movzbl 0x34(%r12), %edx
leaq 0xca0(%rip), %rsi       # 0x42e4
movq %r15, %rdi
xorl %eax, %eax
callq 0x21a0
movzbl 0x35(%r12), %edx
andl $0x3, %edx
leaq 0xc92(%rip), %rsi       # 0x42f0
movq %r15, %rdi
xorl %eax, %eax
callq 0x21a0
movzbl 0x35(%r12), %edx
shrl $0x2, %edx
andl $0x1, %edx
leaq 0xc81(%rip), %rsi       # 0x42fc
movq %r15, %rdi
xorl %eax, %eax
callq 0x21a0
movzwl 0x36(%r12), %edx
leaq 0xc74(%rip), %rsi       # 0x4306
movq %r15, %rdi
xorl %eax, %eax
callq 0x21a0
cmpw $0x0, 0x3a(%r12)
movq %r12, 0x88(%rsp)
je 0x3b18
movq %r14, 0x90(%rsp)
leaq 0x102(%r12), %rbx
leaq 0xc4d(%rip), %rbp       # 0x4315
leaq 0x130(%rsp), %r14
xorl %r13d, %r13d
movzwl -0x2(%rbx), %eax
orl $0x60, %eax
shll $0x9, %eax
movzwl %ax, %edx
movzbl (%rbx), %ecx
movq %r15, %rdi
movq %rbp, %rsi
movq %r14, %r8
movl %r13d, %r9d
xorl %eax, %eax
callq 0x21a0
incq %r13
movzwl 0x3a(%r12), %eax
addq $0x3, %rbx
cmpq %rax, %r13
jb 0x36d3
movq %r15, %rdi
callq 0x2100
leaq 0x400(%r12), %r14
movq %r12, %rcx
addq $0x100, %rcx            # imm = 0x100
leaq 0x50(%rsp), %rbx
xorl %r15d, %r15d
movq %rbx, %r12
movq %rcx, 0x80(%rsp)
movzwl (%rcx), %eax
leaq 0x930(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rbp
cmpl $0x4000, %eax           # imm = 0x4000
jb 0x3765
leaq 0x130(%rsp), %rbx
movq %rbx, %rdi
callq 0x2ca3
leaq 0x530(%rsp), %rdi
leaq 0xbc5(%rip), %rsi       # 0x4331
movq 0x90(%rsp), %rdx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x2040
movq %rbx, %rdi
leaq 0xbb1(%rip), %rsi       # 0x4339
callq 0x2130
testq %rax, %rax
je 0x3b3f
movq %rax, %rbx
andl $0x1f00, %ebp           # imm = 0x1F00
movq %rbp, 0x98(%rsp)
leaq 0x200(,%rbp,2), %rdx
movl $0x1, %esi
movq %r14, %rdi
movq %rax, %rcx
callq 0x2250
movq %rbx, %rdi
callq 0x2100
movq 0x80(%rsp), %rcx
movzwl (%rcx), %eax
cmpl $0x4000, %eax           # imm = 0x4000
movq %r12, %rbx
leaq 0x20(%rsp), %r12
jb 0x3af3
shrl $0xe, %eax
cmpl $0x2, %eax
je 0x3880
cmpl $0x1, %eax
jne 0x3ade
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq %rsp, %rdi
leaq 0xb2e(%rip), %rsi       # 0x433b
leaq 0xb34(%rip), %rdx       # 0x4348
callq 0x3c4a
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x3ce0
movq %r12, %rdi
leaq 0x130(%rsp), %rsi
callq 0x2240
movq %r12, %rdi
leaq 0x9a2(%rip), %rsi       # 0x41f3
callq 0x2240
movq %rbx, 0x40(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x38ff
movq %rdx, 0x40(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x50(%rsp)
jmp 0x3905
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq %rsp, %rdi
leaq 0xab6(%rip), %rsi       # 0x4349
leaq 0xabc(%rip), %rdx       # 0x4356
callq 0x3c4a
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x3ce0
movq %r12, %rdi
leaq 0x130(%rsp), %rsi
callq 0x2240
movq %r12, %rdi
leaq 0x91c(%rip), %rsi       # 0x41f3
callq 0x2240
movq %rbx, 0x40(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x3957
movq %rdx, 0x40(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x50(%rsp)
jmp 0x395d
movups (%rcx), %xmm0
movups %xmm0, (%rbx)
movq 0x8(%rax), %rdx
movq %rdx, 0x48(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x40(%rsp), %rdi
leaq 0x530(%rsp), %rsi
callq 0x2240
leaq 0x70(%rsp), %r12
movq %r12, 0x60(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x39b6
movq %rdx, 0x60(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x70(%rsp)
jmp 0x39be
movups (%rcx), %xmm0
movups %xmm0, (%rbx)
movq 0x8(%rax), %rdx
movq %rdx, 0x48(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x40(%rsp), %rdi
leaq 0x530(%rsp), %rsi
callq 0x2240
leaq 0x70(%rsp), %r12
movq %r12, 0x60(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x3a3b
movq %rdx, 0x60(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x70(%rsp)
jmp 0x3a43
movups (%rcx), %xmm0
movups %xmm0, (%r12)
movq 0x8(%rax), %rdx
movq %rdx, 0x68(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %rsp, %rdi
leaq 0x60(%rsp), %rsi
callq 0x2160
movq 0x60(%rsp), %rdi
cmpq %r12, %rdi
je 0x39fa
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2110
movq 0x40(%rsp), %rdi
cmpq %rbx, %rdi
je 0x3a11
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2110
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x3a2d
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2110
movq (%rsp), %rdi
callq 0x20b0
jmp 0x3abb
movups (%rcx), %xmm0
movups %xmm0, (%r12)
movq 0x8(%rax), %rdx
movq %rdx, 0x68(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %rsp, %rdi
leaq 0x60(%rsp), %rsi
callq 0x2160
movq 0x60(%rsp), %rdi
cmpq %r12, %rdi
je 0x3a7f
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2110
movq 0x40(%rsp), %rdi
cmpq %rbx, %rdi
je 0x3a96
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2110
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x3ab2
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2110
movq (%rsp), %rdi
callq 0x20b0
testl %eax, %eax
js 0x3b63
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x3ade
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2110
leaq 0x130(%rsp), %rdi
callq 0x2220
movq 0x80(%rsp), %rcx
movq 0x98(%rsp), %rax
leaq (%r14,%rax,2), %r14
addq $0x200, %r14            # imm = 0x200
incq %r15
addq $0x3, %rcx
cmpq %r15, %r13
jne 0x372a
jmp 0x3b20
movq %r15, %rdi
callq 0x2100
movq 0x88(%rsp), %rdi
callq 0x2140
addq $0xd38, %rsp            # imm = 0xD38
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x742(%rip), %rdi       # 0x4288
xorl %eax, %eax
callq 0x2030
leaq 0x77c(%rip), %rdi       # 0x42d0
callq 0x21d0
movl $0x6, %edi
callq 0x2150
leaq 0x6df(%rip), %rdi       # 0x4249
xorl %eax, %eax
callq 0x2030
leaq 0x758(%rip), %rdi       # 0x42d0
callq 0x21d0
movl $0xb, %edi
callq 0x2150
leaq 0x667(%rip), %rdi       # 0x41f5
xorl %eax, %eax
callq 0x2030
leaq 0x734(%rip), %rdi       # 0x42d0
callq 0x21d0
movl $0x5, %edi
callq 0x2150
leaq 0x6e2(%rip), %rdi       # 0x4294
xorl %eax, %eax
callq 0x2030
leaq 0x710(%rip), %rdi       # 0x42d0
callq 0x21d0
movl $0xe, %edi
callq 0x2150
jmp 0x3bdb
jmp 0x3bfc
jmp 0x3bfc
jmp 0x3c24
jmp 0x3c1f
jmp 0x3c24
movq %rbx, %rcx
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %rcx, %rdi
je 0x3bff
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2110
jmp 0x3bff
jmp 0x3bfc
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x3c27
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2110
jmp 0x3c27
jmp 0x3c24
movq %rax, %rbx
jmp 0x3c42
movq %rax, %rbx
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x3c42
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2110
movq %rbx, %rdi
callq 0x2200