// File: /workspace/github_c_makefile_star2_O3/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 $0xb8, %rsp
movl $0x1, %ebx
cmpl $0x2, %edi
jl 0x6497
movq %rsi, %r14
movl %edi, %ebp
movq 0x8(%rsi), %rax
xorl %r12d, %r12d
cmpb $0x2d, (%rax)
jne 0x5e53
cmpb $0x78, 0x1(%rax)
jne 0x5e53
cmpb $0x0, 0x2(%rax)
leaq 0x8(%r14), %rax
sete %cl
cmoveq %rax, %r14
jmp 0x5e55
xorl %ecx, %ecx
movl %ecx, 0x54(%rsp)
movzbl %cl, %eax
subl %eax, %ebp
cmpl $0x3, %ebp
jb 0x5e8e
movq 0x8(%r14), %rdi
xorl %r12d, %r12d
cmpb $0x2d, (%rdi)
jne 0x5e9b
cmpb $0x66, 0x1(%rdi)
jne 0x5e9b
cmpb $0x0, 0x2(%rdi)
jne 0x5e9b
movq 0x10(%r14), %rdi
addq $0x10, %r14
callq 0x4800
movl %eax, %r12d
addl $-0x2, %ebp
cmpl $0x2, %ebp
jb 0x6497
movq 0x8(%r14), %rdi
leaq 0x8c029(%rip), %rsi     # 0x91ecb
callq 0x9630
testq %rax, %rax
je 0x6497
movq %rax, %rbp
callq 0x163f5
movq %rax, %r13
movq %rbp, %rdi
callq 0x191c1
movq %rax, %r15
callq 0x275f8
movq %rax, %rbx
testq %r15, %r15
setne %al
testq %r13, %r13
setne %cl
testq %rbx, %rbx
setne %dl
andb %cl, %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x646f
movq %rbp, %rdi
movq %r15, %rsi
movq %r13, 0x20(%rsp)
movq %r13, %rdx
callq 0x1d18f
testl %eax, %eax
movq %rbx, 0x28(%rsp)
js 0x64ab
movq %rbp, 0x30(%rsp)
movl %r12d, %edx
leaq 0x8d8ad(%rip), %r12     # 0x937c9
movq 0x20(%rsp), %r13
movl %edx, 0x44(%rsp)
movq %r15, 0x48(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x2760c
testl %eax, %eax
js 0x64fd
cmpl $0x0, 0x18(%r13)
movl 0x54(%rsp), %ebx
leaq 0xb(%rsp), %r14
jle 0x61bb
xorl %r15d, %r15d
movq %r13, %rdi
movq 0x28(%rsp), %rsi
leaq 0x60(%rsp), %rdx
movl $0x5, %ecx
callq 0x27c30
movl 0x14(%r13), %ecx
shll $0x2, %ecx
addq 0x38(%r13), %rcx
movzwl 0x12(%r13), %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, %r13d
andl $0xf, %edx
leaq 0x8941d(%rip), %rax     # 0x8f3c0
movsbl (%rax,%rdx), %edx
leaq 0x89076(%rip), %rdi     # 0x8f024
movq %r15, 0x58(%rsp)
movl %r15d, %esi
xorl %eax, %eax
callq 0x4060
testl %r13d, %r13d
jle 0x619a
movq %r13, %rcx
decl %ecx
cmpq $0x4, %rcx
movl $0x4, %eax
cmovaeq %rax, %rcx
shll $0x4, %ecx
addq $0x10, %rcx
movq %rcx, 0x38(%rsp)
movl $0x9, %r15d
xorl %r13d, %r13d
movl 0x6c(%rsp,%r13), %ecx
cmpl $-0x1, %ecx
je 0x6005
cmpl $-0x2, %ecx
jne 0x600e
movw $0x23, 0x14(%rsp)
jmp 0x6026
movw $0x2e, 0x14(%rsp)
jmp 0x6026
movl $0xa, %esi
leaq 0x14(%rsp), %rdi
leaq 0x96ba3(%rip), %rdx     # 0x9cbc2
xorl %eax, %eax
callq 0x4820
testb %bl, %bl
je 0x60a1
movl 0x60(%rsp,%r13), %esi
movq 0x28(%rsp), %rdi
leaq 0x10(%rsp), %rdx
leaq 0xc(%rsp), %rcx
movq %r14, %r8
callq 0x2832c
testl %eax, %eax
js 0x645b
movsbl 0xb(%rsp), %r14d
cmpl %r14d, 0x64(%rsp,%r13)
jne 0x645b
movslq 0x10(%rsp), %rax
cmpl 0x68(%rsp,%r13), %eax
jne 0x645b
leaq 0x88fc1(%rip), %rcx     # 0x8f037
movsbl (%rax,%rcx), %r8d
movl 0x60(%rsp,%r13), %ecx
testl %ecx, %ecx
jle 0x611c
movb %cl, 0xaa492(%rip)      # 0xb0520
movb $0x0, 0xaa48c(%rip)     # 0xb0521
leaq 0xaa484(%rip), %r12     # 0xb0520
jmp 0x6145
movl 0x60(%rsp,%r13), %ecx
movl 0x64(%rsp,%r13), %ebp
movslq 0x68(%rsp,%r13), %rax
leaq 0x88f80(%rip), %rdx     # 0x8f037
movsbl (%rax,%rdx), %r14d
testl %ecx, %ecx
jle 0x60d6
movb %cl, 0xaa45a(%rip)      # 0xb0520
movb $0x0, 0xaa454(%rip)     # 0xb0521
leaq 0xaa44c(%rip), %r8      # 0xb0520
jmp 0x60ff
negl %ecx
movl $0x14, %esi
leaq 0xaa43c(%rip), %r12     # 0xb0520
movq %r12, %rdi
leaq 0x96f81(%rip), %rdx     # 0x9d06f
xorl %eax, %eax
callq 0x4820
movq %r12, %r8
leaq 0x8d6ca(%rip), %r12     # 0x937c9
leaq 0x88f37(%rip), %rdi     # 0x8f03d
movl %r15d, %esi
movl %ebp, %edx
movl %r14d, %ecx
leaq 0x14(%rsp), %r9
xorl %eax, %eax
callq 0x4060
jmp 0x6180
negl %ecx
movl $0x14, %esi
leaq 0xaa3f6(%rip), %r12     # 0xb0520
movq %r12, %rdi
leaq 0x96f3b(%rip), %rdx     # 0x9d06f
xorl %eax, %eax
movl %ebx, %ebp
movl %r8d, %ebx
callq 0x4820
movl %ebx, %r8d
movl %ebp, %ebx
movslq 0xc(%rsp), %rax
leaq 0x88ee9(%rip), %rcx     # 0x8f03a
movsbl (%rax,%rcx), %r9d
leaq 0x14(%rsp), %rax
movq %rax, (%rsp)
leaq 0x88ec4(%rip), %rdi     # 0x8f02a
movl %r15d, %esi
movl %r14d, %edx
movl %r8d, %ecx
movq %r12, %r8
xorl %eax, %eax
callq 0x4060
leaq 0x8d649(%rip), %r12     # 0x937c9
addq $0x10, %r13
movl $0x20, %r15d
cmpq %r13, 0x38(%rsp)
leaq 0xb(%rsp), %r14
jne 0x5fed
movl $0xa, %edi
callq 0x46c0
movq 0x58(%rsp), %r15
incl %r15d
movq 0x20(%rsp), %r13
cmpl 0x18(%r13), %r15d
jl 0x5f54
leaq 0x88e86(%rip), %rdi     # 0x8f048
callq 0x47e0
movq %r13, %rdi
movq 0x28(%rsp), %rbx
movq %rbx, %rsi
movl 0x44(%rsp), %edx
callq 0x2760c
movl $0x0, 0x10(%rsp)
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x28320
movq %rax, 0x38(%rsp)
leaq 0x88e50(%rip), %rdi     # 0x8f04c
xorl %eax, %eax
callq 0x4060
cmpl $0x0, 0x10(%rsp)
movq %r12, %r15
leaq 0x88e28(%rip), %r12     # 0x8f03a
leaq 0x14(%rsp), %r13
jle 0x626e
movq %r14, %rbp
xorl %r14d, %r14d
movq %rbx, %rdi
movl %r14d, %esi
movq %r13, %rdx
leaq 0xc(%rsp), %rcx
movq %rbp, %r8
callq 0x28396
movq 0x38(%rsp), %rax
movl (%rax,%r14,4), %esi
testl %esi, %esi
leaq 0x8d1c8(%rip), %rdi     # 0x9340f
cmovgq %r15, %rdi
xorl %eax, %eax
callq 0x4060
movslq 0xc(%rsp), %rax
movsbl (%rax,%r12), %edi
callq 0x46c0
incq %r14
movslq 0x10(%rsp), %rax
cmpq %rax, %r14
jl 0x621f
movl $0xa, %edi
callq 0x46c0
movq 0x20(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
leaq 0x60(%rsp), %rdx
movl $0x5, %ecx
leaq 0xc(%rsp), %r8
callq 0x2803b
movl %eax, %r15d
testl %eax, %eax
jle 0x6414
leaq 0x14(%rsp), %r14
leaq 0x88d8f(%rip), %rbp     # 0x8f03d
movl 0xc(%rsp), %esi
movl 0x14(%r13), %eax
shll $0x2, %eax
addq 0x38(%r13), %rax
movzwl 0x12(%r13), %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 0x890d9(%rip), %rcx     # 0x8f3c0
movsbl (%rcx,%rax), %edx
leaq 0x88d32(%rip), %rdi     # 0x8f024
xorl %eax, %eax
callq 0x4060
movl %r15d, %eax
movq %rax, 0x38(%rsp)
movl $0x9, %r15d
leaq 0x6c(%rsp), %rbx
xorl %r12d, %r12d
movl (%rbx), %ecx
cmpl $-0x1, %ecx
je 0x6324
cmpl $-0x2, %ecx
jne 0x632d
movw $0x23, 0x14(%rsp)
jmp 0x6343
movw $0x2e, 0x14(%rsp)
jmp 0x6343
movl $0xa, %esi
movq %r14, %rdi
leaq 0x96886(%rip), %rdx     # 0x9cbc2
xorl %eax, %eax
callq 0x4820
movl -0xc(%rbx), %ecx
movl -0x8(%rbx), %r14d
movslq -0x4(%rbx), %rax
leaq 0x88ce2(%rip), %rdx     # 0x8f037
movsbl (%rax,%rdx), %r13d
testl %ecx, %ecx
jle 0x6374
movb %cl, 0xaa1bc(%rip)      # 0xb0520
movb $0x0, 0xaa1b6(%rip)     # 0xb0521
leaq 0xaa1ae(%rip), %r8      # 0xb0520
jmp 0x639d
negl %ecx
movl $0x14, %esi
leaq 0xaa19e(%rip), %rbp     # 0xb0520
movq %rbp, %rdi
leaq 0x96ce3(%rip), %rdx     # 0x9d06f
xorl %eax, %eax
callq 0x4820
movq %rbp, %r8
leaq 0x88ca0(%rip), %rbp     # 0x8f03d
movq %rbp, %rdi
movl %r15d, %esi
movl %r14d, %edx
movl %r13d, %ecx
leaq 0x14(%rsp), %r14
movq %r14, %r9
xorl %eax, %eax
callq 0x4060
leaq 0x1(%r12), %rax
cmpq 0x38(%rsp), %rax
jae 0x63db
addq $0x10, %rbx
movl $0x20, %r15d
cmpq $0x4, %r12
movq %rax, %r12
jb 0x630f
movl $0xa, %edi
callq 0x46c0
movq 0x20(%rsp), %r13
movq %r13, %rdi
movq 0x28(%rsp), %rbx
movq %rbx, %rsi
leaq 0x60(%rsp), %rdx
movl $0x5, %ecx
leaq 0xc(%rsp), %r8
callq 0x2803b
movl %eax, %r15d
testl %eax, %eax
jg 0x62ae
testl %r15d, %r15d
js 0x64ee
leaq 0x88c31(%rip), %rdi     # 0x8f055
callq 0x47e0
movq 0x30(%rsp), %rdi
movq 0x48(%rsp), %r15
movq %r15, %rsi
movq %r13, %rdx
callq 0x1d18f
testl %eax, %eax
movl 0x44(%rsp), %edx
leaq 0x8d37e(%rip), %r12     # 0x937c9
jns 0x5f2a
movl %eax, %r14d
movq 0x30(%rsp), %rbp
jmp 0x64ae
movq 0x28(%rsp), %rbx
movq 0x48(%rsp), %r15
movq 0x20(%rsp), %r13
movq 0x30(%rsp), %rbp
movq %r13, %rdi
callq 0x164cb
movq %r15, %rdi
callq 0x15aa2
movq %rbx, %rdi
callq 0x27607
movq %rbp, %rdi
callq 0xa892
xorl %ebx, %ebx
cmpl $0x1, %eax
adcl $0x1, %ebx
movl %ebx, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %r14d
movq 0xa9b0b(%rip), %rax     # 0xaffc0
movq (%rax), %rdi
callq 0x45a0
movq %rbp, %rdi
callq 0xa892
notl %r14d
xorl %ebx, %ebx
orl %eax, %r14d
setne %bl
movq 0x20(%rsp), %rdi
callq 0x164cb
movq %r15, %rdi
callq 0x15aa2
movq 0x28(%rsp), %rdi
callq 0x27607
jmp 0x6497
movq 0x30(%rsp), %rbp
movq 0x48(%rsp), %r15
jmp 0x646f
callq 0x49e0
jmp 0x646a
nop