// File: /workspace/github_c_makefile_star2_O1/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, %r15
leaq 0x130(%rsp), %rdi
movq %r15, %rsi
callq 0x20e0
leaq 0xba2(%rip), %rsi       # 0x40e7
movq %r15, %rdi
callq 0x2130
testq %rax, %rax
je 0x3b32
movq %rax, %r14
leaq 0xa0(%rsp), %rsi
movq %r15, %rdi
callq 0x20c0
testl %eax, %eax
js 0x3b7a
movq 0xd0(%rsp), %r12
movq %r12, %rdi
callq 0x2170
testq %rax, %rax
je 0x3b9e
movq %rax, %rbx
movl $0x1, %esi
movq %rax, %rdi
movq %r12, %rdx
movq %r14, %rcx
callq 0x21c0
movq %r14, %rdi
callq 0x2100
leaq 0xcf3(%rip), %rsi       # 0x42a2
leaq 0x930(%rsp), %r14
movq %r14, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2040
leaq 0xc70(%rip), %rsi       # 0x423b
movq %r14, %rdi
callq 0x2130
testq %rax, %rax
je 0x3b32
movq %rax, %r13
movq %r15, 0x90(%rsp)
leaq 0xcbb(%rip), %rsi       # 0x42a9
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x21a0
movzwl 0x30(%rbx), %edx
leaq 0xcae(%rip), %rsi       # 0x42b4
movq %r13, %rdi
xorl %eax, %eax
callq 0x21a0
movzwl 0x32(%rbx), %edx
leaq 0xca8(%rip), %rsi       # 0x42c3
movq %r13, %rdi
xorl %eax, %eax
callq 0x21a0
movzwl 0x38(%rbx), %edx
leaq 0xca2(%rip), %rsi       # 0x42d2
movq %r13, %rdi
xorl %eax, %eax
callq 0x21a0
movzbl 0x34(%rbx), %edx
leaq 0xc9f(%rip), %rsi       # 0x42e4
movq %r13, %rdi
xorl %eax, %eax
callq 0x21a0
movzbl 0x35(%rbx), %edx
andl $0x3, %edx
leaq 0xc93(%rip), %rsi       # 0x42f0
movq %r13, %rdi
xorl %eax, %eax
callq 0x21a0
movzbl 0x35(%rbx), %edx
shrl $0x2, %edx
andl $0x1, %edx
leaq 0xc84(%rip), %rsi       # 0x42fc
movq %r13, %rdi
xorl %eax, %eax
callq 0x21a0
movzwl 0x36(%rbx), %edx
leaq 0xc79(%rip), %rsi       # 0x4306
movq %r13, %rdi
xorl %eax, %eax
callq 0x21a0
cmpw $0x0, 0x3a(%rbx)
je 0x36ed
leaq 0x102(%rbx), %r15
leaq 0xc69(%rip), %r14       # 0x4315
leaq 0x130(%rsp), %r12
xorl %ebp, %ebp
movzwl -0x2(%r15), %eax
orl $0x60, %eax
shll $0x9, %eax
movzwl %ax, %edx
movzbl (%r15), %ecx
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %r8
movl %ebp, %r9d
xorl %eax, %eax
callq 0x21a0
incq %rbp
movzwl 0x3a(%rbx), %eax
addq $0x3, %r15
cmpq %rax, %rbp
jb 0x36b6
jmp 0x36ed
movq %r13, %rdi
callq 0x2100
testl %ebp, %ebp
jle 0x3b18
leaq 0x400(%rbx), %r15
movq %rbx, %rcx
addq $0x100, %rcx            # imm = 0x100
leaq 0x50(%rsp), %r14
movl %ebp, %eax
movq %rax, 0x88(%rsp)
xorl %ebp, %ebp
movq %rbx, %r12
movq %r14, %rbx
movq %rcx, 0x80(%rsp)
movzwl (%rcx), %eax
leaq 0x930(%rsp), %r14
movq %r14, %rdi
movq %rax, %r13
cmpl $0x4000, %eax           # imm = 0x4000
jb 0x375d
leaq 0x130(%rsp), %r14
movq %r14, %rdi
callq 0x2ca4
leaq 0x530(%rsp), %rdi
leaq 0xbcd(%rip), %rsi       # 0x4331
movq 0x90(%rsp), %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2040
movq %r14, %rdi
leaq 0xbba(%rip), %rsi       # 0x4339
callq 0x2130
testq %rax, %rax
je 0x3b32
movq %rax, %r14
andl $0x1f00, %r13d          # imm = 0x1F00
movq %r13, 0x98(%rsp)
leaq 0x200(,%r13,2), %rdx
movl $0x1, %esi
movq %r15, %rdi
movq %rax, %rcx
callq 0x2250
movq %r14, %rdi
callq 0x2100
movq 0x80(%rsp), %rcx
movzwl (%rcx), %eax
cmpl $0x4000, %eax           # imm = 0x4000
movq %rbx, %r14
movq %r12, %rbx
leaq 0x20(%rsp), %r12
jb 0x3af0
shrl $0xe, %eax
cmpl $0x2, %eax
je 0x387b
cmpl $0x1, %eax
jne 0x3adb
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq %rsp, %rdi
leaq 0xb33(%rip), %rsi       # 0x433b
leaq 0xb39(%rip), %rdx       # 0x4348
callq 0x3c3a
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x3cca
movq %r12, %rdi
leaq 0x130(%rsp), %rsi
callq 0x2240
movq %r12, %rdi
leaq 0x9a7(%rip), %rsi       # 0x41f3
callq 0x2240
movq %r14, 0x40(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x38fa
movq %rdx, 0x40(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x50(%rsp)
jmp 0x3901
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq %rsp, %rdi
leaq 0xabb(%rip), %rsi       # 0x4349
leaq 0xac1(%rip), %rdx       # 0x4356
callq 0x3c3a
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x3cca
movq %r12, %rdi
leaq 0x130(%rsp), %rsi
callq 0x2240
movq %r12, %rdi
leaq 0x921(%rip), %rsi       # 0x41f3
callq 0x2240
movq %r14, 0x40(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x3953
movq %rdx, 0x40(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x50(%rsp)
jmp 0x395a
movups (%rcx), %xmm0
movups %xmm0, (%r14)
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 0x39b3
movq %rdx, 0x60(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x70(%rsp)
jmp 0x39bb
movups (%rcx), %xmm0
movups %xmm0, (%r14)
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 0x3a38
movq %rdx, 0x60(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x70(%rsp)
jmp 0x3a40
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 0x39f7
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2110
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x3a0e
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2110
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x3a2a
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2110
movq (%rsp), %rdi
callq 0x20b0
jmp 0x3ab8
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 0x3a7c
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2110
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x3a93
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2110
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x3aaf
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2110
movq (%rsp), %rdi
callq 0x20b0
testl %eax, %eax
js 0x3b56
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x3adb
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2110
leaq 0x130(%rsp), %rdi
callq 0x2220
movq 0x80(%rsp), %rcx
movq 0x98(%rsp), %rax
leaq (%r15,%rax,2), %r15
addq $0x200, %r15            # imm = 0x200
incq %rbp
addq $0x3, %rcx
cmpq %rbp, 0x88(%rsp)
jne 0x371f
movq %rbx, %rdi
callq 0x2140
addq $0xd38, %rsp            # imm = 0xD38
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x74f(%rip), %rdi       # 0x4288
xorl %eax, %eax
callq 0x2030
leaq 0x789(%rip), %rdi       # 0x42d0
callq 0x21d0
movl $0x6, %edi
callq 0x2150
leaq 0x6ec(%rip), %rdi       # 0x4249
xorl %eax, %eax
callq 0x2030
leaq 0x765(%rip), %rdi       # 0x42d0
callq 0x21d0
movl $0xb, %edi
callq 0x2150
leaq 0x674(%rip), %rdi       # 0x41f5
xorl %eax, %eax
callq 0x2030
leaq 0x741(%rip), %rdi       # 0x42d0
callq 0x21d0
movl $0x5, %edi
callq 0x2150
leaq 0x6ef(%rip), %rdi       # 0x4294
xorl %eax, %eax
callq 0x2030
leaq 0x71d(%rip), %rdi       # 0x42d0
callq 0x21d0
movl $0xe, %edi
callq 0x2150
jmp 0x3bce
jmp 0x3bec
jmp 0x3bec
jmp 0x3c14
jmp 0x3c0f
jmp 0x3c14
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x3bef
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2110
jmp 0x3bef
jmp 0x3bec
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x3c17
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2110
jmp 0x3c17
jmp 0x3c14
movq %rax, %rbx
jmp 0x3c32
movq %rax, %rbx
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x3c32
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2110
movq %rbx, %rdi
callq 0x2200