// File: /workspace/github_cpp_makefile_star2_O1/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 $0x98, %rsp
movq %r9, 0x50(%rsp)
movq %r8, 0x48(%rsp)
movq %rcx, %r15
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %r14
testq %rdx, %rdx
je 0xb6fe0
movl 0x14(%rbx), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl $0x1, %esi
callq 0x2120
movq %rax, 0x40(%rsp)
jmp 0xb6fe9
movq $0x0, 0x40(%rsp)
movq %rbx, 0x80(%rsp)
testq %rbx, %rbx
setne %sil
testq %r15, %r15
setne %al
cmpq $0x0, 0x48(%rsp)
setne %cl
cmpq $0x0, 0x50(%rsp)
setne %dl
cmpq $0x0, 0x8(%r14)
je 0xb766e
andb %dl, %cl
andb %al, %sil
andb %cl, %sil
movb %sil, 0xf(%rsp)
xorl %ecx, %ecx
movq %r15, 0x58(%rsp)
movq %r14, 0x30(%rsp)
movq %r13, 0x28(%rsp)
movq (%r14), %rax
movq %rcx, 0x38(%rsp)
shlq $0x5, %rcx
leaq (%rax,%rcx), %rdx
cmpq $0x0, 0x8(%rax,%rcx)
movq %rdx, 0x20(%rsp)
je 0xb7115
movq 0x18(%r14), %r14
xorl %r12d, %r12d
movq $0x0, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
xorl %ebp, %ebp
movq 0x10(%r14), %rax
movq 0x18(%rdx), %rcx
movq (%rcx,%r12,8), %r13
movl %r13d, %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
movl (%rax,%rcx,8), %ebx
cmpb $0x0, 0xf(%rsp)
je 0xb70f1
leaq (%rax,%rcx,8), %rdi
movq 0x80(%rsp), %rsi
movq %r15, %rdx
movq 0x48(%rsp), %rcx
movq 0x50(%rsp), %r8
movq 0x40(%rsp), %r9
leaq 0x88(%rsp), %rax
pushq %rax
leaq 0x98(%rsp), %rax
pushq %rax
callq 0xb62e5
addq $0x10, %rsp
movq 0x10(%rsp), %rax
addq 0x90(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rax
addq 0x88(%rsp), %rax
movq %rax, 0x18(%rsp)
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
shrq $0x20, %r13
addq %r13, %rbp
addq %rbx, %rbp
incq %r12
movq 0x20(%rsp), %rdx
cmpq 0x8(%rdx), %r12
jb 0xb7075
jmp 0xb7129
xorl %ebp, %ebp
movq $0x0, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movq 0x38(%rsp), %r9
incq %r9
movq 0x10(%rdx), %rax
leaq 0x1714ef(%rip), %rcx    # 0x22862b
movsbl (%rax,%rcx), %r8d
leaq 0x60(%rsp), %rdi
leaq 0x1731d5(%rip), %rsi    # 0x22a322
movq 0xd8(%rsp), %rdx
movq %r9, 0x38(%rsp)
movq %r9, %rcx
xorl %eax, %eax
callq 0x2050
movq 0x10(%rsp), %rcx
testq %rcx, %rcx
je 0xb717a
movq 0x18(%rsp), %rax
xorl %edx, %edx
divq %rcx
jmp 0xb717c
xorl %eax, %eax
movq 0x30(%rsp), %r14
cmpl $0x0, 0xd0(%rsp)
movq 0x28(%rsp), %r13
je 0xb72c4
movq %rax, 0x10(%rsp)
movq 0xe0(%rsp), %rdi
leaq 0x173211(%rip), %rsi    # 0x22a3b9
leaq 0x60(%rsp), %rdx
xorl %eax, %eax
callq 0x22b0
movq 0x20(%rsp), %rcx
cmpq $0x0, 0x8(%rcx)
je 0xb72a4
xorl %ebx, %ebx
movq 0x18(%r14), %rax
movq 0x18(%rcx), %rcx
movq (%rcx,%rbx,8), %rcx
movl %ecx, %edx
shrl %edx
leaq (%rdx,%rdx,4), %r14
shlq $0x3, %r14
addq 0x10(%rax), %r14
testb $0x1, %cl
jne 0xb71fa
movq 0x20(%r14), %rdi
movq 0xe0(%rsp), %rsi
callq 0x21d0
jmp 0xb7252
movl (%r14), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
je 0xb7252
movq $-0x1, %r12
xorl %r13d, %r13d
movl %eax, %eax
addq 0x20(%r14), %rax
movsbq (%r12,%rax), %rax
testq %rax, %rax
js 0xb722d
leaq 0x1729e9(%rip), %rcx    # 0x229c10
movsbl (%rax,%rcx), %edi
jmp 0xb7232
movl $0x4e, %edi
movq 0xe0(%rsp), %rsi
callq 0x2270
incq %r13
movl (%r14), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
decq %r12
cmpq %rax, %r13
jb 0xb7210
movq 0x20(%rsp), %r13
movq 0x18(%r13), %rax
cmpl $0x0, 0x4(%rax,%rbx,8)
movq 0xe0(%rsp), %r12
je 0xb728a
xorl %r14d, %r14d
movl $0x4e, %edi
movq %r12, %rsi
callq 0x2270
incq %r14
movq 0x18(%r13), %rax
movl 0x4(%rax,%rbx,8), %eax
cmpq %rax, %r14
jb 0xb726d
incq %rbx
cmpq 0x8(%r13), %rbx
movq %r13, %rcx
movq 0x28(%rsp), %r13
movq 0x30(%rsp), %r14
jb 0xb71c6
movq 0xe0(%rsp), %rdi
leaq 0x1730f2(%rip), %rsi    # 0x22a3a5
movq %rbp, %rdx
movq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x22b0
jmp 0xb72e5
movq 0xe0(%rsp), %rdi
leaq 0x1730cc(%rip), %rsi    # 0x22a39f
leaq 0x60(%rsp), %rdx
movq %rbp, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x22b0
movq 0x20(%rsp), %rcx
cmpq $0x0, 0x8(%rcx)
je 0xb765f
xorl %ebx, %ebx
xorl %ebp, %ebp
movq 0x18(%r14), %rax
movq 0x10(%rax), %rax
movq 0x18(%rcx), %rcx
movq (%rcx,%rbx,8), %rcx
movl %ecx, %edx
shrl %edx
leaq (%rdx,%rdx,4), %rdx
leaq (%rax,%rdx,8), %r12
movl 0x10(%r12), %eax
testb $0x1, %cl
movq %rbx, 0x18(%rsp)
jne 0xb748d
testq %rax, %rax
je 0xb7607
xorl %ebx, %ebx
movq 0x18(%r12), %rax
movq (%rax,%rbx,8), %rax
movq %rax, %r10
shrq $0x21, %r10
btq $0x20, %rax
leaq 0x16ee74(%rip), %rax    # 0x2261c3
adcq $0x0, %rax
movsbl (%rax), %r8d
cmpq 0x50(%r13), %r10
jae 0xb7374
testq %r15, %r15
je 0xb738b
leaq (%r10,%r10,2), %rax
movl (%r15,%rax,4), %r11d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %r11d
jmp 0xb738e
leaq (%r10,%r10,2), %rax
testq %r15, %r15
je 0xb739e
movl (%r15,%rax,4), %r11d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %r11d
jmp 0xb73a1
xorl %r11d, %r11d
testq %r15, %r15
je 0xb73b4
leaq (%r10,%r10,2), %rax
movl 0x4(%r15,%rax,4), %r14d
jmp 0xb73bc
xorl %r11d, %r11d
testq %r15, %r15
je 0xb7433
movl 0x4(%r15,%rax,4), %r14d
jmp 0xb743b
movq 0x18(%r13), %rax
movl (%rax,%r10,8), %r14d
movq 0x28(%r13), %rax
movzbl (%rax,%r10), %eax
leal -0x1(%rax), %ecx
cmpb $0x2, %cl
jb 0xb73cf
xorl %eax, %eax
movq 0x40(%r13), %rcx
movl 0x8(%rcx,%r10,8), %r9d
movq (%rcx,%r10,8), %r13
subl %r13d, %r9d
movq 0x28(%rsp), %rcx
addq 0x8(%rcx), %r13
movzbl %al, %eax
leaq 0x172e5b(%rip), %rcx    # 0x22a24d
movsbl (%rax,%rcx), %r15d
subq $0x8, %rsp
movq 0xe8(%rsp), %rdi
leaq 0x172fb5(%rip), %rsi    # 0x22a3bf
leaq 0x68(%rsp), %rdx
movq %rbp, %rcx
xorl %eax, %eax
pushq %r15
pushq %r10
pushq %r14
pushq %r11
pushq %r13
movq 0x58(%rsp), %r13
callq 0x22b0
addq $0x30, %rsp
movq 0x58(%rsp), %r15
jmp 0xb746c
movq 0x18(%r13), %rax
movl (%rax,%r10,8), %r14d
movq 0xe0(%rsp), %rdi
leaq 0x172f9e(%rip), %rsi    # 0x22a3e8
leaq 0x60(%rsp), %rdx
movq %rbp, %rcx
leaq 0x172e20(%rip), %r9     # 0x22a279
xorl %eax, %eax
pushq $0x2a
pushq %r10
pushq %r14
pushq %r11
callq 0x22b0
addq $0x20, %rsp
movq 0x18(%r12), %rax
movl (%rax,%rbx,8), %eax
addq %rax, %rbp
incq %rbx
movl 0x10(%r12), %eax
cmpq %rax, %rbx
jb 0xb7333
jmp 0xb7607
testq %rax, %rax
je 0xb7607
movq $-0x8, %r14
xorl %ebx, %ebx
shlq $0x3, %rax
addq 0x18(%r12), %rax
movq (%r14,%rax), %rax
movq %rax, %r10
shrq $0x21, %r10
btq $0x20, %rax
leaq 0x16ed04(%rip), %rax    # 0x2261c3
sbbq $-0x1, %rax
movsbl (%rax), %r8d
cmpq 0x50(%r13), %r10
jae 0xb74e4
testq %r15, %r15
je 0xb7502
leaq (%r10,%r10,2), %rax
movl (%r15,%rax,4), %r11d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %r11d
jmp 0xb7505
leaq (%r10,%r10,2), %rax
testq %r15, %r15
je 0xb7591
movl (%r15,%rax,4), %r11d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %r11d
jmp 0xb7594
xorl %r11d, %r11d
testq %r15, %r15
je 0xb7515
leaq (%r10,%r10,2), %rax
movl 0x4(%r15,%rax,4), %eax
jmp 0xb751d
movq 0x18(%r13), %rax
movl (%rax,%r10,8), %eax
movq %rax, 0x10(%rsp)
movq 0x28(%r13), %rax
movzbl (%rax,%r10), %eax
leal -0x1(%rax), %ecx
cmpb $0x2, %cl
jb 0xb7535
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 0x172cfa(%rip), %rcx    # 0x22a24d
movsbl (%rax,%rcx), %r13d
subq $0x8, %rsp
movq 0xe8(%rsp), %rdi
leaq 0x172e54(%rip), %rsi    # 0x22a3bf
leaq 0x68(%rsp), %rdx
movq %rbp, %rcx
xorl %eax, %eax
pushq %r13
pushq %r10
pushq 0x28(%rsp)
pushq %r11
pushq %r15
callq 0x22b0
addq $0x30, %rsp
movq 0x58(%rsp), %r15
jmp 0xb75d9
xorl %r11d, %r11d
testq %r15, %r15
je 0xb75a0
movl 0x4(%r15,%rax,4), %r13d
jmp 0xb75a8
movq 0x18(%r13), %rax
movl (%rax,%r10,8), %r13d
movq 0xe0(%rsp), %rdi
leaq 0x172e31(%rip), %rsi    # 0x22a3e8
leaq 0x60(%rsp), %rdx
movq %rbp, %rcx
leaq 0x172cb3(%rip), %r9     # 0x22a279
xorl %eax, %eax
pushq $0x2a
pushq %r10
pushq %r13
pushq %r11
callq 0x22b0
addq $0x20, %rsp
movl 0x10(%r12), %eax
leaq (,%rax,8), %rcx
addq 0x18(%r12), %rcx
movl (%r14,%rcx), %ecx
addq %rcx, %rbp
incq %rbx
addq $-0x8, %r14
cmpq %rax, %rbx
movq 0x28(%rsp), %r13
jb 0xb749f
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rsp), %rbx
movq (%rax,%rbx,8), %r8
shrq $0x20, %r8
movq 0x30(%rsp), %r14
je 0xb7642
movq 0xe0(%rsp), %rdi
leaq 0x172ddc(%rip), %rsi    # 0x22a40f
leaq 0x60(%rsp), %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x22b0
movq 0x20(%rsp), %rcx
movq 0x18(%rcx), %rax
movl 0x4(%rax,%rbx,8), %eax
addq %rax, %rbp
incq %rbx
cmpq 0x8(%rcx), %rbx
jb 0xb72f9
movq 0x38(%rsp), %rcx
cmpq 0x8(%r14), %rcx
jb 0xb7039
movq 0x40(%rsp), %rdi
callq 0x2220
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq