// File: /workspace/github_c_makefile_star2_O3/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, %rbx
testq %rsi, %rsi
js 0x5923
movq %rdi, %r15
cmpq %rsi, 0x18(%rdi)
jle 0x6080
movq %rdx, %r14
movq 0x2018(%r15), %r12
movzbl %r12b, %eax
cmpl $0x1d, %eax
jg 0x5940
addl $-0x3, %eax
cmpl $0x4, %eax
ja 0x609b
leaq 0x479d7(%rip), %rcx     # 0x4d24c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rbx, %r13
movq 0x8(%r15), %rbx
movq 0x20(%r15), %rax
movzbl %r12b, %ecx
movl %ecx, %edx
shrq $0x8, %r12
xorl %edi, %edi
cmpq $0x7, %rcx
cmovnel %edi, %r12d
leaq 0x4776c(%rip), %rcx     # 0x4d010
addl (%rcx,%rdx,4), %r12d
movslq %r12d, %rcx
imulq %rsi, %rcx
leaq (%rax,%rcx), %rsi
addq $0x2000, %rsi           # imm = 0x2000
cmpq 0x8(%rbx), %rsi
je 0x58da
movq %rsi, 0x8(%rbx)
movq (%rbx), %rdi
xorl %edx, %edx
callq 0x3120
movq 0x8(%r15), %rbx
testl %eax, %eax
jne 0x6089
movq (%rbx), %rcx
leaq 0x8(%rsp), %r15
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
callq 0x35b0
addq $0x4, 0x8(%rbx)
cmpl $0x0, 0x10(%r14)
setne %al
movl (%r15), %edx
testl %edx, %edx
sets %cl
andb %al, %cl
cmpb $0x1, %cl
je 0x5f67
leaq 0x47a17(%rip), %rsi     # 0x4d332
movq %r13, %rdi
jmp 0x6032
leaq 0x479d6(%rip), %rdi     # 0x4d300
movq %rbx, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3520
cmpl $0x1e, %eax
je 0x5af5
cmpl $0x1f, %eax
je 0x5c56
cmpl $0x20, %eax
jne 0x609b
movq %rbx, %r13
movq 0x8(%r15), %rbx
movq 0x20(%r15), %rax
movzbl %r12b, %ecx
movl %ecx, %edx
shrq $0x8, %r12
xorl %edi, %edi
cmpq $0x7, %rcx
cmovnel %edi, %r12d
leaq 0x4768f(%rip), %rcx     # 0x4d010
addl (%rcx,%rdx,4), %r12d
movslq %r12d, %rcx
imulq %rsi, %rcx
leaq (%rax,%rcx), %rsi
addq $0x2000, %rsi           # imm = 0x2000
cmpq 0x8(%rbx), %rsi
je 0x59b7
movq %rsi, 0x8(%rbx)
movq (%rbx), %rdi
xorl %edx, %edx
callq 0x3120
movq 0x8(%r15), %rbx
testl %eax, %eax
jne 0x6089
movq (%rbx), %rcx
leaq 0x8(%rsp), %r12
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
callq 0x35b0
addq $0x4, 0x8(%rbx)
movq 0x8(%r15), %rbx
movq (%rbx), %rcx
leaq 0x4(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x35b0
addq $0x4, 0x8(%rbx)
cmpl $0x0, 0x10(%r14)
movss (%r12), %xmm0
je 0x5fc1
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
jbe 0x5fc1
movl $0x9, %edi
movq %r13, %rbx
movq %r13, %rsi
callq 0x3350
movq 0x18(%r14), %rdi
movq %r13, %rsi
jmp 0x5ff1
movq %r8, %r13
movq 0x8(%r15), %rax
movq 0x20(%r15), %rcx
movzbl %r12b, %edx
movl %edx, %edi
shrq $0x8, %r12
xorl %r8d, %r8d
cmpq $0x7, %rdx
movl %r12d, %edx
cmovnel %r8d, %edx
leaq 0x475b7(%rip), %r8      # 0x4d010
addl (%r8,%rdi,4), %edx
movslq %edx, %rdx
imulq %rsi, %rdx
leaq (%rcx,%rdx), %rsi
addq $0x2000, %rsi           # imm = 0x2000
cmpq 0x8(%rax), %rsi
je 0x5a96
movq %rsi, 0x8(%rax)
movq (%rax), %rdi
xorl %edx, %edx
callq 0x3120
testl %eax, %eax
jne 0x60a8
movq 0x2018(%r15), %r12
shrq $0x8, %r12
movq %rbx, %r14
movq (%r13), %rdi
testq %rdi, %rdi
jne 0x5ab3
leaq 0x1(%r12), %rdi
callq 0x3240
movq %rax, %rdi
movq %rax, (%r13)
movq 0x8(%r15), %rbx
movq (%rbx), %rcx
movl $0x1, %esi
movq %r12, %rdx
callq 0x35b0
addq %r12, 0x8(%rbx)
movq (%r13), %rax
cmpb $0x0, -0x1(%rax,%r12)
je 0x5adc
movb $0x0, (%rax,%r12)
movl $0x9, %edi
movq %r14, %rsi
callq 0x3350
movq (%r13), %rdi
movq %r14, %rsi
jmp 0x592d
movq %rbx, %r13
movq 0x8(%r15), %rbx
movq 0x20(%r15), %rax
movzbl %r12b, %ecx
movl %ecx, %edx
shrq $0x8, %r12
xorl %edi, %edi
cmpq $0x7, %rcx
cmovnel %edi, %r12d
leaq 0x474f5(%rip), %rcx     # 0x4d010
addl (%rcx,%rdx,4), %r12d
movslq %r12d, %rcx
imulq %rsi, %rcx
leaq (%rax,%rcx), %rsi
addq $0x2000, %rsi           # imm = 0x2000
cmpq 0x8(%rbx), %rsi
je 0x5b51
movq %rsi, 0x8(%rbx)
movq (%rbx), %rdi
xorl %edx, %edx
callq 0x3120
movq 0x8(%r15), %rbx
testl %eax, %eax
jne 0x6089
movq (%rbx), %rcx
leaq 0x8(%rsp), %r15
movl $0x2, %esi
movl $0x1, %edx
movq %r15, %rdi
callq 0x35b0
addq $0x2, 0x8(%rbx)
movzwl (%r15), %eax
cvtsi2ss %eax, %xmm0
addss 0x476c0(%rip), %xmm0    # 0x4d240
divss 0x476bc(%rip), %xmm0    # 0x4d244
cmpl $0x0, 0x10(%r14)
je 0x5b9b
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
ja 0x5f67
movl (%r14), %edx
cvtss2sd %xmm0, %xmm0
leaq 0x47791(%rip), %rsi     # 0x4d33a
movq %r13, %rdi
movb $0x1, %al
callq 0x37e0
jmp 0x6071
movq %rbx, %r13
movq 0x8(%r15), %rbx
movq 0x20(%r15), %rax
movzbl %r12b, %ecx
movl %ecx, %edx
shrq $0x8, %r12
xorl %edi, %edi
cmpq $0x7, %rcx
cmovnel %edi, %r12d
leaq 0x47432(%rip), %rcx     # 0x4d010
addl (%rcx,%rdx,4), %r12d
movslq %r12d, %rcx
imulq %rsi, %rcx
leaq (%rax,%rcx), %rsi
addq $0x2000, %rsi           # imm = 0x2000
cmpq 0x8(%rbx), %rsi
je 0x5c14
movq %rsi, 0x8(%rbx)
movq (%rbx), %rdi
xorl %edx, %edx
callq 0x3120
movq 0x8(%r15), %rbx
testl %eax, %eax
jne 0x6089
movq (%rbx), %rcx
leaq 0x8(%rsp), %r15
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rdi
callq 0x35b0
addq $0x8, 0x8(%rbx)
cmpl $0x0, 0x10(%r14)
movsd (%r15), %xmm0
je 0x5ffc
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
ja 0x5f67
jmp 0x5ffc
movq %rbx, %r13
movq 0x8(%r15), %rbx
movq 0x20(%r15), %rax
movzbl %r12b, %ecx
movl %ecx, %edx
shrq $0x8, %r12
xorl %edi, %edi
cmpq $0x7, %rcx
cmovnel %edi, %r12d
leaq 0x47394(%rip), %rcx     # 0x4d010
addl (%rcx,%rdx,4), %r12d
movslq %r12d, %rcx
imulq %rsi, %rcx
leaq (%rax,%rcx), %rsi
addq $0x2000, %rsi           # imm = 0x2000
cmpq 0x8(%rbx), %rsi
je 0x5cb2
movq %rsi, 0x8(%rbx)
movq (%rbx), %rdi
xorl %edx, %edx
callq 0x3120
movq 0x8(%r15), %rbx
testl %eax, %eax
jne 0x6089
movq (%rbx), %rcx
leaq 0x8(%rsp), %r12
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
callq 0x35b0
addq $0x4, 0x8(%rbx)
movq 0x8(%r15), %rbx
movq (%rbx), %rcx
leaq 0x4(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x35b0
addq $0x4, 0x8(%rbx)
cmpl $0x0, 0x10(%r14)
movss (%r12), %xmm0
je 0x5f95
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
jbe 0x5f95
movl $0x9, %edi
movq %r13, %rbx
movq %r13, %rsi
callq 0x3350
movq 0x18(%r14), %rdi
movq %r13, %rsi
jmp 0x5fba
movq %rbx, 0x10(%rsp)
movq 0x8(%r15), %rbx
movq 0x20(%r15), %rax
movzbl %r12b, %ecx
movl %ecx, %edx
shrq $0x8, %r12
xorl %edi, %edi
cmpq $0x7, %rcx
cmovnel %edi, %r12d
leaq 0x472be(%rip), %rcx     # 0x4d010
addl (%rcx,%rdx,4), %r12d
movslq %r12d, %rcx
imulq %rsi, %rcx
leaq (%rax,%rcx), %rsi
addq $0x2000, %rsi           # imm = 0x2000
cmpq 0x8(%rbx), %rsi
je 0x5d88
movq %rsi, 0x8(%rbx)
movq (%rbx), %rdi
xorl %edx, %edx
callq 0x3120
movq 0x8(%r15), %rbx
testl %eax, %eax
jne 0x6089
movq (%rbx), %rcx
leaq 0x8(%rsp), %r14
movl $0x8, %esi
movl $0x1, %edx
movq %r14, %rdi
callq 0x35b0
addq $0x8, 0x8(%rbx)
movq 0x8(%r15), %rbx
movq 0x18(%r15), %rax
shlq $0x3, %rax
addq (%r14), %rax
movq 0x20(%r15), %rcx
leaq (%rcx,%rax), %rsi
addq $0x2000, %rsi           # imm = 0x2000
cmpq 0x8(%rbx), %rsi
je 0x5de5
movq %rsi, 0x8(%rbx)
movq (%rbx), %rdi
xorl %edx, %edx
callq 0x3120
movq 0x8(%r15), %rbx
testl %eax, %eax
jne 0x6089
movq (%rbx), %rcx
leaq 0x4(%rsp), %r14
movl $0x1, %esi
movl $0x1, %edx
movq %r14, %rdi
callq 0x35b0
incq 0x8(%rbx)
movb (%r14), %bl
testb %bl, %bl
je 0x5f85
xorl %r14d, %r14d
xorl %r13d, %r13d
xorl %r12d, %r12d
leaq 0x1(%r12), %rbp
cmpq %r13, %rbp
jae 0x5e26
movq %r14, %rax
jmp 0x5e6a
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 %r14, %rdi
movq %r13, %rsi
callq 0x37b0
testq %rax, %rax
je 0x5e7a
movb %bl, (%rax,%r12)
movb $0x0, 0x1(%rax,%r12)
movq %rax, %r14
movq %rbp, %r12
movq 0x8(%r15), %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 0x5e17
testq %r14, %r14
je 0x5f85
movl $0x9, %edi
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
callq 0x3350
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3520
jmp 0x5f88
movq %rbx, %r13
movq 0x8(%r15), %rbx
movq 0x20(%r15), %rax
movzbl %r12b, %ecx
movl %ecx, %edx
shrq $0x8, %r12
xorl %edi, %edi
cmpq $0x7, %rcx
cmovnel %edi, %r12d
leaq 0x4711a(%rip), %rcx     # 0x4d010
addl (%rcx,%rdx,4), %r12d
movslq %r12d, %rcx
imulq %rsi, %rcx
leaq (%rax,%rcx), %rsi
addq $0x2000, %rsi           # imm = 0x2000
cmpq 0x8(%rbx), %rsi
je 0x5f2c
movq %rsi, 0x8(%rbx)
movq (%rbx), %rdi
xorl %edx, %edx
callq 0x3120
movq 0x8(%r15), %rbx
testl %eax, %eax
jne 0x6089
movq (%rbx), %rcx
leaq 0x8(%rsp), %r15
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
callq 0x35b0
addq $0x4, 0x8(%rbx)
cmpl $0x0, 0x10(%r14)
movss (%r15), %xmm0
je 0x5ff8
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
jbe 0x5ff8
movl $0x9, %edi
movq %r13, %rsi
callq 0x3350
movq 0x18(%r14), %rdi
movq %r13, %rsi
callq 0x3520
jmp 0x6071
xorl %r14d, %r14d
movq %r14, %rdi
callq 0x3370
jmp 0x6071
movl 0x34(%r14), %eax
testl %eax, %eax
movq %r13, %rbx
js 0x6008
cmpl %eax, 0x4(%rsp)
jge 0x6008
movl $0x9, %edi
movq %rbx, %rsi
callq 0x3350
movq 0x18(%r14), %rdi
movq %rbx, %rsi
callq 0x3520
jmp 0x601d
movss 0x30(%r14), %xmm1
xorps %xmm2, %xmm2
ucomiss %xmm2, %xmm1
movq %r13, %rbx
jb 0x603b
movss 0x4(%rsp), %xmm2
ucomiss %xmm1, %xmm2
jbe 0x603b
movl $0x9, %edi
movq %rbx, %rsi
callq 0x3350
movq 0x18(%r14), %rdi
movq %rbx, %rsi
callq 0x3520
jmp 0x6050
cvtss2sd %xmm0, %xmm0
leaq 0x47333(%rip), %rsi     # 0x4d336
movq %r13, %rdi
jmp 0x606a
cvtss2sd %xmm0, %xmm0
leaq 0x47323(%rip), %rsi     # 0x4d336
movq %rbx, %rdi
movb $0x1, %al
callq 0x37e0
cmpl $0x0, 0xc(%r14)
je 0x6071
movl 0x4(%rsp), %edx
leaq 0x47303(%rip), %rsi     # 0x4d332
movq %rbx, %rdi
xorl %eax, %eax
callq 0x37e0
jmp 0x6071
cvtss2sd %xmm0, %xmm0
leaq 0x472f0(%rip), %rsi     # 0x4d336
movq %rbx, %rdi
movb $0x1, %al
callq 0x37e0
cmpl $0x0, 0xc(%r14)
je 0x6071
xorps %xmm0, %xmm0
cvtss2sd 0x4(%rsp), %xmm0
leaq 0x472cf(%rip), %rsi     # 0x4d336
movq %rbx, %rdi
movb $0x1, %al
callq 0x37e0
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4727d(%rip), %rdi     # 0x4d304
jmp 0x6094
movq 0x10(%rbx), %rsi
leaq 0x4731e(%rip), %rdi     # 0x4d3b2
xorl %eax, %eax
callq 0x60b2
movzbl %r12b, %esi
leaq 0x4729a(%rip), %rdi     # 0x4d340
jmp 0x6094
movq 0x8(%r15), %rax
movq 0x10(%rax), %rsi
jmp 0x608d