// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Trio.cpp
// Function: yak_ch_restore_core(yak_ch_t*, char const*, int, ...)
static yak_ch_t *yak_ch_restore_core(yak_ch_t *ch0, const char *fn, int mode, ...)
{
	va_list ap;
	FILE *fp;
	uint32_t t[3], f_tmp = 0;
	char magic[4];
	int i, j, absent, min_cnt = 0, mid_cnt = 0, mode_err = 0;
	uint64_t mask = (1ULL<<YAK_COUNTER_BITS) - 1, n_ins = 0, n_new = 0;
	yak_ch_t *ch;

	va_start(ap, mode);
	if (mode == YAK_LOAD_ALL) { // do nothing
	} else if (mode == YAK_LOAD_TRIOBIN1 || mode == YAK_LOAD_TRIOBIN2) {
		assert(YAK_COUNTER_BITS >= 4);
		min_cnt = va_arg(ap, int);
		mid_cnt = va_arg(ap, int);
		if (ch0 == 0 && mode == YAK_LOAD_TRIOBIN2)
			mode_err = 1;
	} else mode_err = 1;
	va_end(ap);
	if (mode_err) return 0;

	if ((fp = fopen(fn, "rb")) == 0) return 0;
	if (fread(magic, 1, 4, fp) != 4) return 0;
	if (strncmp(magic, YAK_MAGIC, 4) != 0) {
		fprintf(stderr, "ERROR: wrong file magic.\n");
		fclose(fp);
		return 0;
	}
	f_tmp += fread(t, 4, 3, fp);
	if (t[2] != YAK_COUNTER_BITS) {
		fprintf(stderr, "ERROR: saved counter bits: %d; compile-time counter bits: %d\n", t[2], YAK_COUNTER_BITS);
		fclose(fp);
		return 0;
	}
	///t[0] = k; t[1] = pre, t[2] = YAK_COUNTER_BITS;
	ch = ch0 == 0? yak_ch_init(t[0], t[1]) : ch0;
	assert((int)t[0] == ch->k && (int)t[1] == ch->pre);
	for (i = 0; i < 1<<ch->pre; ++i) {
		yak_ht_t *h = ch->h[i].h;
		f_tmp += fread(t, 4, 2, fp);
		///t[0] = kh_capacity(h), t[1] = kh_size(h);
		if (ch0 == 0) yak_ht_resize(h, t[0]);
		for (j = 0; j < (int)t[1]; ++j) {
			uint64_t key;
			f_tmp += fread(&key, 8, 1, fp);
			if (mode == YAK_LOAD_ALL) {
				++n_ins;
				yak_ht_put(h, key, &absent);
				if (absent) ++n_new;
			} else if (mode == YAK_LOAD_TRIOBIN1 || mode == YAK_LOAD_TRIOBIN2) {
				int cnt = key & mask, x, shift = mode == YAK_LOAD_TRIOBIN1? 0 : 2;
				//1. filter singleton k-mer; 2. label non-repeat and repeat
				if (cnt >= mid_cnt) x = 2<<shift;
				else if (cnt >= min_cnt) x = 1<<shift;
				else x = -1;
				if (x >= 0) {
					khint_t k;
					///no need cnt at all
					key = (key & ~mask) | x;
					++n_ins;
					k = yak_ht_put(h, key, &absent);
					if (absent) ++n_new;
					else kh_key(h, k) = kh_key(h, k) | x;
				}
			}
		}
	}
	fclose(fp);
	///fprintf(stderr, "[M::%s] inserted %ld k-mers, of which %ld are new\n", __func__, (long)n_ins, (long)n_new);
	return ch;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movl %edx, %ebx
leaq 0x70(%rsp), %rdx
movq %rcx, 0x18(%rdx)
movq %r8, 0x20(%rdx)
movq %r9, 0x28(%rdx)
testb %al, %al
je 0xc5b7f
movaps %xmm0, 0xa0(%rsp)
movaps %xmm1, 0xb0(%rsp)
movaps %xmm2, 0xc0(%rsp)
movaps %xmm3, 0xd0(%rsp)
movaps %xmm4, 0xe0(%rsp)
movaps %xmm5, 0xf0(%rsp)
movaps %xmm6, 0x100(%rsp)
movaps %xmm7, 0x110(%rsp)
movabsq $0x3000000018, %rax     # imm = 0x3000000018
movq %rax, 0x30(%rsp)
leaq 0x160(%rsp), %rax
movq %rax, 0x38(%rsp)
movq %rdx, 0x40(%rsp)
movl %ebx, %eax
andl $-0x2, %eax
cmpl $0x2, %eax
jne 0xc5f29
movq %rdi, %r12
movl 0x30(%rsp), %ecx
cmpq $0x29, %rcx
jae 0xc5be5
movq 0x40(%rsp), %rdx
movq %rcx, %rax
addq $0x8, %rax
movl %eax, 0x30(%rsp)
movl (%rdx,%rcx), %edx
movl %edx, 0x10(%rsp)
cmpl $0x20, %ecx
ja 0xc5bf9
addq 0x40(%rsp), %rax
addl $0x10, %ecx
movl %ecx, 0x30(%rsp)
jmp 0xc5c07
movq 0x38(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x38(%rsp)
movl (%rax), %eax
movl %eax, 0x10(%rsp)
movq 0x38(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x38(%rsp)
cmpl $0x3, %ebx
sete %cl
testq %r12, %r12
sete %dl
testb %cl, %dl
jne 0xc5f29
movl (%rax), %eax
movl %eax, 0x58(%rsp)
leaq 0x11c88d(%rip), %rax    # 0x1e24b5
movq %rsi, %rdi
movq %rax, %rsi
callq 0x2210
testq %rax, %rax
je 0xc5f29
movq %rax, %r15
leaq 0x5c(%rsp), %rdi
pushq $0x1
popq %rsi
pushq $0x4
popq %rdx
movq %rax, %rcx
callq 0x22f0
cmpq $0x4, %rax
jne 0xc5f29
cmpl $0x24b4159, 0x5c(%rsp)  # imm = 0x24B4159
jne 0xc5f05
leaq 0x14(%rsp), %r13
pushq $0x4
popq %rsi
pushq $0x3
popq %rdx
movq %r13, %rdi
movq %r15, %rcx
callq 0x22f0
movl 0x8(%r13), %edx
cmpl $0xa, %edx
jne 0xc5f40
testq %r12, %r12
je 0xc5c9f
movl 0x14(%rsp), %r14d
movq %r12, %rdx
jmp 0xc5d2e
movq %r15, 0x28(%rsp)
movl 0x14(%rsp), %r14d
movl 0x18(%rsp), %ebp
xorl %r15d, %r15d
cmpl $0xa, %ebp
jge 0xc5cbe
xorl %edx, %edx
movq 0x28(%rsp), %r15
jmp 0xc5d2e
pushq $0x1
popq %r13
pushq $0x20
popq %rsi
movq %r13, %rdi
callq 0x2120
movq %rax, 0x20(%rsp)
movl %r14d, 0xc(%rsp)
movl %r14d, (%rax)
movl %ebp, 0x4(%rax)
pushq $0x1
popq %rax
movl %ebp, %ecx
shll %cl, %eax
movslq %eax, %r14
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0x2120
movq %rax, %rbp
movq 0x20(%rsp), %rax
movq %rbp, 0x18(%rax)
testl %r14d, %r14d
cmovlel %r15d, %r14d
cmpq %r15, %r14
je 0xc5d1f
movq %r13, %rdi
pushq $0x18
popq %rsi
callq 0x2120
movq %rax, (%rbp,%r15,8)
incq %r15
jmp 0xc5d05
movq 0x28(%rsp), %r15
movq 0x20(%rsp), %rdx
movl 0xc(%rsp), %r14d
cmpl (%rdx), %r14d
jne 0xc5f67
movl 0x18(%rsp), %eax
cmpl 0x4(%rdx), %eax
jne 0xc5f67
cmpl $0x2, %ebx
setne %cl
addb %cl, %cl
pushq $0x2
popq %rsi
shll %cl, %esi
movl %esi, 0xc(%rsp)
pushq $0x1
popq %rsi
shll %cl, %esi
movl %esi, 0x54(%rsp)
xorl %r13d, %r13d
movq %rdx, %rbx
movq %r12, 0x60(%rsp)
movq %rdx, 0x20(%rsp)
pushq $0x1
popq %rdx
movl %eax, %ecx
shll %cl, %edx
movslq %edx, %rax
cmpq %rax, %r13
jge 0xc5f5d
movq 0x18(%rbx), %rax
movq (%rax,%r13,8), %rbx
leaq 0x14(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x2
popq %rdx
movq %r15, %rcx
callq 0x22f0
testq %r12, %r12
jne 0xc5dad
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xc6552
movl 0x18(%rsp), %eax
xorl %r12d, %r12d
testl %eax, %eax
cmovlel %r12d, %eax
movl %eax, 0x28(%rsp)
cmpl 0x28(%rsp), %r12d
je 0xc5ef0
leaq 0x68(%rsp), %rdi
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %r14
movq %r15, %rcx
callq 0x22f0
movq 0x68(%rsp), %rbp
movl %ebp, %eax
andl $0x3ff, %eax            # imm = 0x3FF
cmpl 0x10(%rsp), %eax
movl 0x54(%rsp), %r15d
pushq $-0x1
popq %rcx
cmovll %ecx, %r15d
cmpl 0x58(%rsp), %eax
cmovgel 0xc(%rsp), %r15d
testl %r15d, %r15d
js 0xc5ee5
cmpq $0x0, 0x10(%rbx)
je 0xc5e1e
movb (%rbx), %cl
pushq $0x1
popq %rsi
shll %cl, %esi
jmp 0xc5e20
xorl %esi, %esi
movl %esi, %eax
shrl %eax
movl %esi, %ecx
shrl $0x2, %ecx
addl %eax, %ecx
cmpl %ecx, 0x4(%rbx)
jae 0xc5e34
movl (%rbx), %ecx
jmp 0xc5e4d
incl %esi
movq %rbx, %rdi
callq 0xc6552
testl %eax, %eax
js 0xc5ee5
movl (%rbx), %ecx
pushq $0x1
popq %rsi
shll %cl, %esi
andq $-0x400, %rbp           # imm = 0xFC00
movl %r15d, %edx
orq %rdx, %rbp
movq %rbp, %rax
shrq $0xa, %rax
imull $0x9e3779b9, %eax, %eax # imm = 0x9E3779B9
negb %cl
shrl %cl, %eax
decl %esi
movq 0x8(%rbx), %rdi
movl %eax, %ecx
movl %ecx, %r8d
shrl $0x5, %r8d
movl (%rdi,%r8,4), %r10d
pushq $0x1
popq %r9
shll %cl, %r9d
btl %ecx, %r10d
jae 0xc5ec0
movq 0x10(%rbx), %r11
movl %ecx, %r15d
movq (%r11,%r15,8), %r11
xorq %rbp, %r11
cmpq $0x400, %r11            # imm = 0x400
jb 0xc5ec0
incl %ecx
andl %esi, %ecx
cmpl %eax, %ecx
jne 0xc5e73
movl %eax, %r8d
shrl $0x5, %r8d
pushq $0x1
popq %r9
movl %eax, %ecx
shll %cl, %r9d
movl (%rdi,%r8,4), %r10d
jmp 0xc5ec2
movl %ecx, %eax
movl %eax, %eax
testl %r10d, %r9d
je 0xc5ed3
movq 0x10(%rbx), %rcx
orq %rdx, (%rcx,%rax,8)
jmp 0xc5ee5
movq 0x10(%rbx), %rcx
movq %rbp, (%rcx,%rax,8)
orl %r10d, %r9d
movl %r9d, (%rdi,%r8,4)
incl 0x4(%rbx)
incl %r12d
movq %r14, %r15
jmp 0xc5dbe
incq %r13
movq 0x20(%rsp), %rbx
movl 0x4(%rbx), %eax
movq 0x60(%rsp), %r12
jmp 0xc5d6e
movq 0x18d0d4(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0x12558d(%rip), %rdi    # 0x1eb4a3
pushq $0x19
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
movq %r15, %rdi
callq 0x2190
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18d099(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0x12556c(%rip), %rsi    # 0x1eb4bd
xorl %ebx, %ebx
pushq $0xa
popq %rcx
xorl %eax, %eax
callq 0x22b0
movq %r15, %rdi
callq 0x2190
jmp 0xc5f2b
leaq 0x12558d(%rip), %rdi    # 0x1eb4fb
leaq 0x12534f(%rip), %rsi    # 0x1eb2c4
leaq 0x1255aa(%rip), %rcx    # 0x1eb526
pushq $0x67
popq %rdx
callq 0x2110