// File: /workspace/github_c_makefile_star2_O1/salextpuru[P]speccy-toochain/src/spgbld/func.cxx
// Function: pack_blocks()
void pack_blocks()
{
	struct stat st;
	int s1, s2, sz1, sz2;
	C_PACK p;

	for (int i=0; i<conf.n_blocks; i++) {
		char f1n[16], f2n[16], f3n[16];
		rand_name(f1n);
		rand_name(f2n);
		rand_name(f3n);
		// add errors check here!!!
		FILE* f1 = fopen(f1n, "w+");
		fwrite(blk[i].data, 1, blk[i].size, f1);
		fclose(f1);
		s1 = s2 = 16384;
		sz1 = sz2 = 31;

		if (conf.packer == PM_AUTO || conf.packer == PM_MLZ) {
			std::string cmd("mhmt -mlz ");
			cmd=cmd+f1n+" "+f2n;

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

			// if (_spawnlp(_P_WAIT, "mhmt.exe", "dummy", "-mlz", f1n, f2n, NULL) < 0) error(MHMT);
			stat(f2n, &st);
			s1 = st.st_size;
			sz1 = sz(st.st_size);
		}

		if (conf.packer == PM_AUTO || conf.packer == PM_HST) {
			std::string cmd("mhmt -hst ");
			cmd=cmd+f1n+" "+f3n;

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

			// if (_spawnlp(_P_WAIT, "mhmt.exe", "dummy", "-hst", f1n, f3n, NULL) < 0) error(MHMT);
			stat(f3n, &st);
			s2 = st.st_size;
			sz2 = sz(st.st_size);
		}

		remove(f1n);

		if (sz(blk[i].size) <= min(sz1, sz2))   // if unpacked chunk has the same size as packed, leave it unpacked
			p = PM_NONE;

		else
			p = (sz1 <= sz2) ? PM_MLZ : PM_HST;   // if MegaLZ gives the same chunk size as Hrust, use it as faster

		hdr.blk[i].comp = p;

		switch (p) {
		case PM_MLZ: {
			store_block(i, f2n, s1);
			break;
		}

		case PM_HST: {
			store_block(i, f3n, s2);
			break;
		}
		}

		remove(f2n);
		remove(f3n);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
leaq 0x443fd0(%rip), %rax    # 0x446d60
cmpl $0x0, 0x18(%rax)
jle 0x32ca
movl $0x100, %eax            # imm = 0x100
addq 0x322a(%rip), %rax      # 0x5fd0
movq %rax, 0x88(%rsp)
movl $0x408, %ebx            # imm = 0x408
addq 0x3226(%rip), %rbx      # 0x5fe0
leaq 0xa0(%rsp), %r12
leaq 0x90(%rsp), %r15
leaq 0x1422(%rip), %r13      # 0x41f3
xorl %ebp, %ebp
movq %rbp, 0x80(%rsp)
movq %r15, %r14
leaq 0xb0(%rsp), %r15
movq %r15, %rdi
callq 0x2ca4
movq %r12, %rdi
callq 0x2ca4
movq %r14, %rdi
callq 0x2ca4
movq %r15, %rdi
leaq 0x1433(%rip), %rsi      # 0x423b
callq 0x2130
movq %rax, %r15
movslq -0x4(%rbx), %rdx
movl $0x1, %esi
movq %rbx, %rdi
movq %rax, %rcx
callq 0x2250
movq %r15, %rdi
callq 0x2100
leaq 0x443f2d(%rip), %rcx    # 0x446d60
movl (%rcx), %eax
cmpl $0x1, %eax
je 0x2e43
cmpl $-0x1, %eax
jne 0x2ed2
movq %r13, %rbp
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0x20(%rsp), %r14
movq %r14, %rdi
leaq 0x13df(%rip), %rsi      # 0x423e
leaq 0x13e2(%rip), %rdx      # 0x4248
callq 0x3c3a
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
addq %rsi, %rdx
movq %rsp, %r13
movq %r13, %rdi
callq 0x3cca
leaq 0x50(%rsp), %r15
movq %r13, %rdi
leaq 0xb0(%rsp), %rsi
callq 0x2240
movq %r13, %rdi
movq %rbp, %r13
movq %rbp, %rsi
callq 0x2240
movq %r15, 0x40(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x2eea
movq %rdx, 0x40(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x50(%rsp)
jmp 0x2ef1
movl $0x4000, %ebp           # imm = 0x4000
movl $0x1f, %r15d
leaq 0x20(%rsp), %r14
movq %rsp, %r12
jmp 0x3020
movups (%rcx), %xmm0
movups %xmm0, (%r15)
movq 0x8(%rax), %rdx
movq %rdx, 0x48(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x40(%rsp), %rdi
movq %r12, %rsi
callq 0x2240
leaq 0x70(%rsp), %r12
movq %r12, 0x60(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x2f3e
movq %rdx, 0x60(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x70(%rsp)
jmp 0x2f46
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 %r14, %rdi
leaq 0x60(%rsp), %rsi
callq 0x2160
movq 0x60(%rsp), %rdi
cmpq %r12, %rdi
je 0x2f82
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2110
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
leaq 0xa0(%rsp), %r15
movq %rsp, %r12
je 0x2fa4
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2110
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x2fbf
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2110
movq 0x20(%rsp), %rdi
callq 0x20b0
testl %eax, %eax
js 0x32dc
movq %r15, %rdi
leaq 0xc8(%rsp), %rsi
callq 0x20c0
movq 0xf8(%rsp), %rbp
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x3005
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2110
movq %rbp, %r15
movabsq $0x1ffffffffff, %rax    # imm = 0x1FFFFFFFFFF
addq %rax, %r15
shrq $0x9, %r15
leaq 0x443d40(%rip), %rcx    # 0x446d60
movl (%rcx), %eax
cmpl $0x2, %eax
je 0x3030
cmpl $-0x1, %eax
jne 0x30b1
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %r14, %rdi
leaq 0x1239(%rip), %rsi      # 0x427d
leaq 0x123c(%rip), %rdx      # 0x4287
callq 0x3c3a
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x3cca
movq %r12, %rdi
leaq 0xb0(%rsp), %rsi
callq 0x2240
movq %r12, %rdi
movq %r13, %rsi
callq 0x2240
leaq 0x50(%rsp), %rsi
movq %rsi, 0x40(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x30c2
movq %rdx, 0x40(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x50(%rsp)
jmp 0x30c8
movl $0x1f, %r12d
movl $0x4000, %r14d          # imm = 0x4000
jmp 0x31f4
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq 0x8(%rax), %rdx
movq %rdx, 0x48(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x40(%rsp), %rdi
leaq 0x90(%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 0x311a
movq %rdx, 0x60(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x70(%rsp)
jmp 0x3122
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 %r14, %rdi
leaq 0x60(%rsp), %rsi
callq 0x2160
movq 0x60(%rsp), %rdi
cmpq %r12, %rdi
je 0x315e
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2110
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x317a
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2110
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x3195
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2110
movq 0x20(%rsp), %rdi
callq 0x20b0
testl %eax, %eax
js 0x32dc
leaq 0x90(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x20c0
movq 0xf8(%rsp), %r14
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x31e0
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2110
movq %r14, %r12
movabsq $0x1ffffffffff, %rax    # imm = 0x1FFFFFFFFFF
addq %rax, %r12
shrq $0x9, %r12
leaq 0xb0(%rsp), %rdi
callq 0x2220
movl -0x4(%rbx), %ecx
decl %ecx
sarl $0x9, %ecx
xorl %eax, %eax
cmpl %r12d, %r15d
cmovll %r15d, %r12d
setg %al
incl %eax
cmpl %r12d, %ecx
movl $0x0, %ecx
cmovlel %ecx, %eax
movq 0x88(%rsp), %rsi
movzwl (%rsi), %ecx
movl %eax, %edx
shll $0xe, %edx
andl $0x3fff, %ecx           # imm = 0x3FFF
orl %edx, %ecx
movw %cx, (%rsi)
cmpl $0x2, %eax
je 0x326e
movq %rbp, %rdx
cmpl $0x1, %eax
leaq 0xa0(%rsp), %r12
leaq 0x90(%rsp), %r15
movq 0x80(%rsp), %rbp
jne 0x3293
movl %ebp, %edi
movq %r12, %rsi
callq 0x2cd7
jmp 0x3293
movq 0x80(%rsp), %rbp
movl %ebp, %edi
leaq 0x90(%rsp), %r15
movq %r15, %rsi
movl %r14d, %edx
callq 0x2cd7
leaq 0xa0(%rsp), %r12
movq %r12, %rdi
callq 0x2220
movq %r15, %rdi
callq 0x2220
incq %rbp
leaq 0x443ab3(%rip), %rax    # 0x446d60
movslq 0x18(%rax), %rax
addq $0x3, 0x88(%rsp)
addq $0x4408, %rbx           # imm = 0x4408
cmpq %rax, %rbp
jl 0x2dd3
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xf66(%rip), %rdi       # 0x4249
xorl %eax, %eax
callq 0x2030
leaq 0xfdf(%rip), %rdi       # 0x42d0
callq 0x21d0
movl $0xb, %edi
callq 0x2150
jmp 0x3327
jmp 0x3308
jmp 0x3327
jmp 0x332c
movq %rax, %rbx
jmp 0x3382
jmp 0x332c
jmp 0x3327
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
jne 0x333e
jmp 0x334b
jmp 0x332c
movq %rax, %rbx
jmp 0x3366
movq %rax, %rbx
jmp 0x334b
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
je 0x334b
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2110
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x3366
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2110
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x3382
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2110
movq %rbx, %rdi
callq 0x2200