// File: /workspace/github_c_makefile_star2_O2/zhou-lab[P]tbmate/pack.c
// Function: tbk_write
void tbk_write(
  beddata_t *bd, uint64_t dtype, FILE *out,
  int n, uint8_t *aux,
  FILE *tmp_out, uint64_t *tmp_out_offset,
  conf_pack_t *conf) {

  char *s = bd->s[0];
  switch(DATA_TYPE(dtype)) {
  case DT_INT1: {
    uint8_t d = 0;
    switch(s[0]) {
    case '0': d=0; break;
    case '1': d=1; break;
    default: wzfatal("Error, non-binary data found: %s\n", s); }
    *aux |= d << (n&0x7);
    if((n&0x7)==7) { fwrite(aux, 1, 1, out); *aux=0; }
    break;
  }
  case DT_INT2: {
    uint8_t d = atoi(s);
    *aux |= d << ((n%4)*2);
    if(n%4==3) { fwrite(aux, 1, 1, out); *aux=0; }
    break;
  }
  case DT_INT32: {
    int32_t d;
    if (s[0] == '.' && s[1] == '\0') d = conf->nan;
    else d = atoi(s);
    fwrite(&d, sizeof(int32_t), 1, out);
    break;
  }
  case DT_FLOAT: {
    float d;
    if (s[0] == '.' && s[1] == '\0') d = conf->nan;
    else d = atof(s);
    fwrite(&d, sizeof(float), 1, out);
    break;
  }
  case DT_DOUBLE: {
    double d = atof(s);
    if (s[0] == '.' && s[1] == '\0') d = conf->nan;
    else d = atof(s);
    fwrite(&d, sizeof(double), 1, out);
    break;
  }
  case DT_STRINGF: {
    unsigned n = strlen(s);
    if (n > STRING_MAX(dtype)) s[STRING_MAX(dtype)] = 0;
    fwrite(s, 1, n, out);
    char buf = '\0';
    for (;n < STRING_MAX(dtype);++n) { fwrite(&buf, 1, 1, out); }
    break;
  }
  case DT_STRINGD: {
    int n = strlen(s) + 1;
    fwrite(s, 1, n, tmp_out);
    fwrite(tmp_out_offset, 8, 1, out);
    *tmp_out_offset += strlen(s) + 1;
    break;
  }
  case DT_ONES: {
    uint16_t d;
    if (s[0] == '.' && s[1] == '\0') d = conf->nan;
    else d = float_to_uint16(atof(s));
    fwrite(&d, sizeof(uint16_t), 1, out);
    break;
  }
  case DT_FLOAT_INT: {
    float d; int d2;
    if (s[0] == '.' && s[1] == '\0') d = conf->nan; else d = atof(s);
    s = bd->s[1];
    if (s[0] == '.' && s[1] == '\0') d = conf->nan; else d2 = atoi(s);
    fwrite(&d, sizeof(float), 1, out);
    fwrite(&d2, sizeof(int32_t), 1, out);
    break;
  }
  case DT_FLOAT_FLOAT: {
    float d, d2;
    if (s[0] == '.' && s[1] == '\0') d = conf->nan; else d = atof(s);
    s = bd->s[1];
    if (s[0] == '.' && s[1] == '\0') d = conf->nan; else d2 = atof(s);
    fwrite(&d, sizeof(float), 1, out);
    fwrite(&d2, sizeof(float), 1, out);
    break;
  }
  case DT_NA: wzfatal("Fail to detect data type. Please specify -s explicity.\n"); break;
  default: wzfatal("Unrecognized data type: %d.\n", DATA_TYPE(dtype));
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %ecx, %ebp
movq %rdx, (%rsp)
movq %rsi, %r12
movq 0x58(%rsp), %rdx
movq (%rdi), %r14
movzbl %r12b, %eax
leal -0x1(%rax), %ecx
cmpl $0x6, %ecx
ja 0x6e1b
movq %r8, %r15
leaq 0x38c8f(%rip), %rax     # 0x3fa68
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movzbl (%r14), %ecx
xorl %eax, %eax
cmpl $0x30, %ecx
je 0x6df9
cmpl $0x31, %ecx
jne 0x7102
pushq $0x1
popq %rax
andl $0x7, %ebp
movl %ebp, %ecx
shll %cl, %eax
orb (%r15), %al
movb %al, (%r15)
cmpl $0x7, %ebp
movq (%rsp), %rsi
jne 0x7097
movsbl %al, %edi
jmp 0x6f70
cmpl $0x1e, %eax
je 0x6f7e
movq %rdi, %r13
cmpl $0x1f, %eax
je 0x6ed1
cmpl $0x20, %eax
jne 0x70e2
cmpb $0x2e, (%r14)
jne 0x6e4a
cmpb $0x0, 0x1(%r14)
je 0x70c4
movq %rdx, %rbx
movq %r14, %rdi
callq 0x32f0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
movq 0x8(%r13), %rdi
cmpb $0x2e, (%rdi)
jne 0x6e72
cmpb $0x0, 0x1(%rdi)
je 0x705c
callq 0x32f0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x14(%rsp)
jmp 0x7066
cmpb $0x2e, (%r14)
jne 0x6e97
cmpb $0x0, 0x1(%r14)
je 0x70a6
movq %r14, %rdi
callq 0x32f0
leaq 0x8(%rsp), %rdi
movsd %xmm0, (%rdi)
pushq $0x8
jmp 0x6fbe
cmpb $0x2e, (%r14)
jne 0x6ec0
cmpb $0x0, 0x1(%r14)
je 0x70af
movq %r14, %rdi
callq 0x3560
leaq 0x8(%rsp), %rdi
movl %eax, (%rdi)
jmp 0x6f3e
cmpb $0x2e, (%r14)
jne 0x6ee2
cmpb $0x0, 0x1(%r14)
je 0x70b8
movq %rdx, %rbx
movq %r14, %rdi
callq 0x32f0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
movq 0x8(%r13), %rdi
cmpb $0x2e, (%rdi)
jne 0x6f0a
cmpb $0x0, 0x1(%rdi)
je 0x705c
callq 0x3560
movl %eax, 0x14(%rsp)
jmp 0x7066
cmpb $0x2e, (%r14)
jne 0x6f29
cmpb $0x0, 0x1(%r14)
je 0x70d0
movq %r14, %rdi
callq 0x32f0
cvtsd2ss %xmm0, %xmm0
leaq 0x8(%rsp), %rdi
movss %xmm0, (%rdi)
pushq $0x4
jmp 0x6fbe
movq %r14, %rdi
callq 0x3560
movl %eax, %edi
pushq $0x4
popq %rcx
movl %ebp, %eax
cltd
idivl %ecx
leal (%rdx,%rdx), %ecx
shll %cl, %edi
orb (%r15), %dil
movb %dil, (%r15)
cmpl $0x3, %edx
movq (%rsp), %rsi
jne 0x7097
movsbl %dil, %edi
callq 0x3350
movb $0x0, (%r15)
jmp 0x7097
cmpb $0x2e, (%r14)
jne 0x6f8f
cmpb $0x0, 0x1(%r14)
je 0x70d9
movq %r14, %rdi
callq 0x32f0
cvtsd2ss %xmm0, %xmm0
addss 0x38ac1(%rip), %xmm0    # 0x3fa64
mulss 0x38299(%rip), %xmm0    # 0x3f244
callq 0x30f0
cvttss2si %xmm0, %eax
leaq 0x8(%rsp), %rdi
movw %ax, (%rdi)
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
movq (%rsp), %rcx
jmp 0x7092
movq %r9, %rbx
movq 0x50(%rsp), %r15
movq %r14, %rdi
callq 0x3380
incl %eax
movslq %eax, %rdx
pushq $0x1
popq %r12
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rcx
callq 0x3780
pushq $0x8
popq %rsi
movq %r15, %rdi
movq %r12, %rdx
movq (%rsp), %rcx
callq 0x3780
movq %r14, %rdi
callq 0x3380
movq (%r15), %rcx
addq %rcx, %rax
incq %rax
movq %rax, (%r15)
jmp 0x7097
movq %r14, %rdi
callq 0x3380
movq %rax, %rbx
movl %ebx, %edx
shrq $0x8, %r12
cmpq %r12, %rdx
jbe 0x7035
movb $0x0, (%r14,%r12)
pushq $0x1
popq %rsi
movq %r14, %rdi
movq (%rsp), %r14
movq %r14, %rcx
callq 0x3780
movl %ebx, %eax
cmpq %rax, %r12
jbe 0x7097
xorl %edi, %edi
movq %r14, %rsi
callq 0x3350
incl %ebx
jmp 0x7047
movss (%rbx), %xmm0
movss %xmm0, 0x8(%rsp)
leaq 0x8(%rsp), %rdi
pushq $0x4
popq %rbx
pushq $0x1
popq %r14
movq %rbx, %rsi
movq %r14, %rdx
movq (%rsp), %r15
movq %r15, %rcx
callq 0x3780
leaq 0x14(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x3780
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cvtss2sd (%rdx), %xmm0
jmp 0x6e9f
cvttss2si (%rdx), %eax
jmp 0x6ec8
movq %rdx, %rbx
movss (%rdx), %xmm0
jmp 0x6ef1
movq %rdx, %rbx
movss (%rdx), %xmm0
jmp 0x6e59
movss (%rdx), %xmm0
jmp 0x6f35
movss (%rdx), %xmm0
jmp 0x6fb0
cmpl $0x63, %eax
jne 0x70f5
leaq 0x38a01(%rip), %rdi     # 0x3faef
xorl %eax, %eax
callq 0x6d03
movzbl %r12b, %esi
leaq 0x38240(%rip), %rdi     # 0x3f340
jmp 0x710c
leaq 0x389c4(%rip), %rdi     # 0x3facd
movq %r14, %rsi
xorl %eax, %eax
callq 0x6d03