// File: /workspace/github_c_makefile_star2_O3/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, %rbx
movl %edi, %ebp
movq $0x77, 0x40(%rsp)
movl $0x0, 0x34(%rsp)
leaq 0x8a0f5(%rip), %rdx     # 0x9006c
callq 0x40e0
testl %eax, %eax
js 0x622d
leaq 0x8d531(%rip), %rcx     # 0x934bc
movq $0x0, 0x8(%rsp)
leaq 0x8a0d1(%rip), %r15     # 0x9006c
leaq 0x8a062(%rip), %r12     # 0x90004
movq 0xab017(%rip), %r14     # 0xb0fc0
movl $0x0, 0x20(%rsp)
xorl %r13d, %r13d
movq $0x0, 0x10(%rsp)
movq %rcx, 0x18(%rsp)
movq %rcx, 0x28(%rsp)
leal -0x65(%rax), %ecx
cmpl $0xd, %ecx
ja 0x5fe2
movslq (%r12,%rcx,4), %rax
addq %r12, %rax
jmpq *%rax
movl $0x1, 0x20(%rsp)
jmp 0x601a
movl $0x1, %r13d
cmpl $0x61, %eax
je 0x601a
jmp 0x63c4
movq (%r14), %rax
movq %rax, 0x10(%rsp)
jmp 0x601a
movl $0x1, %eax
movq %rax, 0x8(%rsp)
jmp 0x601a
movq (%r14), %rax
movq %rax, 0x28(%rsp)
jmp 0x601a
movq (%r14), %rax
movq %rax, 0x18(%rsp)
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x40e0
testl %eax, %eax
jns 0x5fc7
movq 0x10(%rsp), %r14
testq %r14, %r14
je 0x622d
xorl %r15d, %r15d
cmpl $0x0, 0x8(%rsp)
setne %bl
xorl %r12d, %r12d
cmpl $0x0, 0x20(%rsp)
setne 0x38(%rsp)
xorl %ebp, %ebp
testl %r13d, %r13d
setne %r13b
movq %r14, %rdi
callq 0x78a6
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x623a
movb %bl, 0x7(%rsp)
callq 0x1bbb5
testq %rax, %rax
je 0x625d
movq %rax, %rbx
leaq 0x89fe9(%rip), %rsi     # 0x90075
movq 0x28(%rsp), %rdi
callq 0xe050
testq %rax, %rax
je 0x626c
movq %rax, 0x10(%rsp)
movq %rax, %rdi
callq 0x1e981
testq %rax, %rax
je 0x6281
movq %rax, %r14
leaq 0x40(%rsp), %rsi
movq 0x18(%rsp), %rdi
callq 0xe050
testq %rax, %rax
je 0x62bc
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movq %r14, %rsi
callq 0x1fb7a
testl %eax, %eax
js 0x62d7
movb %r13b, %bpl
movb 0x38(%rsp), %al
movb %al, %r12b
leal (%rbp,%r12,2), %eax
movb 0x7(%rsp), %cl
movb %cl, %r15b
leal (%rax,%r15,4), %r12d
movq %rbx, %r15
addq $0x8, %r15
movq %r14, %rax
addq $0x20, %rax
movq %rax, 0x38(%rsp)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %r13d, %r13d
movq 0x10(%rsp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x2294f
testl %eax, %eax
js 0x61e0
movl (%r15), %edx
cmpl (%r14), %edx
jge 0x62e6
cmpl %edx, %ebp
sete %al
testl %edx, %edx
sets %cl
orb %al, %cl
jne 0x618d
movq %r13, %rdi
callq 0x45c0
movq 0x38(%rsp), %rax
movq (%rax), %rax
movslq (%r15), %rcx
movq (%rax,%rcx,8), %rsi
movq 0x20(%rsp), %rdi
xorl %edx, %edx
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leaq 0x34(%rsp), %r8
callq 0x8339
testq %rax, %rax
je 0x6360
movq %rax, %r13
movl (%r15), %edx
movl %edx, %ebp
testl %edx, %edx
js 0x61ad
movslq 0x34(%rsp), %rdx
movq %rbx, %rdi
movq %r13, %rsi
movl %r12d, %ecx
callq 0x19daf
cmpl $-0x4, %eax
jle 0x6359
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x26192
testl %eax, %eax
jns 0x611d
movq 0xaae0c(%rip), %rax     # 0xb0fd8
movq (%rax), %rdi
leaq 0x89f6e(%rip), %rsi     # 0x90144
movq 0x18(%rsp), %rdx
jmp 0x631f
movq 0x10(%rsp), %rdi
callq 0xf2b2
testl %eax, %eax
js 0x6300
movq 0x8(%rsp), %rdi
callq 0xf2b2
testl %eax, %eax
js 0x6328
movq %r14, %rdi
callq 0x1b262
movq %rbx, %rdi
callq 0x1bc8b
movq %r13, %rdi
callq 0x45c0
movq 0x20(%rsp), %rdi
callq 0x63dc
xorl %eax, %eax
jmp 0x63b5
movq %rbx, %rdi
callq 0x4b0b
jmp 0x63b0
movq 0xaad97(%rip), %rax     # 0xb0fd8
movq (%rax), %rdi
leaq 0x89e2c(%rip), %rsi     # 0x90077
xorl %r13d, %r13d
movq %r14, %rdx
xorl %eax, %eax
callq 0x4700
jmp 0x639e
xorl %r13d, %r13d
xorl %edi, %edi
callq 0x43d0
jmp 0x639e
movq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x4ac6
xorl %r13d, %r13d
jmp 0x639e
movq 0xaad50(%rip), %rax     # 0xb0fd8
movq (%rax), %rdi
leaq 0x89e18(%rip), %rsi     # 0x900aa
xorl %r13d, %r13d
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x4700
movq %rbx, %rdi
callq 0x1bc8b
movq $0x0, 0x8(%rsp)
movq 0x10(%rsp), %rdi
jmp 0x638a
movq 0x18(%rsp), %rdi
callq 0x4a8a
xorl %r13d, %r13d
movq $0x0, 0x8(%rsp)
jmp 0x6370
movq 0x18(%rsp), %rdi
callq 0x4a4e
jmp 0x636d
movq 0xaaceb(%rip), %rax     # 0xb0fd8
movq (%rax), %rdi
leaq 0x89df2(%rip), %rsi     # 0x900e9
xorl %eax, %eax
callq 0x4700
jmp 0x6370
movq 0xaacd1(%rip), %rax     # 0xb0fd8
movq (%rax), %rdi
leaq 0x89e48(%rip), %rsi     # 0x90159
movq $0x0, 0x10(%rsp)
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x4700
jmp 0x6370
movq 0xaaca9(%rip), %rax     # 0xb0fd8
movq (%rax), %rdi
leaq 0x89e20(%rip), %rsi     # 0x90159
movq $0x0, 0x8(%rsp)
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x4700
movq $0x0, 0x10(%rsp)
jmp 0x6370
callq 0x49f6
jmp 0x6370
movq 0x38(%rsp), %rdi
movq %r15, %rsi
callq 0x4a23
xorl %r13d, %r13d
movq %r14, %rdi
callq 0x1b262
movq %rbx, %rdi
callq 0x1bc8b
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x638f
callq 0xf2b2
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x639e
callq 0xf2b2
movq %r13, %rdi
callq 0x45c0
movq 0x20(%rsp), %rdi
callq 0x63dc
movl $0x1, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x4b26
jmp 0x63b0
movq %rbx, %rdi
callq 0x49db
jmp 0x6226
nop