// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: ma_ug_seq(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(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;
    
    ///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_scaf(u, read_g, sources, coverage_cut, edge, max_hang, min_ovlp, E);
            polish_unitig_advance_scaf(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) {
            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 %r14
pushq %rbx
subq $0x100, %rsp            # imm = 0x100
movl 0x20(%rbp), %eax
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movq %rcx, -0x30(%rbp)
movq %r8, -0x38(%rbp)
movl %r9d, -0x3c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x9850
leaq -0x80(%rbp), %rdi
callq 0x9850
movq $0x0, -0xa8(%rbp)
movl $0x0, -0x84(%rbp)
movl -0x84(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpq (%rcx), %rax
jae 0xed590
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x84(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0xed1d8
jmp 0xed57c
cmpl $0x0, 0x20(%rbp)
je 0xed262
movq -0xb0(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x38(%rbp), %r8
movl -0x3c(%rbp), %r9d
movl 0x10(%rbp), %r10d
movq 0x18(%rbp), %rax
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0xeae00
movq -0xb0(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %r8
movq -0x38(%rbp), %r9
movl -0x3c(%rbp), %r11d
movl 0x10(%rbp), %r10d
movq 0x18(%rbp), %rax
leaq 0x2d8093(%rip), %rdx    # 0x3c52d0
leaq -0x60(%rbp), %r14
leaq -0x80(%rbp), %rbx
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movl %r11d, 0x10(%rsp)
movl %r10d, 0x18(%rsp)
movq %rax, 0x20(%rsp)
callq 0xec990
movq -0xb0(%rbp), %rax
movl (%rax), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0x84(%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, -0xb4(%rbp)
movq -0xb0(%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 -0xb0(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0xb0(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0xb0(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edx
movl $0x4e, %esi
callq 0x2100
movl $0x0, -0x88(%rbp)
movl -0x88(%rbp), %eax
movq -0xb0(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0xed57a
movq -0xb0(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x88(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, -0x90(%rbp)
movq -0xb0(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x88(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
andq $0x1, %rax
movl %eax, -0x94(%rbp)
movl -0xb4(%rbp), %eax
movl %eax, -0x98(%rbp)
movq -0xb0(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x88(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
addl -0xb4(%rbp), %eax
movl %eax, -0xb4(%rbp)
cmpl $0x0, -0x9c(%rbp)
jne 0xed39d
jmp 0xed566
movl -0x90(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jae 0xed3c6
movl -0x90(%rbp), %eax
movl %eax, %edx
leaq -0x60(%rbp), %rdi
leaq 0x2d7f11(%rip), %rsi    # 0x3c52d0
callq 0xa380
jmp 0xed3fc
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rdx
movl -0x90(%rbp), %eax
movq -0x20(%rbp), %rcx
subl 0x18(%rcx), %eax
movl %eax, %eax
imulq $0x28, %rax, %rax
addq %rax, %rdx
movq -0x28(%rbp), %r8
leaq -0x60(%rbp), %rdi
leaq -0x80(%rbp), %rsi
leaq 0x2d7ed9(%rip), %rcx    # 0x3c52d0
callq 0xe8350
movq -0x60(%rbp), %rax
movq -0x28(%rbp), %rcx
movl -0x90(%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, -0xa8(%rbp)
movq -0x28(%rbp), %rax
movl -0x90(%rbp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x28(%rbp), %rcx
movl -0x90(%rbp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, -0xa0(%rbp)
cmpl $0x0, -0x94(%rbp)
jne 0xed4be
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
cmpl -0x9c(%rbp), %eax
jae 0xed4b9
movq -0xa8(%rbp), %rax
movl -0x8c(%rbp), %ecx
movb (%rax,%rcx), %dl
movq -0xb0(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x98(%rbp), %ecx
addl -0x8c(%rbp), %ecx
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0xed46e
jmp 0xed564
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
cmpl -0x9c(%rbp), %eax
jae 0xed562
movq -0xa8(%rbp), %rax
movl -0xa0(%rbp), %ecx
subl $0x1, %ecx
subl -0x8c(%rbp), %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %al
movb %al, -0xb5(%rbp)
movzbl -0xb5(%rbp), %eax
cmpl $0x80, %eax
jl 0xed513
movb $0x4e, %al
movb %al, -0xd9(%rbp)
jmp 0xed52c
movzbl -0xb5(%rbp), %eax
movl %eax, %ecx
leaq 0x2d723d(%rip), %rax    # 0x3c4760
movb (%rax,%rcx), %al
movb %al, -0xd9(%rbp)
movb -0xd9(%rbp), %dl
movq -0xb0(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x98(%rbp), %ecx
addl -0x8c(%rbp), %ecx
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0xed4c8
jmp 0xed564
jmp 0xed566
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0xed2fe
jmp 0xed57c
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0xed197
leaq -0x60(%rbp), %rdi
callq 0x9670
leaq -0x80(%rbp), %rdi
callq 0x9670
movq -0x18(%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 0xed78c
movq -0x18(%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 0xed611
jmp 0xed778
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rax
movq -0x18(%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 -0x18(%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, -0x84(%rbp)
movl -0x84(%rbp), %eax
cmpl -0xc4(%rbp), %eax
jae 0xed776
movq -0xd0(%rbp), %rax
movl -0x84(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0xed6a1
jmp 0xed762
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movq -0xd0(%rbp), %rcx
movl -0x84(%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 -0x84(%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 -0x84(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
shrq $0x20, %rcx
shlq $0x20, %rcx
movq -0xd0(%rbp), %rax
movl -0x84(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0xd0(%rbp), %rax
movl -0x84(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movl -0xc8(%rbp), %eax
orq %rax, %rcx
movq -0xd0(%rbp), %rax
movl -0x84(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0xed66b
jmp 0xed778
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0xed5d9
cmpq $0x0, 0x18(%rbp)
je 0xed851
movq 0x18(%rbp), %rax
cmpq $0x0, (%rax)
jbe 0xed851
movq $0x0, -0xd8(%rbp)
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
movq 0x18(%rbp), %rcx
cmpq (%rcx), %rax
jae 0xed81c
movq -0x20(%rbp), %rdi
callq 0xd4050
movq %rax, -0xd8(%rbp)
movq 0x18(%rbp), %rax
movq 0x10(%rax), %rcx
movl -0x8c(%rbp), %eax
imulq $0x18, %rax, %rax
addq %rax, %rcx
movq -0xd8(%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 -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0xed7ba
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x2210
movq -0x20(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x20(%rbp), %rax
movl 0x4(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x0, %ecx
movl %ecx, 0x4(%rax)
movq -0x20(%rbp), %rdi
callq 0xcdd10
xorl %eax, %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbx
popq %r14
popq %rbp
retq
nop