// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: ma_scg_print(kvect_sec_t const*, All_reads*, asg_t*, ma_sub_t const*, ma_hit_t_alloc*, R_to_U*, int, char const*, _IO_FILE*)
void ma_scg_print(const kvect_sec_t *sc, 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;
	uint64_t i, j, l, pc = read_g && coverage_cut && sources && ruIndex?1:0, tl, m, x; sec_t *scp;
	char name[32]; uint64_t Rb, Cb, tRb, tCb, Cov; ma_utg_t *z = NULL; uint8_t c;
	for (i = 0; i < sc->n; ++i) { // the Segment lines in GFA
        scp = &(sc->a[i]);
        ///debug
        // prt_scaf_stats(scp, sc->ctg, prefix, i);

        for (j = tl = tRb = tCb = 0; j < scp->n; j++) {
            z = &(sc->ctg->u.a[((uint32_t)scp->a[j])>>1]); tl += z->len; tl += (scp->a[j]>>32);
            if(pc) {
                get_ug_coverage(z, read_g, coverage_cut, sources, ruIndex, primary_flag, &Rb, &Cb); tRb += Rb; tCb += Cb;
            }
        }
        sprintf(name, "%s%.6lu%c", prefix, i + 1, "lc"[scp->is_c]); Cov = ((tRb==0)?(0):(tCb/tRb));

        if (!print_seq) {
            fprintf(fp, "S\t%s\t*\tLN:i:%lu\trd:i:%lu\n", name, tl, Cov);
        } else {
            fprintf(fp, "S\t%s\t", name);
            for (j = 0; j < scp->n; j++) {
                z = &(sc->ctg->u.a[((uint32_t)scp->a[j])>>1]); 
                if(!(((uint32_t)scp->a[j])&1)) {
                    fprintf(fp, "%s", z->s);
                } else {
                    for(l = 0; l < z->len; l++) {
                        c = (uint8_t)(z->s[z->len-1-l]);
                        fprintf(fp, "%c", ((c>=128)?'N':comp_tab[c]));
                    }
                }
                for(l = 0; l < (scp->a[j]>>32); l++) fprintf(fp, "N");
            }
            fprintf(fp, "\tLN:i:%lu\trd:i:%lu\n", tl, Cov);
        }


        for (j = tl = 0; j < scp->n; j++) {
            z = &(sc->ctg->u.a[((uint32_t)scp->a[j])>>1]); l = tl;
            if(!(((uint32_t)scp->a[j])&1)) {
                for (m = 0; m < z->n; m++) {
                    x = z->a[m]>>33;
                    if(x<RNF->total_reads) {
                        fprintf(fp, "A\t%s\t%lu\t%c\t%.*s\t%d\t%d\tid:i:%lu\tHG:A:%c\n", name, l, "+-"[(z->a[m]>>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%lu\t%c\t%s\t%d\t%d\tid:i:%lu\tHG:A:%c\n", name, l, "+-"[(z->a[m]>>32)&1],
                            "FAKE", coverage_cut?coverage_cut[x].s:0, coverage_cut?coverage_cut[x].e:(int)Get_READ_LENGTH((*RNF), x), x, '*');
                    }
                    l += (uint32_t)(z->a[m]);
                }
            } else {
                for (m = 0; m < z->n; m++) {
                    x = z->a[z->n-m-1]>>33;
                    if(x<RNF->total_reads) {
                        fprintf(fp, "A\t%s\t%lu\t%c\t%.*s\t%d\t%d\tid:i:%lu\tHG:A:%c\n", name, l, "+-"[((z->a[z->n-m-1]>>32)&1)^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%lu\t%c\t%s\t%d\t%d\tid:i:%lu\tHG:A:%c\n", name, l, "+-"[((z->a[z->n-m-1]>>32)&1)^1],
                            "FAKE", coverage_cut?coverage_cut[x].s:0, coverage_cut?coverage_cut[x].e:(int)Get_READ_LENGTH((*RNF), x), x, '*');
                    }
                    l += (uint32_t)(z->a[z->n-m-1]);
                }
            }
            tl = l;

            if((scp->a[j]>>32) > 0) fprintf(fp, "A\t%s\t%lu\t+\tNs\t0\t%lu\tid:i:-1\tHG:A:a\n", name, l, (scp->a[j]>>32));

            tl += (scp->a[j]>>32);
        }
	}
    free(primary_flag);    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r9, 0x58(%rsp)
movq %r8, 0x50(%rsp)
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %r15
testq %rdx, %rdx
je 0xb7f5e
movl 0x14(%r14), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl $0x1, %esi
callq 0x2120
movq %rax, 0x38(%rsp)
jmp 0xb7f67
movq $0x0, 0x38(%rsp)
movq %r14, 0x88(%rsp)
testq %r14, %r14
setne %sil
testq %rbx, %rbx
setne %al
cmpq $0x0, 0x50(%rsp)
setne %cl
cmpq $0x0, 0x58(%rsp)
setne %dl
cmpq $0x0, 0x8(%r15)
je 0xb85dc
andb %dl, %cl
andb %al, %sil
andb %cl, %sil
movb %sil, 0xf(%rsp)
xorl %ecx, %ecx
movq %r13, 0x48(%rsp)
movq %r15, 0x20(%rsp)
movq %rbx, 0x40(%rsp)
movq (%r15), %rax
movq %rcx, 0x30(%rsp)
shlq $0x5, %rcx
leaq (%rax,%rcx), %rdx
movq %rdx, 0x28(%rsp)
movq 0x8(%rax,%rcx), %rax
testq %rax, %rax
je 0xb80aa
movq 0x18(%r15), %rsi
xorl %r12d, %r12d
movq $0x0, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
xorl %ebp, %ebp
movq %rsi, 0x90(%rsp)
movq 0x10(%rsi), %rcx
movq 0x28(%rsp), %r14
movq 0x18(%r14), %rdx
movq (%rdx,%r12,8), %r15
movl %r15d, %edx
shrl %edx
leaq (%rdx,%rdx,4), %rdx
movl (%rcx,%rdx,8), %ebx
cmpb $0x0, 0xf(%rsp)
je 0xb808c
leaq (%rcx,%rdx,8), %rdi
movq 0x88(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %r8
movq 0x38(%rsp), %r9
leaq 0x98(%rsp), %rax
pushq %rax
leaq 0xa8(%rsp), %rax
pushq %rax
callq 0xb72e7
movq 0xa0(%rsp), %rsi
addq $0x10, %rsp
movq 0x10(%rsp), %rax
addq 0xa0(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rax
addq 0x98(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x8(%r14), %rax
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
shrq $0x20, %r15
addq %r15, %rbp
addq %rbx, %rbp
incq %r12
cmpq %rax, %r12
jb 0xb7ffd
jmp 0xb80be
xorl %ebp, %ebp
movq $0x0, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movq 0x30(%rsp), %r9
incq %r9
movq 0x28(%rsp), %r12
movq 0x10(%r12), %rax
leaq 0x17c664(%rip), %rcx    # 0x23473b
movsbl (%rax,%rcx), %r8d
leaq 0x60(%rsp), %rdi
leaq 0x17e42a(%rip), %rsi    # 0x236512
movq 0xe8(%rsp), %rdx
movq %r9, 0x30(%rsp)
movq %r9, %rcx
xorl %eax, %eax
callq 0x2050
movq 0x10(%rsp), %rcx
testq %rcx, %rcx
je 0xb8115
movq 0x18(%rsp), %rax
xorl %edx, %edx
divq %rcx
jmp 0xb8117
xorl %eax, %eax
movq 0x20(%rsp), %r15
cmpl $0x0, 0xe0(%rsp)
je 0xb8251
movq %rax, 0x10(%rsp)
movq 0xf0(%rsp), %rdi
leaq 0x17e46b(%rip), %rsi    # 0x2365a9
leaq 0x60(%rsp), %rdx
xorl %eax, %eax
callq 0x22b0
cmpq $0x0, 0x8(%r12)
je 0xb8231
movq 0x18(%r12), %rax
xorl %ebx, %ebx
movq 0x18(%r15), %rcx
movq (%rax,%rbx,8), %rax
movl %eax, %edx
shrl %edx
leaq (%rdx,%rdx,4), %r14
shlq $0x3, %r14
addq 0x10(%rcx), %r14
testb $0x1, %al
jne 0xb818c
movq 0x20(%r14), %rdi
movq 0xf0(%rsp), %rsi
callq 0x21d0
jmp 0xb81e4
movl (%r14), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
je 0xb81e4
movq $-0x1, %r15
xorl %r12d, %r12d
movl %eax, %eax
addq 0x20(%r14), %rax
movsbq (%r15,%rax), %rax
testq %rax, %rax
js 0xb81bf
leaq 0x17dc47(%rip), %rcx    # 0x235e00
movsbl (%rax,%rcx), %edi
jmp 0xb81c4
movl $0x4e, %edi
movq 0xf0(%rsp), %rsi
callq 0x2270
incq %r12
movl (%r14), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
decq %r15
cmpq %rax, %r12
jb 0xb81a2
movq 0x28(%rsp), %r12
movq 0x18(%r12), %rax
cmpl $0x0, 0x4(%rax,%rbx,8)
movq 0xf0(%rsp), %r15
je 0xb821e
xorl %r14d, %r14d
movl $0x4e, %edi
movq %r15, %rsi
callq 0x2270
incq %r14
movq 0x18(%r12), %rax
movl 0x4(%rax,%rbx,8), %ecx
cmpq %rcx, %r14
jb 0xb8200
incq %rbx
cmpq 0x8(%r12), %rbx
movq 0x20(%rsp), %r15
jb 0xb815d
movq 0xf0(%rsp), %rdi
leaq 0x17e355(%rip), %rsi    # 0x236595
movq %rbp, %rdx
movq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x22b0
jmp 0xb8272
movq 0xf0(%rsp), %rdi
leaq 0x17e32f(%rip), %rsi    # 0x23658f
leaq 0x60(%rsp), %rdx
movq %rbp, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x22b0
cmpq $0x0, 0x8(%r12)
movq 0x40(%rsp), %rbx
je 0xb85cd
movq 0x18(%r12), %rax
xorl %r12d, %r12d
xorl %ebp, %ebp
movq 0x18(%r15), %rcx
movq 0x10(%rcx), %rcx
movq (%rax,%r12,8), %rdx
movl %edx, %eax
shrl %eax
leaq (%rax,%rax,4), %rax
leaq (%rcx,%rax,8), %rax
movq %rax, 0x10(%rsp)
movl 0x10(%rax), %eax
testb $0x1, %dl
movq %r12, 0x18(%rsp)
jne 0xb840c
testq %rax, %rax
je 0xb8573
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
xorl %r14d, %r14d
movq (%rax,%r14,8), %rax
movq %rax, %r10
shrq $0x21, %r10
btq $0x20, %rax
leaq 0x179fec(%rip), %rax    # 0x2322d3
adcq $0x0, %rax
movsbl (%rax), %r8d
cmpq 0x50(%r13), %r10
jae 0xb8311
testq %rbx, %rbx
je 0xb8334
leaq (%r10,%r10,2), %rax
movl (%rbx,%rax,4), %r11d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %r11d
movl 0x4(%rbx,%rax,4), %r12d
jmp 0xb833f
testq %rbx, %rbx
je 0xb83ac
leaq (%r10,%r10,2), %rax
movl (%rbx,%rax,4), %r11d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %r11d
movl 0x4(%rbx,%rax,4), %r15d
jmp 0xb83b7
movq 0x18(%r13), %rax
movl (%rax,%r10,8), %r12d
xorl %r11d, %r11d
movq 0x28(%r13), %rax
movzbl (%rax,%r10), %eax
leal -0x1(%rax), %ecx
cmpb $0x2, %cl
jb 0xb8352
xorl %eax, %eax
movq 0x40(%r13), %rcx
movl 0x8(%rcx,%r10,8), %r9d
movq (%rcx,%r10,8), %r15
subl %r15d, %r9d
addq 0x8(%r13), %r15
movzbl %al, %eax
leaq 0x17e0cd(%rip), %rcx    # 0x23643d
movsbl (%rax,%rcx), %r13d
subq $0x8, %rsp
movq 0xf8(%rsp), %rdi
leaq 0x17e227(%rip), %rsi    # 0x2365af
leaq 0x68(%rsp), %rdx
movq %rbp, %rcx
xorl %eax, %eax
pushq %r13
movq 0x58(%rsp), %r13
pushq %r10
pushq %r12
pushq %r11
pushq %r15
callq 0x22b0
addq $0x30, %rsp
jmp 0xb83e8
movq 0x18(%r13), %rax
movl (%rax,%r10,8), %r15d
xorl %r11d, %r11d
movq 0xf0(%rsp), %rdi
leaq 0x17e212(%rip), %rsi    # 0x2365d8
leaq 0x60(%rsp), %rdx
movq %rbp, %rcx
leaq 0x17e094(%rip), %r9     # 0x236469
xorl %eax, %eax
pushq $0x2a
pushq %r10
pushq %r15
pushq %r11
callq 0x22b0
addq $0x20, %rsp
movq 0x10(%rsp), %rdx
movq 0x18(%rdx), %rax
movl (%rax,%r14,8), %ecx
addq %rcx, %rbp
incq %r14
movl 0x10(%rdx), %ecx
cmpq %rcx, %r14
jb 0xb82d0
jmp 0xb8573
testq %rax, %rax
je 0xb8573
movq 0x10(%rsp), %rcx
movq 0x18(%rcx), %rcx
movq $-0x8, %r12
xorl %r14d, %r14d
leaq (%rcx,%rax,8), %rax
movq (%r12,%rax), %rax
movq %rax, %r10
shrq $0x21, %r10
btq $0x20, %rax
leaq 0x179e90(%rip), %rax    # 0x2322d3
sbbq $-0x1, %rax
movsbl (%rax), %r8d
cmpq 0x50(%r13), %r10
jae 0xb846d
testq %rbx, %rbx
je 0xb8490
leaq (%r10,%r10,2), %rax
movl (%rbx,%rax,4), %r11d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %r11d
movl 0x4(%rbx,%rax,4), %r15d
jmp 0xb849b
testq %rbx, %rbx
je 0xb8510
leaq (%r10,%r10,2), %rax
movl (%rbx,%rax,4), %r11d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %r11d
movl 0x4(%rbx,%rax,4), %r15d
jmp 0xb851b
movq 0x18(%r13), %rax
movl (%rax,%r10,8), %r15d
xorl %r11d, %r11d
movq 0x28(%r13), %rax
movzbl (%rax,%r10), %eax
leal -0x1(%rax), %ecx
cmpb $0x2, %cl
jb 0xb84ae
xorl %eax, %eax
movq 0x40(%r13), %rcx
movl 0x8(%rcx,%r10,8), %r9d
movq (%rcx,%r10,8), %r13
subl %r13d, %r9d
movq 0x48(%rsp), %rcx
addq 0x8(%rcx), %r13
movzbl %al, %eax
leaq 0x17df6c(%rip), %rcx    # 0x23643d
movsbl (%rax,%rcx), %ebx
subq $0x8, %rsp
movq 0xf8(%rsp), %rdi
leaq 0x17e0c7(%rip), %rsi    # 0x2365af
leaq 0x68(%rsp), %rdx
movq %rbp, %rcx
xorl %eax, %eax
pushq %rbx
pushq %r10
pushq %r15
pushq %r11
pushq %r13
movq 0x78(%rsp), %r13
callq 0x22b0
addq $0x30, %rsp
movq 0x40(%rsp), %rbx
jmp 0xb854c
movq 0x18(%r13), %rax
movl (%rax,%r10,8), %r15d
xorl %r11d, %r11d
movq 0xf0(%rsp), %rdi
leaq 0x17e0ae(%rip), %rsi    # 0x2365d8
leaq 0x60(%rsp), %rdx
movq %rbp, %rcx
leaq 0x17df30(%rip), %r9     # 0x236469
xorl %eax, %eax
pushq $0x2a
pushq %r10
pushq %r15
pushq %r11
callq 0x22b0
addq $0x20, %rsp
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rcx
movl 0x10(%rax), %eax
leaq (%rcx,%rax,8), %rdx
movl (%r12,%rdx), %edx
addq %rdx, %rbp
incq %r14
addq $-0x8, %r12
cmpq %rax, %r14
jb 0xb8428
movq 0x28(%rsp), %r14
movq 0x18(%r14), %rax
movq 0x18(%rsp), %r12
movq (%rax,%r12,8), %rcx
movq %rcx, %r8
shrq $0x20, %r8
movq 0x20(%rsp), %r15
je 0xb85b9
movq 0xf0(%rsp), %rdi
leaq 0x17e05d(%rip), %rsi    # 0x2365ff
leaq 0x60(%rsp), %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x22b0
movq 0x18(%r14), %rax
movq (%rax,%r12,8), %rcx
shrq $0x20, %rcx
addq %rcx, %rbp
incq %r12
cmpq 0x8(%r14), %r12
jb 0xb828d
movq 0x30(%rsp), %rcx
cmpq 0x8(%r15), %rcx
jb 0xb7fb7
movq 0x38(%rsp), %rdi
callq 0x2220
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq