// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: ma_ug_seq_scaffold(ma_ug_t*, asg_t*, ma_sub_t*, ma_hit_t_alloc*, kvec_asg_arc_t_warp*, int, int, kvec_asg_arc_t_warp*, unsigned int)
int ma_ug_seq_scaffold(ma_ug_t *g, asg_t *read_g, ma_sub_t *coverage_cut, ma_hit_t_alloc* sources, 
kvec_asg_arc_t_warp* edge, int max_hang, int min_ovlp, kvec_asg_arc_t_warp *E, uint32_t is_polish)
{
    UC_Read g_read;
    init_UC_Read(&g_read);
    UC_Read tmp;
    init_UC_Read(&tmp);
	///utg_intv_t *tmp;
	uint32_t i, j, k;
    uint32_t rId, /**uId,**/ori, start, eLen, readLen;
    char* readS = NULL;
    if(!(g->g))
    {
        g->g = asg_init();
        for (i = 0; i < g->u.n; ++i) 
        {
            asg_seq_set(g->g, i, g->u.a[i].len, 0);
        }
        asg_cleanup(g->g);
        g->g->r_seq = g->g->n_seq;
    }
    
    ///why we need n_read here? it is just beacuse one read can only be included in one untig
    ///but it is not true
	///tmp = (utg_intv_t*)calloc(n_read, sizeof(utg_intv_t));
	///number of unitigs
	for (i = 0; i < g->u.n; ++i) {
		ma_utg_t *u = &g->u.a[i];
        if(u->m == 0) continue;
        if(is_polish) polish_unitig_scaffold(i, u, read_g, &R_INF, sources, coverage_cut, edge, &g_read, &tmp, max_hang, min_ovlp, E);
        g->g->seq[i].len = u->len;

		uint32_t l = 0;
		u->s = (char*)calloc(1, u->len + 1);
		memset(u->s, 'N', u->len);
		for (j = 0; j < u->n; ++j) {
            // fprintf(stderr, "j=%u, u->a[j]: %lu\n", j, u->a[j]);
            if(u->a[j] == (uint64_t)-1)
            {
                l += GAP_LEN;
                continue;
            }
            rId = u->a[j]>>33;
            ///uId = i;
            ori = u->a[j]>>32&1;
            start = l;
            eLen = (uint32_t)u->a[j];
			l += eLen;

            if(eLen == 0) continue;
            if(rId < read_g->r_seq)
            {
                recover_UC_Read(&g_read, &R_INF, rId);
            }
            else
            {
                recover_fake_read(&g_read, &tmp, &(read_g->F_seq[rId-read_g->r_seq]),
                &R_INF, coverage_cut);
            }

            readS = g_read.seq + coverage_cut[rId].s;
            readLen = coverage_cut[rId].e - coverage_cut[rId].s;
            
            if (!ori) // forward strand
            {
                for (k = 0; k < eLen; k++)
                {
                    u->s[start + k] = readS[k];
                }
            }
            else
            {
                for (k = 0; k < eLen; k++)
                {
                    uint8_t c = (uint8_t)readS[readLen - 1 - k];
                    u->s[start + k] = c >= 128? 'N' : comp_tab[c];
                }
            }
		}
	}

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


    uint32_t n_vtx = g->g->n_seq * 2, v, nv;
    uint32_t vLen = 0;
    asg_arc_t* av = NULL;
    for (v = 0; v < n_vtx; ++v) 
    {
        if (g->g->seq[v>>1].del) continue;
        av = asg_arc_a(g->g, v);
        nv = asg_arc_n(g->g, v);
        for (i = 0; i < nv; i++)
        {
            if(av[i].del) continue;
            vLen = g->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(E && E->a.n > 0)
    {
        asg_arc_t* p = NULL;
        for (k = 0; k < E->a.n; k++)
        {
            p = asg_arc_pushp(read_g);
            *p = E->a.a[k];
        }

        free(read_g->idx);
        read_g->idx = 0;
        read_g->is_srt = 0;
        asg_cleanup(read_g);
    }
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r9, 0x40(%rsp)
movq %r8, 0x38(%rsp)
movq %rcx, 0x30(%rsp)
movq %rdx, 0x18(%rsp)
movq %rsi, 0x8(%rsp)
movq %rdi, %r14
leaq 0x48(%rsp), %rdi
callq 0x3372c
leaq 0x68(%rsp), %rdi
callq 0x3372c
cmpq $0x0, 0x18(%r14)
jne 0xb5f25
movl $0x1, %edi
movl $0x48, %esi
callq 0x2120
movq %rax, 0x18(%r14)
cmpq $0x0, (%r14)
je 0xb5f0d
xorl %r15d, %r15d
movl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
movq 0x10(%r14), %rax
movq 0x18(%r14), %rdi
leaq (%r15,%r15,4), %rcx
movl (%rax,%rcx,8), %edx
andl %ebx, %edx
movl %r15d, %esi
xorl %ecx, %ecx
callq 0xa0115
incl %r15d
cmpq %r15, (%r14)
ja 0xb5eea
movq 0x18(%r14), %rdi
callq 0xa02fa
movq 0x18(%r14), %rax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl 0x14(%rax), %ecx
movl %ecx, 0x18(%rax)
cmpq $0x0, (%r14)
je 0xb6156
xorl %ecx, %ecx
movq %r14, 0x20(%rsp)
movq 0x18(%rsp), %rbp
movq 0x10(%r14), %rax
movq %rcx, 0x28(%rsp)
leaq (%rcx,%rcx,4), %rcx
cmpl $0x0, 0xc(%rax,%rcx,8)
je 0xb6141
leaq (%rax,%rcx,8), %r12
cmpl $0x0, 0xd0(%rsp)
je 0xb5fa2
movq %r12, %rsi
movq 0x8(%rsp), %rdx
movq 0x30(%rsp), %r8
movq 0x18(%rsp), %r9
pushq 0xc8(%rsp)
movl 0xc8(%rsp), %eax
pushq %rax
pushq 0x50(%rsp)
leaq 0x80(%rsp), %rax
pushq %rax
leaq 0x68(%rsp), %rax
pushq %rax
pushq 0x60(%rsp)
callq 0xb5b89
addq $0x30, %rsp
movl (%r12), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
movq 0x20(%rsp), %rcx
movq 0x18(%rcx), %rcx
movq 0x20(%rcx), %rcx
movq 0x28(%rsp), %rsi
movl (%rcx,%rsi,8), %edx
movl $0x80000000, %edi       # imm = 0x80000000
andl %edi, %edx
orl %eax, %edx
movl %edx, (%rcx,%rsi,8)
movl (%r12), %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
leaq 0x1(%r15), %rsi
movl $0x1, %edi
callq 0x2120
movq %rax, 0x20(%r12)
movq %rax, %rdi
movl $0x4e, %esi
movq %r15, %rdx
callq 0x20e0
cmpl $0x0, 0x10(%r12)
movq 0x8(%rsp), %r9
je 0xb6141
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x18(%r12), %rax
movq (%rax,%rbx,8), %r15
cmpq $-0x1, %r15
je 0xb605a
leal (%r14,%r15), %ecx
testl %r15d, %r15d
je 0xb612d
movl %ecx, 0x14(%rsp)
movq %r15, %r13
shrq $0x21, %r13
movl 0x18(%r9), %ecx
movl %r13d, %eax
subl %ecx, %eax
jae 0xb6066
leaq 0x48(%rsp), %rdi
leaq 0x1d51e0(%rip), %rsi    # 0x28b230
movq %r13, %rdx
callq 0x33cf8
jmp 0xb608b
addl $0x64, %r14d
movl %r14d, %ecx
jmp 0xb612d
leaq (%rax,%rax,4), %rdx
shlq $0x3, %rdx
addq 0x40(%r9), %rdx
leaq 0x48(%rsp), %rdi
leaq 0x68(%rsp), %rsi
leaq 0x1d51ad(%rip), %rcx    # 0x28b230
movq %rbp, %r8
callq 0xb21f4
leaq (,%r13,2), %rdx
addq %r13, %rdx
movl (%rbp,%rdx,4), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq 0x48(%rsp), %rax
addq %rcx, %rax
btq $0x20, %r15
jb 0xb60dc
cmpl $0x1, %r15d
adcl $0x0, %r15d
movl %r14d, %ecx
xorl %edx, %edx
movb (%rax,%rdx), %sil
movq 0x20(%r12), %rdi
leal (%rcx,%rdx), %r9d
movb %sil, (%rdi,%r9)
incq %rdx
cmpq %rdx, %r15
jne 0xb60bc
movq 0x8(%rsp), %r9
jmp 0xb6129
leaq (,%rdx,4), %rdx
addq %rbp, %rdx
notl %ecx
addl 0x4(%rdx), %ecx
cmpl $0x1, %r15d
adcl $0x0, %r15d
movq 0x8(%rsp), %r9
leaq 0x173b10(%rip), %r10    # 0x229c10
movl %ecx, %edx
movsbq (%rax,%rdx), %rdx
testq %rdx, %rdx
js 0xb6112
movb (%rdx,%r10), %dl
jmp 0xb6114
movb $0x4e, %dl
movq 0x20(%r12), %rsi
movl %r14d, %edi
movb %dl, (%rsi,%rdi)
decl %ecx
incl %r14d
decq %r15
jne 0xb6100
movl 0x14(%rsp), %ecx
incq %rbx
movl 0x10(%r12), %eax
movl %ecx, %r14d
cmpq %rax, %rbx
jb 0xb6012
movq 0x28(%rsp), %rcx
incl %ecx
movq 0x20(%rsp), %r14
cmpq %rcx, (%r14)
ja 0xb5f3b
leaq 0x48(%rsp), %rdi
callq 0x33667
leaq 0x68(%rsp), %rdi
callq 0x33667
movq 0x18(%r14), %rax
movl 0x14(%rax), %edx
addl %edx, %edx
movq 0xc8(%rsp), %r12
je 0xb61f4
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 0xb61ec
movq 0x28(%rax), %r8
movq (%r8,%rsi,8), %r9
testl %r9d, %r9d
je 0xb61ec
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 0xb61e3
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 0xb61c5
incq %rsi
cmpq %rdx, %rsi
jne 0xb618a
testq %r12, %r12
je 0xb62d1
cmpq $0x0, (%r12)
je 0xb62d1
cmpq $0x0, (%r12)
movq 0x8(%rsp), %r8
je 0xb62b1
movl $0x1, %ebx
xorl %r15d, %r15d
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl $0x10, %r14d
movl 0x4(%r8), %ecx
andl %ebp, %ecx
movl (%r8), %eax
cmpl %eax, %ecx
jne 0xb625e
leal (%rax,%rax), %ecx
testl %eax, %eax
cmovel %r14d, %ecx
movl %ecx, (%r8)
movq 0x8(%r8), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq 0x8(%rsp), %r8
movq %rax, 0x8(%r8)
movq 0x8(%r8), %rax
movl 0x4(%r8), %ecx
movl %ecx, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leal 0x1(%rcx), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %esi, %ecx
movl %ecx, 0x4(%r8)
leaq (%rdx,%rdx,2), %rcx
movq 0x10(%r12), %rdx
leaq (%r15,%r15,2), %rsi
movq 0x10(%rdx,%rsi,8), %rdi
movq %rdi, 0x10(%rax,%rcx,8)
movups (%rdx,%rsi,8), %xmm0
movups %xmm0, (%rax,%rcx,8)
movl %ebx, %r15d
incl %ebx
cmpq %r15, (%r12)
ja 0xb622b
movq 0x28(%r8), %rdi
movq %r8, %rbx
callq 0x2220
movq $0x0, 0x28(%rbx)
andb $0x7f, 0x7(%rbx)
movq %rbx, %rdi
callq 0xa02fa
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq