// File: /workspace/github_c_makefile_star2_O1/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
movq %rdx, (%rsp)
movq %rsi, %r12
movq 0x58(%rsp), %rdx
movq (%rdi), %r14
movzbl %r12b, %eax
cmpl $0x1d, %eax
jg 0x94b0
decl %eax
cmpl $0x6, %eax
ja 0x97d4
movq %r8, %r15
movl %ecx, %r13d
leaq 0x445f2(%rip), %rcx     # 0x4da68
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movzbl (%r14), %ecx
xorl %eax, %eax
cmpl $0x30, %ecx
je 0x9498
cmpl $0x31, %ecx
jne 0x97e1
movl $0x1, %eax
andl $0x7, %r13d
movl %r13d, %ecx
shll %cl, %eax
orb (%r15), %al
movb %al, (%r15)
cmpl $0x7, %r13d
jmp 0x9679
movq %rdi, %rbp
cmpl $0x1f, %eax
jg 0x9515
cmpl $0x1e, %eax
je 0x95ce
cmpl $0x1f, %eax
jne 0x97d4
cmpb $0x2e, (%r14)
jne 0x94db
cmpb $0x0, 0x1(%r14)
je 0x978e
movq %rdx, %r15
movq %r14, %rdi
callq 0x32f0
cvtsd2ss %xmm0, %xmm0
movq (%rsp), %rbx
movss %xmm0, 0x8(%rsp)
movq 0x8(%rbp), %rdi
cmpb $0x2e, (%rdi)
jne 0x9507
cmpb $0x0, 0x1(%rdi)
je 0x973a
callq 0x3550
movl %eax, 0x14(%rsp)
jmp 0x9745
cmpl $0x20, %eax
jne 0x97c1
cmpb $0x2e, (%r14)
jne 0x952f
cmpb $0x0, 0x1(%r14)
je 0x979a
movq %rdx, %r15
movq %r14, %rdi
callq 0x32f0
cvtsd2ss %xmm0, %xmm0
movq (%rsp), %rbx
movss %xmm0, 0x8(%rsp)
movq 0x8(%rbp), %rdi
cmpb $0x2e, (%rdi)
jne 0x955b
cmpb $0x0, 0x1(%rdi)
je 0x973a
callq 0x32f0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x14(%rsp)
jmp 0x9745
movq %rdx, %rbx
movq %r14, %rdi
callq 0x32f0
movsd %xmm0, 0x8(%rsp)
cmpb $0x2e, (%r14)
jne 0x9591
cmpb $0x0, 0x1(%r14)
je 0x9782
movq %r14, %rdi
callq 0x32f0
leaq 0x8(%rsp), %rdi
movsd %xmm0, (%rdi)
movl $0x8, %esi
jmp 0x963e
cmpb $0x2e, (%r14)
jne 0x95bd
cmpb $0x0, 0x1(%r14)
je 0x97a6
movq %r14, %rdi
callq 0x3550
leaq 0x8(%rsp), %rdi
movl %eax, (%rdi)
jmp 0x9639
cmpb $0x2e, (%r14)
jne 0x95df
cmpb $0x0, 0x1(%r14)
je 0x97af
movq %r14, %rdi
callq 0x32f0
cvtsd2ss %xmm0, %xmm0
addss 0x44471(%rip), %xmm0    # 0x4da64
mulss 0x43c49(%rip), %xmm0    # 0x4d244
callq 0x30f0
cvttss2si %xmm0, %eax
leaq 0x8(%rsp), %rdi
movw %ax, (%rdi)
movl $0x2, %esi
jmp 0x963e
cmpb $0x2e, (%r14)
jne 0x9624
cmpb $0x0, 0x1(%r14)
je 0x97b8
movq %r14, %rdi
callq 0x32f0
cvtsd2ss %xmm0, %xmm0
leaq 0x8(%rsp), %rdi
movss %xmm0, (%rdi)
movl $0x4, %esi
movl $0x1, %edx
movq (%rsp), %rcx
jmp 0x976e
movq %r14, %rdi
callq 0x3550
leal 0x3(%r13), %ecx
testl %r13d, %r13d
cmovnsl %r13d, %ecx
andl $-0x4, %ecx
subl %ecx, %r13d
leal (,%r13,2), %ecx
shll %cl, %eax
orb (%r15), %al
movb %al, (%r15)
cmpl $0x3, %r13d
movq (%rsp), %rsi
jne 0x9773
movsbl %al, %edi
callq 0x3350
movb $0x0, (%r15)
jmp 0x9773
movq %r9, %rbx
movq 0x50(%rsp), %r15
movq %r14, %rdi
callq 0x3380
incl %eax
movslq %eax, %rdx
movl $0x1, %esi
movq %r14, %rdi
movq %rbx, %rcx
callq 0x3770
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rdi
movq (%rsp), %rcx
callq 0x3770
movq %r14, %rdi
callq 0x3380
movq (%r15), %rcx
addq %rcx, %rax
incq %rax
movq %rax, (%r15)
jmp 0x9773
movq %r14, %rdi
callq 0x3380
movq %rax, %r15
movl %r15d, %edx
shrq $0x8, %r12
cmpq %r12, %rdx
jbe 0x9704
movb $0x0, (%r14,%r12)
movl $0x1, %esi
movq %r14, %rdi
movq (%rsp), %rbx
movq %rbx, %rcx
callq 0x3770
movl %r15d, %eax
cmpq %rax, %r12
jbe 0x9773
incl %r15d
xorl %edi, %edi
movq %rbx, %rsi
callq 0x3350
movl %r15d, %eax
incl %r15d
cmpq %rax, %r12
ja 0x9723
jmp 0x9773
movss (%r15), %xmm0
movss %xmm0, 0x8(%rsp)
leaq 0x8(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x3770
leaq 0x14(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x3770
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorps %xmm0, %xmm0
cvtss2sd (%rbx), %xmm0
jmp 0x9599
movq %rdx, %r15
movss (%rdx), %xmm0
jmp 0x94ea
movq %rdx, %r15
movss (%rdx), %xmm0
jmp 0x953e
cvttss2si (%rdx), %eax
jmp 0x95c5
movss (%rdx), %xmm0
jmp 0x9600
movss (%rdx), %xmm0
jmp 0x9630
cmpl $0x63, %eax
jne 0x97d4
leaq 0x44322(%rip), %rdi     # 0x4daef
xorl %eax, %eax
callq 0x9395
movzbl %r12b, %esi
leaq 0x43b5d(%rip), %rdi     # 0x4d33c
jmp 0x97eb
leaq 0x442e5(%rip), %rdi     # 0x4dacd
movq %r14, %rsi
xorl %eax, %eax
callq 0x9395