// File: /workspace/github_c_makefile_star2_O1/zhou-lab[P]tbmate/chunk.c
// Function: query_one_chunk
static void query_one_chunk(int *offsets, int n_offsets, tbk_t *tbks, int n_tbks, view_conf_t *conf, kstring_t *ks_out, FILE*out_fh) {

  if (n_offsets == 0) return;
  
  int k;
  tbk_data_t data = {0};
  for(k=0; k<n_tbks; ++k) {     /* iterate through samples */
    int chunk_beg = 0;
    for (chunk_beg = 0; chunk_beg < tbks[k].nmax; chunk_beg += conf->n_chunk_data) { /* iterate data chunk */
      int chunk_end = (chunk_beg + conf->n_chunk_data > tbks[k].nmax) ? tbks[k].nmax : (chunk_beg + conf->n_chunk_data);
      tbk_query_n(&tbks[k], chunk_beg, conf->n_chunk_data, &data);
      if (chunk_end - chunk_beg != data.n) {
        wzfatal("Unequal number of records read %d, expecting %d\n", data.n, chunk_end-chunk_beg);
      }

      /* save to output */
      int i;
      for(i=0; i<n_offsets; ++i) {
        if (offsets[i] >= chunk_beg && offsets[i] < chunk_end) {
          tbk_print1(&data, offsets[i]-chunk_beg, conf, &ks_out[i]);
        }
      }
    }
  }

  /* output and reset */
  int i;
  for (i=0; i < n_offsets; ++i) {
    if (ks_out[i].s) {
      if (offsets[i] >= 0) {
        fputs(ks_out[i].s, out_fh); fputc('\n', out_fh);
      } else if (conf->show_unaddressed) {
        fputs(ks_out[i].s, out_fh);
        for (k=0; k<n_tbks; ++k) fputs("\t-1", out_fh);
        fputc('\n', out_fh);
      }
      free(ks_out[i].s); memset(&ks_out[i], 0 , sizeof(kstring_t));
    }
  }
  memset(offsets, 0, sizeof(int) * conf->n_chunk_index);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, 0x18(%rsp)
movq %rdx, 0x28(%rsp)
testl %esi, %esi
je 0x92cb
movl %esi, %r14d
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
movl %esi, %r13d
movl %ecx, 0x4(%rsp)
testl %ecx, %ecx
movq %r8, 0x10(%rsp)
jle 0x91fa
movl 0x4(%rsp), %eax
movq %rax, 0x30(%rsp)
xorl %eax, %eax
movl %r14d, 0x24(%rsp)
movq %rax, 0x38(%rsp)
imulq $0x2028, %rax, %r12     # imm = 0x2028
movq 0x28(%rsp), %rax
movq 0x18(%rax,%r12), %rax
testq %rax, %rax
jle 0x91e7
addq 0x28(%rsp), %r12
xorl %r15d, %r15d
movq %r12, 0x8(%rsp)
movl 0x2c(%r8), %edx
leal (%rdx,%r15), %ecx
movslq %ecx, %rbp
cmpq %rbp, %rax
cmovll %eax, %ebp
movq %r12, %rdi
movq %r15, %rsi
leaq 0x40(%rsp), %rcx
callq 0x819d
movl %ebp, %edx
subl %r15d, %edx
movl 0x50(%rsp), %esi
cmpl %esi, %edx
jne 0x92da
testl %r14d, %r14d
jle 0x91c0
movq 0x18(%rsp), %r12
xorl %r14d, %r14d
movl (%rbx,%r14,4), %eax
movl %eax, %esi
subl %r15d, %esi
jl 0x91b4
cmpl %ebp, %eax
jge 0x91b4
leaq 0x40(%rsp), %rdi
movq 0x10(%rsp), %rdx
movq %r12, %rcx
callq 0x7968
incq %r14
addq $0x18, %r12
cmpq %r14, %r13
jne 0x9193
movq 0x10(%rsp), %r8
movslq 0x2c(%r8), %rax
movslq %r15d, %r15
addq %rax, %r15
movq 0x8(%rsp), %r12
movq 0x18(%r12), %rax
cmpq %r15, %rax
movl 0x24(%rsp), %r14d
jg 0x9154
movq 0x38(%rsp), %rax
incq %rax
cmpq 0x30(%rsp), %rax
jne 0x9128
testl %r14d, %r14d
jle 0x92b4
movq 0x90(%rsp), %r12
xorl %ebp, %ebp
leaq (,%rbp,2), %rcx
addq %rbp, %rcx
movq 0x18(%rsp), %rax
movq 0x10(%rax,%rcx,8), %rdi
testq %rdi, %rdi
je 0x926d
cmpl $0x0, (%rbx,%rbp,4)
js 0x9277
movq %rcx, 0x8(%rsp)
movq %r12, %rsi
callq 0x3520
movl $0xa, %edi
movq %r12, %rsi
callq 0x3350
movq 0x8(%rsp), %rcx
movq 0x18(%rsp), %rax
leaq (%rax,%rcx,8), %r14
movq 0x10(%r14), %rdi
callq 0x3370
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
movq $0x0, 0x10(%r14)
incq %rbp
cmpq %r13, %rbp
jne 0x920d
jmp 0x92b4
movq 0x10(%rsp), %rax
cmpl $0x0, 0x20(%rax)
je 0x924c
movq %rcx, 0x8(%rsp)
movq %r12, %rsi
callq 0x3520
cmpl $0x0, 0x4(%rsp)
leaq 0x44061(%rip), %r15     # 0x4d2fc
jle 0x923a
movl 0x4(%rsp), %r14d
movq %r15, %rdi
movq %r12, %rsi
callq 0x3520
decl %r14d
jne 0x92a2
jmp 0x923a
movq 0x10(%rsp), %rax
movslq 0x28(%rax), %rdx
shlq $0x2, %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x3090
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x44752(%rip), %rdi     # 0x4da33
xorl %eax, %eax
callq 0x80fa