// File: /workspace/github_c_makefile_star2_O2/zhou-lab[P]tbmate/chunk.c
// Function: tbk_query_n
void tbk_query_n(tbk_t *tbk, int64_t chunk_beg, int n, tbk_data_t *data) {
  if (chunk_beg >= tbk->nmax) {wzfatal("Error: query %d out of range. Wrong idx file?", chunk_beg);}
  if (chunk_beg + n >= tbk->nmax) {
    n = tbk->nmax - chunk_beg;
  }
  if (n <= 0) return;
  
  data->n = n;
  data->dtype = tbk->dtype;

  switch(DATA_TYPE(tbk->dtype)) {
  /* case DT_INT1: { */
  /*   tbk->offset = offset/8;                           /\* always seek *\/ */
  /*   if(fseek(tbk->fh, offset/8+HDR_TOTALBYTES, SEEK_SET)) wzfatal("File %s cannot be seeked.\n", tbk->fname); */
    
  /*   int nb = (offset + n) / 8 - offset / 8 + 1; */
  /*   uint8_t *tmp = calloc(nb, 1); */
  /*   fread(tmp, 1, nb, tbk->fh); tbk->offset += n; */
  /*   data->data = realloc(data->data, n); */
  /*   int i; */
  /*   for (i=0; i<n; ++i) { */
  /*     ((uint8_t*)data->data)[i] = (tmp[(offset+i)/8 - offset/8] >> (offset+i)%8) & 0x1; */
  /*   } */
  /*   break; */
  /* } */
  /* case DT_INT2: { */
  /*   tbk->offset = offset/4;                           /\* always seek *\/ */
  /*   if(fseek(tbk->fh, offset/4+HDR_TOTALBYTES, SEEK_SET)) wzfatal("File %s cannot be seeked.\n", tbk->fname); */
    
  /*   int nb = (offset + n) / 4 - offset / 4; */
  /*   uint8_t *tmp = calloc(nb, 1); */
  /*   fread(tmp, 1, nb, tbk->fh); tbk->offset += n; */
  /*   data->data = realloc(data->data, n); */
  /*   int i; */
  /*   for (i=0; i<n; ++i) { */
  /*     ((uint8_t*)data->data)[i] = (tmp[(offset+i)/4 - offset/4] >> (((offset+i)%4)*2)) & 0x3; */
  /*   } */
  /*   break; */
  /* } */
  case DT_INT32: {
    tbk_seek_n(tbk, chunk_beg);
    data->data = realloc(data->data, 4*n);
    tbf_read(tbk->tbf, data->data, 4, n);
    break;
  }
  case DT_FLOAT: {
    tbk_seek_n(tbk, chunk_beg);
    data->data = realloc(data->data, 4*n);
    tbf_read(tbk->tbf, data->data, 4, n);
    break;
  }
  case DT_DOUBLE: {
    tbk_seek_n(tbk, chunk_beg);
    data->data = realloc(data->data, 8*n);
    tbf_read(tbk->tbf, data->data, 8, n);
    break;
  }
  case DT_STRINGF: {
    int smax = STRING_MAX(tbk->dtype);
    tbk_seek_n(tbk, chunk_beg);
    data->data = realloc(data->data, n*smax);
    tbf_read(tbk->tbf, data->data, 1, n*smax);
    break;
  }
  case DT_STRINGD: {
    tbk_seek_n(tbk, chunk_beg);

    data->data = realloc(data->data, sizeof(char*)*n);
    uint64_t *string_offsets = malloc(sizeof(uint64_t)*n);
    tbf_read(tbk->tbf, string_offsets, 8, n);

    int i;
    for (i=0; i<n; ++i) {
      tbk_seek_offset(tbk, string_offsets[i]+tbk->nmax*8);
      kstring_t ks = {0}; char c;
      while (1) {
        tbf_read(tbk->tbf, &c, 1, 1);
        if (c) kputc(c, &ks);
        else break;
      }
      ((char**) data->data)[i] = ks.s;
    }
    free(string_offsets);
    break;
  }
  case DT_ONES: {
    tbk_seek_n(tbk, chunk_beg);
    data->data = realloc(data->data, 2*n);
    uint16_t *tmp = calloc(n, 2);
    tbf_read(tbk->tbf, tmp, 2, n);
    int ii;
    for (ii=0; ii<n; ++ii) ((float*)data->data)[ii] = uint16_to_float(tmp[ii]);
    free(tmp);
    break;
  }
  case DT_FLOAT_INT: {
    tbk_seek_n(tbk, chunk_beg);
    data->data = realloc(data->data, 8*n);
    tbf_read(tbk->tbf, data->data, 8, n);
    break;
  }
  case DT_FLOAT_FLOAT: {
    tbk_seek_n(tbk, chunk_beg);
    data->data = realloc(data->data, 8*n);
    tbf_read(tbk->tbf, data->data, 8, n);
    break;
  }
  default: wzfatal("Unrecognized data type: %d.\n", DATA_TYPE(tbk->dtype));
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rcx, 0x10(%rsp)
movq 0x18(%rdi), %rax
cmpq %rsi, %rax
jle 0x62bf
movslq %edx, %rbx
leaq (%rbx,%rsi), %rcx
movl %eax, %edx
subl %esi, %edx
cmpq %rax, %rcx
cmovgel %edx, %ebx
testl %ebx, %ebx
jle 0x608a
movq %rdi, %r14
movq 0x10(%rsp), %rax
movl %ebx, 0x10(%rax)
movq 0x2018(%rdi), %r15
movq %r15, (%rax)
movzbl %r15b, %eax
leal -0x3(%rax), %ecx
cmpl $0x4, %ecx
ja 0x6099
leaq 0x3999a(%rip), %rax     # 0x3f9f0
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movq %r14, %rdi
callq 0x62da
movq 0x10(%rsp), %r15
movq 0x8(%r15), %rdi
leal (,%rbx,4), %esi
callq 0x37c0
movq %rax, 0x8(%r15)
movq 0x8(%r14), %rdi
movl %ebx, %ecx
pushq $0x4
jmp 0x60d9
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x1e, %eax
je 0x6129
cmpl $0x1f, %eax
je 0x60b0
cmpl $0x20, %eax
jne 0x62cd
movq %r14, %rdi
callq 0x62da
movq 0x10(%rsp), %r15
movq 0x8(%r15), %rdi
leal (,%rbx,8), %esi
callq 0x37c0
movq %rax, 0x8(%r15)
movq 0x8(%r14), %rdi
movl %ebx, %ecx
pushq $0x8
popq %rdx
movq %rax, %rsi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x634f
shrq $0x8, %r15
movq %r14, %rdi
callq 0x62da
movq 0x10(%rsp), %r12
movq 0x8(%r12), %rdi
imull %r15d, %ebx
movslq %ebx, %rbx
movq %rbx, %rsi
callq 0x37c0
movq %rax, 0x8(%r12)
movq 0x8(%r14), %rdi
pushq $0x1
popq %rdx
movq %rax, %rsi
movq %rbx, %rcx
jmp 0x60dd
movq %r14, %rdi
callq 0x62da
movq 0x10(%rsp), %r13
movq 0x8(%r13), %rdi
leal (%rbx,%rbx), %esi
callq 0x37c0
movq %rax, 0x8(%r13)
movl %ebx, %ebx
pushq $0x2
popq %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x36b0
movq %rax, %r15
movq 0x8(%r14), %rdi
movq %rax, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x634f
movq 0x8(%r13), %rax
xorl %ecx, %ecx
movss 0x390c6(%rip), %xmm0    # 0x3f240
movss 0x390c2(%rip), %xmm1    # 0x3f244
cmpq %rcx, %rbx
je 0x61a2
movzwl (%r15,%rcx,2), %edx
cvtsi2ss %edx, %xmm2
addss %xmm0, %xmm2
divss %xmm1, %xmm2
movss %xmm2, (%rax,%rcx,4)
incq %rcx
jmp 0x6182
movq %r15, %rdi
jmp 0x629b
movq %r14, %rdi
callq 0x62da
movq 0x10(%rsp), %r15
movq 0x8(%r15), %rdi
movl %ebx, %r13d
leaq (,%r13,8), %rbx
movq %rbx, %rsi
callq 0x37c0
movq %rax, 0x8(%r15)
movq %rbx, %rdi
callq 0x3240
movq 0x8(%r14), %rdi
pushq $0x8
popq %rdx
movq %rax, 0x18(%rsp)
movq %rax, %rsi
movq %r13, %r12
movq %r13, %rcx
callq 0x634f
xorl %r15d, %r15d
leaq 0xf(%rsp), %r13
pushq $0x1
popq %rbp
leaq 0x20(%rsp), %rbx
cmpq %r12, %r15
je 0x6296
movq 0x8(%r14), %rax
movq 0x18(%r14), %rcx
shlq $0x3, %rcx
movq 0x18(%rsp), %rdx
addq (%rdx,%r15,8), %rcx
movq 0x20(%r14), %rdx
leaq (%rdx,%rcx), %rsi
addq $0x2000, %rsi           # imm = 0x2000
cmpq 0x8(%rax), %rsi
je 0x6249
movq %rsi, 0x8(%rax)
movq (%rax), %rdi
xorl %edx, %edx
callq 0x3120
testl %eax, %eax
jne 0x62ae
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
movq 0x8(%r14), %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %rbp, %rcx
callq 0x634f
movsbl 0xf(%rsp), %edi
testl %edi, %edi
je 0x627c
movq %rbx, %rsi
callq 0x5e3a
jmp 0x6257
movq 0x30(%rsp), %rax
movq 0x10(%rsp), %rcx
movq 0x8(%rcx), %rcx
movq %rax, (%rcx,%r15,8)
incq %r15
jmp 0x6204
movq 0x18(%rsp), %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3370
movq 0x8(%r14), %rax
movq 0x10(%rax), %rsi
leaq 0x390f5(%rip), %rdi     # 0x3f3b2
jmp 0x62c6
leaq 0x3903e(%rip), %rdi     # 0x3f304
xorl %eax, %eax
callq 0x5f58
movzbl %r15b, %esi
leaq 0x39068(%rip), %rdi     # 0x3f340
jmp 0x62c6