// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: append_rId_to_Unitig(asg_t*, ma_ug_t*, unsigned int, unsigned int, ma_hit_t_alloc*, ma_sub_t*, R_to_U*, int, int)
void append_rId_to_Unitig(asg_t *r_g, ma_ug_t* ug, uint32_t uId, uint32_t rId,
ma_hit_t_alloc* sources, ma_sub_t *coverage_cut, R_to_U* ruIndex, int max_hang, int min_ovlp)
{
    asg_arc_t t;
    t.ul = (uint64_t)-1;
    ma_utg_t *nsu = &(ug->u.a[uId>>1]);
    uint32_t l, i, beg, end;

    if(nsu->m < (nsu->n+1))
    {
        nsu->m = nsu->n+1;
        nsu->a = (uint64_t*)realloc(nsu->a, nsu->m*sizeof(uint64_t));
    }

    if((uId&1) == 0)
    {
        beg = nsu->end^1;
        end = rId;
    }
    
    if((uId&1) == 1)
    {
        beg = rId^1;
        end = nsu->start;
    }

    ///t is the edge that from beg to end
    get_edge_from_source(sources, coverage_cut, ruIndex, max_hang, min_ovlp, beg, 
    end, &t);

    ///add rId to the end of nsu
    if((uId&1) == 0)
    {
        nsu->len -= (uint32_t)nsu->a[nsu->n-1];

        l = r_g->seq[rId>>1].len;
        nsu->a[nsu->n] = rId;
        nsu->a[nsu->n] <<=32;
        nsu->a[nsu->n] = nsu->a[nsu->n] | (uint64_t)(l);

        l = asg_arc_len(t);
        nsu->a[nsu->n-1] = nsu->a[nsu->n-1]>>32;
        nsu->a[nsu->n-1] = nsu->a[nsu->n-1]<<32;
        nsu->a[nsu->n-1] = nsu->a[nsu->n-1] | (uint64_t)(l);

        nsu->len = nsu->len + (uint32_t)nsu->a[nsu->n-1] + (uint32_t)nsu->a[nsu->n];
        nsu->end = rId^1;
    
        nsu->n++;
    }

    ///add rId to the start of nsu
    if((uId&1) == 1)
    {
        rId = rId^1;
        for(i = nsu->n; i >= 1; i--)
        {
            nsu->a[i] = nsu->a[i-1];
        }
        
        l = asg_arc_len(t);
        nsu->a[0] = rId;
        nsu->a[0] = nsu->a[0]<<32;
        nsu->a[0] = nsu->a[0] | (uint64_t)(l);
        nsu->len = nsu->len + (uint32_t)nsu->a[0];
        nsu->start = rId;

        nsu->n++;
    }


    set_R_to_U(ruIndex, rId>>1, uId>>1, 1, &(r_g->seq[rId>>1].c));




    /*************************just for debug**************************/
    uint32_t v, totalLen = 0;
    v = (uint64_t)(nsu->a[0])>>32;
    if(nsu->start != UINT32_MAX && nsu->start != v) fprintf(stderr, "hehe\n");

    v = (uint64_t)(nsu->a[nsu->n-1])>>32;
    if(nsu->end != UINT32_MAX && nsu->end != (v^1)) fprintf(stderr, "haha\n");

    for (i = 0; i < nsu->n; i++)
    {
        l = (uint32_t)(nsu->a[i]);
        totalLen = totalLen + l;
    }
    if(totalLen != nsu->len) fprintf(stderr, "xxxx\n");

    ////fprintf(stderr, "utg%.6dl, dir: %u\n", uId+1, uId&1);
    /*************************just for debug**************************/

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movl 0x20(%rbp), %eax
movl 0x18(%rbp), %eax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movq %r8, -0x20(%rbp)
movq %r9, -0x28(%rbp)
movq $-0x1, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x14(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movl 0xc(%rax), %eax
movq -0x48(%rbp), %rcx
movl 0x10(%rcx), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jae 0x150bc5
movq -0x48(%rbp), %rax
movl 0x10(%rax), %ecx
addl $0x1, %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x48(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, %esi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x18(%rax)
movl -0x14(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x150be3
movq -0x48(%rbp), %rax
movl 0x8(%rax), %eax
xorl $0x1, %eax
movl %eax, -0x54(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x14(%rbp), %eax
andl $0x1, %eax
cmpl $0x1, %eax
jne 0x150c01
movl -0x18(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x54(%rbp)
movq -0x48(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x58(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq 0x10(%rbp), %rdx
movl 0x18(%rbp), %ecx
movl 0x20(%rbp), %r8d
movl -0x54(%rbp), %r9d
movl -0x58(%rbp), %r10d
leaq -0x40(%rbp), %rax
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0xe9560
movl -0x14(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x150e00
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x48(%rbp), %rcx
movl 0x10(%rcx), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movl %eax, %ecx
movq -0x48(%rbp), %rax
movl (%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
subl %ecx, %edx
movl (%rax), %ecx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %edx, %ecx
movl %ecx, (%rax)
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x18(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x4c(%rbp)
movl -0x18(%rbp), %eax
movl %eax, %edx
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x48(%rbp), %rcx
movl 0x10(%rcx), %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x48(%rbp), %rcx
movl 0x10(%rcx), %ecx
movq (%rax,%rcx,8), %rdx
shlq $0x20, %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x48(%rbp), %rcx
movl 0x10(%rcx), %ecx
movq (%rax,%rcx,8), %rdx
movl -0x4c(%rbp), %eax
orq %rax, %rdx
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x48(%rbp), %rcx
movl 0x10(%rcx), %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x40(%rbp), %rax
movl %eax, -0x4c(%rbp)
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x48(%rbp), %rcx
movl 0x10(%rcx), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
shrq $0x20, %rdx
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x48(%rbp), %rcx
movl 0x10(%rcx), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x48(%rbp), %rcx
movl 0x10(%rcx), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
shlq $0x20, %rdx
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x48(%rbp), %rcx
movl 0x10(%rcx), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x48(%rbp), %rcx
movl 0x10(%rcx), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
movl -0x4c(%rbp), %eax
orq %rax, %rdx
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x48(%rbp), %rcx
movl 0x10(%rcx), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x48(%rbp), %rax
movl (%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x48(%rbp), %rcx
movl 0x10(%rcx), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
addl %eax, %edx
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x48(%rbp), %rcx
movl 0x10(%rcx), %ecx
movq (%rax,%rcx,8), %rax
addl %eax, %edx
movq -0x48(%rbp), %rax
movl (%rax), %ecx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %edx, %ecx
movl %ecx, (%rax)
movl -0x18(%rbp), %ecx
xorl $0x1, %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x48(%rbp), %rax
movl 0x10(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x10(%rax)
movl -0x14(%rbp), %eax
andl $0x1, %eax
cmpl $0x1, %eax
jne 0x150ee9
movl -0x18(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x18(%rbp)
movq -0x48(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x50(%rbp)
cmpl $0x1, -0x50(%rbp)
jb 0x150e56
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x50(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x50(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movl -0x50(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x150e22
movq -0x40(%rbp), %rax
movl %eax, -0x4c(%rbp)
movl -0x18(%rbp), %eax
movl %eax, %ecx
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq %rcx, (%rax)
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rcx
shlq $0x20, %rcx
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq %rcx, (%rax)
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rcx
movl -0x4c(%rbp), %eax
orq %rax, %rcx
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq %rcx, (%rax)
movq -0x48(%rbp), %rax
movl (%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
addl %eax, %edx
movq -0x48(%rbp), %rax
movl (%rax), %ecx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %edx, %ecx
movl %ecx, (%rax)
movl -0x18(%rbp), %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x48(%rbp), %rax
movl 0x10(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x10(%rax)
movq 0x10(%rbp), %rdi
movl -0x18(%rbp), %esi
shrl %esi
movl -0x14(%rbp), %edx
shrl %edx
movq -0x8(%rbp), %rax
movq 0x20(%rax), %r8
movl -0x18(%rbp), %eax
shrl %eax
movl %eax, %eax
shlq $0x3, %rax
addq %rax, %r8
addq $0x4, %r8
movl $0x1, %ecx
callq 0xd0e20
movl $0x0, -0x60(%rbp)
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, -0x5c(%rbp)
movq -0x48(%rbp), %rax
cmpl $-0x1, 0x4(%rax)
je 0x150f62
movq -0x48(%rbp), %rax
movl 0x4(%rax), %eax
cmpl -0x5c(%rbp), %eax
je 0x150f62
movq 0x27308f(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x238328(%rip), %rsi    # 0x389283
movb $0x0, %al
callq 0x22a0
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x48(%rbp), %rcx
movl 0x10(%rcx), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x5c(%rbp)
movq -0x48(%rbp), %rax
cmpl $-0x1, 0x8(%rax)
je 0x150fb4
movq -0x48(%rbp), %rax
movl 0x8(%rax), %eax
movl -0x5c(%rbp), %ecx
xorl $0x1, %ecx
cmpl %ecx, %eax
je 0x150fb4
movq 0x27303d(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x2382dc(%rip), %rsi    # 0x389289
movb $0x0, %al
callq 0x22a0
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x150fed
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x50(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x4c(%rbp)
movl -0x60(%rbp), %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x60(%rbp)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x150fbb
movl -0x60(%rbp), %eax
movq -0x48(%rbp), %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
je 0x151018
movq 0x272fd9(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x23896f(%rip), %rsi    # 0x389980
movb $0x0, %al
callq 0x22a0
addq $0x70, %rsp
popq %rbp
retq
nop