// File: /workspace/github_cpp_makefile_star2_O2/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, %r15
leaq 0x48(%rsp), %rdi
callq 0x6bd1
leaq 0x68(%rsp), %rdi
callq 0x6bd1
cmpq $0x0, 0x18(%r15)
jne 0x7bf30
callq 0x68f5d
movq %rax, 0x18(%r15)
xorl %eax, %eax
movl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
movl %eax, %r14d
movq 0x18(%r15), %rdi
cmpq %r14, (%r15)
jbe 0x7bf1c
movq 0x10(%r15), %rax
imulq $0x28, %r14, %rcx
movl (%rax,%rcx), %edx
andl %ebx, %edx
movl %r14d, %esi
xorl %ecx, %ecx
callq 0x69316
leal 0x1(%r14), %eax
jmp 0x7bef3
callq 0x694eb
movq 0x18(%r15), %rax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl 0x14(%rax), %ecx
movl %ecx, 0x18(%rax)
xorl %eax, %eax
movq %r15, 0x20(%rsp)
movl %eax, %eax
cmpq %rax, (%r15)
jbe 0x7c135
movq 0x10(%r15), %r12
movq %rax, 0x28(%rsp)
imulq $0x28, %rax, %rax
cmpl $0x0, 0xc(%r12,%rax)
je 0x7c124
addq %rax, %r12
cmpl $0x0, 0xd0(%rsp)
je 0x7bfa9
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 0x7bbe3
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), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
leaq 0x1(%r14), %rsi
pushq $0x1
popq %rdi
callq 0x2120
movq %rax, 0x20(%r12)
movq %rax, %rdi
pushq $0x4e
popq %rsi
movq %r14, %rdx
callq 0x20e0
xorl %ebx, %ebx
xorl %ebp, %ebp
movq 0x8(%rsp), %r9
movq 0x18(%rsp), %r15
movl 0x10(%r12), %eax
cmpq %rax, %rbx
jae 0x7c124
movq 0x18(%r12), %rax
movq (%rax,%rbx,8), %r14
cmpq $-0x1, %r14
je 0x7c063
leal (%r14,%rbp), %eax
testl %r14d, %r14d
je 0x7c11a
movl %eax, 0x14(%rsp)
movq %r14, %r13
shrq $0x21, %r13
movl 0x18(%r9), %ecx
movl %r13d, %eax
subl %ecx, %eax
jae 0x7c06d
leaq 0x48(%rsp), %rdi
leaq 0x1d81d7(%rip), %rsi    # 0x254230
movq %r13, %rdx
callq 0x715e
jmp 0x7c08e
addl $0x64, %ebp
movl %ebp, %eax
jmp 0x7c11a
imulq $0x28, %rax, %rdx
addq 0x40(%r9), %rdx
leaq 0x48(%rsp), %rdi
leaq 0x68(%rsp), %rsi
leaq 0x1d81aa(%rip), %rcx    # 0x254230
movq %r15, %r8
callq 0x786c9
imulq $0xc, %r13, %rdx
movl (%r15,%rdx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq 0x48(%rsp), %rax
addq %rcx, %rax
btq $0x20, %r14
jb 0x7c0cd
movl %r14d, %ecx
movl %ebp, %edx
xorl %esi, %esi
cmpq %rsi, %rcx
je 0x7c111
movb (%rax,%rsi), %dil
movq 0x20(%r12), %r10
leal (%rdx,%rsi), %r9d
movb %dil, (%r10,%r9)
incq %rsi
jmp 0x7c0b2
addq %r15, %rdx
notl %ecx
addl 0x4(%rdx), %ecx
movl %r14d, %edx
movq 0x8(%rsp), %r9
leaq 0x16bc1c(%rip), %r11    # 0x1e7d00
subq $0x1, %rdx
jb 0x7c116
movl %ecx, %esi
movsbq (%rax,%rsi), %rsi
testq %rsi, %rsi
js 0x7c0fc
movb (%rsi,%r11), %sil
jmp 0x7c0ff
movb $0x4e, %sil
movq 0x20(%r12), %rdi
movl %ebp, %r10d
movb %sil, (%rdi,%r10)
incl %ebp
decl %ecx
jmp 0x7c0e4
movq 0x8(%rsp), %r9
movl 0x14(%rsp), %eax
incq %rbx
movl %eax, %ebp
jmp 0x7c00d
movq 0x28(%rsp), %rax
incl %eax
movq 0x20(%rsp), %r15
jmp 0x7bf37
leaq 0x48(%rsp), %rdi
callq 0x6b07
leaq 0x68(%rsp), %rdi
callq 0x6b07
movq 0x18(%r15), %rax
movl 0x14(%rax), %ecx
addl %ecx, %ecx
xorl %edx, %edx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
cmpq %rcx, %rdx
je 0x7c1bb
movq 0x20(%rax), %rdi
movl %edx, %r8d
shrl %r8d
cmpl $0x0, (%rdi,%r8,8)
js 0x7c1b6
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 0x7c1b6
movl 0xc(%r8,%r10), %r11d
testl %r11d, %r11d
js 0x7c1b0
movq (%r8,%r10), %rbx
shrq $0x21, %rbx
movl (%rdi,%rbx,8), %ebx
andl %esi, %ebx
subl %r11d, %ebx
movl %ebx, (%r8,%r10)
addq $0x18, %r10
jmp 0x7c18d
incq %rdx
jmp 0x7c159
movq 0xc8(%rsp), %rax
testq %rax, %rax
je 0x7c22b
movq (%rax), %rax
testq %rax, %rax
je 0x7c22b
xorl %ecx, %ecx
movl %ecx, %ebx
cmpq %rbx, %rax
movq 0x8(%rsp), %rax
jbe 0x7c20e
movq %rax, %rdi
callq 0x6c45d
movq 0xc8(%rsp), %rdi
movq 0x10(%rdi), %rcx
imulq $0x18, %rbx, %rdx
movq 0x10(%rcx,%rdx), %rsi
movq %rsi, 0x10(%rax)
movups (%rcx,%rdx), %xmm0
movups %xmm0, (%rax)
leal 0x1(%rbx), %ecx
movq (%rdi), %rax
jmp 0x7c1d2
movq 0x28(%rax), %rdi
movq %rax, %rbx
callq 0x2220
andq $0x0, 0x28(%rbx)
andb $0x7f, 0x7(%rbx)
movq %rbx, %rdi
callq 0x694eb
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq