// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xe38, %rsp            # imm = 0xE38
movq %rdi, 0xe30(%rsp)
leaq 0x630(%rsp), %rdi
movq 0xe30(%rsp), %rdx
leaq 0x14c2(%rip), %rsi      # 0x5198
movb $0x0, %al
callq 0x2050
movq 0xe30(%rsp), %rdi
leaq 0x13fc(%rip), %rsi      # 0x50e8
callq 0x21f0
movq %rax, 0x228(%rsp)
cmpq $0x0, 0x228(%rsp)
jne 0x3d2a
leaq 0x15a4(%rip), %rdi      # 0x52af
movb $0x0, %al
callq 0x2030
leaq 0x1326(%rip), %rdi      # 0x503f
movb $0x0, %al
callq 0x2030
movl $0x6, %edi
callq 0x2210
movq 0xe30(%rsp), %rdi
leaq 0x198(%rsp), %rsi
callq 0x2160
cmpl $0x0, %eax
jge 0x3d6a
leaq 0x14d1(%rip), %rdi      # 0x521c
movb $0x0, %al
callq 0x2030
leaq 0x12e6(%rip), %rdi      # 0x503f
movb $0x0, %al
callq 0x2030
movl $0x5, %edi
callq 0x2210
movq 0x1c8(%rsp), %rdi
callq 0x2240
movq %rax, 0x190(%rsp)
cmpq $0x0, 0x190(%rsp)
jne 0x3db0
leaq 0x152a(%rip), %rdi      # 0x52bb
movb $0x0, %al
callq 0x2030
leaq 0x12a0(%rip), %rdi      # 0x503f
movb $0x0, %al
callq 0x2030
movl $0xe, %edi
callq 0x2210
movq 0x190(%rsp), %rdi
movq 0x1c8(%rsp), %rdx
movq 0x228(%rsp), %rcx
movl $0x1, %esi
callq 0x2290
movq 0x228(%rsp), %rdi
callq 0x2190
leaq 0xa30(%rsp), %rdi
movq 0xe30(%rsp), %rdx
leaq 0x14d3(%rip), %rsi      # 0x52c9
movb $0x0, %al
callq 0x2050
leaq 0xa30(%rsp), %rdi
leaq 0x1456(%rip), %rsi      # 0x5262
callq 0x21f0
movq %rax, 0x228(%rsp)
cmpq $0x0, 0x228(%rsp)
jne 0x3e4a
leaq 0x1484(%rip), %rdi      # 0x52af
movb $0x0, %al
callq 0x2030
leaq 0x1206(%rip), %rdi      # 0x503f
movb $0x0, %al
callq 0x2030
movl $0x6, %edi
callq 0x2210
movq 0x190(%rsp), %rax
movq %rax, 0x188(%rsp)
movq 0x228(%rsp), %rdi
movq 0x188(%rsp), %rdx
leaq 0x145f(%rip), %rsi      # 0x52d0
movb $0x0, %al
callq 0x2260
movq 0x228(%rsp), %rdi
movq 0x188(%rsp), %rax
movzwl 0x30(%rax), %edx
leaq 0x1448(%rip), %rsi      # 0x52db
movb $0x0, %al
callq 0x2260
movq 0x228(%rsp), %rdi
movq 0x188(%rsp), %rax
movzwl 0x32(%rax), %edx
leaq 0x1435(%rip), %rsi      # 0x52ea
movb $0x0, %al
callq 0x2260
movq 0x228(%rsp), %rdi
movq 0x188(%rsp), %rax
movzwl 0x38(%rax), %edx
leaq 0x1422(%rip), %rsi      # 0x52f9
movb $0x0, %al
callq 0x2260
movq 0x228(%rsp), %rdi
movq 0x188(%rsp), %rax
movzbl 0x34(%rax), %edx
leaq 0x1412(%rip), %rsi      # 0x530b
movb $0x0, %al
callq 0x2260
movq 0x228(%rsp), %rdi
movq 0x188(%rsp), %rax
movb 0x35(%rax), %al
andb $0x3, %al
movzbl %al, %edx
leaq 0x13f8(%rip), %rsi      # 0x5317
movb $0x0, %al
callq 0x2260
movq 0x228(%rsp), %rdi
movq 0x188(%rsp), %rax
movb 0x35(%rax), %al
shrb $0x2, %al
andb $0x1, %al
movzbl %al, %edx
leaq 0x13db(%rip), %rsi      # 0x5323
movb $0x0, %al
callq 0x2260
movq 0x228(%rsp), %rdi
movq 0x188(%rsp), %rax
movzwl 0x36(%rax), %edx
leaq 0x13c3(%rip), %rsi      # 0x532d
movb $0x0, %al
callq 0x2260
movl $0x0, 0x180(%rsp)
movl 0x180(%rsp), %eax
movq 0x188(%rsp), %rcx
movzwl 0x3a(%rcx), %ecx
cmpl %ecx, %eax
jge 0x4035
movq 0x228(%rsp), %rdi
movq 0x188(%rsp), %rax
addq $0x100, %rax            # imm = 0x100
movslq 0x180(%rsp), %rcx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movw (%rax), %ax
andw $0x1f, %ax
movzbl %al, %edx
shll $0x9, %edx
addl $0xc000, %edx           # imm = 0xC000
movq 0x188(%rsp), %rax
addq $0x100, %rax            # imm = 0x100
movslq 0x180(%rsp), %rcx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movzbl 0x2(%rax), %ecx
leaq 0x630(%rsp), %r8
movl 0x180(%rsp), %r9d
leaq 0x1335(%rip), %rsi      # 0x533c
movb $0x0, %al
callq 0x2260
movl 0x180(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x184(%rsp)
movl 0x180(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x180(%rsp)
jmp 0x3f7c
movq 0x228(%rsp), %rdi
callq 0x2190
movq 0x190(%rsp), %rax
addq $0x400, %rax            # imm = 0x400
movq %rax, 0x178(%rsp)
movl $0x0, 0x174(%rsp)
movl 0x174(%rsp), %eax
cmpl 0x184(%rsp), %eax
jge 0x4605
movq 0x188(%rsp), %rax
addq $0x100, %rax            # imm = 0x100
movslq 0x174(%rsp), %rcx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movw (%rax), %ax
shrw $0x8, %ax
andw $0x1f, %ax
movzbl %al, %eax
addl $0x1, %eax
shll $0x9, %eax
movl %eax, 0x170(%rsp)
movq 0x178(%rsp), %rax
movq %rax, 0x168(%rsp)
movl 0x170(%rsp), %ecx
movq 0x178(%rsp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x178(%rsp)
movq 0x188(%rsp), %rax
addq $0x100, %rax            # imm = 0x100
movslq 0x174(%rsp), %rcx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movw (%rax), %ax
shrw $0xe, %ax
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x414b
leaq 0xa30(%rsp), %rdi
movq 0xe30(%rsp), %rdx
movl 0x174(%rsp), %ecx
leaq 0x1232(%rip), %rsi      # 0x5358
movb $0x0, %al
callq 0x2050
leaq 0xa30(%rsp), %rdi
leaq 0x1224(%rip), %rsi      # 0x5360
callq 0x21f0
movq %rax, 0x228(%rsp)
jmp 0x4199
leaq 0x630(%rsp), %rdi
callq 0x3250
leaq 0x230(%rsp), %rdi
movq 0xe30(%rsp), %rdx
movl 0x174(%rsp), %ecx
leaq 0x11e2(%rip), %rsi      # 0x5358
movb $0x0, %al
callq 0x2050
leaq 0x630(%rsp), %rdi
leaq 0x11d4(%rip), %rsi      # 0x5360
callq 0x21f0
movq %rax, 0x228(%rsp)
cmpq $0x0, 0x228(%rsp)
jne 0x41ca
leaq 0x1104(%rip), %rdi      # 0x52af
movb $0x0, %al
callq 0x2030
leaq 0xe86(%rip), %rdi       # 0x503f
movb $0x0, %al
callq 0x2030
movl $0x6, %edi
callq 0x2210
movq 0x168(%rsp), %rdi
movslq 0x170(%rsp), %rdx
movq 0x228(%rsp), %rcx
movl $0x1, %esi
callq 0x2330
movq 0x228(%rsp), %rdi
callq 0x2190
movq 0x188(%rsp), %rax
addq $0x100, %rax            # imm = 0x100
movslq 0x174(%rsp), %rcx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movw (%rax), %ax
shrw $0xe, %ax
movzbl %al, %eax
cmpl $0x0, %eax
je 0x45ed
movq 0x188(%rsp), %rax
movslq 0x174(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
movzwl 0x100(%rax,%rcx), %eax
shrl $0xe, %eax
movl %eax, 0x44(%rsp)
subb $0x1, %al
je 0x4264
jmp 0x4252
movl 0x44(%rsp), %eax
subl $0x2, %eax
je 0x4432
jmp 0x45e0
leaq 0x147(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x22e0
movq 0x38(%rsp), %rdx
leaq 0x10e0(%rip), %rsi      # 0x5362
leaq 0x148(%rsp), %rdi
callq 0x4630
jmp 0x4291
leaq 0x147(%rsp), %rdi
callq 0x21b0
leaq 0xd0(%rsp), %rdi
leaq 0x148(%rsp), %rsi
leaq 0x630(%rsp), %rdx
callq 0x4740
jmp 0x42bd
leaq 0xf56(%rip), %rdx       # 0x521a
leaq 0xf0(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x46f0
jmp 0x42db
leaq 0x110(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
leaq 0x230(%rsp), %rdx
callq 0x46f0
jmp 0x42fa
leaq 0x148(%rsp), %rdi
movq %rdi, 0x28(%rsp)
leaq 0x110(%rsp), %rsi
movq %rsi, 0x20(%rsp)
callq 0x2220
movq 0x20(%rsp), %rdi
callq 0x20f0
leaq 0xf0(%rsp), %rdi
callq 0x20f0
leaq 0xd0(%rsp), %rdi
callq 0x20f0
movq 0x28(%rsp), %rdi
callq 0x2090
movq %rax, %rdi
callq 0x2150
movl %eax, 0x34(%rsp)
jmp 0x4355
movl 0x34(%rsp), %eax
cmpl $0x0, %eax
jge 0x440e
leaq 0xf07(%rip), %rdi       # 0x5270
xorl %eax, %eax
callq 0x2030
jmp 0x4372
leaq 0xcc6(%rip), %rdi       # 0x503f
xorl %eax, %eax
callq 0x2030
jmp 0x4382
movl $0xb, %edi
callq 0x2210
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x138(%rsp)
movl %eax, 0x134(%rsp)
leaq 0x147(%rsp), %rdi
callq 0x21b0
jmp 0x461a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x138(%rsp)
movl %eax, 0x134(%rsp)
jmp 0x4420
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x138(%rsp)
movl %eax, 0x134(%rsp)
jmp 0x43ff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x138(%rsp)
movl %eax, 0x134(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x20f0
leaq 0xd0(%rsp), %rdi
callq 0x20f0
jmp 0x4420
leaq 0x148(%rsp), %rdi
callq 0x20f0
jmp 0x45e0
leaq 0x148(%rsp), %rdi
callq 0x20f0
jmp 0x461a
leaq 0xaf(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x22e0
movq 0x18(%rsp), %rdx
leaq 0xf20(%rip), %rsi       # 0x5370
leaq 0xb0(%rsp), %rdi
callq 0x4630
jmp 0x445f
leaq 0xaf(%rsp), %rdi
callq 0x21b0
leaq 0x48(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
leaq 0x630(%rsp), %rdx
callq 0x4740
jmp 0x4488
leaq 0xd8b(%rip), %rdx       # 0x521a
leaq 0x68(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x46f0
jmp 0x44a0
leaq 0x88(%rsp), %rdi
leaq 0x68(%rsp), %rsi
leaq 0x230(%rsp), %rdx
callq 0x46f0
jmp 0x44bc
leaq 0xb0(%rsp), %rdi
movq %rdi, 0x8(%rsp)
leaq 0x88(%rsp), %rsi
movq %rsi, (%rsp)
callq 0x2220
movq (%rsp), %rdi
callq 0x20f0
leaq 0x68(%rsp), %rdi
callq 0x20f0
leaq 0x48(%rsp), %rdi
callq 0x20f0
movq 0x8(%rsp), %rdi
callq 0x2090
movq %rax, %rdi
callq 0x2150
movl %eax, 0x14(%rsp)
jmp 0x450f
movl 0x14(%rsp), %eax
cmpl $0x0, %eax
jge 0x45c2
leaq 0xd4d(%rip), %rdi       # 0x5270
xorl %eax, %eax
callq 0x2030
jmp 0x452c
leaq 0xb0c(%rip), %rdi       # 0x503f
xorl %eax, %eax
callq 0x2030
jmp 0x453c
movl $0xb, %edi
callq 0x2210
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x138(%rsp)
movl %eax, 0x134(%rsp)
leaq 0xaf(%rsp), %rdi
callq 0x21b0
jmp 0x461a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x138(%rsp)
movl %eax, 0x134(%rsp)
jmp 0x45d1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x138(%rsp)
movl %eax, 0x134(%rsp)
jmp 0x45b6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x138(%rsp)
movl %eax, 0x134(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x20f0
leaq 0x48(%rsp), %rdi
callq 0x20f0
jmp 0x45d1
leaq 0xb0(%rsp), %rdi
callq 0x20f0
jmp 0x45e0
leaq 0xb0(%rsp), %rdi
callq 0x20f0
jmp 0x461a
leaq 0x630(%rsp), %rdi
callq 0x2300
jmp 0x45ef
movl 0x174(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x174(%rsp)
jmp 0x4063
movq 0x190(%rsp), %rdi
callq 0x2200
addq $0xe38, %rsp            # imm = 0xE38
retq
movq 0x138(%rsp), %rdi
callq 0x22d0
nopw (%rax,%rax)