// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x28(%rbp), %rdi
callq 0x244d0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x5b61
movq 0x121498(%rip), %rax    # 0x126fd0
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
leaq 0xfe5a2(%rip), %rsi     # 0x1040f0
movb $0x0, %al
callq 0x4700
movl $0x1, -0x4(%rbp)
jmp 0x60a7
movq -0x20(%rbp), %rax
movl 0x10(%rax), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jle 0x5c04
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rdi
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x20(%rbp), %rax
movslq 0x18(%rax), %rax
addq %rax, %rsi
callq 0x4850
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x5bc5
movq 0x121421(%rip), %rax    # 0x126fd0
movq (%rax), %rdi
leaq 0xfe556(%rip), %rsi     # 0x10410f
movb $0x0, %al
callq 0x4700
jmp 0x6070
movq -0x58(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rdi
movq -0x40(%rbp), %rax
addq 0x8(%rax), %rdi
movq -0x20(%rbp), %rax
movslq 0x18(%rax), %rdx
xorl %esi, %esi
callq 0x4250
movq -0x20(%rbp), %rax
movslq 0x18(%rax), %rcx
movq -0x40(%rbp), %rax
addq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
callq 0x1f9b0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x5c31
movq 0x1213b5(%rip), %rax    # 0x126fd0
movq (%rax), %rdi
leaq 0xfe508(%rip), %rsi     # 0x10412d
movb $0x0, %al
callq 0x4700
jmp 0x6070
movq -0x20(%rbp), %rax
movl 0x8(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x5c6a
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
je 0x5c6a
movq -0x30(%rbp), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rsi
callq 0xd880
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
je 0x5c68
jmp 0x6070
jmp 0x5c6a
movq -0x20(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x5ca3
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x254e0
cmpl $0x0, %eax
jge 0x5ca3
movq 0x121343(%rip), %rax    # 0x126fd0
movq (%rax), %rdi
leaq 0xfe4bb(%rip), %rsi     # 0x104152
movb $0x0, %al
callq 0x4700
jmp 0x6070
movq -0x20(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x5cee
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x20(%rbp), %rax
movl 0x30(%rax), %edx
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rcx
callq 0x21e40
cmpl $0x0, %eax
jge 0x5cec
movq 0x1212fa(%rip), %rax    # 0x126fd0
movq (%rax), %rdi
leaq 0xfe490(%rip), %rsi     # 0x104170
movb $0x0, %al
callq 0x4700
jmp 0x6070
jmp 0x5cee
movl -0x14(%rbp), %eax
addl $0x1, %eax
cmpl -0x8(%rbp), %eax
jge 0x5f86
movq -0x20(%rbp), %rax
movl 0x8(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x5f86
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x23a50
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
jne 0x5d53
movq 0x12129a(%rip), %rax    # 0x126fd0
movq (%rax), %rdi
leaq 0xfe44c(%rip), %rsi     # 0x10418c
leaq 0xfe469(%rip), %rdx     # 0x1041b0
movb $0x0, %al
callq 0x4700
jmp 0x6070
movq -0x20(%rbp), %rax
cmpl $0x0, 0x24(%rax)
je 0x5e4c
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x10(%rbp), %rdx
movl -0x14(%rbp), %eax
addl $0x1, %eax
cltq
shlq $0x3, %rax
addq %rax, %rdx
movl -0x8(%rbp), %ecx
subl -0x14(%rbp), %ecx
subl $0x1, %ecx
callq 0x23e40
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0x5d9a
jmp 0x6070
jmp 0x5d9c
movq -0x28(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x50(%rbp), %rdx
callq 0x60b0
movl %eax, -0x34(%rbp)
cmpl $0x0, %eax
jl 0x5e1b
movq -0x20(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x5dfb
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x50(%rbp), %rdx
callq 0x2c3d0
cmpl $0x0, %eax
jge 0x5dfb
movq 0x1211f4(%rip), %rax    # 0x126fd0
movq (%rax), %rdi
leaq 0xfe3d3(%rip), %rsi     # 0x1041b9
movb $0x0, %al
callq 0x4700
movq -0x68(%rbp), %rdi
callq 0x11760
jmp 0x6070
movq -0x20(%rbp), %rax
cmpl $0x0, 0x14(%rax)
je 0x5e19
movq -0x20(%rbp), %rcx
movl 0x14(%rcx), %eax
addl $-0x1, %eax
movl %eax, 0x14(%rcx)
cmpl $0x0, %eax
jne 0x5e19
jmp 0x5e1b
jmp 0x5d9c
movq -0x68(%rbp), %rdi
callq 0x11760
cmpl $-0x1, -0x34(%rbp)
jge 0x5e47
movq 0x12119f(%rip), %rax    # 0x126fd0
movq (%rax), %rdi
leaq 0xfe395(%rip), %rsi     # 0x1041d0
movb $0x0, %al
callq 0x4700
jmp 0x6070
jmp 0x5f73
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x5f71
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x10(%rbp), %rax
movslq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
callq 0x23de0
movq %rax, -0x70(%rbp)
cmpq $0x0, %rax
jne 0x5eb4
movq 0x121145(%rip), %rax    # 0x126fd0
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movslq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
leaq 0xfe345(%rip), %rsi     # 0x1041e6
leaq 0xfe308(%rip), %rdx     # 0x1041b0
movb $0x0, %al
callq 0x4700
jmp 0x6070
jmp 0x5eb6
movq -0x28(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x50(%rbp), %rdx
callq 0x60b0
movl %eax, -0x34(%rbp)
cmpl $0x0, %eax
jl 0x5f35
movq -0x20(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x5f15
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x50(%rbp), %rdx
callq 0x2c3d0
cmpl $0x0, %eax
jge 0x5f15
movq 0x1210da(%rip), %rax    # 0x126fd0
movq (%rax), %rdi
leaq 0xfe2b9(%rip), %rsi     # 0x1041b9
movb $0x0, %al
callq 0x4700
movq -0x70(%rbp), %rdi
callq 0x11760
jmp 0x6070
movq -0x20(%rbp), %rax
cmpl $0x0, 0x14(%rax)
je 0x5f33
movq -0x20(%rbp), %rcx
movl 0x14(%rcx), %eax
addl $-0x1, %eax
movl %eax, 0x14(%rcx)
cmpl $0x0, %eax
jne 0x5f33
jmp 0x5f35
jmp 0x5eb6
movq -0x70(%rbp), %rdi
callq 0x11760
cmpl $-0x1, -0x34(%rbp)
jge 0x5f61
movq 0x121085(%rip), %rax    # 0x126fd0
movq (%rax), %rdi
leaq 0xfe27b(%rip), %rsi     # 0x1041d0
movb $0x0, %al
callq 0x4700
jmp 0x6070
jmp 0x5f63
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x5e55
jmp 0x5f73
movq -0x48(%rbp), %rdi
callq 0xd1f0
movq $0x0, -0x48(%rbp)
jmp 0x6000
jmp 0x5f88
movq -0x28(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x50(%rbp), %rdx
callq 0x29610
movl %eax, -0x34(%rbp)
cmpl $0x0, %eax
jl 0x5ffe
movq -0x20(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x5fde
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x50(%rbp), %rdx
callq 0x2c3d0
cmpl $0x0, %eax
jge 0x5fde
movq 0x121008(%rip), %rax    # 0x126fd0
movq (%rax), %rdi
leaq 0xfe1e7(%rip), %rsi     # 0x1041b9
movb $0x0, %al
callq 0x4700
jmp 0x6070
movq -0x20(%rbp), %rax
cmpl $0x0, 0x14(%rax)
je 0x5ffc
movq -0x20(%rbp), %rcx
movl 0x14(%rcx), %eax
addl $-0x1, %eax
movl %eax, 0x14(%rcx)
cmpl $0x0, %eax
jne 0x5ffc
jmp 0x5ffe
jmp 0x5f88
jmp 0x6000
cmpl $-0x1, -0x34(%rbp)
jge 0x6020
movq 0x120fc3(%rip), %rax    # 0x126fd0
movq (%rax), %rdi
leaq 0xfe1f2(%rip), %rsi     # 0x104209
movb $0x0, %al
callq 0x4700
jmp 0x6070
movq -0x20(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x6055
movq -0x30(%rbp), %rdi
callq 0x22020
cmpl $0x0, %eax
jge 0x6053
movq 0x120f90(%rip), %rax    # 0x126fd0
movq (%rax), %rdi
leaq 0xfe1d5(%rip), %rsi     # 0x10421f
movb $0x0, %al
callq 0x4700
jmp 0x6070
jmp 0x6055
movq -0x50(%rbp), %rdi
callq 0x1fbf0
movq -0x40(%rbp), %rdi
callq 0x1ea70
movl $0x0, -0x4(%rbp)
jmp 0x60a7
cmpq $0x0, -0x50(%rbp)
je 0x6080
movq -0x50(%rbp), %rdi
callq 0x1fbf0
cmpq $0x0, -0x40(%rbp)
je 0x6090
movq -0x40(%rbp), %rdi
callq 0x1ea70
cmpq $0x0, -0x48(%rbp)
je 0x60a0
movq -0x48(%rbp), %rdi
callq 0xd1f0
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq