// File: chhylp123[P]hifiasm/htab.cpp
// Function: uidx_write(void*, ha_pt_s*, char*, ma_ug_t*)
int uidx_write(void *flt_tab, ha_pt_t *ha_idx, char* file_name, ma_ug_t *ug)
{
	char* gfa_name = (char*)malloc(strlen(file_name)+25);
    sprintf(gfa_name, "%s.uidx.bin", file_name);
    FILE* fp = fopen(gfa_name, "w");
    if (!fp) {
        free(gfa_name);
        return 0;
    }

	if(ug) write_dbug(ug, fp);

	yak_ft_t *ha_flt_tab = (yak_ft_t*)flt_tab;

    if(ha_flt_tab)
    {
        fwrite("f", 1, 1, fp);
        yak_ft_save(ha_flt_tab, fp);
    }

	if(ha_idx)
    {
        int i;
        ha_pt1_t *g;
        fwrite("h", 1, 1, fp);
        fwrite(&ha_idx->k, sizeof(ha_idx->k), 1, fp);
        fwrite(&ha_idx->pre, sizeof(ha_idx->pre), 1, fp);
        fwrite(&ha_idx->tot, sizeof(ha_idx->tot), 1, fp);
        fwrite(&ha_idx->tot_pos, sizeof(ha_idx->tot_pos), 1, fp);

        for (i = 0; i < 1<<ha_idx->pre; ++i) 
        {
            g = &(ha_idx->h[i]);
            yak_pt_save(g->h, fp);
            fwrite(&g->n, sizeof(g->n), 1, fp);
            fwrite(g->al, sizeof(ha_idxposl_t), g->n, fp);
        }
    }
	fprintf(stderr, "[M::%s] Index has been written.\n", __func__);
    free(gfa_name);
    fclose(fp);
    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x20(%rbp), %rdi
callq 0x20a0
movq %rax, %rdi
addq $0x19, %rdi
callq 0x2270
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x1ff93c(%rip), %rsi    # 0x38af7c
movb $0x0, %al
callq 0x2050
movq -0x30(%rbp), %rdi
leaq 0x20dd80(%rip), %rsi    # 0x3993d2
callq 0x2200
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x18b677
movq -0x30(%rbp), %rdi
callq 0x2210
movl $0x0, -0x4(%rbp)
jmp 0x18b810
cmpq $0x0, -0x28(%rbp)
je 0x18b68b
movq -0x28(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x1018e0
movq -0x10(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x18b6bf
movq -0x38(%rbp), %rcx
leaq 0x201bf9(%rip), %rdi    # 0x38d29e
movl $0x1, %edx
movq %rdx, %rsi
callq 0x2400
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x18ade0
cmpq $0x0, -0x18(%rbp)
je 0x18b7d8
movq -0x38(%rbp), %rcx
leaq 0x1fad86(%rip), %rdi    # 0x38645b
movl $0x1, %edx
movq %rdx, %rsi
callq 0x2400
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x2400
movq -0x18(%rbp), %rdi
addq $0x4, %rdi
movq -0x38(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x2400
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
movq -0x38(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x2400
movq -0x18(%rbp), %rdi
addq $0x10, %rdi
movq -0x38(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x2400
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
cmpl %ecx, %eax
jge 0x18b7d6
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x44(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq (%rax), %rdi
movq -0x38(%rbp), %rsi
callq 0x18ae10
movq -0x50(%rbp), %rdi
addq $0x8, %rdi
movq -0x38(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x2400
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x38(%rbp), %rcx
movl $0x10, %esi
callq 0x2400
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x18b751
jmp 0x18b7d8
movq 0x238801(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x1ff659(%rip), %rsi    # 0x38ae42
leaq 0x1ff798(%rip), %rdx    # 0x38af88
movb $0x0, %al
callq 0x22a0
movq -0x30(%rbp), %rdi
callq 0x2210
movq -0x38(%rbp), %rdi
callq 0x21a0
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)