// File: /workspace/github_c_makefile_star2_O1/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, 0x8(%rsp)
movq %r8, %rbp
movq %rcx, %r12
movl %edx, %r13d
movq %rsi, 0x18(%rsp)
movl %edi, (%rsp)
movq %r8, %rdi
callq 0x1a5dd
testq %rax, %rax
je 0x66ee
movq %rax, %rbx
movl 0x10(%r12), %eax
movl %eax, 0x4(%rbx)
movslq 0x18(%r12), %rsi
testq %rsi, %rsi
jle 0x64c6
movq 0x28(%rbx), %rdi
addq 0x8(%rbx), %rsi
callq 0x4870
movq %rax, %r14
testq %rax, %rax
je 0x689f
movq %r14, 0x28(%rbx)
movq 0x8(%rbx), %rdi
addq %r14, %rdi
movslq 0x18(%r12), %r15
xorl %esi, %esi
movq %r15, %rdx
callq 0x4260
addq %r15, 0x8(%rbx)
testq %r14, %r14
je 0x6869
callq 0x17827
testq %rax, %rax
je 0x6864
movq %rax, %r14
testb $0x8, 0x8(%r12)
je 0x64fa
movq (%r12), %rsi
testq %rsi, %rsi
je 0x64fa
movq 0x8(%rsp), %rdi
callq 0xbd92
testl %eax, %eax
jne 0x686c
cmpl $0x0, 0x1c(%r12)
jne 0x6517
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x1b95e
testl %eax, %eax
js 0x68d5
movq 0x28(%r12), %rcx
testq %rcx, %rcx
je 0x653b
movl 0x30(%r12), %edx
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x18e5b
testl %eax, %eax
js 0x68ce
leal 0x1(%r13), %r15d
cmpl (%rsp), %r15d
jge 0x6689
testb $0x1, 0x8(%r12)
jne 0x6689
movslq %r13d, %rax
movq 0x18(%rsp), %rcx
movq (%rcx,%rax,8), %rsi
movq %rbp, %rdi
callq 0x1a022
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x68e3
cmpl $0x0, 0x24(%r12)
je 0x671c
movslq %r15d, %rax
movq 0x18(%rsp), %rcx
leaq (%rcx,%rax,8), %rdx
notl %r13d
addl (%rsp), %r13d
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
movl %r13d, %ecx
callq 0x1a202
movq %rax, %r15
movl $0x2, %eax
movq %rax, (%rsp)
testq %r15, %r15
je 0x68a9
movl (%rbp), %eax
testb $0x18, %al
je 0x65d0
testb $0x10, (%r15)
jne 0x6604
testb $0x10, %al
jne 0x6614
xorl %edi, %edi
jmp 0x6618
movq 0x28(%rbp), %rcx
testq %rcx, %rcx
leaq 0xa42aa(%rip), %rax     # 0xaa888
cmoveq %rax, %rcx
movl $0x1, %edi
leaq 0xa4276(%rip), %rsi     # 0xaa864
leaq 0xa427c(%rip), %rdx     # 0xaa871
xorl %eax, %eax
callq 0xa63c
movl $0xfffffffe, %r13d      # imm = 0xFFFFFFFE
jmp 0x6629
movq %rbp, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x110b6
jmp 0x6626
movq 0x38(%rbp), %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %rbp, %rcx
callq 0x10e7e
movl %eax, %r13d
testl %r13d, %r13d
js 0x6668
cmpl $0x0, 0x1c(%r12)
jne 0x664e
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x21fa3
testl %eax, %eax
js 0x696b
movl 0x14(%r12), %eax
testl %eax, %eax
je 0x65bb
decl %eax
movl %eax, 0x14(%r12)
jne 0x65bb
movq %r15, %rdi
callq 0xed17
cmpl $-0x2, %r13d
jle 0x695f
movq $0x0, (%rsp)
movb $0x1, %al
jmp 0x68ae
movq %rbp, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x1e7b9
movl %eax, %r13d
xorl %r15d, %r15d
testl %eax, %eax
js 0x6902
cmpl $0x0, 0x1c(%r12)
jne 0x66c5
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x21fa3
testl %eax, %eax
js 0x6955
movl 0x14(%r12), %eax
testl %eax, %eax
je 0x66db
decl %eax
movl %eax, 0x14(%r12)
je 0x6902
movq %rbp, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x1e7b9
movl %eax, %r13d
jmp 0x669d
movq 0xc98e3(%rip), %rax     # 0xcffd8
movq (%rax), %rdi
movslq %r13d, %rax
movq 0x18(%rsp), %rcx
movq (%rcx,%rax,8), %rdx
leaq 0xa39ad(%rip), %rsi     # 0xaa0b8
xorl %eax, %eax
callq 0x4730
movl $0x1, %ebp
jmp 0x6930
xorl %r13d, %r13d
cmpl (%rsp), %r15d
jge 0x68b2
movslq %r15d, %r15
xorl %r13d, %r13d
movq 0x18(%rsp), %rax
movq %r15, 0x20(%rsp)
movq (%rax,%r15,8), %rdx
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x1a1d1
movq %rax, %r15
testq %rax, %rax
je 0x6830
movl (%rbp), %eax
testb $0x18, %al
je 0x676b
testb $0x10, (%r15)
jne 0x679f
testb $0x10, %al
jne 0x67af
xorl %edi, %edi
jmp 0x67b3
movq 0x28(%rbp), %rcx
testq %rcx, %rcx
leaq 0xa410f(%rip), %rax     # 0xaa888
cmoveq %rax, %rcx
movl $0x1, %edi
leaq 0xa40db(%rip), %rsi     # 0xaa864
leaq 0xa40e1(%rip), %rdx     # 0xaa871
xorl %eax, %eax
callq 0xa63c
movl $0xfffffffe, %r13d      # imm = 0xFFFFFFFE
jmp 0x67c4
movq %rbp, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x110b6
jmp 0x67c1
movq 0x38(%rbp), %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %rbp, %rcx
callq 0x10e7e
movl %eax, %r13d
testl %r13d, %r13d
js 0x67ff
cmpl $0x0, 0x1c(%r12)
jne 0x67e5
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x21fa3
testl %eax, %eax
js 0x685a
movl 0x14(%r12), %eax
testl %eax, %eax
je 0x6756
decl %eax
movl %eax, 0x14(%r12)
jne 0x6756
movq %r15, %rdi
callq 0xed17
cmpl $-0x2, %r13d
jle 0x684c
xorl %ecx, %ecx
movb $0x1, %al
movq 0x20(%rsp), %r15
testb %al, %al
je 0x68dc
incq %r15
cmpl (%rsp), %r15d
jne 0x672f
jmp 0x68b2
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %r15
leaq (%rax,%r15,8), %rdi
callq 0x4ade
movl $0x2, %ecx
xorl %eax, %eax
jmp 0x6816
callq 0x4a99
movl $0x2, %ecx
xorl %eax, %eax
jmp 0x6811
movq %r15, %rdi
callq 0x4ab5
jmp 0x6851
callq 0x4b76
xorl %r14d, %r14d
xorl %r15d, %r15d
testq %r14, %r14
je 0x687c
movq %r14, %rdi
callq 0x17904
movq %rbx, %rdi
callq 0x16ea6
movl $0x1, %ebp
testq %r15, %r15
je 0x6930
movq %r15, %rdi
callq 0xbb22
jmp 0x6930
callq 0x4a00
jmp 0x64bd
xorl %eax, %eax
xorl %r13d, %r13d
testb %al, %al
je 0x68c3
movq 0x10(%rsp), %rdi
callq 0xbb22
xorl %ecx, %ecx
xorl %r15d, %r15d
jmp 0x68f3
movq 0x10(%rsp), %r15
movq (%rsp), %rcx
jmp 0x68f3
callq 0x4a1c
jmp 0x686c
callq 0x4b5a
jmp 0x686c
movq 0x10(%rsp), %r15
jmp 0x68f3
callq 0x4b03
xorl %r15d, %r15d
movl $0x2, %ecx
xorl %r13d, %r13d
testl %ecx, %ecx
je 0x6902
cmpl $0x2, %ecx
je 0x686f
jmp 0x6930
cmpl $-0x2, %r13d
jle 0x6941
cmpq $0x0, 0x28(%r12)
je 0x691e
movq 0x8(%rsp), %rdi
callq 0x18f64
testl %eax, %eax
js 0x694b
movq %r14, %rdi
callq 0x17904
movq %rbx, %rdi
callq 0x16ea6
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4b3e
jmp 0x686f
callq 0x4b22
jmp 0x686f
callq 0x4a38
jmp 0x686c
callq 0x4a54
xorl %eax, %eax
jmp 0x68ae
movq %r15, %rdi
callq 0x4a70
jmp 0x6964