// File: /workspace/github_c_makefile_star2_O2/zhou-lab[P]tbmate/header.c
// Function: main_header
int main_header(int argc, char *argv[]) {

  int c;
  if (argc<1) return usage();

  char *msg = NULL;
  while ((c = getopt(argc, argv, "m:h"))>=0) {
    switch (c) {
    case 'm': msg = optarg; break;
    case 'h': return usage(); break;
    default: usage(); wzfatal("Unrecognized option: %c.\n", c);
    }
  }

  if (optind > argc) { 
    usage(); 
    wzfatal("Please supply tbk file.\n"); 
  }

  tbf_t *tbf = NULL; tbk_t tbk = {0}; int i=0; int j=0;
  for (;optind < argc; optind++, i++) {
    /* set message  */
    if(msg) {
      
      tbf = tbf_open_update(argv[optind]);
      fseek(tbf->fh, HDR_ID+HDR_VERSION+HDR_DATA_TYPE+HDR_NMAX, SEEK_SET);
      /* truncate if too long */
      if (strlen(msg) >= HDR_EXTRA-1) {
        fprintf(stderr, "[Warning] Message too long, truncated to %d:\n%s", HDR_EXTRA-1, msg);
        msg[HDR_EXTRA-1] = '\0';
      }
      fwrite(msg, strlen(msg)+1, 1, tbf->fh);
      tbf_close(tbf);

    } else {

      tbf = tbf_open(argv[optind]);
      fprintf(stdout, "\nTBK File: %s\n", tbf->fname);
      fprintf(stderr, "Number of samples: %d\n", tbf_num_samples(argv[optind]));

      j = 1;
      while (1) {
        tbf_next(tbf, &tbk);
        fprintf(stdout, "  Sample %d: %s\n", j++, tbk.sname);
        fprintf(stdout, "  TBK Version: %d\n", tbk.version);
        fputs("  Data type: ", stdout);
        switch(DATA_TYPE(tbk.dtype)) {
        case DT_INT1:        fputs("INT1\n", stdout);        break;
        case DT_INT2:        fputs("INT2\n", stdout);        break;
        case DT_INT32:       fputs("INT32\n", stdout);       break;
        case DT_FLOAT:       fputs("FLOAT\n", stdout);       break;
        case DT_DOUBLE:      fputs("DOUBLE\n", stdout);      break;
        case DT_STRINGF:     fprintf(stdout, "STRINGF (n=%"PRIu64")\n", STRING_MAX(tbk.dtype)); break;
        case DT_STRINGD:     fputs("STRINGD\n", stdout);     break;
        case DT_ONES:        fputs("ONES\n", stdout);        break;
        case DT_FLOAT_INT:   fputs("FLOAT.INT\n", stdout);   break;
        case DT_FLOAT_FLOAT: fputs("FLOAT.FLOAT\n", stdout); break;
        default: wzfatal("  Data type %"PRIu64" unrecognized.\n", tbk.dtype);
        }
        fprintf(stdout, "  Number of data: %"PRId64"\n", tbk.nmax);
        fputs("  Message: ", stdout);
        if (tbk.extra[0]) fputs(tbk.extra, stdout);
        fputs("\n\n", stdout);
        if (tbk.version < 100) break;
        tbf_skip_data(&tbk);
      }

      tbf_close(tbf);
    }
  }
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4068, %rsp           # imm = 0x4068
testl %edi, %edi
jle 0x8258
movq %rsi, %r13
movl %edi, %ebp
xorl %r14d, %r14d
leaq 0x37f10(%rip), %rbx     # 0x3fdfc
movq 0x4b0ed(%rip), %r12     # 0x52fe0
movl %ebp, %edi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x3200
testl %eax, %eax
js 0x7f16
movl %eax, %r15d
cmpl $0x6d, %eax
jne 0x8252
movq (%r12), %r14
jmp 0x7ef3
movq %r13, 0x8(%rsp)
movq 0x4b09e(%rip), %rax     # 0x52fc0
movl (%rax), %ebx
movl %ebp, 0x4(%rsp)
cmpl %ebp, %ebx
jg 0x82a5
leaq 0x18(%rsp), %rdi
movl $0x2028, %edx           # imm = 0x2028
xorl %esi, %esi
callq 0x3090
movq 0x4b0b0(%rip), %rbp     # 0x52ff8
leaq 0x2040(%rsp), %r13
cmpl 0x4(%rsp), %ebx
jge 0x824e
movslq %ebx, %rax
movq 0x8(%rsp), %rcx
movq (%rcx,%rax,8), %r15
testq %r14, %r14
je 0x7fff
pushq $0x1
popq %rdi
pushq $0x20
popq %rsi
callq 0x36b0
movq %rax, %rbx
movq %r15, %rdi
callq 0x3600
movq %rax, %r12
movq %rax, 0x10(%rbx)
movq %r15, %rdi
leaq 0x37fee(%rip), %rsi     # 0x3ff84
callq 0x3250
movq %rax, (%rbx)
testq %rax, %rax
je 0x8288
movq %rax, %r15
movq %rax, %rdi
pushq $0x17
popq %rsi
xorl %edx, %edx
callq 0x3120
movq %r14, %rdi
callq 0x3380
cmpq $0x1fe8, %rax           # imm = 0x1FE8
jae 0x8221
movq %r14, %rdi
callq 0x3380
leaq 0x1(%rax), %rsi
movq %r14, %rdi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x3780
movq %rbx, %rdi
callq 0x8359
movq 0x4afcc(%rip), %rax     # 0x52fc0
movl (%rax), %ebx
incl %ebx
movl %ebx, (%rax)
jmp 0x7f50
movq %r15, %rdi
callq 0x8378
movq %rax, %r12
movq (%rbp), %rdi
movq 0x10(%rax), %rdx
xorl %ebx, %ebx
leaq 0x37e15(%rip), %rsi     # 0x3fe30
xorl %eax, %eax
callq 0x37f0
movq 0x4afbf(%rip), %rax     # 0x52fe8
movq (%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x4af88(%rip), %rax     # 0x52fc0
movslq (%rax), %rax
movq 0x8(%rsp), %rcx
movq (%rcx,%rax,8), %rdi
callq 0x8378
movq %rax, %r15
movl $0x2028, %edx           # imm = 0x2028
movq %r13, %rdi
xorl %esi, %esi
callq 0x3090
movq %r15, %rdi
movq %r13, %rsi
callq 0x83ce
movq %r13, %rdi
callq 0x84ad
incl %ebx
cmpl $0x63, 0x2050(%rsp)
jg 0x805b
movq %r15, %rdi
callq 0x8359
movq 0x10(%rsp), %rdi
leaq 0x37db1(%rip), %rsi     # 0x3fe3f
movl %ebx, %edx
xorl %eax, %eax
callq 0x37f0
pushq $0x1
popq %r15
leaq 0x18(%rsp), %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x83ce
movq (%rbp), %rdi
movq 0x18(%rsp), %rcx
leaq 0x37d9b(%rip), %rsi     # 0x3fe56
movl %r15d, %edx
xorl %eax, %eax
callq 0x37f0
movq (%rbp), %rdi
movl 0x28(%rsp), %ebx
leaq 0x37d93(%rip), %rsi     # 0x3fe67
movl %ebx, %edx
xorl %eax, %eax
callq 0x37f0
movq (%rbp), %rsi
leaq 0x37d92(%rip), %rdi     # 0x3fe7a
callq 0x3530
movq 0x2030(%rsp), %rdx
movzbl %dl, %eax
leal -0x1(%rax), %ecx
cmpl $0x6, %ecx
ja 0x811d
leaq 0x37cd9(%rip), %rsi     # 0x3fde0
movslq (%rsi,%rcx,4), %rax
addq %rsi, %rax
jmpq *%rax
movq (%rbp), %rsi
leaq 0x37d6d(%rip), %rdi     # 0x3fe88
jmp 0x8196
cmpl $0x1e, %eax
je 0x8164
cmpl $0x1f, %eax
je 0x814a
cmpl $0x20, %eax
jne 0x8299
movq (%rbp), %rsi
leaq 0x37d93(%rip), %rdi     # 0x3fece
jmp 0x8196
movq (%rbp), %rsi
leaq 0x37d53(%rip), %rdi     # 0x3fe9b
jmp 0x8196
movq (%rbp), %rsi
leaq 0x37d6e(%rip), %rdi     # 0x3fec3
jmp 0x8196
movq (%rbp), %rsi
leaq 0x37d32(%rip), %rdi     # 0x3fe94
jmp 0x8196
movq (%rbp), %rsi
leaq 0x37d4e(%rip), %rdi     # 0x3febd
jmp 0x8196
movq (%rbp), %rsi
leaq 0x37d58(%rip), %rdi     # 0x3fed4
jmp 0x8196
movq (%rbp), %rsi
leaq 0x37d05(%rip), %rdi     # 0x3fe8e
jmp 0x8196
movq (%rbp), %rsi
leaq 0x37d1e(%rip), %rdi     # 0x3feb4
callq 0x3530
movq (%rbp), %rdi
movq 0x30(%rsp), %rdx
leaq 0x37d4f(%rip), %rsi     # 0x3fefa
xorl %eax, %eax
callq 0x37f0
movq (%rbp), %rsi
leaq 0x37d54(%rip), %rdi     # 0x3ff11
callq 0x3530
cmpb $0x0, 0x40(%rsp)
je 0x81d7
movq (%rbp), %rsi
leaq 0x40(%rsp), %rdi
callq 0x3530
movq (%rbp), %rsi
leaq 0x36f7a(%rip), %rdi     # 0x3f15c
callq 0x3530
cmpl $0x64, %ebx
jl 0x8219
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x84ad
incl %r15d
jmp 0x80a0
movq (%rbp), %rdi
shrq $0x8, %rdx
leaq 0x37c93(%rip), %rsi     # 0x3fea3
xorl %eax, %eax
callq 0x37f0
jmp 0x819b
movq %r12, %rdi
jmp 0x7fe8
movq 0x4adc0(%rip), %rax     # 0x52fe8
movq (%rax), %rdi
leaq 0x37bce(%rip), %rsi     # 0x3fe00
movl $0x1fe8, %edx           # imm = 0x1FE8
movq %r14, %rcx
xorl %eax, %eax
callq 0x37f0
movb $0x0, 0x1fe8(%r14)
jmp 0x7fcb
xorl %eax, %eax
jmp 0x8260
cmpl $0x68, %r15d
jne 0x8272
callq 0x8531
pushq $0x1
popq %rax
addq $0x4068, %rsp           # imm = 0x4068
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x8531
leaq 0x37101(%rip), %rdi     # 0x3f37f
movl %r15d, %esi
xorl %eax, %eax
callq 0x82b8
leaq 0x37cf9(%rip), %rdi     # 0x3ff88
movq %r12, %rsi
xorl %eax, %eax
callq 0x82b8
leaq 0x37c3b(%rip), %rdi     # 0x3fedb
movq %rdx, %rsi
jmp 0x8292
callq 0x8531
leaq 0x370e8(%rip), %rdi     # 0x3f399
xorl %eax, %eax
callq 0x82b8