// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/test/test_mod.c
// Function: main
int main(int argc, char **argv) {
    int extended = 0;
    uint32_t flags = 0;

    if (argc > 1 && strcmp(argv[1], "-x") == 0) {
        extended = 1;
        argv++;
        argc--;
    }

    if (argc > 2 && strcmp(argv[1], "-f") == 0) {
        flags = atoi(argv[2]);
        argv+=2;
        argc-=2;
    }

    if (argc < 2)
        return 1;

    samFile *in = sam_open(argv[1], "r");
    if (!in)
        return 1;

    bam1_t *b = bam_init1();
    sam_hdr_t *h = sam_hdr_read(in);
    hts_base_mod_state *m = hts_base_mod_state_alloc();
    if (!h || !b || !m)
        goto err;

    int r;
    while ((r = sam_read1(in, h, b)) >= 0) {
        if (bam_parse_basemod2(b, m, flags) < 0) {
            fprintf(stderr, "Failed to parse MM/ML aux tags\n");
            goto err;
        }

        // per-base iterator
        int i, j, n;
        hts_base_mod mods[5];
        for (i = 0; i < b->core.l_qseq; i++) {
            char sp = '\t';
            n = bam_mods_at_next_pos(b, m, mods, 5);
            printf("%d\t%c", i, seq_nt16_str[bam_seqi(bam_get_seq(b), i)]);
            for (j = 0; j < n && j < 5; j++) {
                char qstr[10];
                if (mods[j].qual == HTS_MOD_UNCHECKED)
                    qstr[0] = '#', qstr[1] = 0;
                else if (mods[j].qual == HTS_MOD_UNKNOWN)
                    qstr[0] = '.', qstr[1] = 0;
                else
                    snprintf(qstr, 10, "%d", mods[j].qual);

                if (extended) {
                    int m_strand, m_implicit;
                    char m_canonical;
                    int ret = bam_mods_query_type(m, mods[j].modified_base,
                                                  &m_strand, &m_implicit,
                                                  &m_canonical);
                    if (ret < 0 ||
                        m_canonical != mods[j].canonical_base ||
                        m_strand    != mods[j].strand)
                        goto err;
                    printf("%c%c%c%s%c%s",
                           sp, mods[j].canonical_base,
                           "+-"[mods[j].strand],
                           code(mods[j].modified_base),
                           "?."[m_implicit],
                           qstr);
                } else {
                    printf("%c%c%c%s%s",
                           sp, mods[j].canonical_base,
                           "+-"[mods[j].strand],
                           code(mods[j].modified_base),
                           qstr);
                }
                sp = ' ';
            }
            putchar('\n');
        }

        puts("---");

        bam_parse_basemod2(b, m, flags);

        // List possible mod choices.
        int *all_mods;
        int all_mods_n = 0;
        all_mods = bam_mods_recorded(m, &all_mods_n);
        printf("Present:");
        for (i = 0; i < all_mods_n; i++) {
            int m_strand, m_implicit;
            char m_canonical;
            bam_mods_queryi(m, i, &m_strand, &m_implicit, &m_canonical);
            printf(all_mods[i] > 0 ? " %c" : " #%d", all_mods[i]);
            putchar("?."[m_implicit]);
        }
        putchar('\n');

        int pos;
        while ((n=bam_next_basemod(b, m, mods, 5, &pos)) > 0) {
            char sp = '\t';
            printf("%d\t%c", pos,
                   seq_nt16_str[bam_seqi(bam_get_seq(b), pos)]);
            for (j = 0; j < n && j < 5; j++) {
                char qstr[10];
                if (mods[j].qual == HTS_MOD_UNCHECKED)
                    qstr[0] = '#', qstr[1] = 0;
                else if (mods[j].qual == HTS_MOD_UNKNOWN)
                    qstr[0] = '.', qstr[1] = 0;
                else
                    snprintf(qstr, 10, "%d", mods[j].qual);

                printf("%c%c%c%s%s",
                       sp, mods[j].canonical_base,
                       "+-"[mods[j].strand],
                       code(mods[j].modified_base),
                       qstr);
                sp = ' ';
            }
            putchar('\n');
        }

        if (n < 0)
            goto err;

        puts("\n===\n");
    }
    fflush(stdout);
    int ret = 0;
    if (sam_close(in) != 0 || r < -1)
        ret = 1;

    bam_destroy1(b);
    sam_hdr_destroy(h);
    hts_base_mod_state_free(m);
    return ret;

 err:
    bam_destroy1(b);
    sam_hdr_destroy(h);
    hts_base_mod_state_free(m);
    return sam_close(in) != 0 ? 1 : 2;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
pushq $0x1
popq %rbx
cmpl $0x2, %edi
jl 0x6000
movq %rsi, %r14
movl %edi, %ebp
movq 0x8(%rsi), %rax
xorl %r12d, %r12d
cmpb $0x2d, (%rax)
jne 0x5b02
cmpb $0x78, 0x1(%rax)
jne 0x5b02
cmpb $0x0, 0x2(%rax)
leaq 0x8(%r14), %rax
sete %r15b
cmoveq %rax, %r14
jmp 0x5b05
xorl %r15d, %r15d
movzbl %r15b, %eax
subl %eax, %ebp
cmpl $0x3, %ebp
jb 0x5b3b
movq 0x8(%r14), %rdi
xorl %r12d, %r12d
cmpb $0x2d, (%rdi)
jne 0x5b48
cmpb $0x66, 0x1(%rdi)
jne 0x5b48
cmpb $0x0, 0x2(%rdi)
jne 0x5b48
movq 0x10(%r14), %rdi
addq $0x10, %r14
callq 0x4800
movl %eax, %r12d
addl $-0x2, %ebp
cmpl $0x2, %ebp
jb 0x6000
movq 0x8(%r14), %rdi
leaq 0x6e347(%rip), %rsi     # 0x73e96
callq 0x82f7
testq %rax, %rax
je 0x6000
movq %rax, %r14
callq 0x136a4
movq %rax, %r13
movq %r14, %rdi
callq 0x15e2e
movq %rax, %rbx
callq 0x1f928
movq %rbx, %rsi
movq %rax, %rbx
testq %rsi, %rsi
setne %al
testq %r13, %r13
setne %cl
testq %rbx, %rbx
setne %dl
andb %cl, %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x5fd5
movq %r13, 0x20(%rsp)
leaq 0x6fbc8(%rip), %rbp     # 0x75771
movq %rbx, 0x38(%rsp)
movl %r12d, 0x40(%rsp)
movl %r15d, 0x44(%rsp)
movq %r14, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movq %r14, %rdi
movq 0x20(%rsp), %r13
movq %r13, %rdx
callq 0x18bc2
testl %eax, %eax
js 0x6014
movq %r13, %rdi
movq %rbx, %rsi
movl %r12d, %edx
callq 0x1f93a
testl %eax, %eax
js 0x605a
xorl %eax, %eax
movq 0x20(%rsp), %r14
cmpl 0x18(%r14), %eax
pushq $0x5
popq %rcx
jge 0x5dd6
movq %r14, %rdi
movq %rax, %r15
movq %rbx, %rsi
leaq 0x50(%rsp), %r12
movq %r12, %rdx
callq 0x1ff7d
movl 0x14(%r14), %ecx
shll $0x2, %ecx
addq 0x38(%r14), %rcx
movzwl 0x12(%r14), %edx
addq %rcx, %rdx
movl %r15d, %ecx
shrl %ecx
movzbl (%rcx,%rdx), %edx
leal (,%r15,4), %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %edx
movl %eax, %r14d
andl $0xf, %edx
leaq 0x6b76e(%rip), %rax     # 0x713c0
movsbl (%rax,%rdx), %edx
leaq 0x6b3c7(%rip), %rdi     # 0x71024
movq %r15, 0x48(%rsp)
movl %r15d, %esi
xorl %eax, %eax
callq 0x4060
movslq %r14d, %rbp
xorl %ebx, %ebx
pushq $0x9
popq %r15
cmpq %rbp, %rbx
jge 0x5db1
cmpq $0x4, %rbx
ja 0x5db1
movl 0xc(%r12), %ecx
cmpl $-0x1, %ecx
je 0x5ca0
cmpl $-0x2, %ecx
jne 0x5ca9
movw $0x23, 0x14(%rsp)
jmp 0x5cbf
movw $0x2e, 0x14(%rsp)
jmp 0x5cbf
leaq 0x14(%rsp), %rdi
pushq $0xa
popq %rsi
leaq 0x787f1(%rip), %rdx     # 0x7e4a9
xorl %eax, %eax
callq 0x4820
cmpb $0x0, 0x44(%rsp)
je 0x5d5f
movl (%r12), %esi
movq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rdx
leaq 0xc(%rsp), %rcx
leaq 0xb(%rsp), %r8
callq 0x205ca
testl %eax, %eax
js 0x5fc1
movsbl 0xb(%rsp), %r14d
cmpl %r14d, 0x4(%r12)
jne 0x5fc1
movslq 0x10(%rsp), %rax
cmpl 0x8(%r12), %eax
jne 0x5fc1
leaq 0x6b320(%rip), %rcx     # 0x71037
movsbl (%rax,%rcx), %r13d
movl (%r12), %edi
callq 0x6080
movslq 0xc(%rsp), %rax
leaq 0x6b309(%rip), %rcx     # 0x7103a
movsbl (%rax,%rcx), %r9d
leaq 0x14(%rsp), %rax
movq %rax, (%rsp)
leaq 0x6b2e4(%rip), %rdi     # 0x7102a
movl %r15d, %esi
movl %r14d, %edx
movl %r13d, %ecx
leaq 0x917ca(%rip), %r8      # 0x97520
xorl %eax, %eax
callq 0x4060
jmp 0x5da1
movl (%r12), %edi
movl 0x4(%r12), %r14d
movslq 0x8(%r12), %rax
leaq 0x6b2c3(%rip), %rcx     # 0x71037
movsbl (%rax,%rcx), %r13d
callq 0x6080
leaq 0x6b2b8(%rip), %rdi     # 0x7103d
movl %r15d, %esi
movl %r14d, %edx
movl %r13d, %ecx
leaq 0x9178b(%rip), %r8      # 0x97520
leaq 0x14(%rsp), %r9
xorl %eax, %eax
callq 0x4060
incq %rbx
addq $0x10, %r12
pushq $0x20
popq %r15
jmp 0x5c75
pushq $0xa
popq %rdi
callq 0x46c0
movq 0x48(%rsp), %rax
incl %eax
movl 0x40(%rsp), %r12d
movq 0x38(%rsp), %rbx
leaq 0x6f9a0(%rip), %rbp     # 0x75771
jmp 0x5bf2
leaq 0x6b26b(%rip), %rdi     # 0x71048
callq 0x47e0
movq %r14, %rdi
movq %rbx, %rsi
movl %r12d, %edx
callq 0x1f93a
andl $0x0, 0x10(%rsp)
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x205be
movq %rax, %r14
leaq 0x6b240(%rip), %rdi     # 0x7104c
xorl %eax, %eax
callq 0x4060
xorl %r15d, %r15d
movslq 0x10(%rsp), %rax
cmpq %rax, %r15
jge 0x5e6c
movq %rbx, %rdi
movl %r15d, %esi
leaq 0x14(%rsp), %rdx
leaq 0xc(%rsp), %rcx
leaq 0xb(%rsp), %r8
callq 0x20638
movl (%r14,%r15,4), %esi
testl %esi, %esi
leaq 0x6f570(%rip), %rdi     # 0x753b7
cmovgq %rbp, %rdi
xorl %eax, %eax
callq 0x4060
movslq 0xc(%rsp), %rax
leaq 0x6b1dc(%rip), %rcx     # 0x7103a
movsbl (%rax,%rcx), %edi
callq 0x46c0
incq %r15
jmp 0x5e16
pushq $0xa
popq %rdi
callq 0x46c0
movq 0x20(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
leaq 0x50(%rsp), %rdx
pushq $0x5
popq %rbp
movl %ebp, %ecx
leaq 0xc(%rsp), %r8
callq 0x2030d
testl %eax, %eax
jle 0x5f99
movl %eax, %r14d
movl 0xc(%rsp), %esi
movl 0x14(%r15), %eax
shll $0x2, %eax
addq 0x38(%r15), %rax
movzwl 0x12(%r15), %ecx
addq %rax, %rcx
movl %esi, %eax
sarl %eax
cltq
movzbl (%rax,%rcx), %eax
leal (,%rsi,4), %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %eax
andl $0xf, %eax
leaq 0x6b4e9(%rip), %rcx     # 0x713c0
movsbl (%rcx,%rax), %edx
leaq 0x6b142(%rip), %rdi     # 0x71024
xorl %eax, %eax
callq 0x4060
cmpl $0x5, %r14d
cmovael %ebp, %r14d
shll $0x4, %r14d
xorl %ebx, %ebx
pushq $0x9
popq %r15
cmpq %rbx, %r14
je 0x5f87
movl 0x5c(%rsp,%rbx), %ecx
cmpl $-0x1, %ecx
je 0x5f1b
cmpl $-0x2, %ecx
jne 0x5f24
movw $0x23, 0x14(%rsp)
jmp 0x5f3a
movw $0x2e, 0x14(%rsp)
jmp 0x5f3a
leaq 0x14(%rsp), %rdi
pushq $0xa
popq %rsi
leaq 0x78576(%rip), %rdx     # 0x7e4a9
xorl %eax, %eax
callq 0x4820
movl 0x50(%rsp,%rbx), %edi
movl 0x54(%rsp,%rbx), %r13d
movslq 0x58(%rsp,%rbx), %rax
leaq 0x6b0e8(%rip), %rcx     # 0x71037
movsbl (%rax,%rcx), %ebp
callq 0x6080
leaq 0x6b0de(%rip), %rdi     # 0x7103d
movl %r15d, %esi
movl %r13d, %edx
movl %ebp, %ecx
leaq 0x915b2(%rip), %r8      # 0x97520
leaq 0x14(%rsp), %r9
xorl %eax, %eax
callq 0x4060
addq $0x10, %rbx
pushq $0x20
popq %r15
jmp 0x5efb
pushq $0xa
popq %rdi
callq 0x46c0
movq 0x38(%rsp), %rbx
jmp 0x5e74
leaq 0x6f7d1(%rip), %rbp     # 0x75771
js 0x6076
leaq 0x6b0a8(%rip), %rdi     # 0x71055
callq 0x47e0
movq 0x30(%rsp), %r14
movq 0x28(%rsp), %rsi
jmp 0x5bc2
movq 0x30(%rsp), %r14
movq 0x38(%rsp), %rbx
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %r13
movq %r13, %rdi
movq %rsi, %r15
callq 0x13772
movq %r15, %rdi
callq 0x12dfe
movq %rbx, %rdi
callq 0x1f935
movq %r14, %rdi
callq 0x94cd
xorl %ebx, %ebx
cmpl $0x1, %eax
adcl $0x1, %ebx
movl %ebx, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %r14d
movq 0x90fa2(%rip), %rax     # 0x96fc0
movq (%rax), %rdi
callq 0x45a0
movq 0x30(%rsp), %rdi
callq 0x94cd
notl %r14d
movq %rbx, %r15
xorl %ebx, %ebx
orl %eax, %r14d
setne %bl
movq %r13, %rdi
callq 0x13772
movq 0x28(%rsp), %rdi
callq 0x12dfe
movq %r15, %rdi
callq 0x1f935
jmp 0x6000
movq 0x90f7f(%rip), %rax     # 0x96fe0
movq (%rax), %rcx
leaq 0x6af99(%rip), %rdi     # 0x71004
pushq $0x1f
popq %rsi
pushq $0x1
popq %rdx
callq 0x49b0
movq 0x30(%rsp), %r14
jmp 0x5fcb