// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: gen_polished_ug(ug_opt_t const*, asg_t*)
ma_ug_t *gen_polished_ug(const ug_opt_t *uopt, asg_t *sg)
{
	kvec_asg_arc_t_warp e, d;
	uint32_t i; ma_utg_t *u;
	kv_init(e.a); kv_init(d.a);
	ma_ug_t *ug = ma_ug_gen(sg);
	UC_Read g_read, tmp;
    init_UC_Read(&g_read); init_UC_Read(&tmp);

	for (i = e.a.n = d.a.n = 0; i < ug->u.n; ++i) {
		u = &ug->u.a[i];
        if(u->m == 0) continue;
		polish_unitig(u, sg, uopt->sources, uopt->coverage_cut, &e, uopt->max_hang, uopt->min_ovlp, &d);
		polish_unitig_advance(u, sg, &R_INF, uopt->sources, uopt->coverage_cut, &e, &g_read, &tmp, uopt->max_hang, uopt->min_ovlp, &d);
        ug->g->seq[i].len = u->len;
	}

    destory_UC_Read(&g_read); destory_UC_Read(&tmp);

    uint32_t n_vtx = ug->g->n_seq*2, v, nv, vLen = 0;
    asg_arc_t* av = NULL;
    for (v = 0; v < n_vtx; ++v) {
        if (ug->g->seq[v>>1].del) continue;
        av = asg_arc_a(ug->g, v); nv = asg_arc_n(ug->g, v);
        for (i = 0; i < nv; i++) {
            if(av[i].del) continue;
            vLen = ug->g->seq[(av[i].ul>>33)].len - av[i].ol;
            av[i].ul = (av[i].ul>>32)<<32;
            av[i].ul = av[i].ul | vLen;
        }
    }

    // if(d.a.n > 0) {
    //     for (k = 0; k < d.a.n; k++) {
    //         p = asg_arc_pushp(sg);
    //         *p = d.a.a[k];
    //     }
    //     free(sg->idx); sg->idx = 0; sg->is_srt = 0;
    //     asg_cleanup(sg);
    // }

	kv_destroy(e.a); kv_destroy(d.a);
	return ug;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %r15
xorps %xmm0, %xmm0
leaq 0x78(%rsp), %rbx
movups %xmm0, 0x8(%rbx)
leaq 0x58(%rsp), %r13
movups %xmm0, 0x8(%r13)
movq %rsi, 0x8(%rsp)
movq %rsi, %rdi
callq 0x76d91
movq %rax, %r12
leaq 0x38(%rsp), %rdi
callq 0x6bd1
leaq 0x18(%rsp), %rdi
callq 0x6bd1
andq $0x0, (%r13)
andq $0x0, (%rbx)
movq (%r12), %rax
movq %rax, 0x10(%rsp)
xorl %r13d, %r13d
movq 0x8(%rsp), %r14
movl %r13d, %r13d
cmpq %r13, 0x10(%rsp)
jbe 0x7b7e1
movq 0x10(%r12), %rbp
imulq $0x28, %r13, %rax
cmpl $0x0, 0xc(%rbp,%rax)
je 0x7b7d9
addq %rax, %rbp
movq (%r15), %rcx
movq 0x8(%r15), %rdx
movl 0x40(%r15), %r9d
movl 0x44(%r15), %eax
movq %rbp, %rdi
movq %r14, %rsi
leaq 0x78(%rsp), %r8
leaq 0x58(%rsp), %rbx
pushq %rbx
pushq %rax
callq 0x79ed9
popq %rcx
popq %rdx
movq (%r15), %r8
movq 0x8(%r15), %rcx
movl 0x40(%r15), %eax
movl 0x44(%r15), %r10d
subq $0x8, %rsp
movq %rbp, %rdi
movq %r14, %rsi
leaq 0x1d8a9c(%rip), %rdx    # 0x254230
leaq 0x80(%rsp), %r9
pushq %rbx
pushq %r10
pushq %rax
leaq 0x38(%rsp), %rax
pushq %rax
leaq 0x60(%rsp), %rax
pushq %rax
callq 0x7b187
addq $0x30, %rsp
movl (%rbp), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
movq 0x18(%r12), %rcx
movq 0x20(%rcx), %rcx
movl (%rcx,%r13,8), %edx
movl $0x80000000, %esi       # imm = 0x80000000
andl %esi, %edx
orl %eax, %edx
movl %edx, (%rcx,%r13,8)
incl %r13d
jmp 0x7b727
leaq 0x38(%rsp), %rdi
callq 0x6b07
leaq 0x18(%rsp), %rdi
callq 0x6b07
movq 0x18(%r12), %rax
movl 0x14(%rax), %ecx
addl %ecx, %ecx
xorl %edx, %edx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
cmpq %rcx, %rdx
je 0x7b869
movq 0x20(%rax), %rdi
movl %edx, %r8d
shrl %r8d
cmpl $0x0, (%rdi,%r8,8)
js 0x7b864
movq 0x28(%rax), %r8
movq (%r8,%rdx,8), %r8
movl %r8d, %r9d
shrq $0x20, %r8
imulq $0x18, %r8, %r8
addq 0x8(%rax), %r8
imulq $0x18, %r9, %r9
xorl %r10d, %r10d
cmpq %r10, %r9
je 0x7b864
movl 0xc(%r8,%r10), %r11d
testl %r11d, %r11d
js 0x7b85e
movq (%r8,%r10), %r14
shrq $0x21, %r14
movl (%rdi,%r14,8), %ebp
andl %esi, %ebp
subl %r11d, %ebp
movl %ebp, (%r8,%r10)
addq $0x18, %r10
jmp 0x7b83a
incq %rdx
jmp 0x7b806
movq 0x68(%rsp), %rdi
callq 0x2220
movq %r12, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq