// File: /workspace/github_cpp_makefile_star2_O1/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 $0xa8, %rsp
movq %rdi, %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
xorl %ebx, %ebx
movq %rbx, 0x50(%rsp)
movaps %xmm0, 0x20(%rsp)
movq %rbx, 0x30(%rsp)
movq %rsi, 0x10(%rsp)
movq %rsi, %rdi
callq 0xb0285
movq %rax, %r12
leaq 0x88(%rsp), %rdi
callq 0x3372c
leaq 0x68(%rsp), %rdi
callq 0x3372c
movq %rbx, 0x20(%rsp)
movq %rbx, 0x40(%rsp)
movq %r12, (%rsp)
movq (%r12), %rax
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0xb56bb
movq (%rsp), %rax
movq 0x10(%rax), %rcx
movl $0x1, %ebp
xorl %r12d, %r12d
movq %rcx, 0x8(%rsp)
movq 0x10(%rsp), %rbx
leaq (%r12,%r12,4), %rax
cmpl $0x0, 0xc(%rcx,%rax,8)
je 0xb56ab
leaq (%rcx,%rax,8), %r13
movq (%r15), %rcx
movq 0x8(%r15), %rdx
movl 0x40(%r15), %r9d
movl 0x44(%r15), %eax
movq %r13, %rdi
movq %rbx, %rsi
leaq 0x40(%rsp), %r8
leaq 0x20(%rsp), %r14
pushq %r14
pushq %rax
callq 0xb3a75
addq $0x10, %rsp
movq (%r15), %r8
movq 0x8(%r15), %rcx
movl 0x40(%r15), %eax
movl 0x44(%r15), %r10d
subq $0x8, %rsp
movq %r13, %rdi
movq %rbx, %rsi
leaq 0x1d5bd7(%rip), %rdx    # 0x28b230
leaq 0x48(%rsp), %r9
pushq %r14
pushq %r10
pushq %rax
leaq 0x88(%rsp), %rax
pushq %rax
leaq 0xb0(%rsp), %rax
pushq %rax
callq 0xb5008
addq $0x30, %rsp
movl (%r13), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
movq (%rsp), %rcx
movq 0x18(%rcx), %rcx
movq 0x20(%rcx), %rcx
movl (%rcx,%r12,8), %edx
movl $0x80000000, %esi       # imm = 0x80000000
andl %esi, %edx
orl %eax, %edx
movl %edx, (%rcx,%r12,8)
movq 0x8(%rsp), %rcx
movl %ebp, %r12d
incl %ebp
cmpq %r12, 0x18(%rsp)
ja 0xb55fb
leaq 0x88(%rsp), %rdi
callq 0x33667
leaq 0x68(%rsp), %rdi
callq 0x33667
movq (%rsp), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %edx
addl %edx, %edx
je 0xb5758
movq 0x20(%rax), %rcx
movl %edx, %edx
xorl %esi, %esi
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl %esi, %r8d
shrl %r8d
cmpl $0x0, (%rcx,%r8,8)
js 0xb5750
movq 0x28(%rax), %r8
movq (%r8,%rsi,8), %r9
testl %r9d, %r9d
je 0xb5750
movq %r9, %r8
shrq $0x20, %r8
leaq (%r8,%r8,2), %r8
shlq $0x3, %r8
addq 0x8(%rax), %r8
movl %r9d, %r9d
shlq $0x3, %r9
leaq (%r9,%r9,2), %r9
xorl %r10d, %r10d
movl 0xc(%r8,%r10), %r11d
testl %r11d, %r11d
js 0xb5747
movq (%r8,%r10), %rbx
shrq $0x21, %rbx
movl (%rcx,%rbx,8), %ebx
andl %edi, %ebx
subl %r11d, %ebx
movl %ebx, (%r8,%r10)
addq $0x18, %r10
cmpq %r10, %r9
jne 0xb5729
incq %rsi
cmpq %rdx, %rsi
jne 0xb56ee
movq 0x30(%rsp), %rdi
callq 0x2220
movq (%rsp), %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq