// File: /workspace/github_c_makefile_star2_O2/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 $0x178, %rsp            # imm = 0x178
movl $0x100, %r14d           # imm = 0x100
addq 0x326f(%rip), %r14      # 0x5fd0
movl $0x408, %ebx            # imm = 0x408
addq 0x3273(%rip), %rbx      # 0x5fe0
leaq 0x44400c(%rip), %r15    # 0x446d80
leaq 0x70(%rsp), %r13
xorl %r12d, %r12d
movslq 0x18(%r15), %rax
cmpq %rax, %r12
jge 0x3080
movq %r12, 0x10(%rsp)
movq %r14, 0x48(%rsp)
leaq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x2c6f
movq %r13, %rdi
callq 0x2c6f
leaq 0x20(%rsp), %rdi
callq 0x2c6f
movq %r14, %rdi
leaq 0x147f(%rip), %rsi      # 0x423b
callq 0x2150
movq %rax, %r14
movslq -0x4(%rbx), %rdx
movq %rbx, 0x18(%rsp)
movq %rbx, %rdi
pushq $0x1
popq %rsi
movq %rax, %rcx
callq 0x2290
movq %r14, %rdi
callq 0x2120
movl (%r15), %eax
cmpl $0x1, %eax
je 0x2e04
movl $0x4000, %ecx           # imm = 0x4000
movq %rcx, 0x8(%rsp)
pushq $0x1f
popq %rcx
movl %ecx, %r12d
cmpl $-0x1, %eax
jne 0x2ed6
leaq 0x50(%rsp), %r14
movq %r14, %rdi
leaq 0x142b(%rip), %rsi      # 0x423e
leaq 0xc8(%rsp), %rdx
callq 0x37a6
leaq 0x88(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
leaq 0x30(%rsp), %rdx
callq 0x3800
leaq 0xa8(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
leaq 0x13a6(%rip), %rdx      # 0x41f3
callq 0x37e1
leaq 0xc8(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x37e1
movq %r14, %rdi
movq %r12, %rsi
callq 0x2180
movq %r12, %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x2070
movq %rbp, %rdi
callq 0x2070
movq 0x50(%rsp), %rdi
callq 0x20d0
testl %eax, %eax
js 0x3092
movq %r13, %rdi
leaq 0xe8(%rsp), %rsi
callq 0x20e0
movq 0x118(%rsp), %r12
movq %r12, 0x8(%rsp)
movabsq $0x1ffffffffff, %rax    # imm = 0x1FFFFFFFFFF
addq %rax, %r12
shrq $0x9, %r12
movq %r14, %rdi
callq 0x2070
movl (%r15), %eax
movq %r15, %r13
cmpl $0x2, %eax
je 0x2ef3
movl $0x4000, %r15d          # imm = 0x4000
pushq $0x1f
popq %rcx
movl %ecx, %r14d
cmpl $-0x1, %eax
jne 0x2fc6
leaq 0x50(%rsp), %r14
movq %r14, %rdi
leaq 0x137b(%rip), %rsi      # 0x427d
leaq 0xc8(%rsp), %r15
movq %r15, %rdx
callq 0x37a6
leaq 0x88(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
leaq 0x30(%rsp), %rdx
callq 0x3800
leaq 0xa8(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
leaq 0x12b4(%rip), %rdx      # 0x41f3
callq 0x37e1
movq %r15, %rdi
movq %rbx, %rsi
leaq 0x20(%rsp), %rdx
callq 0x37e1
movq %r14, %rdi
movq %r15, %rsi
callq 0x2180
movq %r15, %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x2070
leaq 0x88(%rsp), %rdi
callq 0x2070
movq 0x50(%rsp), %rdi
callq 0x20d0
testl %eax, %eax
js 0x3092
leaq 0x20(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x20e0
movq 0x118(%rsp), %r15
movq %r15, %r14
movabsq $0x1ffffffffff, %rax    # imm = 0x1FFFFFFFFFF
addq %rax, %r14
shrq $0x9, %r14
leaq 0x50(%rsp), %rdi
callq 0x2070
leaq 0x30(%rsp), %rdi
callq 0x2260
movq 0x18(%rsp), %rax
movl -0x4(%rax), %ecx
decl %ecx
sarl $0x9, %ecx
xorl %eax, %eax
cmpl %r14d, %r12d
cmovll %r12d, %r14d
setg %al
incl %eax
cmpl %r14d, %ecx
movl $0x0, %ecx
cmovlel %ecx, %eax
movq 0x48(%rsp), %r14
movzwl (%r14), %ecx
movl %eax, %edx
shll $0xe, %edx
andl $0x3fff, %ecx           # imm = 0x3FFF
orl %edx, %ecx
movw %cx, (%r14)
cmpl $0x2, %eax
je 0x3039
cmpl $0x1, %eax
movq %r13, %r15
leaq 0x70(%rsp), %r13
movq 0x10(%rsp), %r12
jne 0x3056
movl %r12d, %edi
movq %r13, %rsi
movq 0x8(%rsp), %rdx
callq 0x2ca4
jmp 0x3056
movq 0x10(%rsp), %r12
movl %r12d, %edi
leaq 0x20(%rsp), %rsi
movl %r15d, %edx
callq 0x2ca4
movq %r13, %r15
leaq 0x70(%rsp), %r13
movq %r13, %rdi
callq 0x2260
leaq 0x20(%rsp), %rdi
callq 0x2260
incq %r12
addq $0x3, %r14
movq 0x18(%rsp), %rbx
addq $0x4408, %rbx           # imm = 0x4408
jmp 0x2d7c
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x11b0(%rip), %rdi      # 0x4249
xorl %eax, %eax
callq 0x2030
leaq 0x1229(%rip), %rdi      # 0x42d0
callq 0x2210
pushq $0xb
popq %rdi
callq 0x2170
jmp 0x30b6
movq %rax, %rbx
jmp 0x30f6
jmp 0x30bf
jmp 0x30d7
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x2070
jmp 0x30df
jmp 0x30d7
jmp 0x30dc
jmp 0x30d7
movq %rax, %rbx
jmp 0x30ec
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x2070
leaq 0x50(%rsp), %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x2240