// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/test/test_view.c
// Function: sam_loop
int sam_loop(int argc, char **argv, int optind, struct opts *opts, htsFile *in, htsFile *out) {
    int r = 0;
    sam_hdr_t *h = NULL;
    hts_idx_t *idx = NULL;
    bam1_t *b = NULL;

    h = sam_hdr_read(in);
    if (h == NULL) {
        fprintf(stderr, "Couldn't read header for \"%s\"\n", argv[optind]);
        return EXIT_FAILURE;
    }
    h->ignore_sam_err = opts->ignore_sam_err;
    if (opts->extra_hdr_nuls > 0) {
        char *new_text = realloc(h->text, h->l_text + opts->extra_hdr_nuls);
        if (new_text == NULL) {
            fprintf(stderr, "Error reallocing header text\n");
            goto fail;
        }
        h->text = new_text;
        memset(&h->text[h->l_text], 0, opts->extra_hdr_nuls);
        h->l_text += opts->extra_hdr_nuls;
    }

    b = bam_init1();
    if (b == NULL) {
        fprintf(stderr, "Out of memory allocating BAM struct\n");
        goto fail;
    }

    /* CRAM output */
    if ((opts->flag & WRITE_CRAM) && opts->fn_ref) {
        // Create CRAM references arrays
        int ret = hts_set_fai_filename(out, opts->fn_ref);

        if (ret != 0)
            goto fail;
    }

    if (!opts->benchmark && sam_hdr_write(out, h) < 0) {
        fprintf(stderr, "Error writing output header.\n");
        goto fail;
    }

    if (opts->index) {
        if (sam_idx_init(out, h, opts->min_shift, opts->index) < 0) {
            fprintf(stderr, "Failed to initialise index\n");
            goto fail;
        }
    }

    if (optind + 1 < argc && !(opts->flag & READ_COMPRESSED)) { // BAM input and has a region
        int i;
        if ((idx = sam_index_load(in, argv[optind])) == 0) {
            fprintf(stderr, "[E::%s] fail to load the BAM index\n", __func__);
            goto fail;
        }
        if (opts->multi_reg) {
            hts_itr_t *iter = sam_itr_regarray(idx, h, &argv[optind + 1], argc - optind-1);
            if (!iter)
                goto fail;
            while ((r = sam_itr_next(in, iter, b)) >= 0) {
                if (!opts->benchmark && sam_write1(out, h, b) < 0) {
                    fprintf(stderr, "Error writing output.\n");
                    hts_itr_destroy(iter);
                    goto fail;
                }
                if (opts->nreads && --opts->nreads == 0)
                    break;
            }
            hts_itr_destroy(iter);
            if (r < -1) {
                fprintf(stderr, "Error reading input.\n");
                goto fail;
            }
        } else {
            for (i = optind + 1; i < argc; ++i) {
                hts_itr_t *iter;
                if ((iter = sam_itr_querys(idx, h, argv[i])) == 0) {
                    fprintf(stderr, "[E::%s] fail to parse region '%s'\n", __func__, argv[i]);
                    goto fail;
                }
                while ((r = sam_itr_next(in, iter, b)) >= 0) {
                    if (!opts->benchmark && sam_write1(out, h, b) < 0) {
                        fprintf(stderr, "Error writing output.\n");
                        hts_itr_destroy(iter);
                        goto fail;
                    }
                    if (opts->nreads && --opts->nreads == 0)
                        break;
                }
                hts_itr_destroy(iter);
                if (r < -1) {
                    fprintf(stderr, "Error reading input.\n");
                    goto fail;
                }
            }
        }
        hts_idx_destroy(idx); idx = NULL;
    } else while ((r = sam_read1(in, h, b)) >= 0) {
        if (!opts->benchmark && sam_write1(out, h, b) < 0) {
            fprintf(stderr, "Error writing output.\n");
            goto fail;
        }
        if (opts->nreads && --opts->nreads == 0)
            break;
    }

    if (r < -1) {
        fprintf(stderr, "Error parsing input.\n");
        goto fail;
    }

    if (opts->index) {
        if (sam_idx_save(out) < 0) {
            fprintf(stderr, "Error saving index\n");
            goto fail;
        }
    }

    bam_destroy1(b);
    sam_hdr_destroy(h);

    return 0;
 fail:
    if (b) bam_destroy1(b);
    if (h) sam_hdr_destroy(h);
    if (idx) hts_idx_destroy(idx);

    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, 0x10(%rsp)
movq %r8, %r13
movq %rcx, %r12
movl %edx, %ebp
movq %rsi, 0x18(%rsp)
movl %edi, %r15d
movq %r8, %rdi
callq 0x1698e
movslq %ebp, %rcx
testq %rax, %rax
je 0x5d63
movq %rax, %rbx
movq %rcx, 0x8(%rsp)
movl 0x10(%r12), %eax
movl %eax, 0x4(%rbx)
movslq 0x18(%r12), %rsi
testq %rsi, %rsi
jle 0x5b7b
movq 0x28(%rbx), %rdi
addq 0x8(%rbx), %rsi
callq 0x4870
testq %rax, %rax
je 0x5e62
movq %rax, 0x28(%rbx)
movq %rbp, (%rsp)
movl %r15d, %ebp
movq %rbx, %r15
movq 0x8(%rbx), %rbx
addq %rbx, %rax
movslq 0x18(%r12), %r14
movq %rax, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x4260
addq %r14, %rbx
movq %rbx, 0x8(%r15)
movq %r15, %rbx
movl %ebp, %r15d
movq (%rsp), %rbp
callq 0x14204
testq %rax, %rax
je 0x5e4d
movq %rax, %r14
testb $0x8, 0x8(%r12)
je 0x5baf
movq (%r12), %rsi
testq %rsi, %rsi
je 0x5baf
movq 0x10(%rsp), %rdi
callq 0xa3e1
testl %eax, %eax
jne 0x5f53
cmpl $0x0, 0x1c(%r12)
jne 0x5bcc
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x1748d
testl %eax, %eax
js 0x5efa
movq 0x28(%r12), %rcx
testq %rcx, %rcx
je 0x5bf0
movl 0x30(%r12), %edx
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x15526
testl %eax, %eax
js 0x5e86
movq %rbx, (%rsp)
leal 0x1(%rbp), %ebx
cmpl %r15d, %ebx
jge 0x5cdf
testb $0x1, 0x8(%r12)
jne 0x5cdf
movl %r15d, 0x20(%rsp)
movq 0x18(%rsp), %rax
movq 0x8(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
movq %r13, %rdi
callq 0x16461
testq %rax, %rax
je 0x5f0f
movq %rax, %r15
cmpl $0x0, 0x24(%r12)
je 0x5d8c
movslq %ebx, %rax
movq 0x18(%rsp), %rcx
leaq (%rcx,%rax,8), %rdx
notl %ebp
addl 0x20(%rsp), %ebp
movq %r15, %rdi
movq (%rsp), %rbx
movq %rbx, %rsi
movl %ebp, %ecx
callq 0x165fc
testq %rax, %rax
je 0x5f56
movq %rax, %rbp
movq %r15, 0x8(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x5fdd
movl %eax, %r15d
testl %eax, %eax
js 0x5cba
cmpl $0x0, 0x1c(%r12)
jne 0x5ca8
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x1b0a9
testl %eax, %eax
js 0x5fb4
movl 0x14(%r12), %eax
testl %eax, %eax
je 0x5c73
decl %eax
movl %eax, 0x14(%r12)
jne 0x5c73
movq %rbp, %rdi
callq 0xca25
cmpl $-0x2, %r15d
movq 0x8(%rsp), %r15
jle 0x5e2c
movq %r15, %rdi
callq 0xa182
movq (%rsp), %rbx
jmp 0x5d32
movq (%rsp), %rbx
movq %r13, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x19722
testl %eax, %eax
js 0x5d29
cmpl $0x0, 0x1c(%r12)
jne 0x5d15
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x1b0a9
testl %eax, %eax
js 0x5f37
movl 0x14(%r12), %eax
testl %eax, %eax
je 0x5ce3
decl %eax
movl %eax, 0x14(%r12)
jne 0x5ce3
jmp 0x5d32
cmpl $-0x1, %eax
jne 0x5eb6
cmpq $0x0, 0x28(%r12)
je 0x5d4c
movq 0x10(%rsp), %rdi
callq 0x15610
testl %eax, %eax
js 0x5e9e
movq %r14, %rdi
callq 0x142d2
movq %rbx, %rdi
callq 0x1395e
xorl %ebx, %ebx
jmp 0x5f76
movq 0xa926e(%rip), %rax     # 0xaefd8
movq (%rax), %rdi
movq 0x18(%rsp), %rax
movq (%rax,%rcx,8), %rdx
leaq 0x7c33b(%rip), %rsi     # 0x820b8
xorl %eax, %eax
callq 0x4730
pushq $0x1
popq %rbx
jmp 0x5f76
movslq %ebx, %rbx
movslq 0x20(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %r15, 0x8(%rsp)
cmpq 0x20(%rsp), %rbx
jge 0x5cd1
movq 0x18(%rsp), %rax
movq (%rax,%rbx,8), %rdx
movq %r15, %rdi
movq (%rsp), %rsi
callq 0x165cb
movq %rax, %rbp
testq %rax, %rax
je 0x5f87
movq %r13, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x5fdd
movl %eax, %r15d
testl %eax, %eax
js 0x5e12
cmpl $0x0, 0x1c(%r12)
jne 0x5e00
movq 0x10(%rsp), %rdi
movq (%rsp), %rsi
movq %r14, %rdx
callq 0x1b0a9
testl %eax, %eax
js 0x5ecb
movl 0x14(%r12), %eax
testl %eax, %eax
je 0x5dca
decl %eax
movl %eax, 0x14(%r12)
jne 0x5dca
movq %rbp, %rdi
callq 0xca25
incq %rbx
cmpl $-0x2, %r15d
movq 0x8(%rsp), %r15
jg 0x5d9e
movq 0xa91a5(%rip), %rax     # 0xaefd8
movq (%rax), %rcx
leaq 0x7c35b(%rip), %rdi     # 0x82198
pushq $0x15
popq %rsi
pushq $0x1
popq %rdx
callq 0x49c0
jmp 0x5f31
movq 0xa9184(%rip), %rax     # 0xaefd8
movq (%rax), %rcx
leaq 0x7c297(%rip), %rdi     # 0x820f5
pushq $0x24
jmp 0x5e75
movq 0xa916f(%rip), %rax     # 0xaefd8
movq (%rax), %rcx
leaq 0x7c264(%rip), %rdi     # 0x820d7
pushq $0x1d
popq %rsi
pushq $0x1
popq %rdx
callq 0x49c0
xorl %r15d, %r15d
jmp 0x5f5e
movq 0xa914b(%rip), %rax     # 0xaefd8
movq (%rax), %rcx
leaq 0x7c2a1(%rip), %rdi     # 0x82138
pushq $0x1b
jmp 0x5f4a
movq 0xa9133(%rip), %rax     # 0xaefd8
movq (%rax), %rcx
leaq 0x7c338(%rip), %rdi     # 0x821e7
pushq $0x13
jmp 0x5f4a
movq 0xa911b(%rip), %rax     # 0xaefd8
movq (%rax), %rcx
leaq 0x7c30a(%rip), %rdi     # 0x821d1
pushq $0x15
jmp 0x5f4a
movq 0xa9106(%rip), %rax     # 0xaefd8
movq (%rax), %rcx
leaq 0x7c2a5(%rip), %rdi     # 0x82181
pushq $0x16
popq %rsi
pushq $0x1
popq %rdx
callq 0x49c0
movq %rbp, %rdi
callq 0xca25
movq (%rsp), %rbx
movq 0x8(%rsp), %r15
jmp 0x5f56
movq 0xa90d7(%rip), %rax     # 0xaefd8
movq (%rax), %rcx
leaq 0x7c20f(%rip), %rdi     # 0x8211a
pushq $0x1d
jmp 0x5f4a
movq 0xa90c2(%rip), %rax     # 0xaefd8
movq (%rax), %rdi
leaq 0x7c234(%rip), %rsi     # 0x82154
leaq 0x7c251(%rip), %rdx     # 0x82178
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x4730
movq (%rsp), %rbx
jmp 0x5f56
movq 0xa909a(%rip), %rax     # 0xaefd8
movq (%rax), %rcx
leaq 0x7c239(%rip), %rdi     # 0x82181
pushq $0x16
popq %rsi
pushq $0x1
popq %rdx
callq 0x49c0
xorl %r15d, %r15d
movq %r14, %rdi
callq 0x142d2
movq %rbx, %rdi
callq 0x1395e
pushq $0x1
popq %rbx
testq %r15, %r15
je 0x5f76
movq %r15, %rdi
callq 0xa182
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xa904a(%rip), %rax     # 0xaefd8
movq (%rax), %rdi
movq 0x18(%rsp), %rax
movq (%rax,%rbx,8), %rcx
leaq 0x7c20d(%rip), %rsi     # 0x821ae
leaq 0x7c1d0(%rip), %rdx     # 0x82178
xorl %eax, %eax
callq 0x4730
jmp 0x5eef
movq 0xa901d(%rip), %rax     # 0xaefd8
movq (%rax), %rcx
leaq 0x7c1bc(%rip), %rdi     # 0x82181
pushq $0x16
popq %rsi
pushq $0x1
popq %rdx
callq 0x49c0
movq %rbp, %rdi
callq 0xca25
jmp 0x5ef3