// File: /workspace/github_c_makefile_star2_O1/zhou-lab[P]tbmate/chunk.c
// Function: chunk_query_region
int chunk_query_region(char *fname, char **regs, int nregs, tbk_t *tbks, int n_tbks, view_conf_t *conf, FILE *out_fh) {

  int i;
  htsFile *fp = hts_open(fname,"r");
  if(!fp) error("Could not read %s\n", fname);
  
  tbx_t *tbx = tbx_index_load(fname);
  if(!tbx) error("Could not load .tbi/.csi index of %s\n", fname);
  kstring_t str = {0,0,0};
  const char **seq = NULL;

  /* line reading and splitting */
  char **fields = NULL;
  int nfields = -1;
  char *aux = NULL;
  
  int n, ii;
  int linenum=0;

  int index_chunk_beg = 0;
  int index_chunk_end = 1;

  /* offsets in the current chunk */
  int *ns = calloc(conf->n_chunk_index, sizeof(int));
  /* output */
  kstring_t *ks_out = calloc(conf->n_chunk_index, sizeof(kstring_t));
  
  for(i=0; i<nregs; i++) {
    hts_itr_t *itr = tbx_itr_querys(tbx, regs[i]);
    if(!itr) continue;
    while (tbx_itr_next(fp, tbx, itr, &str) >= 0) {

      line_get_fields2(str.s, "\t", &fields, &nfields, &aux);

      if (nfields < 3)
        wzfatal("[%s:%d] Bed file has fewer than 3 columns.\n", __func__, __LINE__);

      if (!linenum && conf->column_name) { /* header */
        tbk_print_columnnames(tbks, n_tbks, nfields, out_fh, conf);
      }
      
      ensure_number2(fields[3]);
      n = atoi(fields[3]);

      if (n >= 0 || conf->show_unaddressed) {
        kstring_t *ks = &ks_out[index_chunk_end - index_chunk_beg - 1];
        kputs(fields[0], ks); kputc('\t', ks);
        kputs(fields[1], ks); kputc('\t', ks);
        kputs(fields[2], ks);
        if (conf->print_all) {
          for (ii=3; ii<nfields; ++ii)
            ksprintf(ks, "\t%s", fields[ii]);
        }
        ns[index_chunk_end - index_chunk_beg - 1] = n;
      
        if (index_chunk_end % conf->n_chunk_index == 0) {
          query_one_chunk(ns, index_chunk_end-index_chunk_beg, tbks, n_tbks, conf, ks_out, out_fh);
          index_chunk_beg = index_chunk_end;
        }
                
        index_chunk_end++;
      }
      
      linenum++;
    }
    tbx_itr_destroy(itr);
  }

  query_one_chunk(ns, index_chunk_end-index_chunk_beg-1, tbks, n_tbks, conf, ks_out, out_fh);

  free_fields(fields, nfields);
  free(aux);
  free(ns);
  free(seq);
  free(str.s);
  tbx_destroy(tbx);

  if(hts_close(fp)) error("hts_close returned non-zero status: %s\n", fname);
  
  for(i=0; i<nregs; i++) free(regs[i]);
  free(regs);
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r9, 0x8(%rsp)
movl %r8d, 0x10(%rsp)
movq %rcx, %r14
movl %edx, %ebp
movq %rsi, %r12
movq %rdi, %rbx
leaq 0x48927(%rip), %rsi     # 0x5105d
callq 0xd846
movq %rax, 0x40(%rsp)
testq %rax, %rax
je 0x9008
movq %rbx, %rdi
callq 0x13f4f
testq %rax, %rax
je 0x9011
movq %rax, %r13
movq %rbx, 0x88(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movq $0x0, 0x80(%rsp)
movq 0x8(%rsp), %rax
movslq 0x28(%rax), %rbx
movl $0x4, %esi
movq %rbx, %rdi
callq 0x36a0
movq %rax, 0x30(%rsp)
movl $0x18, %esi
movq %rbx, %rdi
callq 0x36a0
movq %rax, 0x48(%rsp)
movl %ebp, %eax
movq %rax, 0x60(%rsp)
testl %ebp, %ebp
jle 0x8f11
movl %ebp, 0x50(%rsp)
movq %r12, 0x58(%rsp)
movl 0x10(%rsp), %eax
imulq $0x2028, %rax, %rax     # imm = 0x2028
movq %rax, 0xb0(%rsp)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl $0x1, 0x14(%rsp)
xorl %ecx, %ecx
movl $0x0, 0x2c(%rsp)
movl $0x0, 0x18(%rsp)
xorl %ebp, %ebp
xorl %r15d, %r15d
movq %r13, 0x68(%rsp)
movq %r14, 0x38(%rsp)
movq 0x18(%r13), %rdi
movq 0x58(%rsp), %rax
movq %rcx, 0x90(%rsp)
movq (%rax,%rcx,8), %rsi
leaq 0xacf8(%rip), %rdx      # 0x13514
movq %r13, %rcx
leaq 0x7d7b(%rip), %r8       # 0x105a1
leaq 0xb000(%rip), %r9       # 0x1382d
callq 0x110e5
testq %rax, %rax
je 0x8edc
movq %rax, %rbx
movq 0x40(%rsp), %rdi
callq 0xe0b4
movq %rax, %rdi
movq %rbx, %rsi
leaq 0x70(%rsp), %rdx
movq %r13, %rcx
callq 0x11205
testl %eax, %eax
js 0x8ed4
movq %rbx, 0x98(%rsp)
movq 0x80(%rsp), %r14
movq %r14, %rdi
movl $0x9, %esi
callq 0x3580
movl $0x1, %ebx
leaq 0x470dc(%rip), %r13     # 0x4f968
testq %rax, %rax
je 0x88a5
incl %ebx
incq %rax
movq %rax, %rdi
movl $0x9, %esi
callq 0x3580
jmp 0x888c
testl %r12d, %r12d
js 0x88b4
cmpl %ebx, %r12d
je 0x88c6
jmp 0x8ffe
movl %ebx, %edi
movl $0x8, %esi
callq 0x36a0
movq %rax, %r15
movl %ebx, %r12d
movq %r14, %rdi
callq 0x3380
leaq 0x1(%rax), %rsi
movq %rbp, %rdi
callq 0x37b0
movq %rax, %rbx
movq %rax, %rdi
movq %r14, %rsi
callq 0x3650
movq %rbx, 0x20(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3660
testq %rax, %rax
je 0x8943
movq %rax, %rbx
movq %r13, %rbp
movq %r15, %r13
movq (%r13), %r14
movq %rbx, %rdi
callq 0x3380
leaq 0x1(%rax), %rsi
movq %r14, %rdi
callq 0x37b0
movq %rax, (%r13)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3650
xorl %edi, %edi
movq %rbp, %rsi
callq 0x3660
movq %rax, %rbx
addq $0x8, %r13
testq %rax, %rax
jne 0x8906
cmpl $0x2, %r12d
jle 0x8fe4
movl %r12d, 0x1c(%rsp)
cmpl $0x0, 0x2c(%rsp)
leaq 0x44fae(%rip), %rbp     # 0x4d90c
jne 0x8a95
movq 0x8(%rsp), %rax
cmpl $0x0, 0x4(%rax)
je 0x8a95
leaq 0x44fe5(%rip), %rdi     # 0x4d95f
movq 0xf0(%rsp), %r13
movq %r13, %rsi
callq 0x3520
movq 0x8(%rsp), %rax
cmpl $0x0, 0x8(%rax)
movl 0x1c(%rsp), %ebx
leaq 0x44fdb(%rip), %r12     # 0x4d979
je 0x89df
leaq 0x44fca(%rip), %rdi     # 0x4d971
movq %r13, %rsi
callq 0x3520
cmpl $0x5, %ebx
jl 0x89df
movl %ebx, %r14d
negl %r14d
movl $0x5, %ebx
movq %r13, %rdi
movq %r12, %rsi
movl %ebx, %edx
xorl %eax, %eax
callq 0x37e0
leal (%r14,%rbx), %eax
incl %eax
movl %ebx, %ecx
incl %ecx
movl %ecx, %ebx
cmpl $0x1, %eax
jne 0x89bf
cmpl $0x0, 0x10(%rsp)
movq 0x38(%rsp), %rbx
jle 0x8a83
xorl %r13d, %r13d
movq (%rbx,%r13), %rdx
movq 0xf0(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
xorl %eax, %eax
callq 0x37e0
movq 0x8(%rsp), %rax
cmpl $0x0, 0xc(%rax)
je 0x8a6e
movq (%rbx,%r13), %rbx
movq %rbx, %rdi
callq 0x3380
leaq 0x5(%rax), %rdi
callq 0x3240
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3650
movq %r14, %rdi
callq 0x3380
movl $0x6769735f, (%r14,%rax) # imm = 0x6769735F
movw $0x32, 0x4(%r14,%rax)
movq %r12, %rdi
movq %rbp, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x37e0
movq %r14, %rdi
movq 0x38(%rsp), %rbx
callq 0x3370
addq $0x2028, %r13           # imm = 0x2028
cmpq %r13, 0xb0(%rsp)
jne 0x89f2
movl $0xa, %edi
movq 0xf0(%rsp), %rsi
callq 0x3350
movq 0x18(%r15), %rbx
movb (%rbx), %r12b
testb %r12b, %r12b
movq 0x20(%rsp), %rbp
je 0x8aee
movl $0x1, %r13d
movl %r12d, %r14d
cmpb $0x2d, %r12b
sete %al
cmpq $0x1, %r13
sete %cl
testb %cl, %al
jne 0x8ae2
callq 0x3410
cmpb $0x2e, %r14b
je 0x8ae2
movq (%rax), %rax
movsbq %r14b, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x800, %eax            # imm = 0x800
je 0x8fdc
movb (%rbx,%r13), %r14b
incq %r13
testb %r14b, %r14b
jne 0x8aaf
movq %rbx, %rdi
callq 0x3550
movl %eax, %ecx
testl %eax, %eax
jns 0x8b0b
movq 0x8(%rsp), %rax
cmpl $0x0, 0x20(%rax)
je 0x8ec3
movl %ecx, 0x54(%rsp)
movl 0x14(%rsp), %eax
subl 0x18(%rsp), %eax
movq %rax, 0xa0(%rsp)
decl %eax
cltq
movq %rax, 0xa8(%rsp)
leaq (%rax,%rax,2), %r12
movq 0x48(%rsp), %rbp
leaq (,%r12,8), %r13
addq %rbp, %r13
movq (%r15), %rbx
movq %rbx, %rdi
callq 0x3380
movq (%rbp,%r12,8), %rcx
movslq %eax, %r14
leaq (%rcx,%r14), %rax
incq %rax
cmpq 0x8(%rbp,%r12,8), %rax
jb 0x8baa
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rsi, 0x8(%r13)
movq 0x10(%r13), %rdi
callq 0x37b0
testq %rax, %rax
je 0x8bce
movq %rax, 0x10(%r13)
movq 0x10(%r13), %rdi
addq (%r13), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x3850
addq (%r13), %r14
movq %r14, (%r13)
movq 0x10(%r13), %rax
movb $0x0, (%rax,%r14)
movq (%r13), %rax
incq %rax
cmpq 0x8(%r13), %rax
movl 0x1c(%rsp), %r12d
leaq 0x44d27(%rip), %rbp     # 0x4d90c
jb 0x8c31
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rsi, 0x8(%r13)
movq 0x10(%r13), %rdi
callq 0x37b0
testq %rax, %rax
je 0x8c51
movq %rax, 0x10(%r13)
movq (%r13), %rax
movq 0x10(%r13), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r13)
movb $0x9, (%rcx,%rax)
movq (%r13), %rax
movq 0x10(%r13), %rcx
movb $0x0, (%rcx,%rax)
movq 0x8(%r15), %rbx
movq %rbx, %rdi
callq 0x3380
movq (%r13), %rcx
movslq %eax, %r14
leaq (%rcx,%r14), %rax
incq %rax
cmpq 0x8(%r13), %rax
jb 0x8cbb
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rsi, 0x8(%r13)
movq 0x10(%r13), %rdi
callq 0x37b0
testq %rax, %rax
je 0x8cdf
movq %rax, 0x10(%r13)
movq 0x10(%r13), %rdi
addq (%r13), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x3850
addq (%r13), %r14
movq %r14, (%r13)
movq 0x10(%r13), %rax
movb $0x0, (%rax,%r14)
movq (%r13), %rax
incq %rax
cmpq 0x8(%r13), %rax
jb 0x8d36
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rsi, 0x8(%r13)
movq 0x10(%r13), %rdi
callq 0x37b0
testq %rax, %rax
je 0x8d56
movq %rax, 0x10(%r13)
movq (%r13), %rax
movq 0x10(%r13), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r13)
movb $0x9, (%rcx,%rax)
movq (%r13), %rax
movq 0x10(%r13), %rcx
movb $0x0, (%rcx,%rax)
movq 0x10(%r15), %rbx
movq %rbx, %rdi
callq 0x3380
movq (%r13), %rcx
movslq %eax, %r14
leaq (%rcx,%r14), %rax
incq %rax
cmpq 0x8(%r13), %rax
jb 0x8dc0
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rsi, 0x8(%r13)
movq 0x10(%r13), %rdi
callq 0x37b0
testq %rax, %rax
je 0x8de4
movq %rax, 0x10(%r13)
movq 0x10(%r13), %rdi
addq (%r13), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x3850
addq (%r13), %r14
movq %r14, (%r13)
movq 0x10(%r13), %rax
movb $0x0, (%rax,%r14)
movq 0x8(%rsp), %rax
cmpl $0x0, 0x8(%rax)
je 0x8e21
cmpl $0x3, %r12d
je 0x8e21
cmpl $0x5, %r12d
movl $0x4, %ebx
cmovgel %r12d, %ebx
movl $0x3, %r14d
movq (%r15,%r14,8), %rdx
movq %r13, %rdi
movq %rbp, %rsi
xorl %eax, %eax
callq 0xb4fc
incq %r14
cmpq %r14, %rbx
jne 0x8e08
movq 0x30(%rsp), %rax
movl 0x54(%rsp), %ecx
movq 0xa8(%rsp), %rdx
movl %ecx, (%rax,%rdx,4)
movl 0x14(%rsp), %ebx
movl %ebx, %eax
cltd
movq 0x8(%rsp), %rcx
idivl 0x28(%rcx)
testl %edx, %edx
movq 0x38(%rsp), %r14
jne 0x8e80
movq 0xf0(%rsp), %rax
movq %rax, (%rsp)
movq 0x30(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movq %r14, %rdx
movl 0x10(%rsp), %ecx
movq 0x8(%rsp), %r8
movq 0x48(%rsp), %r9
callq 0x90cd
movl %ebx, 0x18(%rsp)
incl %ebx
movl %ebx, 0x14(%rsp)
movq 0x68(%rsp), %r13
movq 0x20(%rsp), %rbp
incl 0x2c(%rsp)
movq 0x40(%rsp), %rdi
callq 0xe0b4
movq %rax, %rdi
movq 0x98(%rsp), %rbx
movq %rbx, %rsi
leaq 0x70(%rsp), %rdx
movq %r13, %rcx
callq 0x11205
testl %eax, %eax
jns 0x886b
jmp 0x8ed4
movq 0x68(%rsp), %r13
movq 0x38(%rsp), %r14
movl 0x1c(%rsp), %r12d
jmp 0x8e90
movq %rbx, %rdi
callq 0x10de4
movq 0x90(%rsp), %rcx
incq %rcx
cmpq 0x60(%rsp), %rcx
jne 0x8800
movq %rbp, 0x20(%rsp)
movl 0x18(%rsp), %eax
notl %eax
movl 0x14(%rsp), %esi
addl %eax, %esi
movl %r12d, %ebx
movq 0x58(%rsp), %r12
movl 0x50(%rsp), %ebp
jmp 0x8f24
xorl %r15d, %r15d
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq $0x0, 0x20(%rsp)
xorl %esi, %esi
movq 0xf0(%rsp), %rax
movq %rax, (%rsp)
movq 0x30(%rsp), %rdi
movq %r14, %rdx
movl 0x10(%rsp), %ecx
movq 0x8(%rsp), %r8
movq 0x48(%rsp), %r9
callq 0x90cd
testl %ebx, %ebx
jle 0x8f65
movl %ebx, %ebx
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
callq 0x3370
incq %r14
cmpq %r14, %rbx
jne 0x8f54
movq %r15, %rdi
callq 0x3370
movq 0x20(%rsp), %rdi
callq 0x3370
movq 0x30(%rsp), %rdi
callq 0x3370
movq 0x80(%rsp), %rdi
callq 0x3370
movq %r13, %rdi
callq 0x13cf6
movq 0x40(%rsp), %rdi
callq 0xdf1c
testl %eax, %eax
jne 0x901d
testl %ebp, %ebp
movq 0x60(%rsp), %r14
jle 0x8fc0
xorl %ebx, %ebx
movq (%r12,%rbx,8), %rdi
callq 0x3370
incq %rbx
cmpq %rbx, %r14
jne 0x8faf
movq %r12, %rdi
callq 0x3370
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x3d0d
leaq 0x448e7(%rip), %rdi     # 0x4d8d2
leaq 0x44a2e(%rip), %rsi     # 0x4da20
movl $0x13c, %edx            # imm = 0x13C
xorl %eax, %eax
callq 0x80fa
movl %ebx, %edi
movl %r12d, %esi
callq 0x3ce0
leaq 0x4488a(%rip), %rdi     # 0x4d899
jmp 0x9018
leaq 0x44894(%rip), %rdi     # 0x4d8ac
movq %rbx, %rsi
jmp 0x902c
leaq 0x448ec(%rip), %rdi     # 0x4d910
movq 0x88(%rsp), %rsi
xorl %eax, %eax
callq 0x9033