// File: /workspace/github_c_makefile_star2_O3/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 0x1573
movq %rsi, %r14
movq 0x8(%rsi), %rdi
leaq 0x2c7c(%rip), %rsi      # 0x401d
callq 0x1771
testq %rax, %rax
je 0x157a
movq %rax, %rbx
movq %rax, %rdi
callq 0x2379
testl %eax, %eax
jle 0x1412
leaq 0x10(%rsp), %r14
leaq 0xc(%rsp), %r15
leaq 0x8(%rsp), %r12
leaq 0x2c73(%rip), %r13      # 0x4047
xorl %ebp, %ebp
movq %rbx, %rdi
movl %ebp, %esi
movq %r14, %rdx
movq %r15, %rcx
movq %r12, %r8
callq 0x2381
testl %ebp, %ebp
je 0x13f7
movl $0x9, %edi
callq 0x1040
movq %r13, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x10e0
incl %ebp
movq %rbx, %rdi
callq 0x2379
cmpl %eax, %ebp
jl 0x13d6
movl $0xa, %edi
callq 0x1040
movq %rbx, %rdi
callq 0x237d
testl %eax, %eax
jle 0x155a
leaq 0x10(%rsp), %r14
leaq 0xc(%rsp), %r15
leaq 0x8(%rsp), %r12
xorl %r13d, %r13d
movq %rbx, %rdi
callq 0x2379
testl %eax, %eax
jle 0x152d
xorl %ebp, %ebp
testl %ebp, %ebp
je 0x145e
movl $0x9, %edi
callq 0x1040
movq %rbx, %rdi
movl %r13d, %esi
movl %ebp, %edx
callq 0x22ce
testl %eax, %eax
je 0x1482
incl %ebp
movq %rbx, %rdi
callq 0x2379
cmpl %eax, %ebp
jl 0x1450
jmp 0x152d
movq %rbx, %rdi
movl %ebp, %esi
movq %r14, %rdx
movq %r15, %rcx
movq %r12, %r8
callq 0x2381
cmpl $0x2, %eax
je 0x14e2
cmpl $0x1, %eax
je 0x14c3
testl %eax, %eax
jne 0x146f
movq %rbx, %rdi
movl %r13d, %esi
movl %ebp, %edx
callq 0x22ba
leaq 0x2b90(%rip), %rdi      # 0x4047
movq %rax, %rsi
xorl %eax, %eax
callq 0x10e0
jmp 0x146f
movq %rbx, %rdi
movl %r13d, %esi
movl %ebp, %edx
callq 0x216e
leaq 0x2bc3(%rip), %rdi      # 0x409a
movl %eax, %esi
xorl %eax, %eax
callq 0x10e0
jmp 0x146f
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %ecx
movq %r15, %r12
movq %r14, %r15
leaq 0x1c(%rsp), %r14
movq %r14, %rdi
leaq 0x2b4b(%rip), %rsi      # 0x404a
xorl %eax, %eax
callq 0x11d0
movq %rbx, %rdi
movl %r13d, %esi
movl %ebp, %edx
callq 0x229f
movq %r14, %rdi
movq %r15, %r14
movq %r12, %r15
leaq 0x8(%rsp), %r12
movb $0x1, %al
callq 0x10e0
jmp 0x146f
movl $0xa, %edi
callq 0x1040
movq 0x4a9a(%rip), %rax      # 0x5fd8
movq (%rax), %rdi
callq 0x1170
incl %r13d
movq %rbx, %rdi
callq 0x237d
cmpl %eax, %r13d
jl 0x143e
movq %rbx, %rdi
callq 0x1c0b
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1210
jmp 0x1586
addq $0x8, %r14
movq %r14, %rdi
callq 0x122c
movl $0x1, %eax
jmp 0x1564
nopl (%rax)