// File: /workspace/github_c_makefile_star2_O0/bmottershead[P]DBF2TSV/tsv2dbf.c
// Function: main
int main( int argc, char ** argv ) {
  FILE*     tsv_file = NULL;
  DBFHandle dbf_file = NULL;
  int       num_columns, i, j, rows=0, values=0;
  column    *columns = NULL;
  column    *fields = NULL;
  char      c;

  // Check that there are two arguments, the input TSV file and
  // the output DBF file.
  if (argc!=3) {
    fprintf(stderr, "Usage: tsv2dbf tsv-file dbf-file\n");
    return EXIT_FAILURE;
  }
  
  // Open DBF file
  dbf_file = DBFCreate(argv[2]);
  if (dbf_file == NULL) {
    fprintf(stderr, "%s file cannot be created\n", argv[2]);
    return EXIT_FAILURE;
  }

  // Open TSV file.
  tsv_file = fopen(argv[1],"r");
  if (tsv_file == NULL) {
    fprintf(stderr, "%s cannot be opened\n", argv[1]);
    DBFClose(dbf_file);
    return EXIT_FAILURE;
  }

  // Read  header row of TSV file for titles.
  num_columns = get_columns(tsv_file, &fields, 1);
  if (num_columns <= 0) {
    fprintf(stderr, "%s can't be read or is not a DBF file\n", argv[1]);
    return EXIT_FAILURE;
  }

  // Initialize type, widths in header array
  for (j=0; j<num_columns; j++) {
    fields[j].type=FTInteger;
    fields[j].width=0;
    fields[j].decimals=0;
  }

  // Loop through TSV data rows, determining the 
  // most restrictive data type for each column which will
  // permit all the actual TSV values to be loaded.
  for (i=1; !feof(tsv_file); i++) {
    int num_field_columns = get_columns(tsv_file, &columns, i+1);

    if (num_field_columns==0)
      continue;
    if (num_field_columns != num_columns) {
      fprintf(stderr, "Wrong number of fields at row %d. Row ignored.", i+1);
      continue;
    } 
    for (j=0; j<num_columns; j++) {
      fields[j].width=max(columns[j].width, fields[j].width);
      switch(columns[j].type) {
      case FTString:
        fields[j].type=FTString;
        break;
      case FTDouble:
        if (fields[j].type==FTInteger) {
          fields[j].type=FTDouble;
          fields[j].decimals=columns[j].decimals;
        } else if (fields[j].type==FTDouble) {
          fields[j].decimals=max(columns[j].decimals, fields[j].decimals);
        } 
        break;
      default:
        break;
      }
    }
  }

  // Make columns that had all empty values into strings.
  for (j=0; j<num_columns; j++) {
    if (fields[j].width==0) {
      fields[j].type=FTString;
      fields[j].width=1;
    }
  }
  
  // Define the fields of the  DBF file.
  for (i=0; i<num_columns; i++) {
    int ret=DBFAddField(dbf_file, fields[i].value, fields[i].type, 
                fields[i].width, fields[i].decimals);
    if (ret==-1) 
      fprintf(stderr,"Error adding field %d to DBF file\n",i);
  }

  // Reset TSV file and skip over header row.
  fseek(tsv_file, 0, SEEK_SET);
  while((c = getc(tsv_file)) != RS) { /* empty loop */}

  // Read the data rows and write the column values into the DBF file.
  for (i=0; !feof(tsv_file); i++) {
    int num_field_columns = get_columns(tsv_file, &columns, i+1);

    if (num_field_columns != num_columns)
      continue;

    rows++;
    for (j=0; j<num_columns; j++) {
      int ret=0;

      if (columns[j].value[0]==0)
        continue;

      if (debug==2)
        dump_column("row", i, j, &columns[j]);

      values++;
      switch (fields[j].type) {
      case FTInteger:
        ret=DBFWriteIntegerAttribute(dbf_file, i, j, atoi(columns[j].value));
        break;
      case FTDouble:
        ret=DBFWriteDoubleAttribute(dbf_file, i, j, atof(columns[j].value));
        break;
      case FTString:
        ret=DBFWriteStringAttribute(dbf_file, i, j, columns[j].value);
        break;
      // These won't occur, but are mentioned to avoid a compiler warning.
      case FTLogical:
      case FTInvalid:
        break;
      }
      if (ret==0) 
        fprintf(stderr,"Error writing column %d of row %d\n",j,rows);
    }
  }


  // Some information on stderr
  for (i=0; i<DBFGetFieldCount(dbf_file); i++) {
    int width, decimals; 
    char title[12];
    DBFFieldType type = DBFGetFieldInfo(dbf_file, i, title, &width, &decimals);
    fprintf(stderr, "%d %s %s %d.%d\n", i, title, type_to_str(type), width, decimals);
  }
  fprintf(stderr, "Data rows: %d, Non-null values: %d\n", rows, values);

  // Finish
  free(columns);
  free(fields);
  DBFClose(dbf_file);
  return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
cmpl $0x3, -0x8(%rbp)
je 0x1361
movq 0x6cb4(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
leaq 0x4cde(%rip), %rsi      # 0x602c
movb $0x0, %al
callq 0x1110
movl $0x1, -0x4(%rbp)
jmp 0x1b20
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x29f0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x13a5
movq 0x6c78(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x4cbc(%rip), %rsi      # 0x604e
movb $0x0, %al
callq 0x1110
movl $0x1, -0x4(%rbp)
jmp 0x1b20
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x4e02(%rip), %rsi      # 0x61b6
callq 0x1190
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x13f9
movq 0x6c2d(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x4c8c(%rip), %rsi      # 0x6069
movb $0x0, %al
callq 0x1110
movq -0x20(%rbp), %rdi
callq 0x2920
movl $0x1, -0x4(%rbp)
jmp 0x1b20
movq -0x18(%rbp), %rdi
leaq -0x48(%rbp), %rsi
movl $0x1, %edx
callq 0x1b30
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jg 0x1440
movq 0x6bdd(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x4c51(%rip), %rsi      # 0x607e
movb $0x0, %al
callq 0x1110
movl $0x1, -0x4(%rbp)
jmp 0x1b20
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x14a4
movq -0x48(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x100c, %rcx, %rcx     # imm = 0x100C
addq %rcx, %rax
movl $0x1, (%rax)
movq -0x48(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x100c, %rcx, %rcx     # imm = 0x100C
addq %rcx, %rax
movl $0x0, 0x4(%rax)
movq -0x48(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x100c, %rcx, %rcx     # imm = 0x100C
addq %rcx, %rax
movl $0x0, 0x8(%rax)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1447
movl $0x1, -0x28(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1120
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x14c5
jmp 0x16ee
movq -0x18(%rbp), %rdi
movl -0x28(%rbp), %edx
addl $0x1, %edx
leaq -0x40(%rbp), %rsi
callq 0x1b30
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x50(%rbp)
jne 0x14e6
jmp 0x16e0
movl -0x50(%rbp), %eax
cmpl -0x24(%rbp), %eax
je 0x1511
movq 0x6b03(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
movl -0x28(%rbp), %edx
addl $0x1, %edx
leaq 0x4ba0(%rip), %rsi      # 0x60a5
movb $0x0, %al
callq 0x1110
jmp 0x16e0
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x16de
movq -0x40(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x100c, %rcx, %rcx     # imm = 0x100C
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x48(%rbp), %rcx
movslq -0x2c(%rbp), %rdx
imulq $0x100c, %rdx, %rdx     # imm = 0x100C
addq %rdx, %rcx
cmpl 0x4(%rcx), %eax
jl 0x156a
movq -0x40(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x100c, %rcx, %rcx     # imm = 0x100C
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x78(%rbp)
jmp 0x1582
movq -0x48(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x100c, %rcx, %rcx     # imm = 0x100C
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x78(%rbp)
movl -0x78(%rbp), %edx
movq -0x48(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x100c, %rcx, %rcx     # imm = 0x100C
movl %edx, 0x4(%rax,%rcx)
movq -0x40(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x100c, %rcx, %rcx     # imm = 0x100C
movl (%rax,%rcx), %eax
movl %eax, -0x7c(%rbp)
testl %eax, %eax
je 0x15c0
jmp 0x15b3
movl -0x7c(%rbp), %eax
subl $0x2, %eax
je 0x15dd
jmp 0x16cc
movq -0x48(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x100c, %rcx, %rcx     # imm = 0x100C
addq %rcx, %rax
movl $0x0, (%rax)
jmp 0x16ce
movq -0x48(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x100c, %rcx, %rcx     # imm = 0x100C
addq %rcx, %rax
cmpl $0x1, (%rax)
jne 0x163b
movq -0x48(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x100c, %rcx, %rcx     # imm = 0x100C
addq %rcx, %rax
movl $0x2, (%rax)
movq -0x40(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x100c, %rcx, %rcx     # imm = 0x100C
addq %rcx, %rax
movl 0x8(%rax), %ecx
movq -0x48(%rbp), %rax
movslq -0x2c(%rbp), %rdx
imulq $0x100c, %rdx, %rdx     # imm = 0x100C
addq %rdx, %rax
movl %ecx, 0x8(%rax)
jmp 0x16ca
movq -0x48(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x100c, %rcx, %rcx     # imm = 0x100C
addq %rcx, %rax
cmpl $0x2, (%rax)
jne 0x16c8
movq -0x40(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x100c, %rcx, %rcx     # imm = 0x100C
addq %rcx, %rax
movl 0x8(%rax), %eax
movq -0x48(%rbp), %rcx
movslq -0x2c(%rbp), %rdx
imulq $0x100c, %rdx, %rdx     # imm = 0x100C
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
jl 0x1698
movq -0x40(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x100c, %rcx, %rcx     # imm = 0x100C
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x80(%rbp)
jmp 0x16b0
movq -0x48(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x100c, %rcx, %rcx     # imm = 0x100C
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x80(%rbp)
movl -0x80(%rbp), %ecx
movq -0x48(%rbp), %rax
movslq -0x2c(%rbp), %rdx
imulq $0x100c, %rdx, %rdx     # imm = 0x100C
addq %rdx, %rax
movl %ecx, 0x8(%rax)
jmp 0x16ca
jmp 0x16ce
jmp 0x16ce
jmp 0x16d0
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1518
jmp 0x16e0
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x14ab
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x1753
movq -0x48(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x100c, %rcx, %rcx     # imm = 0x100C
addq %rcx, %rax
cmpl $0x0, 0x4(%rax)
jne 0x1746
movq -0x48(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x100c, %rcx, %rcx     # imm = 0x100C
addq %rcx, %rax
movl $0x0, (%rax)
movq -0x48(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x100c, %rcx, %rcx     # imm = 0x100C
addq %rcx, %rax
movl $0x1, 0x4(%rax)
jmp 0x1748
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x16f5
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x17f8
movq -0x20(%rbp), %rdi
movq -0x48(%rbp), %rsi
movslq -0x28(%rbp), %rax
imulq $0x100c, %rax, %rax     # imm = 0x100C
addq %rax, %rsi
addq $0xc, %rsi
movq -0x48(%rbp), %rax
movslq -0x28(%rbp), %rcx
imulq $0x100c, %rcx, %rcx     # imm = 0x100C
addq %rcx, %rax
movl (%rax), %edx
movq -0x48(%rbp), %rax
movslq -0x28(%rbp), %rcx
imulq $0x100c, %rcx, %rcx     # imm = 0x100C
addq %rcx, %rax
movl 0x4(%rax), %ecx
movq -0x48(%rbp), %rax
movslq -0x28(%rbp), %r8
imulq $0x100c, %r8, %r8       # imm = 0x100C
addq %r8, %rax
movl 0x8(%rax), %r8d
callq 0x2d90
movl %eax, -0x54(%rbp)
cmpl $-0x1, -0x54(%rbp)
jne 0x17e8
movq 0x6824(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
movl -0x28(%rbp), %edx
leaq 0x48f3(%rip), %rsi      # 0x60d4
movb $0x0, %al
callq 0x1110
jmp 0x17ea
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x175a
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1160
movq -0x18(%rbp), %rdi
callq 0x11d0
movb %al, -0x49(%rbp)
movsbl %al, %eax
cmpl $0xa, %eax
je 0x181d
jmp 0x1807
movl $0x0, -0x28(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1120
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x183e
jmp 0x1a32
movq -0x18(%rbp), %rdi
movl -0x28(%rbp), %edx
addl $0x1, %edx
leaq -0x40(%rbp), %rsi
callq 0x1b30
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x24(%rbp), %eax
je 0x1861
jmp 0x1a24
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x1a22
movl $0x0, -0x5c(%rbp)
movq -0x40(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x100c, %rcx, %rcx     # imm = 0x100C
addq %rcx, %rax
movsbl 0xc(%rax), %eax
cmpl $0x0, %eax
jne 0x18a4
jmp 0x1a14
cmpl $0x2, 0x6861(%rip)      # 0x810c
jne 0x18d1
movl -0x28(%rbp), %esi
movl -0x2c(%rbp), %edx
movq -0x40(%rbp), %rcx
movslq -0x2c(%rbp), %rax
imulq $0x100c, %rax, %rax     # imm = 0x100C
addq %rax, %rcx
leaq 0x482b(%rip), %rdi      # 0x60f7
callq 0x1d60
movl -0x34(%rbp), %eax
incl %eax
movl %eax, -0x34(%rbp)
movq -0x48(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x100c, %rcx, %rcx     # imm = 0x100C
movl (%rax,%rcx), %eax
movq %rax, -0x88(%rbp)
subq $0x4, %rax
ja 0x19ee
movq -0x88(%rbp), %rax
leaq 0x46fa(%rip), %rcx      # 0x6004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x20(%rbp), %rax
movq %rax, -0x98(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0x90(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x8c(%rbp)
movq -0x40(%rbp), %rdi
movslq -0x2c(%rbp), %rax
imulq $0x100c, %rax, %rax     # imm = 0x100C
addq %rax, %rdi
addq $0xc, %rdi
callq 0x11a0
movq -0x98(%rbp), %rdi
movl -0x90(%rbp), %esi
movl -0x8c(%rbp), %edx
movl %eax, %ecx
callq 0x40b0
movl %eax, -0x5c(%rbp)
jmp 0x19ee
movq -0x20(%rbp), %rax
movq %rax, -0xa8(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0xa0(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x9c(%rbp)
movq -0x40(%rbp), %rdi
movslq -0x2c(%rbp), %rax
imulq $0x100c, %rax, %rax     # imm = 0x100C
addq %rax, %rdi
addq $0xc, %rdi
callq 0x10a0
movq -0xa8(%rbp), %rdi
movl -0xa0(%rbp), %esi
movl -0x9c(%rbp), %edx
callq 0x3be0
movl %eax, -0x5c(%rbp)
jmp 0x19ee
movq -0x20(%rbp), %rdi
movl -0x28(%rbp), %esi
movl -0x2c(%rbp), %edx
movq -0x40(%rbp), %rcx
movslq -0x2c(%rbp), %rax
imulq $0x100c, %rax, %rax     # imm = 0x100C
addq %rax, %rcx
addq $0xc, %rcx
callq 0x40f0
movl %eax, -0x5c(%rbp)
jmp 0x19ee
jmp 0x19ee
cmpl $0x0, -0x5c(%rbp)
jne 0x1a12
movq 0x65fd(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
movl -0x2c(%rbp), %edx
movl -0x30(%rbp), %ecx
leaq 0x46f0(%rip), %rsi      # 0x60fb
movb $0x0, %al
callq 0x1110
jmp 0x1a14
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1871
jmp 0x1a24
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x1824
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0xac(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3760
movl %eax, %ecx
movl -0xac(%rbp), %eax
cmpl %ecx, %eax
jge 0x1ae0
movq -0x20(%rbp), %rdi
movl -0x28(%rbp), %esi
leaq -0x70(%rbp), %rdx
leaq -0x60(%rbp), %rcx
leaq -0x64(%rbp), %r8
callq 0x37a0
movl %eax, -0x74(%rbp)
movq 0x657b(%rip), %rax      # 0x7ff8
movq (%rax), %rax
movq %rax, -0xc8(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0xbc(%rbp)
leaq -0x70(%rbp), %rax
movq %rax, -0xb8(%rbp)
movl -0x74(%rbp), %edi
callq 0x1df0
movq -0xc8(%rbp), %rdi
movl -0xbc(%rbp), %edx
movq -0xb8(%rbp), %rcx
movq %rax, %r8
movl -0x60(%rbp), %r9d
movl -0x64(%rbp), %eax
leaq 0x4656(%rip), %rsi      # 0x611e
movl %eax, (%rsp)
movb $0x0, %al
callq 0x1110
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x1a39
movq 0x6511(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
movl -0x30(%rbp), %edx
movl -0x34(%rbp), %ecx
leaq 0x4637(%rip), %rsi      # 0x612e
movb $0x0, %al
callq 0x1110
movq -0x40(%rbp), %rdi
callq 0x1030
movq -0x48(%rbp), %rdi
callq 0x1030
movq -0x20(%rbp), %rdi
callq 0x2920
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
nopl (%rax)