// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movq %r9, 0x18(%rsp)
movq %r8, %r13
movq %rcx, %r12
movl %edx, %ebp
movq %rsi, 0x28(%rsp)
movl %edi, 0x24(%rsp)
movq %r8, %rdi
callq 0x19ea1
testq %rax, %rax
je 0x672a
movq %rax, %rbx
movl 0x10(%r12), %eax
movl %eax, 0x4(%rbx)
movslq 0x18(%r12), %rsi
testq %rsi, %rsi
jle 0x6526
movq 0x28(%rbx), %rdi
addq 0x8(%rbx), %rsi
callq 0x4870
testq %rax, %rax
je 0x684d
movq %rax, 0x28(%rbx)
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
callq 0x170d5
testq %rax, %rax
je 0x6821
movq %rax, %r14
testb $0x8, 0x8(%r12)
je 0x655e
movq (%r12), %rsi
testq %rsi, %rsi
je 0x655e
movq 0x18(%rsp), %rdi
callq 0xb964
testl %eax, %eax
je 0x655e
xorl %r15d, %r15d
jmp 0x68d3
cmpl $0x0, 0x1c(%r12)
jne 0x657b
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0x1b09a
testl %eax, %eax
js 0x6896
movq 0x28(%r12), %rcx
testq %rcx, %rcx
je 0x659f
movl 0x30(%r12), %edx
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0x18764
testl %eax, %eax
js 0x6864
movq %rbx, 0x8(%rsp)
leal 0x1(%rbp), %ebx
cmpl 0x24(%rsp), %ebx
jge 0x66a2
testb $0x1, 0x8(%r12)
jne 0x66a2
movslq %ebp, %rax
movq 0x28(%rsp), %rcx
movq (%rcx,%rax,8), %rsi
movq %r13, %rdi
callq 0x19936
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x68a0
cmpl $0x0, 0x24(%r12)
je 0x6753
movslq %ebx, %rax
movq 0x28(%rsp), %rcx
leaq (%rcx,%rax,8), %rdx
notl %ebp
addl 0x24(%rsp), %ebp
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
movl %ebp, %ecx
callq 0x19adf
testq %rax, %rax
je 0x68ce
movq %rax, %r15
movl (%r13), %eax
testb $0x18, %al
je 0x68b9
testb $0x10, (%r15)
jne 0x6637
testb $0x10, %al
jne 0x6647
xorl %edi, %edi
jmp 0x664b
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x10c9d
jmp 0x6659
movq 0x38(%r13), %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %r13, %rcx
callq 0x10a5e
movl %eax, %ebp
testl %eax, %eax
js 0x6828
cmpl $0x0, 0x1c(%r12)
jne 0x6683
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x216b2
testl %eax, %eax
js 0x693a
movl 0x14(%r12), %eax
testl %eax, %eax
je 0x661d
decl %eax
movl %eax, 0x14(%r12)
jne 0x661d
movq %r15, %rdi
callq 0xe951
jmp 0x6839
movq %r13, %rdi
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
movq %r14, %rdx
callq 0x1de6f
testl %eax, %eax
js 0x66f0
cmpl $0x0, 0x1c(%r12)
jne 0x66d9
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x216b2
testl %eax, %eax
js 0x6906
movl 0x14(%r12), %eax
testl %eax, %eax
je 0x66eb
decl %eax
movl %eax, 0x14(%r12)
je 0x66f9
movq %r13, %rdi
jmp 0x66aa
cmpl $-0x1, %eax
jne 0x686e
cmpq $0x0, 0x28(%r12)
je 0x6713
movq 0x18(%rsp), %rdi
callq 0x18861
testl %eax, %eax
js 0x688c
movq %r14, %rdi
callq 0x171ab
movq %rbx, %rdi
callq 0x16782
xorl %ebx, %ebx
jmp 0x68f5
movq 0xc88a7(%rip), %rax     # 0xcefd8
movq (%rax), %rdi
movslq %ebp, %rax
movq 0x28(%rsp), %rcx
movq (%rcx,%rax,8), %rdx
leaq 0xa1971(%rip), %rsi     # 0xa80b8
xorl %eax, %eax
callq 0x4730
jmp 0x685a
leaq 0x28(%r13), %rax
movq %rax, 0x30(%rsp)
movslq %ebx, %rbx
movq 0x28(%rsp), %rax
movq (%rax,%rbx,8), %rdx
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x19aae
testq %rax, %rax
je 0x6910
movq %rax, %rbp
movl (%r13), %eax
testb $0x18, %al
je 0x6878
testb $0x10, (%rbp)
jne 0x679d
testb $0x10, %al
jne 0x67ad
xorl %edi, %edi
jmp 0x67b1
movq %r13, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x10c9d
jmp 0x67bf
movq 0x38(%r13), %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq %r13, %rcx
callq 0x10a5e
movl %eax, %r15d
testl %eax, %eax
js 0x6804
cmpl $0x0, 0x1c(%r12)
jne 0x67e8
movq 0x18(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0x216b2
testl %eax, %eax
js 0x68af
movl 0x14(%r12), %eax
testl %eax, %eax
je 0x6783
decl %eax
movl %eax, 0x14(%r12)
jne 0x6783
movq %rbp, %rdi
callq 0xe951
jmp 0x6812
movq %rbp, %rdi
callq 0xe951
cmpl $-0x1, %r15d
jne 0x6885
incq %rbx
cmpl 0x24(%rsp), %ebx
jne 0x675f
jmp 0x6839
callq 0x4ba2
jmp 0x6852
movq %r15, %rdi
callq 0xe951
cmpl $-0x1, %ebp
jne 0x68c4
movq 0x10(%rsp), %rdi
callq 0xb6ef
movq 0x8(%rsp), %rbx
jmp 0x66f9
callq 0x4bbe
movq %rbx, %rdi
callq 0x16782
movl $0x1, %ebx
jmp 0x68f5
callq 0x4a00
jmp 0x6556
callq 0x4a1c
jmp 0x6556
movq 0x30(%rsp), %rdi
movq %rbp, %rsi
callq 0x4b15
callq 0x4b4b
jmp 0x68c9
callq 0x4ad0
jmp 0x6556
callq 0x4b86
jmp 0x6556
callq 0x4b67
xorl %r15d, %r15d
movq 0x8(%rsp), %rbx
jmp 0x68d3
movq %rbp, %rdi
callq 0x4aec
jmp 0x68c9
movq %r13, %rdi
movq %r15, %rsi
callq 0x4a7d
callq 0x4ab4
movq 0x8(%rsp), %rbx
movq 0x10(%rsp), %r15
movq %r14, %rdi
callq 0x171ab
movq %rbx, %rdi
callq 0x16782
movl $0x1, %ebx
testq %r15, %r15
je 0x68f5
movq %r15, %rdi
callq 0xb6ef
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4a38
jmp 0x6556
movq 0xc86c1(%rip), %rax     # 0xcefd8
movq (%rax), %rdi
movq 0x28(%rsp), %rax
movq (%rax,%rbx,8), %rcx
leaq 0xa1884(%rip), %rsi     # 0xa81ae
leaq 0xa1847(%rip), %rdx     # 0xa8178
xorl %eax, %eax
callq 0x4730
jmp 0x68c9
movq %r15, %rdi
callq 0x4a54
jmp 0x68ce