// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: ug_ext_free(asg64_v*, asg64_v*, ul_resolve_t*, usg_t*, unsigned int, unsigned char**, unsigned int**, unsigned long**, asg64_v*, asg64_v*, unsigned int, unsigned int)
uint32_t ug_ext_free(asg64_v *ob, asg64_v *ub, ul_resolve_t *uidx, usg_t *ng, uint32_t max_ext, 
uint8_t **ff, uint32_t **ng_occ, uint64_t **i_idx, asg64_v *b64, asg64_v *ub64, uint32_t rocc_cut, 
uint32_t thread_path)
{
    // fprintf(stderr, "[M::%s::] rocc_cut::%u\n", __func__, rocc_cut);
    uint32_t k, n_vtx = ng->n<<1, a_n; unique_bridge_check_t u_aux;
    REALLOC((*ff), n_vtx); REALLOC((*ng_occ), n_vtx); REALLOC((*i_idx), n_vtx);
    renew_usg_t_bub(uidx, ng, *ng_occ, *ff, rocc_cut);
    u2g_hybrid_aln(uidx, ng, ob, ub);
    gen_hybrid_aln_idx(ng, ub->a, ub->n, ob->a, ob->n, b64, *i_idx);
    // if(rocc_cut == 10) 
    // {
    //     fprintf(stderr, "\n[M::%s::] rocc_cut::%u\n", __func__, rocc_cut);
    //     debug_prt_renew_aln(ng, ub->a, ub->n, ob->a, ob->n, b64, *i_idx);
    // }

    u_aux.f = *ff; u_aux.ng = ng; u_aux.int_idx = ub->a; u_aux.int_idx_n = ub->n; u_aux.uidx = uidx;
    u_aux.int_a = ob->a; u_aux.int_an = ob->n; u_aux.ridx_a = b64->a; u_aux.ridx = *i_idx;
    for (k = 0; k < uidx->str_b.n_thread; k++) {
        uidx->str_b.buf[k].res_dump.n = uidx->str_b.buf[k].u.n = uidx->str_b.buf[k].o.n = 0;
    }

    if(thread_path) {
        kt_for(uidx->str_b.n_thread, worker_ul_aln_path, &u_aux, u_aux.int_idx_n);
    } else {
        kt_for(uidx->str_b.n_thread, worker_ul_aln_pair, &u_aux, u_aux.int_idx_n);
    }

    for (k = b64->n = a_n = 0; k < uidx->str_b.n_thread; k++) {
        a_n += uidx->str_b.buf[k].res_dump.n;
        kv_resize(uint64_t, *b64, a_n);
        memcpy(b64->a+b64->n, uidx->str_b.buf[k].res_dump.a, uidx->str_b.buf[k].res_dump.n*(sizeof(*(b64->a))));
        b64->n = a_n;
    }
    radix_sort_srt64(b64->a, b64->a + b64->n);///keeps the coordinates within int_a[]
    // fprintf(stderr, "[M::%s::] a_n::%u, int_idx_n::%u, int_n::%u\n", 
    //                 __func__, a_n, (uint32_t)ub->n, (uint32_t)ob->n);
    a_n = ug_ext_0(uidx, ng, ub->a, ub->n, ob->a, max_ext, *ff, *ng_occ, *i_idx, b64, ub64, a_n);
    // if(a_n) usg_cleanup(ng);
    return a_n;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r9, %rbp
movl %r8d, 0x4(%rsp)
movq %rcx, %r13
movq %rdx, %r12
movq %rsi, 0x10(%rsp)
movq %rdi, %r15
movq 0xd0(%rsp), %r14
movl 0x8(%rcx), %ebx
addl %ebx, %ebx
movq (%r9), %rdi
movq %rbx, %rsi
callq 0x2370
movq %rax, (%rbp)
movq 0xc0(%rsp), %rax
movq (%rax), %rdi
leaq (,%rbx,4), %rsi
callq 0x2370
movq 0xc0(%rsp), %rcx
movq %rax, (%rcx)
movq 0xc8(%rsp), %rax
movq (%rax), %rdi
shlq $0x3, %rbx
movq %rbx, %rsi
callq 0x2370
movq 0xc8(%rsp), %rcx
movq %rax, (%rcx)
movq 0xc0(%rsp), %rax
movq (%rax), %rdx
movq (%rbp), %rcx
movq %r12, %rdi
movq %r13, %rsi
movl 0xe0(%rsp), %r8d
callq 0x21354c
movq %r12, %rdi
movq %r15, %rdx
movq 0x10(%rsp), %rbx
movq %rbx, %rcx
callq 0x212940
movq (%rbx), %rdx
movq 0x10(%rbx), %rsi
movq (%r15), %r8
movq 0x10(%r15), %rcx
subq $0x8, %rsp
movq %r13, %rdi
movq %r14, %r9
movq 0xd0(%rsp), %rax
pushq (%rax)
callq 0x213cc3
addq $0x10, %rsp
movq %rbp, 0x8(%rsp)
movq (%rbp), %rax
movq %rax, 0x48(%rsp)
movq %r13, 0x18(%rsp)
movq %r13, 0x50(%rsp)
movq 0x10(%rbx), %rax
movq %rax, 0x58(%rsp)
movq (%rbx), %rcx
movq %rcx, 0x60(%rsp)
movq %r12, 0x20(%rsp)
movq 0x10(%r15), %rax
movq %rax, 0x68(%rsp)
movq %r15, %r13
movq (%r15), %rax
movq %rax, 0x70(%rsp)
movq 0x10(%r14), %rax
movq %rax, 0x78(%rsp)
movq 0xc8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x80(%rsp)
movq 0x1e0(%r12), %rdi
testq %rdi, %rdi
je 0x215240
movq 0x1d8(%r12), %rax
addq $0x78, %rax
movl $0x1, %edx
xorl %esi, %esi
movq %rsi, (%rax)
movq %rsi, 0x18(%rax)
movq %rsi, 0x288(%rax)
movl %edx, %r8d
addq $0x328, %rax            # imm = 0x328
incl %edx
cmpq %r8, %rdi
ja 0x215222
movq 0xd8(%rsp), %r15
cmpl $0x0, 0xe8(%rsp)
leaq 0x2cd(%rip), %rax       # 0x215524
leaq 0x144(%rip), %rsi       # 0x2153a2
cmoveq %rax, %rsi
leaq 0x20(%rsp), %rdx
callq 0x1047b4
movq $0x0, (%r14)
xorl %ebp, %ebp
cmpq $0x0, 0x1e0(%r12)
je 0x21534a
movl $0x1, %ebx
xorl %eax, %eax
movq 0x1d8(%r12), %rcx
imulq $0x328, %rax, %r15      # imm = 0x328
addl 0x300(%rcx,%r15), %ebp
cmpq %rbp, 0x8(%r14)
jae 0x2152fa
leaq -0x1(%rbp), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%r14)
movq 0x10(%r14), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rdi
shlq $0x3, %rdi
addq 0x10(%r14), %rdi
movq 0x1d8(%r12), %rax
movq 0x300(%rax,%r15), %rdx
movq 0x310(%rax,%r15), %rsi
shlq $0x3, %rdx
callq 0x2160
movq %rbp, (%r14)
movl %ebx, %eax
incl %ebx
cmpq %rax, 0x1e0(%r12)
ja 0x21528b
movq 0x8(%rsp), %rbx
movq 0xd8(%rsp), %r15
jmp 0x21534f
movq 0x8(%rsp), %rbx
movq (%r14), %rax
movq 0x10(%r14), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1e3b6a
movq 0x10(%r13), %r8
movq %r12, %rdi
movq 0x18(%rsp), %rsi
movl 0x4(%rsp), %r9d
pushq %rbp
pushq %r15
pushq %r14
movq 0xe0(%rsp), %rax
pushq (%rax)
movq 0xe0(%rsp), %rax
pushq (%rax)
pushq (%rbx)
callq 0x214c80
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq