// File: 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
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0xf0, %rsp
movq %rdi, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x20(%rbp), %rdi
callq 0xe5910
movq %rax, -0x78(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x9850
leaq -0xb8(%rbp), %rdi
callq 0x9850
movq $0x0, -0x60(%rbp)
movq $0x0, -0x40(%rbp)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
movq -0x78(%rbp), %rcx
cmpq (%rcx), %rax
jae 0xecf32
movq -0x78(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x64(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0xece57
jmp 0xecf24
movq -0x70(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x18(%rbp), %rax
movq (%rax), %rcx
movq -0x18(%rbp), %rax
movl 0x40(%rax), %r9d
movq -0x18(%rbp), %rax
movl 0x44(%rax), %r10d
leaq -0x40(%rbp), %r8
leaq -0x60(%rbp), %rax
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0xeaa00
movq -0x70(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x18(%rbp), %rax
movq (%rax), %r8
movq -0x18(%rbp), %rax
movl 0x40(%rax), %r11d
movq -0x18(%rbp), %rax
movl 0x44(%rax), %r10d
leaq 0x2d840e(%rip), %rdx    # 0x3c52d0
leaq -0x40(%rbp), %r9
leaq -0x98(%rbp), %r14
leaq -0xb8(%rbp), %rbx
leaq -0x60(%rbp), %rax
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movl %r11d, 0x10(%rsp)
movl %r10d, 0x18(%rsp)
movq %rax, 0x20(%rsp)
callq 0xec620
movq -0x70(%rbp), %rax
movl (%rax), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq -0x78(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0x64(%rbp), %ecx
movl (%rax,%rcx,8), %edx
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl $0x80000000, %edx       # imm = 0x80000000
orl %esi, %edx
movl %edx, (%rax,%rcx,8)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0xece22
leaq -0x98(%rbp), %rdi
callq 0x9670
leaq -0xb8(%rbp), %rdi
callq 0x9670
movq -0x78(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0xbc(%rbp)
movl $0x0, -0xc8(%rbp)
movq $0x0, -0xd0(%rbp)
movl $0x0, -0xc0(%rbp)
movl -0xc0(%rbp), %eax
cmpl -0xbc(%rbp), %eax
jae 0xed113
movq -0x78(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0xc0(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0xecfb9
jmp 0xed0ff
movq -0x78(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rax
movq -0x78(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq 0x28(%rcx), %rcx
movl -0xc0(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xd0(%rbp)
movq -0x78(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x28(%rax), %rax
movl -0xc0(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0xc4(%rbp)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0xc4(%rbp), %eax
jae 0xed0fd
movq -0xd0(%rbp), %rax
movl -0x64(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0xed040
jmp 0xed0ef
movq -0x78(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movq -0xd0(%rbp), %rcx
movl -0x64(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
shrq $0x21, %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq -0xd0(%rbp), %rcx
movl -0x64(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, -0xc8(%rbp)
movq -0xd0(%rbp), %rax
movl -0x64(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
shrq $0x20, %rcx
shlq $0x20, %rcx
movq -0xd0(%rbp), %rax
movl -0x64(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0xd0(%rbp), %rax
movl -0x64(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movl -0xc8(%rbp), %eax
orq %rax, %rcx
movq -0xd0(%rbp), %rax
movl -0x64(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0xed010
jmp 0xed0ff
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0xecf81
movq -0x30(%rbp), %rdi
callq 0x2210
movq -0x50(%rbp), %rdi
callq 0x2210
movq -0x78(%rbp), %rax
addq $0xf0, %rsp
popq %rbx
popq %r14
popq %rbp
retq
nopw %cs:(%rax,%rax)