// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movq %rcx, 0x8(%rsp)
cmpq %rsi, 0x18(%rdi)
jle 0x86e2
movq %rdi, %r13
movslq %edx, %rbp
leaq (%rsi,%rbp), %rax
movq 0x18(%rdi), %rcx
movl %ecx, %edx
subl %esi, %edx
cmpq %rcx, %rax
cmovgel %edx, %ebp
testl %ebp, %ebp
jle 0x834a
movq 0x8(%rsp), %rcx
movl %ebp, 0x10(%rcx)
movq 0x2018(%r13), %rax
movq %rax, (%rcx)
movzbl %al, %ecx
cmpl $0x1d, %ecx
jg 0x82a4
addl $-0x3, %ecx
cmpl $0x4, %ecx
ja 0x86f6
leaq 0x457e5(%rip), %rdx     # 0x4d9f0
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movq 0x8(%r13), %rcx
movq 0x20(%r13), %rdx
movzbl %al, %edi
movl %edi, %r8d
shrq $0x8, %rax
xorl %r9d, %r9d
cmpq $0x7, %rdi
cmovnel %r9d, %eax
leaq 0x44dd8(%rip), %rdi     # 0x4d010
addl (%rdi,%r8,4), %eax
cltq
imulq %rsi, %rax
leaq (%rdx,%rax), %rsi
addq $0x2000, %rsi           # imm = 0x2000
cmpq 0x8(%rcx), %rsi
je 0x8269
movq %rsi, 0x8(%rcx)
movq (%rcx), %rdi
xorl %edx, %edx
callq 0x3120
testl %eax, %eax
jne 0x86f0
movq 0x8(%rsp), %rbx
movq 0x8(%rbx), %rdi
leal (,%rbp,4), %esi
callq 0x37b0
movq %rax, 0x8(%rbx)
movq 0x8(%r13), %r14
movl %ebp, %ebx
movq (%r14), %rcx
movl $0x4, %esi
movq %rax, %rdi
movq %rbx, %rdx
callq 0x35b0
shlq $0x2, %rbx
jmp 0x8346
cmpl $0x1e, %ecx
je 0x83ec
cmpl $0x1f, %ecx
je 0x82bb
cmpl $0x20, %ecx
jne 0x86f6
movq 0x8(%r13), %rcx
movq 0x20(%r13), %rdx
movzbl %al, %edi
movl %edi, %r8d
shrq $0x8, %rax
xorl %r9d, %r9d
cmpq $0x7, %rdi
cmovnel %r9d, %eax
leaq 0x44d31(%rip), %rdi     # 0x4d010
addl (%rdi,%r8,4), %eax
cltq
imulq %rsi, %rax
leaq (%rdx,%rax), %rsi
addq $0x2000, %rsi           # imm = 0x2000
cmpq 0x8(%rcx), %rsi
je 0x8310
movq %rsi, 0x8(%rcx)
movq (%rcx), %rdi
xorl %edx, %edx
callq 0x3120
testl %eax, %eax
jne 0x86f0
movq 0x8(%rsp), %rbx
movq 0x8(%rbx), %rdi
leal (,%rbp,8), %esi
callq 0x37b0
movq %rax, 0x8(%rbx)
movq 0x8(%r13), %r14
movl %ebp, %ebx
movq (%r14), %rcx
movl $0x8, %esi
movq %rax, %rdi
movq %rbx, %rdx
callq 0x35b0
shlq $0x3, %rbx
addq %rbx, 0x8(%r14)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
shrq $0x8, %rbx
movq 0x8(%r13), %rcx
movq 0x20(%r13), %rdx
movzbl %al, %eax
movl %eax, %edi
xorl %r8d, %r8d
cmpq $0x7, %rax
cmovel %ebx, %r8d
leaq 0x44c91(%rip), %rax     # 0x4d010
addl (%rax,%rdi,4), %r8d
movslq %r8d, %rax
imulq %rsi, %rax
leaq (%rdx,%rax), %rsi
addq $0x2000, %rsi           # imm = 0x2000
cmpq 0x8(%rcx), %rsi
je 0x83b1
movq %rsi, 0x8(%rcx)
movq (%rcx), %rdi
xorl %edx, %edx
callq 0x3120
testl %eax, %eax
jne 0x86f0
movq 0x8(%rsp), %r14
movq 0x8(%r14), %rdi
imull %ebx, %ebp
movslq %ebp, %r15
movq %r15, %rsi
callq 0x37b0
movq %rax, 0x8(%r14)
movq 0x8(%r13), %rbx
movq (%rbx), %rcx
movl $0x1, %esi
movq %rax, %rdi
movq %r15, %rdx
callq 0x35b0
addq %r15, 0x8(%rbx)
jmp 0x834a
movq 0x8(%r13), %rcx
movq 0x20(%r13), %rdx
movzbl %al, %edi
movl %edi, %r8d
shrq $0x8, %rax
xorl %r9d, %r9d
cmpq $0x7, %rdi
cmovnel %r9d, %eax
leaq 0x44c00(%rip), %rdi     # 0x4d010
addl (%rdi,%r8,4), %eax
cltq
imulq %rsi, %rax
leaq (%rdx,%rax), %rsi
addq $0x2000, %rsi           # imm = 0x2000
cmpq 0x8(%rcx), %rsi
je 0x8441
movq %rsi, 0x8(%rcx)
movq (%rcx), %rdi
xorl %edx, %edx
callq 0x3120
testl %eax, %eax
jne 0x86f0
movq 0x8(%rsp), %r14
movq 0x8(%r14), %rdi
leal (,%rbp,2), %esi
callq 0x37b0
movq %rax, 0x8(%r14)
movl %ebp, %r12d
movl $0x2, %esi
movq %r12, %rdi
callq 0x36a0
movq %rax, %r15
movq 0x8(%r13), %rbx
movq (%rbx), %rcx
movl $0x2, %esi
movq %rax, %rdi
movq %r12, %rdx
callq 0x35b0
addq %r12, %r12
addq %r12, 0x8(%rbx)
cmpl $0x2, %ebp
movl $0x1, %eax
cmovgel %ebp, %eax
movq 0x8(%r14), %rcx
xorl %edx, %edx
movss 0x44d9c(%rip), %xmm0    # 0x4d240
movss 0x44d98(%rip), %xmm1    # 0x4d244
movzwl (%r15,%rdx,2), %esi
xorps %xmm2, %xmm2
cvtsi2ss %esi, %xmm2
addss %xmm0, %xmm2
divss %xmm1, %xmm2
movss %xmm2, (%rcx,%rdx,4)
incq %rdx
cmpq %rdx, %rax
jne 0x84ac
movq %r15, %rdi
jmp 0x86be
movq 0x8(%r13), %rcx
movq %r13, %r12
movq 0x20(%r13), %rdx
movzbl %al, %edi
movl %edi, %r8d
shrq $0x8, %rax
xorl %r9d, %r9d
cmpq $0x7, %rdi
cmovnel %r9d, %eax
leaq 0x44b14(%rip), %rdi     # 0x4d010
addl (%rdi,%r8,4), %eax
cltq
imulq %rsi, %rax
leaq (%rdx,%rax), %rsi
addq $0x2000, %rsi           # imm = 0x2000
cmpq 0x8(%rcx), %rsi
je 0x852d
movq %rsi, 0x8(%rcx)
movq (%rcx), %rdi
xorl %edx, %edx
callq 0x3120
testl %eax, %eax
jne 0x8702
movq 0x8(%rsp), %rbx
movq 0x8(%rbx), %rdi
movl %ebp, %r14d
leaq (,%r14,8), %r13
movq %r13, %rsi
callq 0x37b0
movq %rax, 0x8(%rbx)
movq %r13, %rdi
callq 0x3240
movq 0x8(%r12), %rbx
movq (%rbx), %rcx
movl $0x8, %esi
movq %rax, 0x10(%rsp)
movq %rax, %rdi
movq %r14, %rdx
callq 0x35b0
addq %r13, 0x8(%rbx)
cmpl $0x2, %ebp
movl $0x1, %eax
cmovgel %ebp, %eax
movq %rax, 0x18(%rsp)
xorl %r14d, %r14d
movq %r12, %rbx
movq 0x8(%r12), %rax
movq 0x18(%r12), %rcx
shlq $0x3, %rcx
movq 0x10(%rsp), %rdx
addq (%rdx,%r14,8), %rcx
movq 0x20(%r12), %rdx
leaq (%rdx,%rcx), %rsi
addq $0x2000, %rsi           # imm = 0x2000
cmpq 0x8(%rax), %rsi
je 0x85cf
movq %rsi, 0x8(%rax)
movq (%rax), %rdi
xorl %edx, %edx
callq 0x3120
testl %eax, %eax
jne 0x86d1
movq %r14, 0x20(%rsp)
movq 0x8(%rbx), %rbx
movq (%rbx), %rcx
movl $0x1, %esi
movl $0x1, %edx
leaq 0x7(%rsp), %rdi
callq 0x35b0
incq 0x8(%rbx)
movb 0x7(%rsp), %r14b
xorl %r13d, %r13d
testb %r14b, %r14b
je 0x8699
xorl %ebp, %ebp
xorl %r15d, %r15d
leaq 0x1(%r15), %rbx
cmpq %rbp, %rbx
jae 0x8617
movq %r13, %rax
jmp 0x865b
movq %rbx, %rax
shrq %rax
orq %rbx, %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, %rbp
shrq $0x10, %rbp
orq %rcx, %rbp
incq %rbp
movq %r13, %rdi
movq %rbp, %rsi
callq 0x37b0
testq %rax, %rax
je 0x866b
movb %r14b, (%rax,%r15)
movb $0x0, 0x1(%rax,%r15)
movq %rax, %r13
movq %rbx, %r15
movq 0x8(%r12), %rbx
movq (%rbx), %rcx
movl $0x1, %esi
movl $0x1, %edx
leaq 0x7(%rsp), %rdi
callq 0x35b0
incq 0x8(%rbx)
movb 0x7(%rsp), %r14b
testb %r14b, %r14b
jne 0x8609
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rsp), %r14
movq %r13, (%rax,%r14,8)
incq %r14
cmpq 0x18(%rsp), %r14
jne 0x8589
movq 0x10(%rsp), %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3370
movq 0x8(%rbx), %rax
movq 0x10(%rax), %rsi
leaq 0x44cce(%rip), %rdi     # 0x4d3ae
jmp 0x86e9
leaq 0x44c17(%rip), %rdi     # 0x4d300
xorl %eax, %eax
callq 0x80fa
movq 0x8(%r13), %rax
jmp 0x86d5
movzbl %al, %esi
leaq 0x44c3c(%rip), %rdi     # 0x4d33c
jmp 0x86e9
movq 0x8(%r12), %rax
jmp 0x86d5