// File: /workspace/github_c_makefile_star2_O1/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 $0x18, %rsp
movq %rcx, %r13
testq %rsi, %rsi
js 0x5a55
movq %rdi, %r14
cmpq %rsi, 0x18(%rdi)
jle 0x613e
movq %rdx, %r15
movq 0x2018(%r14), %rax
movzbl %al, %ecx
cmpl $0x1d, %ecx
jg 0x5a72
addl $-0x3, %ecx
cmpl $0x4, %ecx
ja 0x615d
leaq 0x47894(%rip), %rdx     # 0x4d248
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movq 0x8(%r14), %rcx
movq 0x20(%r14), %rdx
movzbl %al, %edi
movl %edi, %r8d
shrq $0x8, %rax
xorl %r9d, %r9d
cmpq $0x7, %rdi
cmovnel %r9d, %eax
leaq 0x4762f(%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 0x5a12
movq %rsi, 0x8(%rcx)
movq (%rcx), %rdi
xorl %edx, %edx
callq 0x3120
testl %eax, %eax
jne 0x6147
movq 0x8(%r14), %rbx
movq (%rbx), %rcx
leaq 0x8(%rsp), %r14
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rdi
callq 0x35b0
addq $0x4, 0x8(%rbx)
cmpl $0x0, 0x10(%r15)
setne %al
movl (%r14), %edx
testl %edx, %edx
sets %cl
andb %al, %cl
cmpb $0x1, %cl
je 0x60a5
jmp 0x60e6
leaq 0x478a0(%rip), %rdi     # 0x4d2fc
movq %r13, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3520
cmpl $0x1e, %ecx
je 0x5c33
cmpl $0x1f, %ecx
je 0x5d8e
cmpl $0x20, %ecx
jne 0x615d
movq 0x8(%r14), %rcx
movq 0x20(%r14), %rdx
movzbl %al, %edi
movl %edi, %r8d
shrq $0x8, %rax
xorl %r9d, %r9d
cmpq $0x7, %rdi
cmovnel %r9d, %eax
leaq 0x4755f(%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 0x5ae2
movq %rsi, 0x8(%rcx)
movq (%rcx), %rdi
xorl %edx, %edx
callq 0x3120
testl %eax, %eax
jne 0x6147
movq 0x8(%r14), %rbx
movq (%rbx), %rcx
leaq 0x8(%rsp), %r12
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
callq 0x35b0
addq $0x4, 0x8(%rbx)
movq 0x8(%r14), %rbx
movq (%rbx), %rcx
leaq 0x4(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x35b0
addq $0x4, 0x8(%rbx)
cmpl $0x0, 0x10(%r15)
movss (%r12), %xmm0
je 0x5b3a
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
ja 0x5b5b
movss 0x30(%r15), %xmm1
xorps %xmm2, %xmm2
ucomiss %xmm2, %xmm1
jb 0x60f9
movss 0x4(%rsp), %xmm2
ucomiss %xmm1, %xmm2
jbe 0x60f9
movl $0x9, %edi
movq %r13, %rsi
callq 0x3350
movq 0x18(%r15), %rdi
movq %r13, %rsi
callq 0x3520
jmp 0x610e
movq %r8, %r12
movq 0x8(%r14), %rcx
movq 0x20(%r14), %rdx
movzbl %al, %edi
movl %edi, %r8d
shrq $0x8, %rax
xorl %r9d, %r9d
cmpq $0x7, %rdi
cmovnel %r9d, %eax
leaq 0x47470(%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 0x5bd1
movq %rsi, 0x8(%rcx)
movq (%rcx), %rdi
xorl %edx, %edx
callq 0x3120
testl %eax, %eax
jne 0x6147
movq 0x2018(%r14), %r15
shrq $0x8, %r15
cmpq $0x0, (%r12)
jne 0x5bf0
leaq 0x1(%r15), %rdi
callq 0x3240
movq %rax, (%r12)
movq 0x8(%r14), %rbx
movq (%r12), %rdi
movq (%rbx), %rcx
movl $0x1, %esi
movq %r15, %rdx
callq 0x35b0
addq %r15, 0x8(%rbx)
movq (%r12), %rax
cmpb $0x0, -0x1(%rax,%r15)
je 0x5c1d
movb $0x0, (%rax,%r15)
movl $0x9, %edi
movq %r13, %rsi
callq 0x3350
movq (%r12), %rdi
jmp 0x5a5c
movq 0x8(%r14), %rcx
movq 0x20(%r14), %rdx
movzbl %al, %edi
movl %edi, %r8d
shrq $0x8, %rax
xorl %r9d, %r9d
cmpq $0x7, %rdi
cmovnel %r9d, %eax
leaq 0x473b9(%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 0x5c88
movq %rsi, 0x8(%rcx)
movq (%rcx), %rdi
xorl %edx, %edx
callq 0x3120
testl %eax, %eax
jne 0x6147
movq 0x8(%r14), %rbx
movq (%rbx), %rcx
leaq 0x8(%rsp), %r14
movl $0x2, %esi
movl $0x1, %edx
movq %r14, %rdi
callq 0x35b0
addq $0x2, 0x8(%rbx)
movzwl (%r14), %eax
cvtsi2ss %eax, %xmm0
addss 0x47585(%rip), %xmm0    # 0x4d240
divss 0x47581(%rip), %xmm0    # 0x4d244
cmpl $0x0, 0x10(%r15)
je 0x5cd6
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
ja 0x60a5
movl (%r15), %edx
cvtss2sd %xmm0, %xmm0
leaq 0x47652(%rip), %rsi     # 0x4d336
movq %r13, %rdi
movb $0x1, %al
callq 0x37e0
jmp 0x612f
movq 0x8(%r14), %rcx
movq 0x20(%r14), %rdx
movzbl %al, %edi
movl %edi, %r8d
shrq $0x8, %rax
xorl %r9d, %r9d
cmpq $0x7, %rdi
cmovnel %r9d, %eax
leaq 0x472f9(%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 0x5d48
movq %rsi, 0x8(%rcx)
movq (%rcx), %rdi
xorl %edx, %edx
callq 0x3120
testl %eax, %eax
jne 0x6147
movq 0x8(%r14), %rbx
movq (%rbx), %rcx
leaq 0x8(%rsp), %r14
movl $0x8, %esi
movl $0x1, %edx
movq %r14, %rdi
callq 0x35b0
addq $0x8, 0x8(%rbx)
cmpl $0x0, 0x10(%r15)
movsd (%r14), %xmm0
je 0x611e
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
ja 0x60a5
jmp 0x611e
movq 0x8(%r14), %rcx
movq 0x20(%r14), %rdx
movzbl %al, %edi
movl %edi, %r8d
shrq $0x8, %rax
xorl %r9d, %r9d
cmpq $0x7, %rdi
cmovnel %r9d, %eax
leaq 0x4725e(%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 0x5de3
movq %rsi, 0x8(%rcx)
movq (%rcx), %rdi
xorl %edx, %edx
callq 0x3120
testl %eax, %eax
jne 0x6147
movq 0x8(%r14), %rbx
movq (%rbx), %rcx
leaq 0x8(%rsp), %r12
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
callq 0x35b0
addq $0x4, 0x8(%rbx)
movq 0x8(%r14), %rbx
movq (%rbx), %rcx
leaq 0x4(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x35b0
addq $0x4, 0x8(%rbx)
cmpl $0x0, 0x10(%r15)
movss (%r12), %xmm0
je 0x5e3b
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
ja 0x5e51
movl 0x34(%r15), %eax
testl %eax, %eax
js 0x60c6
cmpl %eax, 0x4(%rsp)
jge 0x60c6
movl $0x9, %edi
movq %r13, %rsi
callq 0x3350
movq 0x18(%r15), %rdi
movq %r13, %rsi
callq 0x3520
jmp 0x60db
movq 0x8(%r14), %rcx
movq 0x20(%r14), %rdx
movzbl %al, %edi
movl %edi, %r8d
shrq $0x8, %rax
xorl %r9d, %r9d
cmpq $0x7, %rdi
cmovnel %r9d, %eax
leaq 0x4717d(%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 0x5ec4
movq %rsi, 0x8(%rcx)
movq (%rcx), %rdi
xorl %edx, %edx
callq 0x3120
testl %eax, %eax
jne 0x6147
movq 0x8(%r14), %rbx
movq (%rbx), %rcx
leaq 0x8(%rsp), %r15
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rdi
callq 0x35b0
addq $0x8, 0x8(%rbx)
movq 0x8(%r14), %rax
movq 0x18(%r14), %rcx
shlq $0x3, %rcx
addq (%r15), %rcx
movq 0x20(%r14), %rdx
leaq (%rdx,%rcx), %rsi
addq $0x2000, %rsi           # imm = 0x2000
cmpq 0x8(%rax), %rsi
je 0x5f21
movq %rsi, 0x8(%rax)
movq (%rax), %rdi
xorl %edx, %edx
callq 0x3120
testl %eax, %eax
jne 0x6147
movq %r13, 0x10(%rsp)
movq 0x8(%r14), %rbx
movq (%rbx), %rcx
leaq 0x4(%rsp), %r15
movl $0x1, %esi
movl $0x1, %edx
movq %r15, %rdi
callq 0x35b0
incq 0x8(%rbx)
movb (%r15), %bl
xorl %r15d, %r15d
testb %bl, %bl
je 0x5fea
xorl %r13d, %r13d
xorl %r12d, %r12d
leaq 0x1(%r12), %rbp
cmpq %r13, %rbp
jae 0x5f6b
movq %r15, %rax
jmp 0x5faf
movq %rbp, %rax
shrq %rax
orq %rbp, %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, %r13
shrq $0x10, %r13
orq %rcx, %r13
incq %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0x37b0
testq %rax, %rax
je 0x5fbf
movb %bl, (%rax,%r12)
movb $0x0, 0x1(%rax,%r12)
movq %rax, %r15
movq %rbp, %r12
movq 0x8(%r14), %rbx
movq (%rbx), %rcx
movl $0x1, %esi
movl $0x1, %edx
leaq 0x4(%rsp), %rdi
callq 0x35b0
incq 0x8(%rbx)
movb 0x4(%rsp), %bl
testb %bl, %bl
jne 0x5f5c
testq %r15, %r15
je 0x600c
movl $0x9, %edi
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
callq 0x3350
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3520
movq %r15, %rdi
callq 0x3370
jmp 0x612f
movq 0x8(%r14), %rcx
movq 0x20(%r14), %rdx
movzbl %al, %edi
movl %edi, %r8d
shrq $0x8, %rax
xorl %r9d, %r9d
cmpq $0x7, %rdi
cmovnel %r9d, %eax
leaq 0x46fd3(%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 0x606e
movq %rsi, 0x8(%rcx)
movq (%rcx), %rdi
xorl %edx, %edx
callq 0x3120
testl %eax, %eax
jne 0x6147
movq 0x8(%r14), %rbx
movq (%rbx), %rcx
leaq 0x8(%rsp), %r14
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rdi
callq 0x35b0
addq $0x4, 0x8(%rbx)
cmpl $0x0, 0x10(%r15)
movss (%r14), %xmm0
je 0x60c0
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
jbe 0x60c0
movl $0x9, %edi
movq %r13, %rsi
callq 0x3350
movq 0x18(%r15), %rdi
movq %r13, %rsi
callq 0x3520
jmp 0x612f
cvtss2sd %xmm0, %xmm0
jmp 0x611e
cvtss2sd %xmm0, %xmm0
leaq 0x47261(%rip), %rsi     # 0x4d332
movq %r13, %rdi
movb $0x1, %al
callq 0x37e0
cmpl $0x0, 0xc(%r15)
je 0x612f
movl 0x4(%rsp), %edx
leaq 0x47241(%rip), %rsi     # 0x4d32e
movq %r13, %rdi
xorl %eax, %eax
callq 0x37e0
jmp 0x612f
cvtss2sd %xmm0, %xmm0
leaq 0x4722e(%rip), %rsi     # 0x4d332
movq %r13, %rdi
movb $0x1, %al
callq 0x37e0
cmpl $0x0, 0xc(%r15)
je 0x612f
xorps %xmm0, %xmm0
cvtss2sd 0x4(%rsp), %xmm0
leaq 0x4720d(%rip), %rsi     # 0x4d332
movq %r13, %rdi
movb $0x1, %al
callq 0x37e0
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x471bb(%rip), %rdi     # 0x4d300
jmp 0x6156
movq 0x8(%r14), %rax
movq 0x10(%rax), %rsi
leaq 0x47258(%rip), %rdi     # 0x4d3ae
xorl %eax, %eax
callq 0x6169
movzbl %al, %esi
leaq 0x471d5(%rip), %rdi     # 0x4d33c
jmp 0x6156