// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: ma_ug_print2(ma_ug_t const*, All_reads*, asg_t*, ma_sub_t const*, ma_hit_t_alloc*, R_to_U*, int, char const*, _IO_FILE*)
void ma_ug_print2(const ma_ug_t *ug, All_reads *RNF, asg_t* read_g, const ma_sub_t *coverage_cut, 
ma_hit_t_alloc* sources, R_to_U* ruIndex, int print_seq, const char* prefix, FILE *fp)
{
    uint8_t* primary_flag = read_g?(uint8_t*)calloc(read_g->n_seq, sizeof(uint8_t)):NULL;
	uint32_t i, j, l, pc = read_g && coverage_cut && sources && ruIndex?1:0, co;
	char name[32];
	for (i = 0; i < ug->u.n; ++i) { // the Segment lines in GFA
		ma_utg_t *p = &ug->u.a[i];
        if(p->m == 0) continue;
        sprintf(name, "%s%.6d%c", prefix, i + 1, "lc"[p->circ]);
		if (print_seq) fprintf(fp, "S\t%s\t%s\tLN:i:%d\trd:i:%u\n", name, p->s? p->s : "*", p->len, 
        pc?get_ug_coverage(p, read_g, coverage_cut, sources, ruIndex, primary_flag, NULL, NULL):0);
		else fprintf(fp, "S\t%s\t*\tLN:i:%d\trd:i:%u\n", name, p->len, 
        pc?get_ug_coverage(p, read_g, coverage_cut, sources, ruIndex, primary_flag, NULL, NULL):0);
        
		for (j = l = 0; j < p->n; j++) {
            if(p->a[j] != (uint64_t)-1)
            {
                uint32_t x = p->a[j]>>33;
                if(x<RNF->total_reads)
                {

                    fprintf(fp, "A\t%s\t%d\t%c\t%.*s\t%d\t%d\tid:i:%d\tHG:A:%c\n", name, l, "+-"[p->a[j]>>32&1],
                    (int)Get_NAME_LENGTH((*RNF), x), Get_NAME((*RNF), x), 
                    coverage_cut?coverage_cut[x].s:0, coverage_cut?coverage_cut[x].e:(int)Get_READ_LENGTH((*RNF), x), x, 
                    "apmaaa"[((RNF->trio_flag[x]!=FATHER && RNF->trio_flag[x]!=MOTHER)?AMBIGU:RNF->trio_flag[x])]);
                }
                else
                {
                    fprintf(fp, "A\t%s\t%d\t%c\t%s\t%d\t%d\tid:i:%d\tHG:A:%c\n", name, l, "+-"[p->a[j]>>32&1],
                        "FAKE", coverage_cut?coverage_cut[x].s:0, coverage_cut?coverage_cut[x].e:(int)Get_READ_LENGTH((*RNF), x), x, '*');
                }
            }
            else
            {
                fprintf(fp, "A\t%s\t%d\t*\t*\t*\t*\tid:i:*\tHG:A:*\n", name, l);
            }
            l += (uint32_t)p->a[j];
        }
	}
	// for (i = 0; i < ug->g->n_arc; ++i) { // the Link lines in GFA
	// 	uint32_t u = ug->g->arc[i].ul>>32, v = ug->g->arc[i].v;
	// 	fprintf(fp, "L\t%s%.6d%c\t%c\t%s%.6d%c\t%c\t%dM\tL1:i:%d\n", 
    //     prefix, (u>>1)+1, "lc"[ug->u.a[u>>1].circ], "+-"[u&1],
	// 	prefix,	(v>>1)+1, "lc"[ug->u.a[v>>1].circ], "+-"[v&1], ug->g->arc[i].ol, asg_arc_len(ug->g->arc[i]));
	// }
    if(ug->g)
    {
        asg_arc_t* au = NULL;
        uint32_t nu, u, v;
        for (i = 0; i < ug->u.n; ++i) {
            if(ug->u.a[i].m == 0) continue;
            if(ug->u.a[i].circ) {
                co = cal_circle_ov(ug, i, 0, read_g);
                fprintf(fp, "L\t%s%.6dc\t+\t%s%.6dc\t+\t%dM\tL1:i:%d\tL2:i:%u\n", 
                prefix, i+1, prefix, i+1, co, ((ug->u.a[i].len>=co)?(ug->u.a[i].len-co):0), 0);
                co = cal_circle_ov(ug, i, 1, read_g);
                fprintf(fp, "L\t%s%.6dc\t-\t%s%.6dc\t-\t%dM\tL1:i:%d\tL2:i:%u\n", 
                prefix, i+1, prefix, i+1, co, ((ug->u.a[i].len>=co)?(ug->u.a[i].len-co):0), 0);
            } else {
                u = i<<1;
                au = asg_arc_a(ug->g, u);
                nu = asg_arc_n(ug->g, u);
                for (j = 0; j < nu; j++)
                {
                    if(au[j].del) continue;
                    v = au[j].v;
                    fprintf(fp, "L\t%s%.6d%c\t%c\t%s%.6d%c\t%c\t%dM\tL1:i:%d\tL2:i:%u\n", 
                    prefix, (u>>1)+1, "lc"[ug->u.a[u>>1].circ], "+-"[u&1],
                    prefix,	(v>>1)+1, "lc"[ug->u.a[v>>1].circ], "+-"[v&1], au[j].ol, asg_arc_len(au[j]), 0/**au[j].ou**/);
                }


                u = (i<<1) + 1;
                au = asg_arc_a(ug->g, u);
                nu = asg_arc_n(ug->g, u);
                for (j = 0; j < nu; j++)
                {
                    if(au[j].del) continue;
                    v = au[j].v;
                    fprintf(fp, "L\t%s%.6d%c\t%c\t%s%.6d%c\t%c\t%dM\tL1:i:%d\tL2:i:%u\n", 
                    prefix, (u>>1)+1, "lc"[ug->u.a[u>>1].circ], "+-"[u&1],
                    prefix,	(v>>1)+1, "lc"[ug->u.a[v>>1].circ], "+-"[v&1], au[j].ol, asg_arc_len(au[j]), 0/**au[j].ou**/);
                }
            } 
        }
    }
    free(primary_flag);    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r9, 0x40(%rsp)
movq %r8, 0x38(%rsp)
movq %rcx, %r15
movq %rdx, %rbx
movq %rsi, %rbp
movq %rdi, %r13
testq %rdx, %rdx
je 0xb7866
movl 0x14(%rbx), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl $0x1, %esi
callq 0x2120
movq %rax, 0x20(%rsp)
jmp 0xb786f
movq $0x0, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
testq %rbx, %rbx
setne %sil
testq %r15, %r15
setne %al
cmpq $0x0, 0x38(%rsp)
setne %cl
cmpq $0x0, 0x40(%rsp)
setne %dl
cmpq $0x0, (%r13)
je 0xb7ed1
andb %dl, %cl
andb %al, %sil
andb %cl, %sil
movb %sil, 0x30(%rsp)
xorl %eax, %eax
xorl %edx, %edx
movq %r15, 0x18(%rsp)
movq %r13, 0x48(%rsp)
movq 0x10(%r13), %rcx
leaq (%rax,%rax,4), %rax
incl %edx
movl %edx, 0x8(%rsp)
cmpl $0x0, 0xc(%rcx,%rax,8)
je 0xb7b78
leaq (%rcx,%rax,8), %r13
movl (%r13), %eax
shrl $0x1f, %eax
leaq 0x17ce57(%rip), %rcx    # 0x23473b
movsbl (%rax,%rcx), %r8d
leaq 0x50(%rsp), %rdi
leaq 0x17eae5(%rip), %rsi    # 0x2363da
movq 0xb8(%rsp), %rdx
movl 0x8(%rsp), %ecx
xorl %eax, %eax
callq 0x2050
cmpl $0x0, 0xb0(%rsp)
je 0xb798e
movq 0x20(%r13), %r14
testq %r14, %r14
leaq 0x17eadc(%rip), %rax    # 0x2363fc
cmoveq %rax, %r14
movl (%r13), %r12d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %r12d
xorl %r9d, %r9d
cmpb $0x0, 0x30(%rsp)
je 0xb796b
subq $0x10, %rsp
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
movq %r13, %rdi
movq 0x38(%rsp), %rsi
movq %r15, %rdx
movq 0x48(%rsp), %rcx
movq 0x50(%rsp), %r8
movq 0x30(%rsp), %r9
callq 0xb72e7
addq $0x10, %rsp
movl %eax, %r9d
movq 0xc0(%rsp), %rdi
leaq 0x17ea69(%rip), %rsi    # 0x2363e3
leaq 0x50(%rsp), %rdx
movq %r14, %rcx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x22b0
jmp 0xb79f3
movl (%r13), %r14d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %r14d
xorl %r8d, %r8d
cmpb $0x0, 0x30(%rsp)
je 0xb79d5
subq $0x10, %rsp
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
movq %r13, %rdi
movq 0x38(%rsp), %rsi
movq %r15, %rdx
movq 0x48(%rsp), %rcx
movq 0x50(%rsp), %r8
movq 0x30(%rsp), %r9
callq 0xb72e7
addq $0x10, %rsp
movl %eax, %r8d
movq 0xc0(%rsp), %rdi
leaq 0x17ea1a(%rip), %rsi    # 0x2363fe
leaq 0x50(%rsp), %rdx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x22b0
cmpl $0x0, 0x10(%r13)
je 0xb7b78
movq 0x18(%r13), %rax
xorl %r12d, %r12d
xorl %r14d, %r14d
movq (%rax,%r12,8), %rax
cmpq $-0x1, %rax
je 0xb7a54
movq %rax, %r10
shrq $0x21, %r10
btq $0x20, %rax
leaq 0x17a8ae(%rip), %rax    # 0x2322d3
adcq $0x0, %rax
movsbl (%rax), %r8d
cmpq 0x50(%rbp), %r10
jae 0xb7a77
testq %r15, %r15
je 0xb7a9a
leaq (%r10,%r10,2), %rax
movl (%r15,%rax,4), %r11d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %r11d
movl 0x4(%r15,%rax,4), %eax
movq %rax, 0x10(%rsp)
jmp 0xb7aaa
movq 0xc0(%rsp), %rdi
leaq 0x17ea0b(%rip), %rsi    # 0x23646e
leaq 0x50(%rsp), %rdx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x22b0
jmp 0xb7b60
testq %r15, %r15
je 0xb7b25
leaq (%r10,%r10,2), %rax
movl (%r15,%rax,4), %r11d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %r11d
movl 0x4(%r15,%rax,4), %ebx
jmp 0xb7b30
movq 0x18(%rbp), %rax
movl (%rax,%r10,8), %eax
movq %rax, 0x10(%rsp)
xorl %r11d, %r11d
movq %r13, %r15
movq 0x28(%rbp), %rax
movzbl (%rax,%r10), %eax
leal -0x1(%rax), %ecx
cmpb $0x2, %cl
jb 0xb7ac0
xorl %eax, %eax
movq 0x40(%rbp), %rcx
movl 0x8(%rcx,%r10,8), %r9d
movq (%rcx,%r10,8), %rbx
subl %ebx, %r9d
addq 0x8(%rbp), %rbx
movzbl %al, %eax
movq %rbp, %r13
leaq 0x17e95c(%rip), %rcx    # 0x23643d
movsbl (%rax,%rcx), %ebp
subq $0x8, %rsp
movq 0xc8(%rsp), %rdi
leaq 0x17e91e(%rip), %rsi    # 0x236416
leaq 0x58(%rsp), %rdx
movl %r14d, %ecx
xorl %eax, %eax
pushq %rbp
movq %r13, %rbp
pushq %r10
pushq 0x28(%rsp)
pushq %r11
pushq %rbx
callq 0x22b0
addq $0x30, %rsp
movq 0x18(%rsp), %rax
movq %r15, %r13
movq %rax, %r15
jmp 0xb7b60
movq 0x18(%rbp), %rax
movl (%rax,%r10,8), %ebx
xorl %r11d, %r11d
movq 0xc0(%rsp), %rdi
leaq 0x17e905(%rip), %rsi    # 0x236444
leaq 0x50(%rsp), %rdx
movl %r14d, %ecx
leaq 0x17e91b(%rip), %r9     # 0x236469
xorl %eax, %eax
pushq $0x2a
pushq %r10
pushq %rbx
pushq %r11
callq 0x22b0
addq $0x20, %rsp
movq 0x18(%r13), %rax
addl (%rax,%r12,8), %r14d
incq %r12
movl 0x10(%r13), %ecx
cmpq %rcx, %r12
jb 0xb7a08
movl 0x8(%rsp), %edx
movl %edx, %eax
movq 0x48(%rsp), %r13
movq (%r13), %rcx
cmpq %rax, %rcx
ja 0xb78b9
testq %rcx, %rcx
sete %al
cmpq $0x0, 0x18(%r13)
sete %cl
orb %al, %cl
jne 0xb7ed1
xorl %eax, %eax
xorl %ecx, %ecx
movq %rcx, 0x8(%rsp)
movq 0x10(%r13), %rcx
leaq (%rax,%rax,4), %rbx
cmpl $0x0, 0xc(%rcx,%rbx,8)
je 0xb7ebd
leaq (%rcx,%rbx,8), %rax
movl (%rax), %r15d
testl %r15d, %r15d
js 0xb7dec
movq 0x8(%rsp), %rsi
leal (%rsi,%rsi), %edi
movq 0x18(%r13), %rcx
movq 0x8(%rcx), %rax
movq 0x28(%rcx), %rcx
movq (%rcx,%rdi,8), %rdx
testl %edx, %edx
je 0xb7cee
movq %rdi, 0x30(%rsp)
movq %rdx, %rcx
shrq $0x20, %rcx
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq %rsi, 0x10(%rsp)
incl %esi
movl %esi, 0x18(%rsp)
movl %edx, %edx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %r12
addq $0xc, %r12
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rbp
xorl %ebx, %ebx
movl (%r12,%rbx), %r10d
testl %r10d, %r10d
js 0xb7cd0
movl -0xc(%r12,%rbx), %r11d
movl -0x4(%r12,%rbx), %eax
movq 0x10(%r13), %rcx
movq 0x10(%rsp), %rdx
leaq (%rdx,%rdx,4), %rdx
movl (%rcx,%rdx,8), %edx
shrl $0x1f, %edx
leaq 0x17cae3(%rip), %rsi    # 0x23473b
movsbl (%rdx,%rsi), %r8d
movl %eax, %r13d
shrl %r13d
leaq (,%r13,4), %rdx
addq %r13, %rdx
incl %r13d
movl (%rcx,%rdx,8), %ecx
shrl $0x1f, %ecx
movsbl (%rcx,%rsi), %r14d
andl $0x1, %eax
leaq 0x17a64d(%rip), %rcx    # 0x2322d3
movsbl (%rax,%rcx), %r15d
subq $0x8, %rsp
movq 0xc8(%rsp), %rdi
leaq 0x17e845(%rip), %rsi    # 0x2364e3
movq 0xc0(%rsp), %rdx
movl 0x20(%rsp), %ecx
movl $0x2b, %r9d
xorl %eax, %eax
pushq $0x0
pushq %r11
pushq %r10
pushq %r15
pushq %r14
pushq %r13
movq 0x80(%rsp), %r13
pushq %rdx
callq 0x22b0
addq $0x40, %rsp
addq $0x18, %rbx
cmpq %rbx, %rbp
jne 0xb7c27
movq 0x18(%r13), %rcx
movq 0x8(%rcx), %rax
movq 0x28(%rcx), %rcx
movq 0x30(%rsp), %rdi
movl %edi, %edx
movq 0x8(%rcx,%rdx,8), %rcx
testl %ecx, %ecx
je 0xb7ebd
movq %rcx, %rdx
shrq $0x20, %rdx
movq 0x8(%rsp), %rsi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
leal 0x1(%rsi), %edi
movl %edi, 0x10(%rsp)
movl %ecx, %ecx
leaq (%rdx,%rdx,2), %rdx
leaq (%rax,%rdx,8), %r15
addq $0xc, %r15
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %r12
leaq (%rsi,%rsi,4), %rax
movq %rax, 0x18(%rsp)
xorl %ebx, %ebx
movl (%r15,%rbx), %r10d
testl %r10d, %r10d
js 0xb7dda
movl -0xc(%r15,%rbx), %r11d
movl -0x4(%r15,%rbx), %eax
movq 0x10(%r13), %rcx
movq 0x18(%rsp), %rdx
movl (%rcx,%rdx,8), %edx
shrl $0x1f, %edx
leaq 0x17c9d7(%rip), %rsi    # 0x23473b
movsbl (%rdx,%rsi), %r8d
movl %eax, %r13d
shrl %r13d
leaq (,%r13,4), %rdx
addq %r13, %rdx
incl %r13d
movl (%rcx,%rdx,8), %ecx
shrl $0x1f, %ecx
movsbl (%rcx,%rsi), %r14d
andl $0x1, %eax
leaq 0x17a541(%rip), %rcx    # 0x2322d3
movsbl (%rax,%rcx), %ebp
subq $0x8, %rsp
movq 0xc8(%rsp), %rdi
leaq 0x17e73a(%rip), %rsi    # 0x2364e3
movq 0xc0(%rsp), %rdx
movl 0x18(%rsp), %ecx
movl $0x2d, %r9d
xorl %eax, %eax
pushq $0x0
pushq %r11
pushq %r10
pushq %rbp
pushq %r14
pushq %r13
movq 0x80(%rsp), %r13
pushq %rdx
callq 0x22b0
addq $0x40, %rsp
addq $0x18, %rbx
cmpq %rbx, %r12
jne 0xb7d37
jmp 0xb7ebd
movq %r13, %rdi
movq 0x8(%rsp), %r12
movl %r12d, %esi
xorl %edx, %edx
movq 0x28(%rsp), %rcx
callq 0xb7789
movl %eax, %r10d
leal 0x1(%r12), %r14d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %r15d
subl %r10d, %r15d
movl $0x0, %eax
cmovbl %eax, %r15d
xorl %r11d, %r11d
subq $0x8, %rsp
movq 0xc8(%rsp), %rdi
leaq 0x17e658(%rip), %rsi    # 0x23648d
movq 0xc0(%rsp), %rbp
movq %rbp, %rdx
movl %r14d, %ecx
movq %rbp, %r8
movl %r14d, %r9d
xorl %eax, %eax
pushq %r11
pushq %r15
pushq %r10
callq 0x22b0
addq $0x20, %rsp
movq %r13, %rdi
movl %r12d, %esi
movl $0x1, %edx
movq 0x28(%rsp), %rcx
callq 0xb7789
movl %eax, %r10d
movq 0x10(%r13), %rax
movl (%rax,%rbx,8), %r11d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %r11d
subl %r10d, %r11d
movl $0x0, %ebx
cmovbl %ebx, %r11d
subq $0x8, %rsp
movq 0xc8(%rsp), %rdi
leaq 0x17e617(%rip), %rsi    # 0x2364b8
movq %rbp, %rdx
movl %r14d, %ecx
movq %rbp, %r8
movl %r14d, %r9d
xorl %eax, %eax
pushq %rbx
pushq %r11
pushq %r10
callq 0x22b0
addq $0x20, %rsp
movq 0x8(%rsp), %rcx
incl %ecx
movq %rcx, %rax
cmpq %rcx, (%r13)
ja 0xb7baa
movq 0x20(%rsp), %rdi
callq 0x2220
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq