// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movl 0x20(%rbp), %eax
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x20(%rbp)
movq %rsi, -0x28(%rbp)
movq %rdx, -0x30(%rbp)
movq %rcx, -0x38(%rbp)
movq %r8, -0x40(%rbp)
movl %r9d, -0x44(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x9850
leaq -0x88(%rbp), %rdi
callq 0x9850
movq $0x0, -0xb0(%rbp)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0xedcf6
callq 0xcd150
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x18(%rax)
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpq (%rcx), %rax
jae 0xedccd
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rdi
movl -0x8c(%rbp), %esi
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x8c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
callq 0xcd8f0
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0xedc7b
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0xcdd10
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movl %ecx, 0x18(%rax)
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpq (%rcx), %rax
jae 0xee101
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x8c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0xedd41
jmp 0xee0ed
cmpl $0x0, 0x20(%rbp)
je 0xedda4
movl -0x8c(%rbp), %edi
movq -0xb8(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x38(%rbp), %r8
movq -0x30(%rbp), %r9
movq -0x40(%rbp), %r15
movl -0x44(%rbp), %r11d
movl 0x10(%rbp), %r10d
movq 0x18(%rbp), %rax
leaq 0x2d7559(%rip), %rcx    # 0x3c52d0
leaq -0x68(%rbp), %r14
leaq -0x88(%rbp), %rbx
movq %r15, (%rsp)
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movl %r11d, 0x18(%rsp)
movl %r10d, 0x20(%rsp)
movq %rax, 0x28(%rsp)
callq 0xed860
movq -0xb8(%rbp), %rax
movl (%rax), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0x8c(%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 $0x0, -0xbc(%rbp)
movq -0xb8(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addl $0x1, %eax
movslq %eax, %rsi
movl $0x1, %edi
callq 0x2140
movq %rax, %rcx
movq -0xb8(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0xb8(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0xb8(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edx
movl $0x4e, %esi
callq 0x2100
movl $0x0, -0x90(%rbp)
movl -0x90(%rbp), %eax
movq -0xb8(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0xee0eb
movq -0xb8(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x90(%rbp), %ecx
cmpq $-0x1, (%rax,%rcx,8)
jne 0xede82
movl -0xbc(%rbp), %eax
addl $0x64, %eax
movl %eax, -0xbc(%rbp)
jmp 0xee0d7
movq -0xb8(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x90(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, -0x98(%rbp)
movq -0xb8(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x90(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
andq $0x1, %rax
movl %eax, -0x9c(%rbp)
movl -0xbc(%rbp), %eax
movl %eax, -0xa0(%rbp)
movq -0xb8(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x90(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
addl -0xbc(%rbp), %eax
movl %eax, -0xbc(%rbp)
cmpl $0x0, -0xa4(%rbp)
jne 0xedf0b
jmp 0xee0d7
movl -0x98(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jae 0xedf34
movl -0x98(%rbp), %eax
movl %eax, %edx
leaq -0x68(%rbp), %rdi
leaq 0x2d73a3(%rip), %rsi    # 0x3c52d0
callq 0xa380
jmp 0xedf6d
movq -0x28(%rbp), %rax
movq 0x40(%rax), %rdx
movl -0x98(%rbp), %eax
movq -0x28(%rbp), %rcx
subl 0x18(%rcx), %eax
movl %eax, %eax
imulq $0x28, %rax, %rax
addq %rax, %rdx
movq -0x30(%rbp), %r8
leaq -0x68(%rbp), %rdi
leaq -0x88(%rbp), %rsi
leaq 0x2d7368(%rip), %rcx    # 0x3c52d0
callq 0xe8350
movq -0x68(%rbp), %rax
movq -0x30(%rbp), %rcx
movl -0x98(%rbp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0xb0(%rbp)
movq -0x30(%rbp), %rax
movl -0x98(%rbp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x30(%rbp), %rcx
movl -0x98(%rbp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, -0xa8(%rbp)
cmpl $0x0, -0x9c(%rbp)
jne 0xee02f
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpl -0xa4(%rbp), %eax
jae 0xee02a
movq -0xb0(%rbp), %rax
movl -0x94(%rbp), %ecx
movb (%rax,%rcx), %dl
movq -0xb8(%rbp), %rax
movq 0x20(%rax), %rax
movl -0xa0(%rbp), %ecx
addl -0x94(%rbp), %ecx
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0xedfdf
jmp 0xee0d5
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpl -0xa4(%rbp), %eax
jae 0xee0d3
movq -0xb0(%rbp), %rax
movl -0xa8(%rbp), %ecx
subl $0x1, %ecx
subl -0x94(%rbp), %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %al
movb %al, -0xbd(%rbp)
movzbl -0xbd(%rbp), %eax
cmpl $0x80, %eax
jl 0xee084
movb $0x4e, %al
movb %al, -0xe1(%rbp)
jmp 0xee09d
movzbl -0xbd(%rbp), %eax
movl %eax, %ecx
leaq 0x2d66cc(%rip), %rax    # 0x3c4760
movb (%rax,%rcx), %al
movb %al, -0xe1(%rbp)
movb -0xe1(%rbp), %dl
movq -0xb8(%rbp), %rax
movq 0x20(%rax), %rax
movl -0xa0(%rbp), %ecx
addl -0x94(%rbp), %ecx
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0xee039
jmp 0xee0d5
jmp 0xee0d7
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0xede40
jmp 0xee0ed
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0xedd00
leaq -0x68(%rbp), %rdi
callq 0x9670
leaq -0x88(%rbp), %rdi
callq 0x9670
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0xc4(%rbp)
movl $0x0, -0xd0(%rbp)
movq $0x0, -0xd8(%rbp)
movl $0x0, -0xc8(%rbp)
movl -0xc8(%rbp), %eax
cmpl -0xc4(%rbp), %eax
jae 0xee300
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0xc8(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0xee185
jmp 0xee2ec
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq 0x28(%rcx), %rcx
movl -0xc8(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xd8(%rbp)
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x28(%rax), %rax
movl -0xc8(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0xcc(%rbp)
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
cmpl -0xcc(%rbp), %eax
jae 0xee2ea
movq -0xd8(%rbp), %rax
movl -0x8c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0xee215
jmp 0xee2d6
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movq -0xd8(%rbp), %rcx
movl -0x8c(%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 -0xd8(%rbp), %rcx
movl -0x8c(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, -0xd0(%rbp)
movq -0xd8(%rbp), %rax
movl -0x8c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
shrq $0x20, %rcx
shlq $0x20, %rcx
movq -0xd8(%rbp), %rax
movl -0x8c(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0xd8(%rbp), %rax
movl -0x8c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movl -0xd0(%rbp), %eax
orq %rax, %rcx
movq -0xd8(%rbp), %rax
movl -0x8c(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0xee1df
jmp 0xee2ec
movl -0xc8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc8(%rbp)
jmp 0xee14d
cmpq $0x0, 0x18(%rbp)
je 0xee3c5
movq 0x18(%rbp), %rax
cmpq $0x0, (%rax)
jbe 0xee3c5
movq $0x0, -0xe0(%rbp)
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
movq 0x18(%rbp), %rcx
cmpq (%rcx), %rax
jae 0xee390
movq -0x28(%rbp), %rdi
callq 0xd4050
movq %rax, -0xe0(%rbp)
movq 0x18(%rbp), %rax
movq 0x10(%rax), %rcx
movl -0x94(%rbp), %eax
imulq $0x18, %rax, %rax
addq %rax, %rcx
movq -0xe0(%rbp), %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rcx
movq %rcx, 0x10(%rax)
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0xee32e
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x2210
movq -0x28(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x28(%rbp), %rax
movl 0x4(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x0, %ecx
movl %ecx, 0x4(%rax)
movq -0x28(%rbp), %rdi
callq 0xcdd10
xorl %eax, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nopw %cs:(%rax,%rax)