// File: /workspace/github_c_makefile_star2_O3/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 $0xd8, %rsp
movq %r9, 0x18(%rsp)
movl %r8d, 0x14(%rsp)
movq %rcx, 0x40(%rsp)
movl %edx, %r13d
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x489a8(%rip), %rsi     # 0x51225
callq 0xda3c
movq %rax, 0x70(%rsp)
testq %rax, %rax
je 0x91f5
movq %rbx, %rdi
callq 0x141b7
testq %rax, %rax
je 0x91fe
movq %rax, %r14
movq %rbx, 0xb0(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x90(%rsp)
movq $0x0, 0xa0(%rsp)
movq 0x18(%rsp), %rax
movslq 0x28(%rax), %rbx
movl $0x4, %esi
movq %rbx, %rdi
callq 0x36a0
movq %rax, %rbp
movl $0x18, %esi
movq %rbx, %rdi
callq 0x36a0
movl %r13d, %r12d
testl %r13d, %r13d
jle 0x910b
movq %rax, 0x78(%rsp)
movq %rbp, 0x50(%rsp)
movq %r12, 0x58(%rsp)
movl %r13d, 0x48(%rsp)
movq %r15, 0x60(%rsp)
movl 0x14(%rsp), %eax
imulq $0x2028, %rax, %rax     # imm = 0x2028
movq %rax, 0xd0(%rsp)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl $0x1, 0x2c(%rsp)
leaq 0x47203(%rip), %rbp     # 0x4fb39
xorl %ecx, %ecx
xorl %r15d, %r15d
movl $0x0, 0x3c(%rsp)
xorl %r13d, %r13d
movq $0x0, 0x20(%rsp)
movq 0x110(%rsp), %rbx
movq %r14, 0x68(%rsp)
movq 0x18(%r14), %rdi
movq 0x60(%rsp), %rax
movq %rcx, 0xb8(%rsp)
movq (%rax,%rcx,8), %rsi
leaq 0xad40(%rip), %rdx      # 0x136b8
movq %r14, %rcx
leaq 0x7f70(%rip), %r8       # 0x108f2
leaq 0xb04b(%rip), %r9       # 0x139d4
callq 0x1141c
testq %rax, %rax
je 0x908c
movq %r13, 0x30(%rsp)
movl %r12d, %r13d
movq 0x70(%rsp), %rdi
movq %rax, %r12
callq 0xe2b2
movq %rax, %rdi
movq %r12, 0x88(%rsp)
movq %r12, %rsi
leaq 0x90(%rsp), %rdx
movq %r14, %rcx
callq 0x1152f
movl %r13d, %r12d
testl %eax, %eax
js 0x907a
movq 0x30(%rsp), %r13
movq 0xa0(%rsp), %r14
movq %r14, %rdi
movl $0x9, %esi
callq 0x3580
movl $0x1, %ebx
testq %rax, %rax
je 0x8a0d
incl %ebx
incq %rax
movq %rax, %rdi
movl $0x9, %esi
callq 0x3580
jmp 0x89f4
testl %r12d, %r12d
js 0x8a22
cmpl %ebx, %r12d
jne 0x91eb
movl %r12d, 0x10(%rsp)
jmp 0x8a37
movl %ebx, %edi
movl $0x8, %esi
callq 0x36a0
movq %rax, 0x20(%rsp)
movl %ebx, 0x10(%rsp)
movq %r14, %rdi
callq 0x3380
leaq 0x1(%rax), %rsi
movq %r13, %rdi
callq 0x37b0
movq %rax, %r13
movq %rax, %rdi
movq %r14, %rsi
callq 0x3650
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3660
testq %rax, %rax
je 0x8ab1
movq %rax, %rbx
movq %rbp, %r12
movq 0x20(%rsp), %rbp
movq (%rbp), %r14
movq %rbx, %rdi
callq 0x3380
leaq 0x1(%rax), %rsi
movq %r14, %rdi
callq 0x37b0
movq %rax, (%rbp)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3650
xorl %edi, %edi
movq %r12, %rsi
callq 0x3660
movq %rax, %rbx
addq $0x8, %rbp
testq %rax, %rax
jne 0x8a74
movl 0x10(%rsp), %r12d
cmpl $0x2, %r12d
jle 0x91d1
movl %r15d, 0x4c(%rsp)
testl %r15d, %r15d
movq %r13, 0x30(%rsp)
jne 0x8c0e
movq 0x18(%rsp), %rax
cmpl $0x0, 0x4(%rax)
je 0x8c0e
leaq 0x44e78(%rip), %rdi     # 0x4d961
movq 0x110(%rsp), %rbp
movq %rbp, %rsi
callq 0x3520
movq 0x18(%rsp), %r13
cmpl $0x0, 0x8(%r13)
leaq 0x44e71(%rip), %r15     # 0x4d97b
je 0x8b4c
leaq 0x44e60(%rip), %rdi     # 0x4d973
movq %rbp, %rsi
callq 0x3520
cmpl $0x5, %r12d
jb 0x8b4c
movl %r12d, %r14d
negl %r14d
movl $0x5, %ebx
movq %rbp, %rdi
movq %r15, %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 0x8b2c
cmpl $0x0, 0x14(%rsp)
movq 0x40(%rsp), %rbx
jle 0x8bf2
xorl %ebp, %ebp
movq (%rbx,%rbp), %rdx
movq 0x110(%rsp), %r12
movq %r12, %rdi
leaq 0x44d9a(%rip), %r15     # 0x4d90e
movq %r15, %rsi
xorl %eax, %eax
callq 0x37e0
cmpl $0x0, 0xc(%r13)
je 0x8bdd
movq (%rbx,%rbp), %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 %r15, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x37e0
movq %r14, %rdi
movq 0x40(%rsp), %rbx
callq 0x3370
addq $0x2028, %rbp           # imm = 0x2028
cmpq %rbp, 0xd0(%rsp)
jne 0x8b5e
movl $0xa, %edi
movq 0x110(%rsp), %rsi
callq 0x3350
movq 0x30(%rsp), %r13
movl 0x10(%rsp), %r12d
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rbx
movb (%rbx), %r15b
testb %r15b, %r15b
je 0x8c67
movl $0x1, %r14d
movl %r15d, %ebp
cmpb $0x2d, %r15b
sete %al
cmpq $0x1, %r14
sete %cl
testb %cl, %al
jne 0x8c5b
callq 0x3410
cmpb $0x2e, %bpl
je 0x8c5b
movq (%rax), %rax
movsbq %bpl, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x800, %eax            # imm = 0x800
je 0x91c9
movb (%rbx,%r14), %bpl
incq %r14
testb %bpl, %bpl
jne 0x8c28
movq %rbx, %rdi
callq 0x3550
movl %eax, %ecx
testl %eax, %eax
jns 0x8c84
movq 0x18(%rsp), %rax
cmpl $0x0, 0x20(%rax)
je 0x9066
movl %ecx, 0x84(%rsp)
movq 0x20(%rsp), %rcx
movl %r12d, 0x10(%rsp)
movl 0x2c(%rsp), %eax
subl 0x3c(%rsp), %eax
movq %rax, 0xc0(%rsp)
decl %eax
cltq
movq %rax, 0xc8(%rsp)
leaq (%rax,%rax,2), %r15
movq 0x78(%rsp), %r12
leaq (%r12,%r15,8), %r14
movq (%rcx), %rbx
movq %rbx, %rdi
callq 0x3380
movq (%r12,%r15,8), %rdi
movslq %eax, %rbp
leaq (%rdi,%rbp), %rax
incq %rax
cmpq 0x8(%r12,%r15,8), %rax
jae 0x8cee
movq 0x10(%r14), %rax
movq 0x18(%rsp), %r15
movl 0x10(%rsp), %r12d
jmp 0x8d45
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(%r14)
movq 0x10(%r14), %rdi
callq 0x37b0
testq %rax, %rax
movq 0x18(%rsp), %r15
movl 0x10(%rsp), %r12d
je 0x8d61
movq %rax, 0x10(%r14)
movq (%r14), %rdi
addq %rax, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x3850
addq (%r14), %rbp
movq %rbp, (%r14)
movq 0x10(%r14), %rax
movb $0x0, (%rax,%rbp)
movq (%r14), %rdx
leaq 0x1(%rdx), %rcx
cmpq 0x8(%r14), %rcx
jae 0x8d74
movq 0x10(%r14), %rax
jmp 0x8dc5
movq %rcx, %rax
shrq %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rsi
shrq $0x10, %rsi
orq %rcx, %rsi
incq %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
callq 0x37b0
testq %rax, %rax
je 0x8dd7
movq %rax, 0x10(%r14)
movq (%r14), %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, (%r14)
movb $0x9, (%rax,%rdx)
movq (%r14), %rax
movq 0x10(%r14), %rcx
movb $0x0, (%rcx,%rax)
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rbx
movq %rbx, %rdi
callq 0x3380
movq (%r14), %rdi
movslq %eax, %rbp
leaq (%rdi,%rbp), %rax
incq %rax
cmpq 0x8(%r14), %rax
jae 0x8e01
movq 0x10(%r14), %rax
jmp 0x8e4e
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(%r14)
movq 0x10(%r14), %rdi
callq 0x37b0
testq %rax, %rax
je 0x8e6a
movq %rax, 0x10(%r14)
movq (%r14), %rdi
addq %rax, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x3850
addq (%r14), %rbp
movq %rbp, (%r14)
movq 0x10(%r14), %rax
movb $0x0, (%rax,%rbp)
movq (%r14), %rdx
leaq 0x1(%rdx), %rcx
cmpq 0x8(%r14), %rcx
jae 0x8e7d
movq 0x10(%r14), %rax
jmp 0x8ece
movq %rcx, %rax
shrq %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rsi
shrq $0x10, %rsi
orq %rcx, %rsi
incq %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
callq 0x37b0
testq %rax, %rax
je 0x8ee0
movq %rax, 0x10(%r14)
movq (%r14), %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, (%r14)
movb $0x9, (%rax,%rdx)
movq (%r14), %rax
movq 0x10(%r14), %rcx
movb $0x0, (%rcx,%rax)
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rbx
movq %rbx, %rdi
callq 0x3380
movq (%r14), %rdi
movslq %eax, %rbp
leaq (%rdi,%rbp), %rax
incq %rax
cmpq 0x8(%r14), %rax
jae 0x8f0a
movq 0x10(%r14), %rax
jmp 0x8f57
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(%r14)
movq 0x10(%r14), %rdi
callq 0x37b0
testq %rax, %rax
je 0x8f73
movq %rax, 0x10(%r14)
movq (%r14), %rdi
addq %rax, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x3850
addq (%r14), %rbp
movq %rbp, (%r14)
movq 0x10(%r14), %rax
movb $0x0, (%rax,%rbp)
movl %r12d, %r13d
cmpl $0x0, 0x8(%r15)
leaq 0x4498c(%rip), %r12     # 0x4d90e
je 0x8fbb
cmpl $0x3, %r13d
je 0x8fbb
cmpl $0x5, %r13d
movl $0x4, %ebx
cmovgel %r13d, %ebx
movl $0x3, %r15d
movq 0x20(%rsp), %rax
movq (%rax,%r15,8), %rdx
movq %r14, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0xb642
incq %r15
cmpq %r15, %rbx
jne 0x8f9d
movq 0x50(%rsp), %rdi
movl 0x84(%rsp), %eax
movq 0xc8(%rsp), %rcx
movl %eax, (%rdi,%rcx,4)
movl 0x2c(%rsp), %ebp
movl %ebp, %eax
cltd
movq 0x18(%rsp), %rcx
idivl 0x28(%rcx)
testl %edx, %edx
movq 0x110(%rsp), %rbx
jne 0x9015
movq %rbx, (%rsp)
movq 0xc0(%rsp), %rsi
movq 0x40(%rsp), %rdx
movl 0x14(%rsp), %ecx
movq 0x18(%rsp), %r8
movq 0x78(%rsp), %r9
callq 0x92ba
movl %ebp, 0x3c(%rsp)
incl %ebp
movl %ebp, 0x2c(%rsp)
movq 0x68(%rsp), %r14
movl 0x4c(%rsp), %r15d
movl %r13d, %r12d
movq 0x30(%rsp), %r13
incl %r15d
movq 0x70(%rsp), %rdi
callq 0xe2b2
movq %rax, %rdi
movq 0x88(%rsp), %rsi
leaq 0x90(%rsp), %rdx
movq %r14, %rcx
callq 0x1152f
testl %eax, %eax
leaq 0x46adb(%rip), %rbp     # 0x4fb39
jns 0x89da
jmp 0x907f
movq 0x68(%rsp), %r14
movq 0x110(%rsp), %rbx
movl 0x4c(%rsp), %r15d
jmp 0x902d
movq 0x30(%rsp), %r13
movq 0x88(%rsp), %rdi
callq 0x110fd
movq 0xb8(%rsp), %rcx
incq %rcx
cmpq 0x58(%rsp), %rcx
jne 0x895c
movq %r13, 0x30(%rsp)
movl 0x3c(%rsp), %eax
notl %eax
movl 0x2c(%rsp), %esi
addl %eax, %esi
movq %rbx, (%rsp)
movq 0x50(%rsp), %rdi
movq 0x40(%rsp), %rdx
movl 0x14(%rsp), %ecx
movq 0x18(%rsp), %r8
movq 0x78(%rsp), %r9
callq 0x92ba
testl %r12d, %r12d
jle 0x913f
movl %r12d, %ebx
xorl %r14d, %r14d
movq 0x60(%rsp), %r15
movl 0x48(%rsp), %r13d
movq 0x58(%rsp), %r12
movq 0x20(%rsp), %rax
movq (%rax,%r14,8), %rdi
callq 0x3370
incq %r14
cmpq %r14, %rbx
jne 0x90ee
movq 0x68(%rsp), %r14
jmp 0x914e
movq 0x110(%rsp), %rcx
movq %rcx, (%rsp)
xorl %ebx, %ebx
movq %rbp, %rdi
xorl %esi, %esi
movq 0x40(%rsp), %rdx
movl 0x14(%rsp), %ecx
movq 0x18(%rsp), %r8
movq %rax, %r9
callq 0x92ba
movq $0x0, 0x20(%rsp)
jmp 0x9158
movq 0x60(%rsp), %r15
movl 0x48(%rsp), %r13d
movq 0x58(%rsp), %r12
movq 0x50(%rsp), %rbp
movq 0x30(%rsp), %rbx
movq 0x20(%rsp), %rdi
callq 0x3370
movq %rbx, %rdi
callq 0x3370
movq %rbp, %rdi
callq 0x3370
movq 0xa0(%rsp), %rdi
callq 0x3370
movq %r14, %rdi
callq 0x13f5b
movq 0x70(%rsp), %rdi
callq 0xe119
testl %eax, %eax
jne 0x920a
testl %r13d, %r13d
jle 0x91ad
xorl %ebx, %ebx
movq (%r15,%rbx,8), %rdi
callq 0x3370
incq %rbx
cmpq %rbx, %r12
jne 0x919c
movq %r15, %rdi
callq 0x3370
xorl %eax, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x3d0d
leaq 0x446fc(%rip), %rdi     # 0x4d8d4
leaq 0x44841(%rip), %rsi     # 0x4da20
movl $0x13c, %edx            # imm = 0x13C
xorl %eax, %eax
callq 0x824b
movl %ebx, %edi
movl %r12d, %esi
callq 0x3ce0
leaq 0x4469f(%rip), %rdi     # 0x4d89b
jmp 0x9205
leaq 0x446a9(%rip), %rdi     # 0x4d8ae
movq %rbx, %rsi
jmp 0x9219
leaq 0x44701(%rip), %rdi     # 0x4d912
movq 0xb0(%rsp), %rsi
xorl %eax, %eax
callq 0x9220