// File: /workspace/github_c_makefile_star2_O2/zhou-lab[P]tbmate/view.c
// Function: tbk_query
void tbk_query(tbk_t *tbk, int64_t offset, view_conf_t *conf, FILE *out_fh, char **aux) {

  /* when the offset is unfound */
  if (offset < 0) { fputs("\t-1", out_fh); return; }
  if (offset >= tbk->nmax) {wzfatal("Error: query %d out of range. Wrong idx file?", offset);}

  switch(DATA_TYPE(tbk->dtype)) {
  /* case DT_INT1: { */
  /*   if(tbk->offset == 0 || offset/8 != tbk->offset-1) { /\* need to read? *\/ */
  /*     if(offset/8 != tbk->offset) {                     /\* need to seek? *\/ */
  /*       tbk->offset = offset/8; */
  /*       if(fseek(tbk->fh, offset/8+HDR_TOTALBYTES, SEEK_SET)) */
  /*         wzfatal("File %s cannot be seeked.\n", tbk->fname); */
  /*     } */
  /*     fread(&(tbk->data), 1, 1, tbk->fh); tbk->offset++; */
  /*   } */
  /*   fprintf(out_fh, "\t%d", ((tbk->data)>>(offset%8)) & 0x1); */
  /*   break; */
  /* } */
  /* case DT_INT2: { */
  /*   if(tbk->offset == 0 || offset/4 != tbk->offset-1) { /\* need to read? *\/ */
  /*     if(offset/4 != tbk->offset) {                       /\* need to seek? *\/ */
  /*       tbk->offset = offset/4; */
  /*       if(fseek(tbk->fh, offset/4+HDR_TOTALBYTES, SEEK_SET)) */
  /*         wzfatal("File %s cannot be seeked.\n", tbk->fname); */
  /*     } */
  /*     fread(&(tbk->data), 1, 1, tbk->fh); tbk->offset++; */
  /*   } */
  /*   fprintf(out_fh, "\t%d", ((tbk->data)>>((offset%4)*2)) & 0x3); */
  /*   break; */
  /* } */
  case DT_INT32: {
    tbk_seek_n(tbk, offset);
    int data;
    tbf_read(tbk->tbf, &data, 4, 1);
    if (conf->na_for_negative && data < 0) {
      fputc('\t', out_fh); fputs(conf->na_token, out_fh);
    } else fprintf(out_fh, "\t%d", data);
    break;
  }
  case DT_FLOAT: {
    tbk_seek_n(tbk, offset);    
    float data;
    tbf_read(tbk->tbf, &data, 4, 1);
    if (conf->na_for_negative && data < 0) {
      fputc('\t', out_fh); fputs(conf->na_token, out_fh);
    } else fprintf(out_fh, "\t%f", data);
    break;
  }
  case DT_DOUBLE: {
    tbk_seek_n(tbk, offset);
    double data;
    tbf_read(tbk->tbf, &data, 8, 1);
    if (conf->na_for_negative && data < 0) {
      fputc('\t', out_fh); fputs(conf->na_token, out_fh);
    } else fprintf(out_fh, "\t%f", data);
    break;
  }
  case DT_STRINGF: {
    tbk_seek_n(tbk, offset);
    
    uint64_t n = STRING_MAX(tbk->dtype);
    if (!*aux) *aux = malloc(n+1);
    tbf_read(tbk->tbf, *aux, 1, n);
    if ((*aux)[n-1] != '\0') (*aux)[n] = '\0';
    fputc('\t', out_fh);
    fputs(*aux, out_fh);
    break;
  }
  case DT_STRINGD: {
    tbk_seek_n(tbk, offset);

    int64_t string_offset;
    tbf_read(tbk->tbf, &string_offset, 8, 1);
    tbk_seek_offset(tbk, string_offset + 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;
    }
    if (ks.s) { fputc('\t', out_fh); fputs(ks.s, out_fh); }
    free(ks.s);
    break;
  }
  case DT_ONES: {
    tbk_seek_n(tbk, offset);
    
    uint16_t data;
    tbf_read(tbk->tbf, &data, 2, 1);
    float dataf = uint16_to_float(data);
    if (conf->na_for_negative && dataf < 0) {
      fputc('\t', out_fh); fputs(conf->na_token, out_fh);
    } else fprintf(out_fh, "\t%.*f", conf->precision, dataf);
    /* fprintf(out_fh, "\t%d", data); */
    break;
  }
  case DT_FLOAT_INT: {
    tbk_seek_n(tbk, offset);
    
    float data; int data2;
    tbf_read(tbk->tbf, &data, 4, 1);
    tbf_read(tbk->tbf, &data2, 4, 1);

    if (conf->na_for_negative && data < 0) {
      fputc('\t', out_fh); fputs(conf->na_token, out_fh);
    } else {
      if (conf->min_coverage >= 0 && data2 < conf->min_coverage) {
        fputc('\t', out_fh); fputs(conf->na_token, out_fh);
      } else {
        fprintf(out_fh, "\t%f", data);
      }
    }
    if (conf->print_all_units) fprintf(out_fh, "\t%d", data2);
    break;
  }
  case DT_FLOAT_FLOAT: {
    tbk_seek_n(tbk, offset);
    
    float data,  data2;
    tbf_read(tbk->tbf, &data, 4, 1);
    tbf_read(tbk->tbf, &data2, 4, 1);
    
    if (conf->na_for_negative && data < 0) {
      fputc('\t', out_fh); fputs(conf->na_token, out_fh);
    } else {
      if (conf->max_pval >= 0 && data2 > conf->max_pval) {
        fputc('\t', out_fh); fputs(conf->na_token, out_fh);
      } else {
        fprintf(out_fh, "\t%f", data);
      }
    }
    if (conf->print_all_units) fprintf(out_fh, "\t%f", data2);
    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, %rbx
testq %rsi, %rsi
js 0x3c63
movq %rdi, %r15
cmpq %rsi, 0x18(%rdi)
jle 0x40ab
movq %rdx, %r14
movq 0x2018(%r15), %rax
movzbl %al, %ecx
leal -0x3(%rcx), %edx
cmpl $0x4, %edx
ja 0x3c80
leaq 0x3b631(%rip), %rax     # 0x3f24c
movslq (%rax,%rdx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movq %r15, %rdi
callq 0x417c
movq 0x8(%r15), %rdi
leaq 0x8(%rsp), %r15
pushq $0x4
popq %rdx
pushq $0x1
popq %rcx
movq %r15, %rsi
callq 0x41f1
cmpl $0x0, 0x10(%r14)
setne %al
movl (%r15), %edx
testl %edx, %edx
sets %cl
andb %al, %cl
cmpb $0x1, %cl
je 0x3fea
jmp 0x4056
leaq 0x3b696(%rip), %rdi     # 0x3f300
movq %rbx, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3530
cmpl $0x1e, %ecx
je 0x3e4d
cmpl $0x1f, %ecx
je 0x3dcc
cmpl $0x20, %ecx
jne 0x40b9
movq %r15, %rdi
callq 0x417c
movq 0x8(%r15), %rdi
leaq 0x8(%rsp), %r12
pushq $0x4
popq %r13
pushq $0x1
popq %rbp
movq %r12, %rsi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x41f1
movq 0x8(%r15), %rdi
leaq 0x4(%rsp), %rsi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x41f1
cmpl $0x0, 0x10(%r14)
movss (%r12), %xmm0
je 0x3cea
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
ja 0x3d0b
movss 0x30(%r14), %xmm1
xorps %xmm2, %xmm2
ucomiss %xmm2, %xmm1
jb 0x4069
movss 0x4(%rsp), %xmm2
ucomiss %xmm1, %xmm2
jbe 0x4069
pushq $0x9
popq %rdi
movq %rbx, %rsi
callq 0x3350
movq 0x18(%r14), %rdi
movq %rbx, %rsi
callq 0x3530
jmp 0x407e
movq %r8, %r12
movq %r15, %rdi
callq 0x417c
movq 0x2018(%r15), %r14
shrq $0x8, %r14
movq (%r12), %rsi
testq %rsi, %rsi
jne 0x3d56
leaq 0x1(%r14), %rdi
callq 0x3240
movq %rax, %rsi
movq %rax, (%r12)
movq 0x8(%r15), %rdi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x41f1
movq (%r12), %rax
cmpb $0x0, -0x1(%rax,%r14)
je 0x3d76
movb $0x0, (%rax,%r14)
pushq $0x9
popq %rdi
movq %rbx, %rsi
callq 0x3350
movq (%r12), %rdi
jmp 0x3c6a
movq %r15, %rdi
callq 0x417c
movq 0x8(%r15), %rdi
leaq 0x8(%rsp), %r15
pushq $0x8
popq %rdx
pushq $0x1
popq %rcx
movq %r15, %rsi
callq 0x41f1
cmpl $0x0, 0x10(%r14)
movsd (%r15), %xmm0
je 0x408b
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
ja 0x3fea
jmp 0x408b
movq %r15, %rdi
callq 0x417c
movq 0x8(%r15), %rdi
leaq 0x8(%rsp), %r12
pushq $0x4
popq %r13
pushq $0x1
popq %rbp
movq %r12, %rsi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x41f1
movq 0x8(%r15), %rdi
leaq 0x4(%rsp), %rsi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x41f1
cmpl $0x0, 0x10(%r14)
movss (%r12), %xmm0
je 0x3e1b
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
ja 0x3e31
movl 0x34(%r14), %eax
testl %eax, %eax
js 0x4036
cmpl %eax, 0x4(%rsp)
jge 0x4036
pushq $0x9
popq %rdi
movq %rbx, %rsi
callq 0x3350
movq 0x18(%r14), %rdi
movq %rbx, %rsi
callq 0x3530
jmp 0x404b
movq %r15, %rdi
callq 0x417c
movq 0x8(%r15), %rdi
leaq 0x8(%rsp), %r15
pushq $0x2
popq %rdx
pushq $0x1
popq %rcx
movq %r15, %rsi
callq 0x41f1
movzwl (%r15), %eax
cvtsi2ss %eax, %xmm0
addss 0x3b3c4(%rip), %xmm0    # 0x3f240
divss 0x3b3c0(%rip), %xmm0    # 0x3f244
cmpl $0x0, 0x10(%r14)
je 0x3e97
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
ja 0x3fea
movl (%r14), %edx
cvtss2sd %xmm0, %xmm0
leaq 0x3b495(%rip), %rsi     # 0x3f33a
movq %rbx, %rdi
movb $0x1, %al
callq 0x37f0
jmp 0x409c
movq %rbx, 0x18(%rsp)
movq %r15, %rdi
callq 0x417c
movq 0x8(%r15), %rdi
leaq 0x8(%rsp), %r14
pushq $0x8
popq %rdx
pushq $0x1
popq %rcx
movq %r14, %rsi
callq 0x41f1
movq 0x8(%r15), %rax
movq 0x18(%r15), %rcx
shlq $0x3, %rcx
addq (%r14), %rcx
movq %r15, 0x10(%rsp)
movq 0x20(%r15), %rdx
leaq (%rdx,%rcx), %rsi
addq $0x2000, %rsi           # imm = 0x2000
cmpq 0x8(%rax), %rsi
je 0x3f17
movq %rsi, 0x8(%rax)
movq (%rax), %rdi
xorl %edx, %edx
callq 0x3120
testl %eax, %eax
jne 0x40c5
xorl %ebx, %ebx
pushq $0x1
popq %r13
xorl %ebp, %ebp
xorl %r14d, %r14d
movq %rbx, 0x20(%rsp)
incq %rbx
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, %r12
shrq $0x10, %r12
orq %rcx, %r12
incq %r12
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
leaq 0x4(%rsp), %rsi
movq %r13, %rdx
movq %r13, %rcx
callq 0x41f1
movb 0x4(%rsp), %r15b
testb %r15b, %r15b
je 0x4006
cmpq %rbp, %rbx
jb 0x3fa3
movq %r14, %rdi
movq %r12, %rsi
callq 0x37c0
movq %r12, %rbp
testq %rax, %rax
je 0x3f5e
movq %rax, %r14
movq %r12, %rbp
movq 0x20(%rsp), %rax
movb %r15b, (%r14,%rax)
movb $0x0, 0x1(%r14,%rax)
jmp 0x3f22
movq %r15, %rdi
callq 0x417c
movq 0x8(%r15), %rdi
leaq 0x8(%rsp), %r15
pushq $0x4
popq %rdx
pushq $0x1
popq %rcx
movq %r15, %rsi
callq 0x41f1
cmpl $0x0, 0x10(%r14)
movss (%r15), %xmm0
je 0x4030
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
jbe 0x4030
pushq $0x9
popq %rdi
movq %rbx, %rsi
callq 0x3350
movq 0x18(%r14), %rdi
movq %rbx, %rsi
callq 0x3530
jmp 0x409c
testq %r14, %r14
je 0x4026
pushq $0x9
popq %rdi
movq 0x18(%rsp), %rbx
movq %rbx, %rsi
callq 0x3350
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3530
movq %r14, %rdi
callq 0x3370
jmp 0x409c
cvtss2sd %xmm0, %xmm0
jmp 0x408b
cvtss2sd %xmm0, %xmm0
leaq 0x3b2f5(%rip), %rsi     # 0x3f336
movq %rbx, %rdi
movb $0x1, %al
callq 0x37f0
cmpl $0x0, 0xc(%r14)
je 0x409c
movl 0x4(%rsp), %edx
leaq 0x3b2d5(%rip), %rsi     # 0x3f332
movq %rbx, %rdi
xorl %eax, %eax
callq 0x37f0
jmp 0x409c
cvtss2sd %xmm0, %xmm0
leaq 0x3b2c2(%rip), %rsi     # 0x3f336
movq %rbx, %rdi
movb $0x1, %al
callq 0x37f0
cmpl $0x0, 0xc(%r14)
je 0x409c
cvtss2sd 0x4(%rsp), %xmm0
leaq 0x3b2a4(%rip), %rsi     # 0x3f336
movq %rbx, %rdi
movb $0x1, %al
callq 0x37f0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3b252(%rip), %rdi     # 0x3f304
xorl %eax, %eax
callq 0x40db
movzbl %al, %esi
leaq 0x3b27d(%rip), %rdi     # 0x3f340
jmp 0x40b2
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rsi
leaq 0x3b2d9(%rip), %rdi     # 0x3f3b2
jmp 0x40b2