// File: /workspace/github_c_makefile_star2_O3/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)
movq 0x18(%rdi), %rax
cmpq %rsi, %rax
jle 0x8828
movslq %edx, %r14
leaq (%r14,%rsi), %rcx
movl %eax, %edx
subl %esi, %edx
cmpq %rax, %rcx
cmovgel %edx, %r14d
testl %r14d, %r14d
jle 0x84a7
movq %rdi, %r12
movq 0x8(%rsp), %rcx
movl %r14d, 0x10(%rcx)
movq 0x2018(%rdi), %rax
movq %rax, (%rcx)
movzbl %al, %ecx
cmpl $0x1d, %ecx
jg 0x83fc
addl $-0x3, %ecx
cmpl $0x4, %ecx
ja 0x8841
leaq 0x45692(%rip), %rdx     # 0x4d9f0
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movq 0x8(%r12), %rcx
movq 0x20(%r12), %rdx
movzbl %al, %edi
movl %edi, %r8d
shrq $0x8, %rax
xorl %r9d, %r9d
cmpq $0x7, %rdi
cmovnel %r9d, %eax
leaq 0x44c83(%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 0x83be
movq %rsi, 0x8(%rcx)
movq (%rcx), %rdi
xorl %edx, %edx
callq 0x3120
testl %eax, %eax
jne 0x8836
movq 0x8(%rsp), %rbx
movq 0x8(%rbx), %rdi
leal (,%r14,4), %esi
callq 0x37b0
movq %rax, 0x8(%rbx)
movq 0x8(%r12), %r15
movl %r14d, %ebx
movq (%r15), %rcx
movl $0x4, %esi
movq %rax, %rdi
movq %rbx, %rdx
callq 0x35b0
shlq $0x2, %rbx
jmp 0x84a3
cmpl $0x1e, %ecx
je 0x854d
cmpl $0x1f, %ecx
je 0x8413
cmpl $0x20, %ecx
jne 0x8841
movq 0x8(%r12), %rcx
movq 0x20(%r12), %rdx
movzbl %al, %edi
movl %edi, %r8d
shrq $0x8, %rax
xorl %r9d, %r9d
cmpq $0x7, %rdi
cmovnel %r9d, %eax
leaq 0x44bd7(%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 0x846a
movq %rsi, 0x8(%rcx)
movq (%rcx), %rdi
xorl %edx, %edx
callq 0x3120
testl %eax, %eax
jne 0x8836
movq 0x8(%rsp), %rbx
movq 0x8(%rbx), %rdi
leal (,%r14,8), %esi
callq 0x37b0
movq %rax, 0x8(%rbx)
movq 0x8(%r12), %r15
movl %r14d, %ebx
movq (%r15), %rcx
movl $0x8, %esi
movq %rax, %rdi
movq %rbx, %rdx
callq 0x35b0
shlq $0x3, %rbx
addq %rbx, 0x8(%r15)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
shrq $0x8, %rbx
movq 0x8(%r12), %rcx
movq 0x20(%r12), %rdx
movzbl %al, %eax
movl %eax, %edi
xorl %r8d, %r8d
cmpq $0x7, %rax
cmovel %ebx, %r8d
leaq 0x44b32(%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 0x8510
movq %rsi, 0x8(%rcx)
movq (%rcx), %rdi
xorl %edx, %edx
callq 0x3120
testl %eax, %eax
jne 0x8836
movq 0x8(%rsp), %r15
movq 0x8(%r15), %rdi
imull %ebx, %r14d
movslq %r14d, %r14
movq %r14, %rsi
callq 0x37b0
movq %rax, 0x8(%r15)
movq 0x8(%r12), %rbx
movq (%rbx), %rcx
movl $0x1, %esi
movq %rax, %rdi
movq %r14, %rdx
callq 0x35b0
addq %r14, 0x8(%rbx)
jmp 0x84a7
movq 0x8(%r12), %rcx
movq 0x20(%r12), %rdx
movzbl %al, %edi
movl %edi, %r8d
shrq $0x8, %rax
xorl %r9d, %r9d
cmpq $0x7, %rdi
cmovnel %r9d, %eax
leaq 0x44a9d(%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 0x85a4
movq %rsi, 0x8(%rcx)
movq (%rcx), %rdi
xorl %edx, %edx
callq 0x3120
testl %eax, %eax
jne 0x8836
movq 0x8(%rsp), %r13
movq 0x8(%r13), %rdi
leal (%r14,%r14), %esi
callq 0x37b0
movq %rax, 0x8(%r13)
movl %r14d, %r14d
movl $0x2, %esi
movq %r14, %rdi
callq 0x36a0
movq %rax, %r15
movq 0x8(%r12), %rbx
movq (%rbx), %rcx
movl $0x2, %esi
movq %rax, %rdi
movq %r14, %rdx
callq 0x35b0
leaq (%r14,%r14), %rax
addq %rax, 0x8(%rbx)
movq 0x8(%r13), %rax
xorl %ecx, %ecx
movss 0x44c45(%rip), %xmm0    # 0x4d240
movss 0x44c41(%rip), %xmm1    # 0x4d244
movzwl (%r15,%rcx,2), %edx
xorps %xmm2, %xmm2
cvtsi2ss %edx, %xmm2
addss %xmm0, %xmm2
divss %xmm1, %xmm2
movss %xmm2, (%rax,%rcx,4)
incq %rcx
cmpq %rcx, %r14
jne 0x8603
movq %r15, %rdi
jmp 0x8808
movq 0x8(%r12), %rcx
movq 0x20(%r12), %rdx
movzbl %al, %edi
movl %edi, %r8d
shrq $0x8, %rax
xorl %r9d, %r9d
cmpq $0x7, %rdi
cmovnel %r9d, %eax
leaq 0x449be(%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 0x8683
movq %rsi, 0x8(%rcx)
movq (%rcx), %rdi
xorl %edx, %edx
callq 0x3120
testl %eax, %eax
jne 0x8836
movq 0x8(%rsp), %rbx
movq 0x8(%rbx), %rdi
movl %r14d, %r15d
leaq (,%r15,8), %r14
movq %r14, %rsi
callq 0x37b0
movq %rax, 0x8(%rbx)
movq %r14, %rdi
callq 0x3240
movq 0x8(%r12), %rbx
movq (%rbx), %rcx
movl $0x8, %esi
movq %rax, 0x10(%rsp)
movq %rax, %rdi
movq %r15, 0x18(%rsp)
movq %r15, %rdx
callq 0x35b0
addq %r14, 0x8(%rbx)
xorl %r14d, %r14d
movq 0x8(%r12), %rbx
movq 0x18(%r12), %rax
shlq $0x3, %rax
movq 0x10(%rsp), %rcx
addq (%rcx,%r14,8), %rax
movq 0x20(%r12), %rcx
leaq (%rcx,%rax), %rsi
addq $0x2000, %rsi           # imm = 0x2000
cmpq 0x8(%rbx), %rsi
je 0x871c
movq %rsi, 0x8(%rbx)
movq (%rbx), %rdi
xorl %edx, %edx
callq 0x3120
movq 0x8(%r12), %rbx
testl %eax, %eax
jne 0x881b
movq %r14, 0x20(%rsp)
movq (%rbx), %rcx
movl $0x1, %esi
movl $0x1, %edx
leaq 0x7(%rsp), %rdi
callq 0x35b0
incq 0x8(%rbx)
movb 0x7(%rsp), %bl
xorl %ebp, %ebp
testb %bl, %bl
je 0x87e3
xorl %r14d, %r14d
xorl %r15d, %r15d
movq %r12, %r13
leaq 0x1(%r15), %r12
cmpq %r14, %r12
jae 0x8761
movq %rbp, %rax
jmp 0x87a5
movq %r12, %rax
shrq %rax
orq %r12, %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, %r14
shrq $0x10, %r14
orq %rcx, %r14
incq %r14
movq %rbp, %rdi
movq %r14, %rsi
callq 0x37b0
testq %rax, %rax
je 0x87b5
movb %bl, (%rax,%r15)
movb $0x0, 0x1(%rax,%r15)
movq %rax, %rbp
movq %r12, %r15
movq %r13, %r12
movq 0x8(%r13), %rbx
movq (%rbx), %rcx
movl $0x1, %esi
movl $0x1, %edx
leaq 0x7(%rsp), %rdi
callq 0x35b0
incq 0x8(%rbx)
movb 0x7(%rsp), %bl
testb %bl, %bl
jne 0x8750
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rsp), %r14
movq %rbp, (%rax,%r14,8)
incq %r14
cmpq 0x18(%rsp), %r14
jne 0x86d4
movq 0x10(%rsp), %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3370
movq 0x10(%rbx), %rsi
leaq 0x44b8c(%rip), %rdi     # 0x4d3b2
jmp 0x882f
leaq 0x44ad5(%rip), %rdi     # 0x4d304
xorl %eax, %eax
callq 0x824b
movq 0x8(%r12), %rax
movq 0x10(%rax), %rsi
jmp 0x881f
movzbl %al, %esi
leaq 0x44af5(%rip), %rdi     # 0x4d340
jmp 0x882f