// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
movl %r8d, 0x44(%rsp)
movl %ecx, %r12d
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %rbp
callq 0x2f3fc
movl $0x1, %edi
movl $0x48, %esi
callq 0x2120
movq %rax, 0x10(%rsp)
testq %r14, %r14
je 0xa407a
movq %rbx, 0x28(%rsp)
addq $0x8, %rbx
xorl %r13d, %r13d
movl -0x8(%rbx), %ecx
movl -0x4(%rbx), %edx
movl %ecx, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subl %eax, %edx
shrl $0x1f, %ecx
movq 0x10(%rsp), %rdi
movl %r13d, %esi
callq 0xa0887
movb (%rbx), %al
movq 0x10(%rsp), %rcx
movq 0x20(%rcx), %rcx
movb %al, 0x4(%rcx,%r13,8)
incq %r13
addq $0xc, %rbx
cmpq %r13, %r14
jne 0xa3ce0
movq 0x10(%rsp), %rax
movl 0x14(%rax), %edi
addl %edi, %edi
movl $0x1, %esi
callq 0x2120
movq 0x10(%rsp), %rdi
movq %rax, 0x30(%rdi)
xorl %eax, %eax
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
movl %ecx, 0x1c(%rsp)
movq 0x28(%rsp), %r10
movq %rbp, 0x48(%rsp)
movl %r12d, 0x20(%rsp)
movq %r14, 0x50(%rsp)
movq %rax, 0x58(%rsp)
leaq (%rax,%rax,2), %rax
cmpl $0x0, 0xc(%rbp,%rax,8)
je 0xa405d
leaq (,%rax,8), %r8
addq %rbp, %r8
movl $0x21, %ebp
xorl %r9d, %r9d
movq %r8, 0x30(%rsp)
movq (%r8), %rax
movq -0x9(%rax,%rbp), %rcx
testq %rcx, %rcx
js 0xa4049
movq %r9, 0x38(%rsp)
movl -0x15(%rax,%rbp), %r15d
leaq (%r15,%r15,2), %rsi
movl 0x4(%r10,%rsi,4), %edx
movl (%r10,%rsi,4), %esi
andl %r11d, %esi
subl %esi, %edx
testl %ecx, %ecx
js 0xa3dc7
movl -0x11(%rax,%rbp), %r9d
movl -0xd(%rax,%rbp), %edi
subl %edi, %edx
movl %edx, %esi
movl %r9d, %edx
jmp 0xa3dd5
movl -0x11(%rax,%rbp), %r9d
movl -0xd(%rax,%rbp), %edi
subl %edi, %edx
movl %r9d, %esi
movq -0x21(%rax,%rbp), %r13
movq %r13, %r8
shrq $0x20, %r8
leaq (%r8,%r8,2), %r8
movl 0x4(%r10,%r8,4), %ebx
movl (%r10,%r8,4), %r8d
andl %r11d, %r8d
subl %r8d, %ebx
cmpl %r13d, %edx
movl %r13d, %r14d
cmovll %edx, %r14d
movl -0x19(%rax,%rbp), %r8d
movl %ebx, 0x24(%rsp)
subl %r8d, %ebx
cmpl %esi, %ebx
movl %esi, %r10d
cmovll %ebx, %r10d
cmpl %r12d, %r14d
jg 0xa400f
cmpl %r12d, %r10d
jg 0xa400f
movq %r15, 0x60(%rsp)
movl %r8d, %r11d
subl %r13d, %r11d
cvtsi2ss %r11, %xmm1
addl %r14d, %r11d
addl %r10d, %r11d
xorps %xmm2, %xmm2
cvtsi2ss %r11, %xmm2
leaq 0x1f190e(%rip), %r15    # 0x295758
movss 0x158(%r15), %xmm0
mulss %xmm0, %xmm2
ucomiss %xmm1, %xmm2
ja 0xa400f
subl %r9d, %edi
xorps %xmm1, %xmm1
cvtsi2ss %rdi, %xmm1
addl %r14d, %r10d
addl %edi, %r10d
xorps %xmm2, %xmm2
cvtsi2ss %r10, %xmm2
mulss %xmm2, %xmm0
ucomiss %xmm1, %xmm0
ja 0xa400f
cmpl %esi, %ebx
setl %dil
setg %bl
cmpl %edx, %r13d
setg %r14b
setl %r9b
orb %bl, %r14b
cmpb $0x1, %r14b
jne 0xa400f
orb %dil, %r9b
je 0xa400f
movl 0x44(%rsp), %edi
cmpl %edi, %r11d
jl 0xa400f
cmpl %edi, %r10d
jl 0xa400f
testl %ecx, %ecx
setns %r9b
addl %r8d, %esi
movl 0x24(%rsp), %edi
subl %esi, %edi
xorl %r12d, %r12d
movl %r13d, %ebx
subl %edx, %ebx
setle %sil
setg %r8b
cmovlel %edi, %ebx
shrl $0x10, %ecx
andl $0x4000, %ecx           # imm = 0x4000
movl 0x1c(%rsp), %edx
andl $0x3fff, %edx           # imm = 0x3FFF
orl %ecx, %edx
movzbl (%rax,%rbp), %ecx
shll $0xf, %ecx
orl %edx, %ecx
movl %ecx, 0x1c(%rsp)
testl %ebx, %ebx
js 0xa400a
movb %r8b, 0xd(%rsp)
movb %r9b, 0xe(%rsp)
movb -0x1(%rax,%rbp), %al
movb %al, 0xf(%rsp)
movq 0x10(%rsp), %rdi
movl 0x4(%rdi), %r14d
movl %r14d, %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
cmpl (%rdi), %r15d
jne 0xa3f78
leal (%r15,%r15), %eax
testl %r15d, %r15d
movl $0x10, %ecx
cmovel %ecx, %eax
movl %eax, (%rdi)
movq 0x10(%rsp), %rcx
movq 0x8(%rcx), %rdi
shlq $0x3, %rax
movb %sil, 0xc(%rsp)
leaq (%rax,%rax,2), %rsi
callq 0x2370
movb 0xc(%rsp), %sil
movq 0x10(%rsp), %rdi
movq %rax, 0x8(%rdi)
jmp 0xa3f7c
movq 0x8(%rdi), %rax
movq 0x28(%rsp), %r10
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
movq 0x30(%rsp), %r8
movq 0x38(%rsp), %r9
movb 0xe(%rsp), %cl
xorb 0xd(%rsp), %cl
movzbl %cl, %ecx
shrq $0x1f, %r13
andl $-0x2, %r13d
movb %sil, %r12b
orl %r13d, %r12d
movq 0x60(%rsp), %rdx
leal (%rcx,%rdx,2), %ecx
shlq $0x20, %r12
leaq (%rbx,%r12), %rdx
movl 0x24(%rsp), %r12d
subl %ebx, %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
leal 0x1(%r14), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl $0x80000000, %r14d      # imm = 0x80000000
orl %esi, %r14d
movl %r14d, 0x4(%rdi)
leaq (%r15,%r15,2), %rsi
movq %rdx, (%rax,%rsi,8)
movl %ecx, 0x8(%rax,%rsi,8)
movl %r12d, 0xc(%rax,%rsi,8)
movl 0x1c(%rsp), %ecx
movw %cx, 0x10(%rax,%rsi,8)
movb 0xf(%rsp), %cl
movb %cl, 0x12(%rax,%rsi,8)
movl 0x20(%rsp), %r12d
jmp 0xa4049
movl 0x20(%rsp), %r12d
movq 0x1f0fca(%rip), %rax    # 0x294fe0
movq (%rax), %rcx
movl $0x6, %esi
movl $0x1, %edx
leaq 0x18d9f3(%rip), %rdi    # 0x231a1d
callq 0x2410
movq 0x10(%rsp), %rdi
movq 0x28(%rsp), %r10
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
movq 0x30(%rsp), %r8
movq 0x38(%rsp), %r9
incq %r9
movl 0xc(%r8), %eax
addq $0x28, %rbp
cmpq %rax, %r9
jb 0xa3d84
movq 0x58(%rsp), %rax
incq %rax
movq 0x50(%rsp), %r14
cmpq %r14, %rax
movq 0x48(%rsp), %rbp
jne 0xa3d58
jmp 0xa408f
movl $0x1, %esi
xorl %edi, %edi
callq 0x2120
movq 0x10(%rsp), %rdi
movq %rax, 0x30(%rdi)
movq %rdi, %rbx
callq 0xa0a7d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl 0x14(%rbx), %eax
movl %eax, 0x18(%rbx)
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq