// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xc0, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq %r9, -0x30(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x71f82(%rip), %rsi     # 0x79423
callq 0xeba0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x74c3
movq -0x8(%rbp), %rsi
leaq 0x6e4a2(%rip), %rdi     # 0x7595e
movb $0x0, %al
callq 0x78f0
movq -0x8(%rbp), %rdi
callq 0x19090
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x74e9
movq -0x8(%rbp), %rsi
leaq 0x6e48f(%rip), %rdi     # 0x75971
movb $0x0, %al
callq 0x78f0
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x3090
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movl $0xffffffff, -0x74(%rbp) # imm = 0xFFFFFFFF
movq $0x0, -0x80(%rbp)
movl $0x0, -0x8c(%rbp)
movl $0x0, -0x90(%rbp)
movl $0x1, -0x94(%rbp)
movq -0x30(%rbp), %rax
movslq 0x28(%rax), %rdi
movl $0x4, %esi
callq 0x36b0
movq %rax, -0xa0(%rbp)
movq -0x30(%rbp), %rax
movslq 0x28(%rax), %rdi
movl $0x18, %esi
callq 0x36b0
movq %rax, -0xa8(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x7815
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0x48(%rbp), %rcx
leaq 0x10bb6(%rip), %rdx     # 0x18150
leaq 0xbfff(%rip), %r8       # 0x135a0
leaq 0x110f8(%rip), %r9      # 0x186a0
callq 0x14420
movq %rax, -0xb0(%rbp)
cmpq $0x0, -0xb0(%rbp)
jne 0x75c3
jmp 0x7807
jmp 0x75c5
movq -0x40(%rbp), %rdi
callq 0xfd40
movq %rax, %rdi
movq -0xb0(%rbp), %rsi
movq -0x48(%rbp), %rcx
leaq -0x60(%rbp), %rdx
callq 0x14580
cmpl $0x0, %eax
jl 0x77fb
movq -0x50(%rbp), %rdi
leaq 0x728aa(%rip), %rsi     # 0x79ea3
leaq -0x70(%rbp), %rdx
leaq -0x74(%rbp), %rcx
leaq -0x80(%rbp), %r8
callq 0x79a0
cmpl $0x3, -0x74(%rbp)
jge 0x762a
leaq 0x6e380(%rip), %rdi     # 0x75997
leaq 0x6e572(%rip), %rsi     # 0x75b90
movl $0x13c, %edx            # imm = 0x13C
movb $0x0, %al
callq 0x6d60
cmpl $0x0, -0x8c(%rbp)
jne 0x7654
movq -0x30(%rbp), %rax
cmpl $0x0, 0x4(%rax)
je 0x7654
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %esi
movl -0x74(%rbp), %edx
movq 0x10(%rbp), %rcx
movq -0x30(%rbp), %r8
callq 0x7b50
movq -0x70(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x7cb0
movq -0x70(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x3550
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
jge 0x768b
movq -0x30(%rbp), %rax
cmpl $0x0, 0x20(%rax)
je 0x77e7
movq -0xa8(%rbp), %rax
movl -0x94(%rbp), %ecx
subl -0x90(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xb8(%rbp)
movq -0x70(%rbp), %rax
movq (%rax), %rdi
movq -0xb8(%rbp), %rsi
callq 0x6d20
movq -0xb8(%rbp), %rsi
movl $0x9, %edi
callq 0x6bd0
movq -0x70(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0xb8(%rbp), %rsi
callq 0x6d20
movq -0xb8(%rbp), %rsi
movl $0x9, %edi
callq 0x6bd0
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0xb8(%rbp), %rsi
callq 0x6d20
movq -0x30(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x7765
movl $0x3, -0x88(%rbp)
movl -0x88(%rbp), %eax
cmpl -0x74(%rbp), %eax
jge 0x7763
movq -0xb8(%rbp), %rdi
movq -0x70(%rbp), %rax
movslq -0x88(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
leaq 0x6e286(%rip), %rsi     # 0x759d1
movb $0x0, %al
callq 0xb660
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x7723
jmp 0x7765
movl -0x84(%rbp), %edx
movq -0xa0(%rbp), %rax
movl -0x94(%rbp), %ecx
subl -0x90(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl -0x94(%rbp), %eax
movq -0x30(%rbp), %rcx
cltd
idivl 0x28(%rcx)
cmpl $0x0, %edx
jne 0x77d8
movq -0xa0(%rbp), %rdi
movl -0x94(%rbp), %esi
subl -0x90(%rbp), %esi
movq -0x20(%rbp), %rdx
movl -0x24(%rbp), %ecx
movq -0x30(%rbp), %r8
movq -0xa8(%rbp), %r9
movq 0x10(%rbp), %rax
movq %rax, (%rsp)
callq 0x7d80
movl -0x94(%rbp), %eax
movl %eax, -0x90(%rbp)
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x75c5
movq -0xb0(%rbp), %rdi
callq 0x13f20
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x756f
movq -0xa0(%rbp), %rdi
movl -0x94(%rbp), %esi
subl -0x90(%rbp), %esi
subl $0x1, %esi
movq -0x20(%rbp), %rdx
movl -0x24(%rbp), %ecx
movq -0x30(%rbp), %r8
movq -0xa8(%rbp), %r9
movq 0x10(%rbp), %rax
movq %rax, (%rsp)
callq 0x7d80
movq -0x70(%rbp), %rdi
movl -0x74(%rbp), %esi
callq 0x8050
movq -0x80(%rbp), %rdi
callq 0x3370
movq -0xa0(%rbp), %rdi
callq 0x3370
movq -0x68(%rbp), %rdi
callq 0x3370
movq -0x50(%rbp), %rdi
callq 0x3370
movq -0x48(%rbp), %rdi
callq 0x18d60
movq -0x40(%rbp), %rdi
callq 0xf950
cmpl $0x0, %eax
je 0x78a6
movq -0x8(%rbp), %rsi
leaq 0x6e136(%rip), %rdi     # 0x759d5
movb $0x0, %al
callq 0x78f0
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x78d1
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x3370
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x78ad
movq -0x10(%rbp), %rdi
callq 0x3370
xorl %eax, %eax
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)