// File: /workspace/github_c_makefile_star2_O2/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, 0x10(%rsp)
movl %r8d, 0x18(%rsp)
movq %rcx, %r15
movl %edx, %r14d
movq %rsi, 0x40(%rsp)
movq %rdi, %rbx
leaq 0x3cbcd(%rip), %rsi     # 0x42f79
callq 0xa84d
movq %rax, 0x78(%rsp)
testq %rax, %rax
je 0x69cd
movq %rbx, 0x68(%rsp)
movq %rbx, %rdi
callq 0x1013a
movq %rax, 0x48(%rsp)
testq %rax, %rax
je 0x69d9
movq 0xf0(%rsp), %r12
xorps %xmm0, %xmm0
leaq 0xa0(%rsp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq 0x10(%rsp), %rax
movslq 0x28(%rax), %rbx
pushq $0x4
popq %rsi
movq %rbx, %rdi
callq 0x36b0
movq %rax, 0x28(%rsp)
pushq $0x18
popq %rsi
movq %rbx, %rdi
callq 0x36b0
movq %rax, 0x50(%rsp)
xorl %eax, %eax
movl 0x18(%rsp), %edx
testl %edx, %edx
movl $0x0, %ecx
cmovgl %edx, %ecx
testl %r14d, %r14d
cmovlel %eax, %r14d
movq %r14, 0x70(%rsp)
imulq $0x2028, %rcx, %rax     # imm = 0x2028
movq %rax, 0x98(%rsp)
pushq $0x1
popq %r14
pushq $-0x1
popq %rbp
pushq $0x9
popq %rbx
xorl %edx, %edx
xorl %r13d, %r13d
movq $0x0, 0x58(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0x0, 0x24(%rsp)
movq %r15, 0x38(%rsp)
cmpq 0x70(%rsp), %rdx
je 0x6897
movq 0x48(%rsp), %rcx
movq 0x18(%rcx), %rdi
movq 0x40(%rsp), %rax
movq %rdx, 0x88(%rsp)
movq (%rax,%rdx,8), %rsi
leaq 0x937a(%rip), %rdx      # 0xf81c
leaq 0x6c0d(%rip), %r8       # 0xd0b6
leaq 0x96bb(%rip), %r9       # 0xfb6b
callq 0xd8bd
movq %rax, 0x80(%rsp)
testq %rax, %rax
je 0x6887
movq 0x78(%rsp), %rdi
callq 0xb0db
movq %rax, %rdi
movq 0x80(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
movq 0x48(%rsp), %rcx
callq 0xd9c5
testl %eax, %eax
js 0x687a
movl %r14d, 0x20(%rsp)
movq 0xb0(%rsp), %r14
pushq $0x1
popq %r15
movq %r14, %rax
leaq 0x3b450(%rip), %r12     # 0x41960
movq %rax, %rdi
movl %ebx, %esi
callq 0x3590
testq %rax, %rax
je 0x6527
incq %rax
incq %r15
jmp 0x6510
testl %ebp, %ebp
js 0x653d
movl %ebp, %eax
cmpl %r15d, %eax
jne 0x69ae
movq %rbp, 0x30(%rsp)
jmp 0x6550
movq %r15, %rdi
pushq $0x8
popq %rsi
callq 0x36b0
movq %rax, %r13
movq %r15, 0x30(%rsp)
movq %r14, %rdi
callq 0x3380
leaq 0x1(%rax), %rsi
movq 0x58(%rsp), %rdi
callq 0x37c0
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
callq 0x3660
movq %r15, 0x58(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x3670
movq %rax, %r14
movq %r13, %rbp
testq %r14, %r14
je 0x65c9
movq (%rbp), %r15
movq %r14, %rdi
callq 0x3380
leaq 0x1(%rax), %rsi
movq %r15, %rdi
callq 0x37c0
movq %rax, (%rbp)
movq %rax, %rdi
movq %r14, %rsi
callq 0x3660
xorl %edi, %edi
movq %r12, %rsi
callq 0x3670
movq %rax, %r14
addq $0x8, %rbp
jmp 0x658a
movq 0x30(%rsp), %r12
cmpl $0x2, %r12d
jle 0x6994
cmpl $0x0, 0x24(%rsp)
jne 0x670d
movq 0x10(%rsp), %rax
cmpl $0x0, 0x4(%rax)
je 0x670d
leaq 0x39368(%rip), %rdi     # 0x3f961
movq 0xf0(%rsp), %r15
movq %r15, %rsi
callq 0x3530
movq 0x10(%rsp), %rax
cmpl $0x0, 0x8(%rax)
leaq 0x39362(%rip), %rbp     # 0x3f97b
je 0x664e
leaq 0x39351(%rip), %rdi     # 0x3f973
movq %r15, %rsi
callq 0x3530
pushq $0x5
popq %rax
movl %eax, %r14d
leal -0x1(%r14), %eax
cmpl %r12d, %eax
jge 0x664e
movq %r15, %rdi
movq %rbp, %rsi
movl %r14d, %edx
xorl %eax, %eax
callq 0x37f0
incl %r14d
jmp 0x6630
xorl %ebp, %ebp
movq 0x38(%rsp), %r14
leaq 0x392b2(%rip), %r15     # 0x3f90e
cmpq %rbp, 0x98(%rsp)
je 0x66fd
movq (%r14,%rbp), %rdx
movq 0xf0(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x37f0
movq 0x10(%rsp), %rax
cmpl $0x0, 0xc(%rax)
je 0x66f1
movq (%r14,%rbp), %r14
movq %r14, %rdi
callq 0x3380
leaq 0x5(%rax), %rdi
callq 0x3240
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
callq 0x3660
movq %r15, %rdi
callq 0x3380
movl $0x6769735f, (%r15,%rax) # imm = 0x6769735F
movw $0x32, 0x4(%r15,%rax)
movq %r12, %rdi
leaq 0x3923b(%rip), %rsi     # 0x3f90e
movq %r15, %rdx
xorl %eax, %eax
callq 0x37f0
movq %r15, %rdi
leaq 0x39227(%rip), %r15     # 0x3f90e
movq 0x38(%rsp), %r14
callq 0x3370
addq $0x2028, %rbp           # imm = 0x2028
jmp 0x665c
pushq $0xa
popq %rdi
movq 0xf0(%rsp), %rsi
callq 0x3350
movq 0x18(%r13), %r14
xorl %r15d, %r15d
movsbq (%r14,%r15), %rbp
testq %rbp, %rbp
je 0x674b
testq %r15, %r15
jne 0x6729
cmpb $0x2d, (%r14)
je 0x6746
callq 0x3410
cmpb $0x2e, %bpl
je 0x6746
movq (%rax), %rax
movzwl (%rax,%rbp,2), %eax
andl $0x800, %eax            # imm = 0x800
je 0x695f
incq %r15
jmp 0x6714
movq %r14, %rdi
callq 0x3560
movl %eax, %ecx
testl %eax, %eax
jns 0x6768
movq 0x10(%rsp), %rax
cmpl $0x0, 0x20(%rax)
je 0x686e
movl %ecx, 0x64(%rsp)
movl 0x20(%rsp), %eax
movl %eax, %ebp
subl 0x1c(%rsp), %ebp
leal -0x1(%rbp), %eax
movslq %eax, %rcx
movq %rcx, 0x90(%rsp)
imulq $0x18, %rcx, %r15
addq 0x50(%rsp), %r15
movq (%r13), %rdi
movq %r15, %rsi
callq 0x5ebf
movl %ebx, %edi
movq %r15, %rsi
callq 0x5e3a
movq 0x8(%r13), %rdi
movq %r15, %rsi
callq 0x5ebf
movl %ebx, %edi
movq %r15, %rsi
callq 0x5e3a
movq 0x10(%r13), %rdi
movq %r15, %rsi
callq 0x5ebf
movq 0x10(%rsp), %rax
cmpl $0x0, 0x8(%rax)
je 0x67f9
movl 0x30(%rsp), %r14d
pushq $0x3
popq %r12
cmpq %r14, %r12
jae 0x67f9
movq (%r13,%r12,8), %rdx
movq %r15, %rdi
leaq 0x39121(%rip), %rsi     # 0x3f90e
xorl %eax, %eax
callq 0x8c8a
incq %r12
jmp 0x67d9
movq 0x28(%rsp), %rax
movl 0x64(%rsp), %ecx
movq 0x90(%rsp), %rdx
movl %ecx, (%rax,%rdx,4)
movl 0x20(%rsp), %r14d
movl %r14d, %eax
cltd
movq 0x10(%rsp), %rcx
idivl 0x28(%rcx)
testl %edx, %edx
movq 0x38(%rsp), %r15
jne 0x6855
movq 0xf0(%rsp), %rax
movq %rax, (%rsp)
movq 0x28(%rsp), %rdi
movl %ebp, %esi
movq %r15, %rdx
movl 0x18(%rsp), %ecx
movq 0x10(%rsp), %r8
movq 0x50(%rsp), %r9
callq 0x6a8d
movl %r14d, 0x1c(%rsp)
incl %r14d
incl 0x24(%rsp)
movq 0xf0(%rsp), %r12
movq 0x30(%rsp), %rbp
jmp 0x64c6
movq 0x38(%rsp), %r15
movl 0x20(%rsp), %r14d
jmp 0x6858
movq 0x80(%rsp), %rdi
callq 0xd5aa
movq 0x88(%rsp), %rdx
incq %rdx
jmp 0x6476
movl 0x1c(%rsp), %eax
notl %eax
addl %eax, %r14d
movq %r12, (%rsp)
movq 0x28(%rsp), %rdi
movl %r14d, %esi
movq %r15, %rdx
movl 0x18(%rsp), %ecx
movq 0x10(%rsp), %r8
movq 0x50(%rsp), %r9
callq 0x6a8d
xorl %ebx, %ebx
testl %ebp, %ebp
cmovlel %ebx, %ebp
cmpq %rbx, %rbp
je 0x68dd
movq (%r13,%rbx,8), %rdi
callq 0x3370
incq %rbx
jmp 0x68c9
movq %r13, %rdi
callq 0x3370
movq 0x58(%rsp), %rdi
callq 0x3370
movq 0x28(%rsp), %rdi
callq 0x3370
movq 0xb0(%rsp), %rdi
callq 0x3370
movq 0x48(%rsp), %rdi
callq 0xff56
movq 0x78(%rsp), %rdi
callq 0xaf35
testl %eax, %eax
jne 0x69e2
xorl %ebx, %ebx
movq 0x70(%rsp), %r14
cmpq %rbx, %r14
je 0x6941
movq 0x40(%rsp), %rax
movq (%rax,%rbx,8), %rdi
callq 0x3370
incq %rbx
jmp 0x6929
movq 0x40(%rsp), %rdi
callq 0x3370
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4c682(%rip), %rbx     # 0x52fe8
movq (%rbx), %rdi
leaq 0x3901b(%rip), %rsi     # 0x3f98b
leaq 0x3904d(%rip), %rdx     # 0x3f9c4
pushq $0x48
popq %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x37f0
movq (%rbx), %rdi
callq 0x38c0
pushq $0x1
popq %rdi
callq 0x3170
leaq 0x38f39(%rip), %rdi     # 0x3f8d4
leaq 0x3907e(%rip), %rsi     # 0x3fa20
movl $0x13c, %edx            # imm = 0x13C
xorl %eax, %eax
callq 0x5f58
movq 0x4c633(%rip), %rbx     # 0x52fe8
movq (%rbx), %rdi
leaq 0x38f7b(%rip), %rsi     # 0x3f93a
movl %r15d, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x37f0
jmp 0x6984
leaq 0x38ec7(%rip), %rdi     # 0x3f89b
movq %rbx, %rsi
jmp 0x69ee
leaq 0x38ece(%rip), %rdi     # 0x3f8ae
jmp 0x69e9
leaq 0x38f29(%rip), %rdi     # 0x3f912
movq 0x68(%rsp), %rsi
xorl %eax, %eax
callq 0x69f5