// File: /workspace/github_c_makefile_star2_O3/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 0x443fd1(%rip), %rax    # 0x446d60
cmpl $0x0, 0x18(%rax)
jle 0x32c9
movl $0x100, %eax            # imm = 0x100
addq 0x322b(%rip), %rax      # 0x5fd0
movq %rax, 0x88(%rsp)
movl $0x408, %ebx            # imm = 0x408
addq 0x3227(%rip), %rbx      # 0x5fe0
leaq 0xa0(%rsp), %r12
leaq 0x90(%rsp), %r15
leaq 0x1423(%rip), %r13      # 0x41f3
xorl %ebp, %ebp
movq %rbp, 0x80(%rsp)
movq %r15, %r14
leaq 0xb0(%rsp), %r15
movq %r15, %rdi
callq 0x2ca3
movq %r12, %rdi
callq 0x2ca3
movq %r14, %rdi
callq 0x2ca3
movq %r15, %rdi
leaq 0x1434(%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 0x443f2e(%rip), %rax    # 0x446d60
movl (%rax), %eax
cmpl $0x1, %eax
je 0x2e42
cmpl $-0x1, %eax
jne 0x2ed1
movq %r13, %rbp
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0x20(%rsp), %r14
movq %r14, %rdi
leaq 0x13e0(%rip), %rsi      # 0x423e
leaq 0x13e3(%rip), %rdx      # 0x4248
callq 0x3c4a
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 0x3ce0
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 0x2ee9
movq %rdx, 0x40(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x50(%rsp)
jmp 0x2ef0
movl $0x4000, %ebp           # imm = 0x4000
movl $0x1f, %r15d
leaq 0x20(%rsp), %r14
movq %rsp, %r12
jmp 0x3021
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 0x2f3d
movq %rdx, 0x60(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x70(%rsp)
jmp 0x2f45
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 0x2f81
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2110
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
leaq 0xa0(%rsp), %r15
movq %rsp, %r12
je 0x2fa3
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2110
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x2fbe
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2110
movq 0x20(%rsp), %rdi
callq 0x20b0
testl %eax, %eax
js 0x32db
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 0x3004
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2110
movq %rbp, %r15
movabsq $0x1ffffffffff, %rax    # imm = 0x1FFFFFFFFFF
addq %rax, %r15
shrq $0x9, %r15
leaq 0x443d41(%rip), %rax    # 0x446d60
movl (%rax), %eax
cmpl $0x2, %eax
je 0x302f
cmpl $-0x1, %eax
jne 0x30b0
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %r14, %rdi
leaq 0x123a(%rip), %rsi      # 0x427d
leaq 0x123d(%rip), %rdx      # 0x4287
callq 0x3c4a
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x3ce0
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 0x30c1
movq %rdx, 0x40(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x50(%rsp)
jmp 0x30c7
movl $0x1f, %r12d
movl $0x4000, %r14d          # imm = 0x4000
jmp 0x31f3
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 0x3119
movq %rdx, 0x60(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x70(%rsp)
jmp 0x3121
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 0x315d
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2110
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x3179
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2110
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x3194
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2110
movq 0x20(%rsp), %rdi
callq 0x20b0
testl %eax, %eax
js 0x32db
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 0x31df
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 0x326d
movq %rbp, %rdx
cmpl $0x1, %eax
leaq 0xa0(%rsp), %r12
leaq 0x90(%rsp), %r15
movq 0x80(%rsp), %rbp
jne 0x3292
movl %ebp, %edi
movq %r12, %rsi
callq 0x2cd6
jmp 0x3292
movq 0x80(%rsp), %rbp
movl %ebp, %edi
leaq 0x90(%rsp), %r15
movq %r15, %rsi
movl %r14d, %edx
callq 0x2cd6
leaq 0xa0(%rsp), %r12
movq %r12, %rdi
callq 0x2220
movq %r15, %rdi
callq 0x2220
incq %rbp
leaq 0x443ab4(%rip), %rax    # 0x446d60
movslq 0x18(%rax), %rax
addq $0x3, 0x88(%rsp)
addq $0x4408, %rbx           # imm = 0x4408
cmpq %rax, %rbp
jl 0x2dd2
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xf67(%rip), %rdi       # 0x4249
xorl %eax, %eax
callq 0x2030
leaq 0xfe0(%rip), %rdi       # 0x42d0
callq 0x21d0
movl $0xb, %edi
callq 0x2150
jmp 0x3326
jmp 0x3307
jmp 0x3326
jmp 0x332b
movq %rax, %rbx
jmp 0x3381
jmp 0x332b
jmp 0x3326
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
jne 0x333d
jmp 0x334a
jmp 0x332b
movq %rax, %rbx
jmp 0x3365
movq %rax, %rbx
jmp 0x334a
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
je 0x334a
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2110
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x3365
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2110
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x3381
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2110
movq %rbx, %rdi
callq 0x2200