// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: create_fake_read(asg_t*, asg_arc_t*, unsigned int)
uint32_t create_fake_read(asg_t *r_g, asg_arc_t* list, uint32_t list_n)
{
    if(list_n < 2) return 0;
    ma_utg_t* u = NULL;
    uint32_t v, w, l, i, k, nv, totalLen = 0;
    asg_arc_t *av = NULL;
    u = asg_F_seq_set(r_g, r_g->n_seq);
    if(u == NULL) return 0;
    u->n = u->m = list_n - 1;
    if(u->a) free(u->a);
    u->a = (uint64_t*)malloc(sizeof(uint64_t)*u->n);

    /*****************for debug**********************/
    // fprintf(stderr, "list_n: %u\n", list_n);
    // for (i = 0; i < list_n; i++)
    // {
    //     fprintf(stderr, "(%u) v: %u, dir: %u, w: %u, w&1: %u, len: %u, ol: %u\n", 
    //             i, (uint32_t)(list[i].ul>>33), (uint32_t)((list[i].ul>>32)&1), 
    //             list[i].v>>1, list[i].v&1, (uint32_t)list[i].ul, list[i].ol);
    // }
    /*****************for debug**********************/


    for (i = 0; i < u->n; i++)
    {
        u->a[i] = list[i].v;
        u->a[i] = u->a[i] << 32;
    }

    /*****************for debug**********************/
    // fprintf(stderr, "u->n: %u\n", u->n);
    // for (i = 0; i < u->n; i++)
    // {
    //     fprintf(stderr, "(%u) v: %u, dir: %u\n", 
    //                 i, (uint32_t)(u->a[i]>>33), (uint32_t)((u->a[i]>>32)&1));
    // }
    /*****************for debug**********************/
    
    
    for (i = 0; i < u->n - 1; i++)
    {
        v = (uint64_t)(u->a[i])>>32;
        w = (uint64_t)(u->a[i + 1])>>32; 
        av = asg_arc_a(r_g, v);
        nv = asg_arc_n(r_g, v);
        l = 0;
        
        for (k = 0; k < nv; k++)
        {
            if(av[k].del) continue;
            if(av[k].v == w) 
            {
                l = asg_arc_len(av[k]);
                break;
            }
        }

        if(k == nv) fprintf(stderr, "####ERROR\n");
        u->a[i] = v; u->a[i] = u->a[i]<<32; u->a[i] = u->a[i] | (uint64_t)(l);
        totalLen += l;
    }

    if(i < u->n)
    {
        v = (uint64_t)(u->a[i])>>32;
        l = r_g->seq[v>>1].len;
        u->a[i] = v;
        u->a[i] = u->a[i]<<32;
        u->a[i] = u->a[i] | (uint64_t)(l);
        totalLen += l;
    }

    /*****************for debug**********************/
    // fprintf(stderr, "u->n: %u\n", u->n);
    // for (i = 0; i < u->n; i++)
    // {
    //     fprintf(stderr, "(%u) v: %u, dir: %u, len: %u\n", 
    //                 i, (uint32_t)(u->a[i]>>33), (uint32_t)((u->a[i]>>32)&1), (uint32_t)(u->a[i]));
    // }
    /*****************for debug**********************/

    

    u->len = totalLen;
    u->circ = 0;
    u->start = list[0].ol;
    u->end = u->len - list[list_n - 1].ol;
    totalLen = u->len - list[0].ol - list[list_n - 1].ol;
    ///u->len = totalLen;

    /*****************for debug**********************/
    // fprintf(stderr, "u->len: %u, u->start: %u, u->end: %u\n", u->len, u->start, u->end);
    // fprintf(stderr, "totalLen: %u, list[0].ol: %u, list[list_n - 1].ol: %u\n", 
    // totalLen, list[0].ol, list[list_n - 1].ol);
    /*****************for debug**********************/

    /*****************for debug**********************/
    /**
    (*coverage_cut) = (ma_sub_t*)realloc((*coverage_cut), (r_g->n_seq+1)*sizeof(ma_sub_t));
    (*coverage_cut)[r_g->n_seq].del = 0;
    (*coverage_cut)[r_g->n_seq].c = PRIMARY_LABLE;
    (*coverage_cut)[r_g->n_seq].s = 0;
    (*coverage_cut)[r_g->n_seq].e = totalLen;
    **/
    asg_seq_set(r_g, r_g->n_seq, totalLen, 0);
    /*****************for debug**********************/
    return 1;     
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
xorl %r12d, %r12d
cmpl $0x2, %edx
jb 0xfd312
movl %edx, %r14d
movq %rsi, %rbp
movq %rdi, %rbx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl 0x14(%rdi), %esi
callq 0xa01c8
testq %rax, %rax
je 0xfd312
movq %rax, %r15
decl %r14d
movl %r14d, 0xc(%rax)
movl %r14d, 0xc(%rsp)
movl %r14d, 0x10(%rax)
movq 0x18(%rax), %rdi
testq %rdi, %rdi
je 0xfd18e
callq 0x2220
movl 0x10(%r15), %r14d
leaq (,%r14,8), %rdi
callq 0x2280
movq %rax, 0x18(%r15)
testq %r14, %r14
je 0xfd1c4
leaq 0x8(%rbp), %rcx
xorl %edx, %edx
movl (%rcx), %esi
shlq $0x20, %rsi
movq %rsi, (%rax,%rdx,8)
incq %rdx
addq $0x18, %rcx
cmpq %rdx, %r14
jne 0xfd1ae
movl 0x10(%r15), %eax
xorl %r13d, %r13d
movl $0x0, %r12d
cmpl $0x1, %eax
je 0xfd299
movq %rbp, 0x10(%rsp)
xorl %r13d, %r13d
movabsq $-0x100000000, %r8      # imm = 0xFFFFFFFF00000000
xorl %r12d, %r12d
movq 0x18(%r15), %rdx
movq (%rdx,%r13,8), %rbp
movq 0x28(%rbx), %rax
movq %rbp, %rcx
shrq $0x20, %rcx
movq (%rax,%rcx,8), %rax
movl $0x0, %ecx
movl $0x0, %r14d
testl %eax, %eax
je 0xfd256
movl 0xc(%rdx,%r13,8), %edx
movq 0x8(%rbx), %rcx
movq %rax, %rdi
shrq $0x20, %rdi
movl %eax, %esi
leaq (%rdi,%rdi,2), %rdi
leaq (%rcx,%rdi,8), %rdi
addq $0xc, %rdi
xorl %ecx, %ecx
cmpl $0x0, (%rdi)
js 0xfd23f
cmpl %edx, -0x4(%rdi)
je 0xfd252
incq %rcx
addq $0x18, %rdi
cmpq %rcx, %rsi
jne 0xfd235
movl %eax, %ecx
xorl %r14d, %r14d
jmp 0xfd256
movl -0xc(%rdi), %r14d
cmpl %eax, %ecx
je 0xfd283
movq 0x18(%r15), %rax
andq %r8, %rbp
movl %r14d, %ecx
orq %rbp, %rcx
movq %rcx, (%rax,%r13,8)
incq %r13
addl %r14d, %r12d
movl 0x10(%r15), %eax
leal -0x1(%rax), %ecx
cmpq %rcx, %r13
jb 0xfd1ef
jmp 0xfd294
callq 0x11e70
movabsq $-0x100000000, %r8      # imm = 0xFFFFFFFF00000000
jmp 0xfd25a
movq 0x10(%rsp), %rbp
cmpl %eax, %r13d
jae 0xfd2bf
movq 0x18(%r15), %rax
movl %r13d, %ecx
movq (%rax,%rcx,8), %rdx
movq 0x20(%rbx), %rsi
shrq $0x21, %rdx
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl (%rsi,%rdx,8), %edi
movl %edi, (%rax,%rcx,8)
addl %edi, %r12d
movl 0xc(%rsp), %ecx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %r12d
movl %r12d, (%r15)
movl 0xc(%rbp), %eax
andl %esi, %eax
movl %eax, 0x4(%r15)
movl %ecx, %eax
leaq (%rax,%rax,2), %rax
movl 0xc(%rbp,%rax,8), %ecx
andl %esi, %ecx
movl %r12d, %edx
subl %ecx, %edx
movl %edx, 0x8(%r15)
movl 0xc(%rbp), %ecx
andl %esi, %ecx
movl 0xc(%rbp,%rax,8), %eax
andl %esi, %eax
addl %ecx, %eax
subl %eax, %r12d
andl 0x14(%rbx), %esi
movq %rbx, %rdi
movl %r12d, %edx
xorl %ecx, %ecx
callq 0xa0115
movl $0x1, %r12d
movl %r12d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq