// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, %r14
movq %r8, %r15
movl %ecx, %ebx
movl %edx, %ebp
movq %rdi, (%rsp)
movl 0x80(%rsp), %r12d
movl 0x78(%rsp), %ecx
movq 0x70(%rsp), %rax
movq %rax, 0x18(%rsp)
orq $-0x1, 0x20(%rsp)
movq 0x10(%rsi), %rsi
movl %edx, %eax
shrl %eax
movq %rax, 0x10(%rsp)
imulq $0x28, %rax, %rdx
leaq (%rsi,%rdx), %r13
movl 0x10(%rsi,%rdx), %eax
incl %eax
cmpl %eax, 0xc(%rsi,%rdx)
jae 0xb4e55
movl %eax, 0xc(%r13)
movq 0x18(%r13), %rdi
movl %eax, %esi
shlq $0x3, %rsi
movq %rbx, 0x8(%rsp)
movl %ecx, %ebx
callq 0x2370
movl %ebx, %ecx
movq 0x8(%rsp), %rbx
movq %rax, 0x18(%r13)
testb $0x1, %bpl
jne 0xb4e63
movl 0x8(%r13), %r9d
movl %ebx, %eax
jmp 0xb4e6a
movl 0x4(%r13), %eax
movl %ebx, %r9d
xorl $0x1, %r9d
leaq 0x20(%rsp), %rdx
movq %r15, %rdi
movq %r14, %rsi
movl %r12d, %r8d
pushq %rdx
pushq %rax
callq 0x79204
popq %rcx
popq %rdx
testb $0x1, %bpl
jne 0xb4f0a
movq 0x18(%r13), %rdx
movl (%r13), %ecx
movl 0x10(%r13), %eax
leal -0x1(%rax), %r8d
movl %ecx, %r9d
subl (%rdx,%r8,8), %r9d
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %r9d
movl $0x80000000, %edi       # imm = 0x80000000
andl %edi, %ecx
orl %r9d, %ecx
movl %ecx, (%r13)
movq (%rsp), %r14
movq 0x20(%r14), %r9
movl %ebx, %ecx
shrl %ecx
movl (%r9,%rcx,8), %r9d
movl %r9d, %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movq %rbx, %r11
shlq $0x20, %r11
orq %r10, %r11
movq %r11, (%rdx,%rax,8)
movl 0x20(%rsp), %r10d
movl %r10d, (%rdx,%r8,8)
movl (%r13), %edx
addl %r10d, %r9d
addl %edx, %r9d
andl %esi, %r9d
andl %edi, %edx
orl %r9d, %edx
movl %edx, (%r13)
xorl $0x1, %ebx
movl %ebx, 0x8(%r13)
movl %ecx, %ebx
jmp 0xb4f70
movl 0x10(%r13), %eax
movq %rax, %rcx
decq %rcx
movq (%rsp), %r14
cmpq $-0x1, %rcx
je 0xb4f32
movq 0x18(%r13), %rdx
movl %ecx, %esi
movq (%rdx,%rsi,8), %rsi
movq %rsi, 0x8(%rdx,%rcx,8)
decq %rcx
jmp 0xb4f18
movl %ebx, %ecx
xorl $0x1, %ecx
movq 0x20(%rsp), %rdx
movl %edx, %esi
movq 0x18(%r13), %rdi
movq %rcx, %r8
shlq $0x20, %r8
orq %rsi, %r8
movq %r8, (%rdi)
movl (%r13), %esi
addl %esi, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl $0x80000000, %esi       # imm = 0x80000000
orl %edx, %esi
movl %esi, (%r13)
movl %ecx, 0x4(%r13)
shrl %ebx
movq %rbx, %rcx
incl %eax
movl %eax, 0x10(%r13)
movq 0x20(%r14), %rax
leaq (%rax,%rcx,8), %r8
addq $0x4, %r8
pushq $0x1
popq %rcx
movq 0x18(%rsp), %rdi
movl %ebx, %esi
movq 0x10(%rsp), %rdx
callq 0x6ab56
movl 0x4(%r13), %eax
cmpl $-0x1, %eax
je 0xb4fa8
movq 0x18(%r13), %rcx
cmpl 0x4(%rcx), %eax
jne 0xb5022
movl 0x8(%r13), %ecx
movl 0x10(%r13), %eax
cmpl $-0x1, %ecx
je 0xb4fc5
movq 0x18(%r13), %rdx
leal -0x1(%rax), %esi
xorl 0x4(%rdx,%rsi,8), %ecx
cmpl $0x1, %ecx
jne 0xb5043
movl %eax, %eax
xorl %edx, %edx
xorl %ecx, %ecx
cmpq %rdx, %rax
je 0xb4fdc
movq 0x18(%r13), %rsi
addl (%rsi,%rdx,8), %ecx
incq %rdx
jmp 0xb4fcb
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl (%r13), %eax
cmpl %eax, %ecx
jne 0xb4ff8
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x19dfe1(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0x135c4c(%rip), %rdi    # 0x1eac55
pushq $0x5
popq %rsi
pushq $0x1
popq %rdx
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2410
movq 0x19dfb7(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0x135525(%rip), %rdi    # 0x1ea558
pushq $0x5
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
jmp 0xb4fa8
movq 0x19df96(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0x13550a(%rip), %rdi    # 0x1ea55e
pushq $0x5
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
movl 0x10(%r13), %eax
jmp 0xb4fc5