// File: /workspace/github_c_makefile_star2_O2/bmottershead[P]DBF2TSV/tsv2dbf.c
// Function: get_columns
int get_columns(FILE* tsv_file, column** columns, int row) {
  int n=0, width=0, decimals=0;
  char c = 0;
  DBFFieldType type = FTInteger;

  // Initialize columns array.
  if (*columns==NULL)
    *columns = malloc(MAX_COLUMNS*sizeof(column));
  memset(*columns, 0, MAX_COLUMNS*sizeof(column));

  // Split line of TSV file into columns and set type, decimals, and width.
  while ((c=getc(tsv_file)) 
         && !feof(tsv_file) 
         && width<MAX_COLUMN_WIDTH 
         && n<MAX_COLUMNS) {
    if (c==RS || c==FS) {
      (*columns)[n].type = type;
      (*columns)[n].width = width;
      if (type==FTDouble) 
        (*columns)[n].decimals = decimals;
      n++;
      type = FTInteger;
      width = 0;
      decimals = 0;
      if (c==RS)
        break;
    } else {
      (*columns)[n].value[width++] = c;
      if (isdigit(c)) {
        if (type==FTDouble)
          decimals++;
      } else {
        switch(type) {
        case FTInteger:
          type = (c=='.'? FTDouble: FTString);
          break;
        default:
          type = FTString;
        }
      }
    }
  }
  
  if (width>=MAX_COLUMN_WIDTH)
    fprintf(stderr,"Column exceeds maximum width at row %d\n", row);
  if (n>=MAX_COLUMNS)
    fprintf(stderr,"Too many columns at row %d\n", row);
  return  n;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, (%rsp)
movq %rdi, %r15
movq %rsi, 0x8(%rsp)
movq (%rsi), %rdi
testq %rdi, %rdi
jne 0x1790
movl $0x1e168, %edi          # imm = 0x1E168
callq 0x1150
movq %rax, %rdi
movq 0x8(%rsp), %rax
movq %rdi, (%rax)
movq $0x0, 0x10(%rsp)
movl $0x1e168, %edx          # imm = 0x1E168
xorl %esi, %esi
callq 0x10d0
pushq $0x1
popq %r14
xorl %ebp, %ebp
xorl %ebx, %ebx
movl $0x0, 0x4(%rsp)
movslq %ebx, %rbx
movq %r15, %rdi
callq 0x11e0
movl %eax, %r12d
shll $0x18, %r12d
je 0x18a8
movl %eax, %r13d
movq %r15, %rdi
callq 0x1130
testl %eax, %eax
jne 0x18a8
cmpl $0xfff, %ebx            # imm = 0xFFF
jg 0x18a8
cmpl $0x1d, %ebp
ja 0x18a8
cmpl $0xa000000, %r12d       # imm = 0xA000000
je 0x1863
cmpl $0x9000000, %r12d       # imm = 0x9000000
je 0x1863
movsbl %r13b, %r13d
movq 0x8(%rsp), %rax
movq (%rax), %rax
addq 0x10(%rsp), %rax
movb %r13b, 0xc(%rbx,%rax)
callq 0x11f0
movq (%rax), %rax
movslq %r13d, %rcx
testb $0x8, 0x1(%rax,%rcx,2)
jne 0x184f
xorl $0x1, %r14d
xorl $0x2e000000, %r12d      # imm = 0x2E000000
xorl %eax, %eax
orl %r14d, %r12d
sete %al
addl %eax, %eax
incq %rbx
movl %eax, %r14d
jmp 0x17b8
xorl %eax, %eax
cmpl $0x2, %r14d
sete %al
addl %eax, 0x4(%rsp)
incl %ebx
jmp 0x17b5
movq 0x8(%rsp), %rax
movq (%rax), %rax
movl %ebp, %ecx
imulq $0x100c, %rcx, %rcx     # imm = 0x100C
movl %r14d, (%rax,%rcx)
movl %ebx, 0x4(%rax,%rcx)
cmpl $0x2, %r14d
jne 0x188a
movl 0x4(%rsp), %edx
movl %edx, 0x8(%rax,%rcx)
incl %ebp
addq $0x100c, 0x10(%rsp)     # imm = 0x100C
cmpl $0xa000000, %r12d       # imm = 0xA000000
pushq $0x1
popq %r14
jne 0x17ab
jmp 0x18b0
cmpl $0x1000, %ebx           # imm = 0x1000
jge 0x18e3
cmpl $0x1e, %ebp
jge 0x18c6
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x572b(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x287b(%rip), %rsi      # 0x4152
movl (%rsp), %edx
xorl %eax, %eax
callq 0x1120
jmp 0x18b5
movq 0x570e(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x2836(%rip), %rsi      # 0x412a
movl (%rsp), %edx
xorl %eax, %eax
callq 0x1120
jmp 0x18b0