// File: /workspace/github_c_makefile_star2_O1/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
movq %rsi, %rbx
movl %edi, %ebp
movb $0x1, %al
movl %eax, 0x3c(%rsp)
cmpl $0x2, %edi
jl 0x5e3f
movq 0x8(%rbx), %rdi
leaq 0x8b1dc(%rip), %rsi     # 0x91004
callq 0x46d0
testl %eax, %eax
jne 0x5e3f
addq $0x8, %rbx
decl %ebp
movl $0x0, 0x3c(%rsp)
movl $0x0, 0x2c(%rsp)
cmpl $0x3, %ebp
jl 0x5e74
movq 0x8(%rbx), %rdi
leaq 0x8b1b0(%rip), %rsi     # 0x91007
callq 0x46d0
testl %eax, %eax
jne 0x5e74
movq 0x10(%rbx), %rdi
addq $0x10, %rbx
callq 0x4800
movl %eax, 0x2c(%rsp)
addl $-0x2, %ebp
movl $0x1, %r14d
cmpl $0x2, %ebp
jl 0x64a3
movq 0x8(%rbx), %rdi
leaq 0x8e0a6(%rip), %rsi     # 0x93f34
callq 0x994f
testq %rax, %rax
je 0x64a3
movq %rax, %rbx
callq 0x16a47
movq %rax, %r15
movq %rbx, %rdi
callq 0x197fd
movq %rax, %r13
callq 0x28508
movq %rax, %r14
testq %r13, %r13
setne %al
testq %r15, %r15
setne %cl
testq %r14, %r14
setne %dl
andb %cl, %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x642f
movq %r15, 0x40(%rsp)
leaq 0x17(%rsp), %r12
movq %r14, 0x58(%rsp)
movq %rbx, 0x50(%rsp)
movq %r13, 0x48(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
movq 0x40(%rsp), %rbx
movq %rbx, %rdx
callq 0x1d9d9
testl %eax, %eax
js 0x645b
movq %rbx, %rdi
movq %r14, %rsi
movl 0x2c(%rsp), %edx
callq 0x2851c
testl %eax, %eax
js 0x64b8
cmpl $0x0, 0x18(%rbx)
leaq 0x20(%rsp), %r13
jle 0x61b1
xorl %r15d, %r15d
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x60(%rsp), %rdx
movl $0x5, %ecx
callq 0x28c11
movl 0x14(%rbx), %ecx
shll $0x2, %ecx
addq 0x38(%rbx), %rcx
movzwl 0x12(%rbx), %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
movq %r15, %rsi
movl %eax, %ebp
andl $0xf, %edx
leaq 0x8b3dd(%rip), %rax     # 0x91360
movsbl (%rax,%rdx), %edx
leaq 0x8b09c(%rip), %rdi     # 0x9102a
movq %r15, 0x30(%rsp)
xorl %eax, %eax
callq 0x4060
testl %ebp, %ebp
jle 0x617f
decl %ebp
cmpq $0x4, %rbp
movl $0x4, %eax
cmovaeq %rax, %rbp
shll $0x4, %ebp
addq $0x10, %rbp
movb $0x9, %bl
xorl %r14d, %r14d
movl 0x6c(%rsp,%r14), %ecx
cmpl $-0x1, %ecx
je 0x5fd5
cmpl $-0x2, %ecx
jne 0x5fde
movw $0x23, 0x20(%rsp)
jmp 0x5ff4
movw $0x2e, 0x20(%rsp)
jmp 0x5ff4
movl $0xa, %esi
movq %r13, %rdi
leaq 0x9846a(%rip), %rdx     # 0x9e457
xorl %eax, %eax
callq 0x4820
cmpb $0x0, 0x3c(%rsp)
je 0x6037
movl 0x60(%rsp,%r14), %ecx
movl 0x64(%rsp,%r14), %r13d
movslq 0x68(%rsp,%r14), %rax
leaq 0x8b02c(%rip), %rdx     # 0x9103d
movsbl (%rax,%rdx), %r15d
testl %ecx, %ecx
jle 0x60b2
movb %cl, 0xab4fc(%rip)      # 0xb1520
movb $0x0, 0xab4f6(%rip)     # 0xb1521
leaq 0xab4ee(%rip), %r12     # 0xb1520
jmp 0x60d1
movl 0x60(%rsp,%r14), %esi
movq 0x58(%rsp), %rdi
leaq 0x1c(%rsp), %rdx
leaq 0x18(%rsp), %rcx
movq %r12, %r8
callq 0x29377
movl $0x2, %r15d
testl %eax, %eax
js 0x60fa
movsbl 0x17(%rsp), %eax
movl 0x64(%rsp,%r14), %r13d
cmpl %eax, %r13d
jne 0x60a6
movslq 0x68(%rsp,%r14), %rax
cmpl %eax, 0x1c(%rsp)
jne 0x60a6
leaq 0x8afbb(%rip), %rcx     # 0x9103d
movsbl (%rax,%rcx), %r8d
movl 0x60(%rsp,%r14), %ecx
testl %ecx, %ecx
jle 0x60fe
movb %cl, 0xab48a(%rip)      # 0xb1520
movb $0x0, 0xab484(%rip)     # 0xb1521
leaq 0xab47c(%rip), %r12     # 0xb1520
jmp 0x6123
xorl %eax, %eax
leaq 0x20(%rsp), %r13
jmp 0x6164
negl %ecx
movl $0x14, %esi
leaq 0xab460(%rip), %r12     # 0xb1520
movq %r12, %rdi
leaq 0x98835(%rip), %rdx     # 0x9e8ff
xorl %eax, %eax
callq 0x4820
movzbl %bl, %esi
leaq 0x8af68(%rip), %rdi     # 0x91043
movl %r13d, %edx
movl %r15d, %ecx
movq %r12, %r8
leaq 0x20(%rsp), %r13
movq %r13, %r9
xorl %eax, %eax
callq 0x4060
leaq 0x17(%rsp), %r12
jmp 0x6168
xorl %eax, %eax
jmp 0x6164
negl %ecx
movl $0x14, %esi
leaq 0xab414(%rip), %r12     # 0xb1520
movq %r12, %rdi
leaq 0x987e9(%rip), %rdx     # 0x9e8ff
xorl %eax, %eax
movl %r8d, %r15d
callq 0x4820
movl %r15d, %r8d
leaq 0x8af16(%rip), %rcx     # 0x91040
movzbl %bl, %esi
movslq 0x18(%rsp), %rax
movsbl (%rax,%rcx), %r9d
leaq 0x20(%rsp), %rax
movq %rax, (%rsp)
xorl %r15d, %r15d
leaq 0x8aee6(%rip), %rdi     # 0x91030
movl %r13d, %edx
movq %rax, %r13
movl %r8d, %ecx
movq %r12, %r8
xorl %eax, %eax
callq 0x4060
movb $0x1, %al
leaq 0x17(%rsp), %r12
testb %al, %al
je 0x616d
movb $0x20, %bl
xorl %r15d, %r15d
testl %r15d, %r15d
jne 0x618c
addq $0x10, %r14
cmpq %r14, %rbp
jne 0x5fbd
movl $0xa, %edi
callq 0x46c0
xorl %r15d, %r15d
testl %r15d, %r15d
movq 0x58(%rsp), %r14
jne 0x6411
movq 0x30(%rsp), %r15
incl %r15d
movq 0x40(%rsp), %rbx
cmpl 0x18(%rbx), %r15d
jl 0x5f36
leaq 0x8ae96(%rip), %rdi     # 0x9104e
callq 0x47e0
movq %rbx, %rdi
movq %r14, %rsi
movl 0x2c(%rsp), %edx
callq 0x2851c
movl $0x0, 0x1c(%rsp)
movq %r14, %rdi
leaq 0x1c(%rsp), %rsi
callq 0x2936b
movq %rax, 0x30(%rsp)
leaq 0x8ae65(%rip), %rdi     # 0x91052
xorl %eax, %eax
callq 0x4060
cmpl $0x0, 0x1c(%rsp)
leaq 0x8f1df(%rip), %r15     # 0x953df
jle 0x6260
movq %r13, %rbp
xorl %r13d, %r13d
movq %r14, %rdi
movl %r13d, %esi
movq %rbp, %rdx
leaq 0x18(%rsp), %rcx
movq %r12, %r8
callq 0x293de
movq 0x30(%rsp), %rax
movl (%rax,%r13,4), %esi
testl %esi, %esi
movq %r15, %rdi
leaq 0x8f566(%rip), %rax     # 0x95799
cmovgq %rax, %rdi
xorl %eax, %eax
callq 0x4060
movslq 0x18(%rsp), %rax
leaq 0x8adf6(%rip), %rcx     # 0x91040
movsbl (%rax,%rcx), %edi
callq 0x46c0
incq %r13
movslq 0x1c(%rsp), %rax
cmpq %rax, %r13
jl 0x6208
movl $0xa, %edi
callq 0x46c0
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x60(%rsp), %rdx
movl $0x5, %ecx
leaq 0x18(%rsp), %r8
callq 0x2908a
movl %eax, %ebp
testl %eax, %eax
jle 0x63f1
leaq 0x20(%rsp), %r15
movl 0x18(%rsp), %esi
movl 0x14(%rbx), %eax
shll $0x2, %eax
addq 0x38(%rbx), %rax
movzwl 0x12(%rbx), %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 0x8b096(%rip), %rcx     # 0x91360
movsbl (%rcx,%rax), %edx
leaq 0x8ad55(%rip), %rdi     # 0x9102a
xorl %eax, %eax
callq 0x4060
movl %ebp, %eax
movq %rax, 0x30(%rsp)
movl $0x9, %ebp
leaq 0x6c(%rsp), %r14
xorl %r12d, %r12d
movl (%r14), %ecx
cmpl $-0x1, %ecx
je 0x6306
cmpl $-0x2, %ecx
jne 0x630f
movw $0x23, 0x20(%rsp)
jmp 0x6325
movw $0x2e, 0x20(%rsp)
jmp 0x6325
movl $0xa, %esi
movq %r15, %rdi
leaq 0x98139(%rip), %rdx     # 0x9e457
xorl %eax, %eax
callq 0x4820
movl -0xc(%r14), %ecx
movl -0x8(%r14), %r15d
movslq -0x4(%r14), %rax
leaq 0x8ad05(%rip), %rdx     # 0x9103d
movsbl (%rax,%rdx), %r13d
testl %ecx, %ecx
jle 0x6357
movb %cl, 0xab1d9(%rip)      # 0xb1520
movb $0x0, 0xab1d3(%rip)     # 0xb1521
leaq 0xab1cb(%rip), %rbx     # 0xb1520
jmp 0x6376
negl %ecx
movl $0x14, %esi
leaq 0xab1bb(%rip), %rbx     # 0xb1520
movq %rbx, %rdi
leaq 0x98590(%rip), %rdx     # 0x9e8ff
xorl %eax, %eax
callq 0x4820
leaq 0x8acc6(%rip), %rdi     # 0x91043
movl %ebp, %esi
movl %r15d, %edx
movl %r13d, %ecx
movq %rbx, %r8
leaq 0x20(%rsp), %r15
movq %r15, %r9
xorl %eax, %eax
callq 0x4060
leaq 0x1(%r12), %rax
cmpq 0x30(%rsp), %rax
jae 0x63b9
addq $0x10, %r14
movl $0x20, %ebp
cmpq $0x4, %r12
movq %rax, %r12
jb 0x62f0
movl $0xa, %edi
callq 0x46c0
movq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq 0x58(%rsp), %r14
movq %r14, %rsi
leaq 0x60(%rsp), %rdx
movl $0x5, %ecx
leaq 0x18(%rsp), %r8
callq 0x2908a
movl %eax, %ebp
testl %eax, %eax
jg 0x6293
testl %ebp, %ebp
js 0x6406
leaq 0x8ac5f(%rip), %rdi     # 0x9105b
callq 0x47e0
xorl %r15d, %r15d
jmp 0x640c
movl $0x2, %r15d
leaq 0x17(%rsp), %r12
testl %r15d, %r15d
movq 0x50(%rsp), %rbx
movq 0x48(%rsp), %r13
je 0x5ef2
cmpl $0x2, %r15d
movq 0x40(%rsp), %r15
jne 0x64a0
movq %r15, %rdi
callq 0x16b24
movq %r13, %rdi
callq 0x160c6
movq %r14, %rdi
callq 0x28517
movq %rbx, %rdi
callq 0xabb9
xorl %r14d, %r14d
cmpl $0x1, %eax
adcl $0x1, %r14d
jmp 0x64a3
movl %eax, %ebp
movq 0xaab5c(%rip), %rax     # 0xb0fc0
movq (%rax), %rdi
callq 0x45a0
movq 0x50(%rsp), %rdi
callq 0xabb9
notl %ebp
movq %rbx, %rdi
movq %r14, %rbx
xorl %r14d, %r14d
orl %eax, %ebp
setne %r14b
callq 0x16b24
movq 0x48(%rsp), %rdi
callq 0x160c6
movq %rbx, %rdi
callq 0x28517
jmp 0x64a3
xorl %r14d, %r14d
movl %r14d, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x49e0
movq %rbx, %r15
movq 0x50(%rsp), %rbx
movq 0x48(%rsp), %r13
jmp 0x642f
nop