// File: /workspace/github_c_makefile_star2_O1/samtools[P]htslib/test/test_realn.c
// Function: main
int main(int argc, char **argv) {
    htsFile *in = NULL;
    htsFile *out = NULL;
    char *in_name = "-";
    char *out_name = "-";
    char *ref_name = NULL;
    char *ref_seq = NULL;
    char modew[8] = "w";
    faidx_t *fai = NULL;
    sam_hdr_t *hdr = NULL;
    bam1_t *rec = NULL;
    int c, res, last_ref = -1, ref_len = 0;
    int adjust = 0, extended = 0, recalc = 0, flags = 0;

    while ((c = getopt(argc, argv, "aef:hi:o:r")) >= 0) {
        switch (c) {
        case 'a': adjust = 1; break;
        case 'e': extended = 1; break;
        case 'f': ref_name = optarg; break;
        case 'h': usage(argv[0]); return EXIT_SUCCESS;
        case 'i': in_name = optarg; break;
        case 'o': out_name = optarg; break;
        case 'r': recalc = 1; break;
        default: usage(argv[0]); return EXIT_FAILURE;
        }
    }

    if (!ref_name) {
        usage(argv[0]);
        return EXIT_FAILURE;
    }

    flags = (adjust ? 1 : 0) | (extended ? 2 : 0) | (recalc ? 4 : 0);

    fai = fai_load(ref_name);
    if (!fai) {
        fprintf(stderr, "Couldn't load reference %s\n", ref_name);
        goto fail;
    }

    rec = bam_init1();
    if (!rec) {
        perror(NULL);
        goto fail;
    }

    in = hts_open(in_name, "r");
    if (!in) {
        fprintf(stderr, "Couldn't open %s : %s\n", in_name, strerror(errno));
        goto fail;
    }

    hdr = sam_hdr_read(in);
    if (!hdr) {
        fprintf(stderr, "Couldn't read header for %s\n", in_name);
        goto fail;
    }

    out = hts_open(out_name, modew);
    if (!out) {
        fprintf(stderr, "Couldn't open %s : %s\n", out_name, strerror(errno));
        goto fail;
    }

    if (sam_hdr_write(out, hdr) < 0) {
        fprintf(stderr, "Couldn't write header to %s : %s\n",
                out_name, strerror(errno));
        goto fail;
    }

    while ((res = sam_read1(in, hdr, rec)) >= 0) {
        if (rec->core.tid >= hdr->n_targets) {
            fprintf(stderr, "Invalid BAM reference id %d\n", rec->core.tid);
            goto fail;
        }
        if (last_ref != rec->core.tid && rec->core.tid >= 0) {
            free(ref_seq);
            ref_seq = faidx_fetch_seq(fai, hdr->target_name[rec->core.tid],
                                      0, INT_MAX, &ref_len);
            if (!ref_seq) {
                fprintf(stderr, "Couldn't get reference %s\n",
                        hdr->target_name[rec->core.tid]);
                goto fail;
            }
            last_ref = rec->core.tid;
        }
        if (rec->core.tid >= 0) {
            res = sam_prob_realn(rec, ref_seq, ref_len, flags);
            if (res <= -4) {
                fprintf(stderr, "Error running sam_prob_realn : %s\n",
                        strerror(errno));
                goto fail;
            }
        }
        if (sam_write1(out, hdr, rec) < 0) {
            fprintf(stderr, "Error writing to %s\n", out_name);
            goto fail;
        }
    }
    res = hts_close(in);
    in = NULL;
    if (res < 0) {
        fprintf(stderr, "Error closing %s\n", in_name);
        goto fail;
    }

    res = hts_close(out);
    out = NULL;
    if (res < 0) {
        fprintf(stderr, "Error closing %s\n", out_name);
        goto fail;
    }

    sam_hdr_destroy(hdr);
    bam_destroy1(rec);
    free(ref_seq);
    fai_destroy(fai);

    return EXIT_SUCCESS;

 fail:
    if (hdr) sam_hdr_destroy(hdr);
    if (rec) bam_destroy1(rec);
    if (in) hts_close(in);
    if (out) hts_close(out);
    free(ref_seq);
    fai_destroy(fai);
    return EXIT_FAILURE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r14
movl %edi, %ebx
movq $0x77, 0x40(%rsp)
movl $0x0, 0x2c(%rsp)
leaq 0x8d115(%rip), %rdx     # 0x9306c
callq 0x40e0
testl %eax, %eax
js 0x603f
leaq 0x9057e(%rip), %rcx     # 0x964e9
movq $0x0, 0x20(%rsp)
leaq 0x8d0f1(%rip), %r12     # 0x9306c
leaq 0x8d082(%rip), %r13     # 0x93004
movq 0xae037(%rip), %r15     # 0xb3fc0
movl $0x0, 0xc(%rsp)
xorl %ebp, %ebp
movq $0x0, 0x10(%rsp)
movq %rcx, 0x18(%rsp)
movq %rcx, 0x30(%rsp)
leal -0x65(%rax), %ecx
cmpl $0xd, %ecx
ja 0x5fc2
movslq (%r13,%rcx,4), %rax
addq %r13, %rax
jmpq *%rax
movl $0x1, 0xc(%rsp)
jmp 0x5ff9
movl $0x1, %ebp
cmpl $0x61, %eax
je 0x5ff9
jmp 0x639b
movq (%r15), %rax
movq %rax, 0x10(%rsp)
jmp 0x5ff9
movl $0x1, %eax
movq %rax, 0x20(%rsp)
jmp 0x5ff9
movq (%r15), %rax
movq %rax, 0x30(%rsp)
jmp 0x5ff9
movq (%r15), %rax
movq %rax, 0x18(%rsp)
movl %ebx, %edi
movq %r14, %rsi
movq %r12, %rdx
callq 0x40e0
testl %eax, %eax
jns 0x5fa6
xorl %r12d, %r12d
testl %ebp, %ebp
setne %r12b
xorl %eax, %eax
cmpl $0x0, 0xc(%rsp)
setne %al
addl %eax, %eax
movl %eax, 0xc(%rsp)
xorl %r13d, %r13d
cmpl $0x0, 0x20(%rsp)
setne %r13b
shll $0x2, %r13d
movq 0x30(%rsp), %rbx
movq 0x10(%rsp), %r15
jmp 0x605c
xorl %r15d, %r15d
leaq 0x904a0(%rip), %rbx     # 0x964e9
movq %rbx, 0x18(%rsp)
xorl %r12d, %r12d
movl $0x0, 0xc(%rsp)
xorl %r13d, %r13d
testq %r15, %r15
je 0x6229
movq %r15, %rdi
callq 0x78cf
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x6236
movl %r12d, 0x3c(%rsp)
callq 0x1c367
testq %rax, %rax
je 0x6256
movq %rax, %r12
leaq 0x8cfdd(%rip), %rsi     # 0x93075
movq %rbx, %rdi
callq 0xe497
testq %rax, %rax
je 0x6270
movq %rax, %r14
movq %rax, %rdi
callq 0x1f11d
testq %rax, %rax
je 0x627f
movq %rax, %r15
leaq 0x40(%rsp), %rsi
movq 0x18(%rsp), %rdi
callq 0xe497
testq %rax, %rax
je 0x62a7
movq %rax, %rbp
movq %rax, %rdi
movq %r15, %rsi
callq 0x2049e
testl %eax, %eax
js 0x62b5
movq %rbp, 0x10(%rsp)
movq %rbx, 0x30(%rsp)
movq %r14, %rbx
movl 0xc(%rsp), %eax
orl 0x3c(%rsp), %eax
orl %r13d, %eax
movl %eax, 0xc(%rsp)
movq %r12, %r14
addq $0x8, %r14
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %r13d, %r13d
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x232f9
testl %eax, %eax
js 0x61de
movl (%r14), %edx
cmpl (%r15), %edx
jge 0x62c4
cmpl %edx, %ebp
sete %al
testl %edx, %edx
sets %cl
orb %al, %cl
jne 0x6181
movq %r13, %rdi
callq 0x45c0
movq 0x20(%r15), %rax
movslq (%r14), %rcx
movq (%rax,%rcx,8), %rsi
movq 0x20(%rsp), %rdi
xorl %edx, %edx
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leaq 0x2c(%rsp), %r8
callq 0x8341
testq %rax, %rax
je 0x632b
movq %rax, %r13
movl (%r14), %ebp
cmpl $0x0, (%r14)
js 0x61a4
movslq 0x2c(%rsp), %rdx
movq %r12, %rdi
movq %r13, %rsi
movl 0xc(%rsp), %ecx
callq 0x1a548
cmpl $-0x4, %eax
jle 0x6324
movq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x26ae3
testl %eax, %eax
jns 0x6119
movq 0xade15(%rip), %rax     # 0xb3fd8
movq (%rax), %rdi
leaq 0x8cf77(%rip), %rsi     # 0x93144
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x4700
jmp 0x6339
movq %rbx, %rdi
callq 0xf701
testl %eax, %eax
js 0x62de
movq 0x10(%rsp), %rdi
callq 0xf701
testl %eax, %eax
js 0x6300
movq %r15, %rdi
callq 0x1b9e6
movq %r12, %rdi
callq 0x1c444
movq %r13, %rdi
callq 0x45c0
movq 0x20(%rsp), %rdi
callq 0x63b4
xorl %eax, %eax
jmp 0x638c
movq %r14, %rdi
callq 0x4b02
jmp 0x6387
movq 0xadd9b(%rip), %rax     # 0xb3fd8
movq (%rax), %rdi
leaq 0x8ce30(%rip), %rsi     # 0x93077
xorl %r14d, %r14d
movq %r15, %rdx
xorl %eax, %eax
callq 0x4700
jmp 0x6260
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x43d0
xorl %ebp, %ebp
xorl %r13d, %r13d
xorl %r15d, %r15d
xorl %r12d, %r12d
jmp 0x6341
movq %rbx, %rdi
callq 0x4ac7
xorl %r14d, %r14d
xorl %ebp, %ebp
jmp 0x629c
movq 0xadd52(%rip), %rax     # 0xb3fd8
movq (%rax), %rdi
leaq 0x8ce1a(%rip), %rsi     # 0x930aa
xorl %ebp, %ebp
movq %rbx, %rdx
xorl %eax, %eax
callq 0x4700
xorl %r13d, %r13d
xorl %r15d, %r15d
jmp 0x6341
movq 0x18(%rsp), %rdi
callq 0x4a8b
xorl %ebp, %ebp
jmp 0x62bf
movq 0x18(%rsp), %rdi
callq 0x4a4f
xorl %r13d, %r13d
jmp 0x6341
movq 0xadd0d(%rip), %rax     # 0xb3fd8
movq (%rax), %rdi
leaq 0x8ce14(%rip), %rsi     # 0x930e9
xorl %eax, %eax
callq 0x4700
jmp 0x6339
movq 0xadcf3(%rip), %rax     # 0xb3fd8
movq (%rax), %rdi
leaq 0x8ce6a(%rip), %rsi     # 0x93159
xorl %r14d, %r14d
movq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x4700
jmp 0x633c
movq 0xadcd1(%rip), %rax     # 0xb3fd8
movq (%rax), %rdi
leaq 0x8ce48(%rip), %rsi     # 0x93159
xorl %r14d, %r14d
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x4700
xorl %ebp, %ebp
jmp 0x6341
callq 0x49f6
jmp 0x6339
movq %r15, %rdi
movq %r14, %rsi
callq 0x4a23
xorl %r13d, %r13d
movq %rbx, %r14
movq 0x10(%rsp), %rbp
testq %r15, %r15
je 0x634e
movq %r15, %rdi
callq 0x1b9e6
testq %r12, %r12
je 0x635b
movq %r12, %rdi
callq 0x1c444
testq %r14, %r14
je 0x6368
movq %r14, %rdi
callq 0xf701
testq %rbp, %rbp
je 0x6375
movq %rbp, %rdi
callq 0xf701
movq %r13, %rdi
callq 0x45c0
movq 0x20(%rsp), %rdi
callq 0x63b4
movl $0x1, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x4b1d
jmp 0x6387
movq %r14, %rdi
callq 0x49db
jmp 0x6222
nop