// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: ma_sg_gen(ma_hit_t_alloc const*, long long, ma_sub_t const*, int, int)
asg_t *ma_sg_gen(const ma_hit_t_alloc* sources, long long n_read, const ma_sub_t *coverage_cut, 
int max_hang, int min_ovlp)
{
    double startTime = Get_T();
	size_t i, j;
	asg_t *g;
	///just calloc
	g = asg_init();

	///add seq to graph, seq just save the length of each read
	for (i = 0; i < (uint64_t)n_read; ++i) 
    {
        ///if a read has been deleted, should we still add them?
		asg_seq_set(g, i, coverage_cut[i].e - coverage_cut[i].s, coverage_cut[i].del);
        g->seq[i].c = coverage_cut[i].c;
	}

    g->seq_vis = (uint8_t*)calloc(g->n_seq*2, sizeof(uint8_t));

    for (i = 0; i < (uint64_t)n_read; ++i) 
    {
        for (j = 0; j < sources[i].length; j++)
        {
            int r;
		    asg_arc_t t, *p;
		    const ma_hit_t *h = &(sources[i].buffer[j]);
            if(h->del) continue;

            //high coverage region [sub[qn].e, sub[qn].s) in query
            int ql = coverage_cut[Get_qn(*h)].e - coverage_cut[Get_qn(*h)].s;
            //high coverage region [sub[qn].e, sub[qn].s) in target
            int tl = coverage_cut[Get_tn(*h)].e - coverage_cut[Get_tn(*h)].s;
		    r = ma_hit2arc(h, ql, tl, max_hang, asm_opt.max_hang_rate, min_ovlp, &t);
            /**
            #define MA_HT_INT       (-1)
            #define MA_HT_QCONT      (-2)
            #define MA_HT_TCONT      (-3)
            #define MA_HT_SHORT_OVLP (-4)
            the short overlaps and the overlaps with contain reads have already been removed
            here we should have overhang
            so r should always >= 0
            **/
            if (r >= 0) 
            {
                ///push node?
                p = asg_arc_pushp(g);
                *p = t;
		    }
            else
            {
                fprintf(stderr, "error\n");
            }
        }
    }

    asg_cleanup(g);
    g->r_seq = g->n_seq;

    if(VERBOSE >= 1)
    {
        fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime);
    }

	return g;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r8d, 0x4(%rsp)
movl %ecx, (%rsp)
movq %rdx, %r15
movq %rsi, %rbp
movq %rdi, 0x8(%rsp)
callq 0x251c
callq 0x68f5d
movq %rax, %r14
leaq 0x8(%r15), %rbx
xorl %r13d, %r13d
cmpq %r13, %rbp
je 0x6c339
movl -0x8(%rbx), %ecx
movl -0x4(%rbx), %edx
movl %ecx, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subl %eax, %edx
shrl $0x1f, %ecx
movq %r14, %rdi
movl %r13d, %esi
callq 0x69316
movb (%rbx), %al
movq 0x20(%r14), %rcx
movb %al, 0x4(%rcx,%r13,8)
incq %r13
addq $0xc, %rbx
jmp 0x6c303
movl 0x14(%r14), %edi
addl %edi, %edi
pushq $0x1
popq %rsi
callq 0x2120
movq %rax, 0x30(%r14)
xorl %eax, %eax
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movq %rbp, 0x10(%rsp)
cmpq %rbp, %rax
je 0x6c436
movq %rax, 0x18(%rsp)
imulq $0x18, %rax, %r13
addq 0x8(%rsp), %r13
xorl %ebx, %ebx
xorl %ebp, %ebp
movl 0xc(%r13), %eax
cmpq %rax, %rbp
jae 0x6c424
movq (%r13), %rax
cmpq $0x0, 0x18(%rax,%rbx)
js 0x6c3fa
leaq (%rax,%rbx), %rdi
movl 0x4(%rax,%rbx), %ecx
movl 0xc(%rax,%rbx), %eax
imulq $0xc, %rcx, %rcx
movl 0x4(%r15,%rcx), %esi
movl (%r15,%rcx), %ecx
andl %r12d, %ecx
subl %ecx, %esi
imulq $0xc, %rax, %rax
movl 0x4(%r15,%rax), %edx
movl (%r15,%rax), %eax
andl %r12d, %eax
subl %eax, %edx
leaq 0x1e7395(%rip), %rax    # 0x253758
movss 0x158(%rax), %xmm0
movl (%rsp), %ecx
movl 0x4(%rsp), %r8d
leaq 0x20(%rsp), %r9
callq 0x6a4a7
testl %eax, %eax
js 0x6c406
movq %r14, %rdi
callq 0x6c45d
movups 0x20(%rsp), %xmm0
movups %xmm0, (%rax)
movq 0x30(%rsp), %rcx
movq %rcx, 0x10(%rax)
incq %rbp
addq $0x28, %rbx
jmp 0x6c373
movq 0x1e6bd3(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0x177556(%rip), %rdi    # 0x1e396d
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
jmp 0x6c3fa
movq 0x18(%rsp), %rax
incq %rax
movq 0x10(%rsp), %rbp
jmp 0x6c358
movq %r14, %rdi
callq 0x694eb
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl 0x14(%r14), %eax
movl %eax, 0x18(%r14)
movq %r14, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq