// File: /workspace/github_c_makefile_star2_O2/bmottershead[P]DBF2TSV/dbf2tsv.c
// Function: main
int main(int argc, char **argv){
  DBFHandle dbf_file = NULL; 
  int       width, decimals, i, r;
  char      title[12];
  char      fmt[12];

  // Check that there is one argument, the input filename
  if (argc!=2) {
    fprintf(stderr, "Usage: dbf2tsv dbf-file\n");
    return EXIT_FAILURE;
  }

  // Open the DBF file.
  dbf_file = DBFOpen(argv[1], "rb");
  if (dbf_file == NULL) {
    fprintf(stderr, "%s can't be read or is not a DBF file\n", argv[1]);
    return EXIT_FAILURE;
  }
    
  // Header row. Prints names of fields, tab-separated.
  for (i = 0; i < DBFGetFieldCount(dbf_file); i++ ) {
    DBFGetFieldInfo(dbf_file, i, title, &width, &decimals);
    if (i>0)
      printf(FS);
    printf("%s", title);
  }
  printf(RS);

  // Data rows. Prints values of fields, tab-separated.
  for (r = 0; r < DBFGetRecordCount(dbf_file); r++) {
    for (i = 0; i < DBFGetFieldCount(dbf_file); i++) {
      if (i>0)
        printf(FS);
      if (!DBFIsAttributeNULL(dbf_file, r, i)) {
        switch (DBFGetFieldInfo(dbf_file, i, title, &width, &decimals)) {
        case FTString:
          // String values not quoted, which will be a problem if 
          // a field value includes a tab.
          printf("%s", DBFReadStringAttribute(dbf_file,r,i));
          break;
        case FTInteger:
          printf("%d", DBFReadIntegerAttribute(dbf_file,r,i));
          break;
        case FTDouble:
          sprintf(fmt,"%%%d.%df",width, decimals);
          printf(fmt, DBFReadDoubleAttribute(dbf_file,r,i));
          break;
        default:
          break;
        }
      }
    }
    printf(RS);
    fflush( stdout );
  }

  // Finished
  DBFClose(dbf_file);
  return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
cmpl $0x2, %edi
jne 0x14c9
movq %rsi, %r14
movq 0x8(%rsi), %rdi
leaq 0x2cfc(%rip), %rsi      # 0x401d
callq 0x1709
testq %rax, %rax
je 0x14e7
movq %rax, %rbx
leaq 0x10(%rsp), %r14
leaq 0xc(%rsp), %r15
leaq 0x8(%rsp), %r12
leaq 0x2cff(%rip), %r13      # 0x4047
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0x22ba
cmpl %eax, %ebp
jge 0x1386
movq %rbx, %rdi
movl %ebp, %esi
movq %r14, %rdx
movq %r15, %rcx
movq %r12, %r8
callq 0x22c2
testl %ebp, %ebp
je 0x1375
pushq $0x9
popq %rdi
callq 0x1040
movq %r13, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x10e0
incl %ebp
jmp 0x134a
pushq $0xa
popq %rdi
callq 0x1040
leaq 0xc(%rsp), %r15
leaq 0x8(%rsp), %r12
pushq $0x9
popq %r13
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0x22be
cmpl %eax, %ebp
jge 0x14b0
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0x22ba
cmpl %eax, %r14d
jge 0x1492
testl %r14d, %r14d
je 0x13cf
movl %r13d, %edi
callq 0x1040
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0x2218
testl %eax, %eax
je 0x13e5
incl %r14d
jmp 0x13b1
movq %rbx, %rdi
movl %r14d, %esi
leaq 0x10(%rsp), %rdx
movq %r15, %rcx
movq %r12, %r8
callq 0x22c2
cmpl $0x2, %eax
je 0x1448
cmpl $0x1, %eax
je 0x1429
testl %eax, %eax
jne 0x13e0
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0x2208
leaq 0x2c2a(%rip), %rdi      # 0x4047
movq %rax, %rsi
xorl %eax, %eax
callq 0x10e0
jmp 0x13e0
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0x20c1
leaq 0x2c5d(%rip), %rdi      # 0x409a
movl %eax, %esi
xorl %eax, %eax
callq 0x10e0
jmp 0x13e0
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %ecx
movq %r12, %r13
movq %r15, %r12
leaq 0x1c(%rsp), %r15
movq %r15, %rdi
leaq 0x2be5(%rip), %rsi      # 0x404a
xorl %eax, %eax
callq 0x11d0
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0x21ef
movq %r15, %rdi
movq %r12, %r15
movq %r13, %r12
pushq $0x9
popq %r13
movb $0x1, %al
callq 0x10e0
jmp 0x13e0
pushq $0xa
popq %rdi
callq 0x1040
movq 0x4b37(%rip), %rax      # 0x5fd8
movq (%rax), %rdi
callq 0x1170
incl %ebp
jmp 0x139e
movq %rbx, %rdi
callq 0x1b7a
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4b28(%rip), %rax      # 0x5ff8
movq (%rax), %rcx
leaq 0x2b2a(%rip), %rdi      # 0x4004
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
callq 0x11e0
jmp 0x1503
movq 0x4b0a(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movq 0x8(%r14), %rdx
leaq 0x2b24(%rip), %rsi      # 0x4020
xorl %eax, %eax
callq 0x1140
pushq $0x1
popq %rax
jmp 0x14ba