// File: /workspace/github_c_makefile_star2_O3/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, 0x4(%rsp)
movq %rdi, %r15
movq %rsi, 0x8(%rsp)
movq (%rsi), %rdi
testq %rdi, %rdi
jne 0x18e9
movl $0x1e168, %edi          # imm = 0x1E168
callq 0x1150
movq %rax, %rdi
movq 0x8(%rsp), %rax
movq %rdi, (%rax)
xorl %ebp, %ebp
movl $0x1e168, %edx          # imm = 0x1E168
xorl %esi, %esi
callq 0x10d0
movq %r15, %rdi
callq 0x11e0
movl %eax, %r12d
shll $0x18, %r12d
je 0x1a31
movl %eax, %ebx
movl $0x0, (%rsp)
movl $0x1, %r14d
xorl %r13d, %r13d
xorl %ebp, %ebp
movq %r15, 0x10(%rsp)
movq %r15, %rdi
callq 0x1130
testl %eax, %eax
jne 0x1a23
cmpl $0xfff, %r13d           # imm = 0xFFF
jg 0x1a23
cmpl $0x1d, %ebp
jg 0x1a23
movslq %ebp, %rax
cmpl $0xa000000, %r12d       # imm = 0xA000000
je 0x1960
cmpl $0x9000000, %r12d       # imm = 0x9000000
jne 0x19a6
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
imulq $0x100c, %rax, %rax     # imm = 0x100C
movl %r14d, (%rcx,%rax)
movl %r13d, 0x4(%rcx,%rax)
cmpl $0x2, %r14d
jne 0x1985
movl (%rsp), %edx
movl %edx, 0x8(%rcx,%rax)
incl %ebp
cmpl $0xa000000, %r12d       # imm = 0xA000000
je 0x1a2c
xorl %r13d, %r13d
movl $0x1, %r14d
movl $0x0, (%rsp)
jmp 0x1a0c
movl %r12d, %r15d
sarl $0x18, %r15d
movslq %r13d, %rcx
incl %r13d
imulq $0x100c, %rax, %rax     # imm = 0x100C
movq 0x8(%rsp), %rdx
addq (%rdx), %rax
movb %bl, 0xc(%rcx,%rax)
callq 0x11f0
movq (%rax), %rax
movslq %r15d, %rcx
testb $0x8, 0x1(%rax,%rcx,2)
jne 0x19f6
cmpl $0x1, %r14d
movq 0x10(%rsp), %r15
jne 0x1a09
xorl %r14d, %r14d
cmpl $0x2e000000, %r12d      # imm = 0x2E000000
sete %r14b
addl %r14d, %r14d
jmp 0x1a0c
xorl %eax, %eax
cmpl $0x2, %r14d
sete %al
addl %eax, (%rsp)
movq 0x10(%rsp), %r15
jmp 0x1a0c
xorl %r14d, %r14d
movq %r15, %rdi
callq 0x11e0
movl %eax, %ebx
movl %eax, %r12d
shll $0x18, %r12d
jne 0x1925
cmpl $0x1000, %r13d          # imm = 0x1000
jge 0x1a60
cmpl $0x1e, %ebp
jge 0x1a42
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x45af(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x26ff(%rip), %rsi      # 0x4152
movl 0x4(%rsp), %edx
xorl %eax, %eax
callq 0x1120
jmp 0x1a31
movq 0x4591(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x26b9(%rip), %rsi      # 0x412a
movl 0x4(%rsp), %edx
xorl %eax, %eax
callq 0x1120
jmp 0x1a2c