// File: /workspace/github_c_makefile_star2_O2/bmottershead[P]DBF2TSV/dbf.c
// Function: DBFAddNativeFieldType
int  DBFAddNativeFieldType(DBFHandle psDBF, const char *pszFieldName,
                           char chType, int nWidth, int nDecimals) {
  char *pszFInfo;
  int i;
  int nOldRecordLength, nOldHeaderLength;
  char *pszRecord;
  char chFieldFill;
  unsigned long nRecordOffset;

  /* make sure that everything is written in .dbf */
  if (!DBFFlushRecord(psDBF))
    return -1;

  /* Do some checking to ensure we can add records to this file. */
  if (nWidth < 1)
    return -1;

  if (nWidth > 255)
    nWidth = 255;
  nOldRecordLength = psDBF->nRecordLength;
  nOldHeaderLength = psDBF->nHeaderLength;

  /* SfRealloc all the arrays larger to hold the additional fields */
  psDBF->nFields++;
  psDBF->panFieldOffset = (int *)SfRealloc(psDBF->panFieldOffset, sizeof(int) * psDBF->nFields);
  psDBF->panFieldSize = (int *) SfRealloc(psDBF->panFieldSize, sizeof(int) * psDBF->nFields);
  psDBF->panFieldDecimals = (int *)SfRealloc(psDBF->panFieldDecimals, sizeof(int) * psDBF->nFields);
  psDBF->pachFieldType = (char *)SfRealloc(psDBF->pachFieldType, sizeof(char) * psDBF->nFields);

  /* Assign the new field information fields. */
  psDBF->panFieldOffset[psDBF->nFields - 1] = psDBF->nRecordLength;
  psDBF->nRecordLength += nWidth;
  psDBF->panFieldSize[psDBF->nFields - 1] = nWidth;
  psDBF->panFieldDecimals[psDBF->nFields - 1] = nDecimals;
  psDBF->pachFieldType[psDBF->nFields - 1] = chType;

  /* Extend the required header information. */
  psDBF->nHeaderLength += 32;
  psDBF->bUpdated = FALSE;
  psDBF->pszHeader = (char *) SfRealloc(psDBF->pszHeader, psDBF->nFields * 32);
  pszFInfo = psDBF->pszHeader + 32 * (psDBF->nFields - 1);
  for (i = 0; i < 32; i++)
    pszFInfo[i] = '\0';
  if ((int) strlen(pszFieldName) < 10)
    strncpy(pszFInfo, pszFieldName, strlen(pszFieldName));
  else
    strncpy(pszFInfo, pszFieldName, 10);
  pszFInfo[11] = psDBF->pachFieldType[psDBF->nFields - 1];
  if (chType == 'C') {
    pszFInfo[16] = (unsigned char) (nWidth % 256);
    pszFInfo[17] = (unsigned char) (nWidth / 256);
  } else {
    pszFInfo[16] = (unsigned char) nWidth;
    pszFInfo[17] = (unsigned char) nDecimals;
  }

  /* Make the current record buffer appropriately larger. */
  psDBF->pszCurrentRecord = (char *) SfRealloc(psDBF->pszCurrentRecord,psDBF->nRecordLength);

  /* we're done if dealing with new .dbf */
  if (psDBF->bNoHeader)
    return (psDBF->nFields - 1);

  /* For existing .dbf file, shift records */
  pszRecord = (char *) malloc(sizeof(char) * psDBF->nRecordLength);
  chFieldFill = DBFGetNullCharacter(chType);

  for (i = psDBF->nRecords - 1; i >= 0; --i) {
    nRecordOffset = nOldRecordLength * (unsigned long) i + nOldHeaderLength;
    fseek(psDBF->fp, nRecordOffset, 0);
    fread(pszRecord, nOldRecordLength, 1, psDBF->fp);
    memset(pszRecord + nOldRecordLength, chFieldFill, nWidth);
    nRecordOffset = psDBF->nRecordLength * (unsigned long) i + psDBF->nHeaderLength;
    fseek(psDBF->fp, nRecordOffset, 0);
    fread(pszRecord, psDBF->nRecordLength, 1, psDBF->fp);
  }

  /* force update of header with new header, record length and new field */
  free(pszRecord);
  psDBF->bNoHeader = TRUE;
  DBFUpdateHeader(psDBF);
  psDBF->nCurrentRecord = -1;
  psDBF->bCurrentRecordModified = FALSE;
  return (psDBF->nFields - 1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r8d, %r15d
movl %ecx, %r14d
movl %edx, %ebp
movq %rsi, %r12
movq %rdi, %rbx
callq 0x1672
movl %eax, %ecx
pushq $-0x1
popq %rax
testl %r14d, %r14d
jle 0x208f
testl %ecx, %ecx
je 0x208f
movl $0xff, %r13d
cmpl %r13d, %r14d
cmovbl %r14d, %r13d
movslq 0xc(%rbx), %rax
movq %rax, 0x18(%rsp)
movslq 0x10(%rbx), %rax
movq %rax, 0x8(%rsp)
movl 0x14(%rbx), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x14(%rbx)
movq 0x18(%rbx), %rdi
leal 0x4(,%rax,4), %esi
callq 0x1b66
movq %rax, 0x18(%rbx)
movq 0x20(%rbx), %rdi
movl 0x14(%rbx), %esi
shll $0x2, %esi
callq 0x1b66
movq %rax, 0x20(%rbx)
movq 0x28(%rbx), %rdi
movl 0x14(%rbx), %esi
shll $0x2, %esi
callq 0x1b66
movq %rax, 0x28(%rbx)
movq 0x30(%rbx), %rdi
movl 0x14(%rbx), %esi
callq 0x1b66
movq %rax, 0x30(%rbx)
movl 0xc(%rbx), %ecx
movq 0x18(%rbx), %rdx
movslq 0x14(%rbx), %rsi
movl %ecx, -0x4(%rdx,%rsi,4)
addl %r13d, %ecx
movl %ecx, 0xc(%rbx)
movq 0x20(%rbx), %rcx
movslq 0x14(%rbx), %rdx
movl %r13d, -0x4(%rcx,%rdx,4)
movq 0x28(%rbx), %rcx
movslq 0x14(%rbx), %rdx
movl %r15d, -0x4(%rcx,%rdx,4)
movslq 0x14(%rbx), %rsi
movl %ebp, 0x10(%rsp)
movb %bpl, -0x1(%rax,%rsi)
addl $0x20, 0x10(%rbx)
andl $0x0, 0x64(%rbx)
movq 0x38(%rbx), %rdi
shll $0x5, %esi
callq 0x1b66
movq %rax, 0x38(%rbx)
movslq 0x14(%rbx), %rbp
movl %ebp, %ecx
shll $0x5, %ecx
addl $-0x20, %ecx
movslq %ecx, %r14
addq %rax, %r14
xorl %eax, %eax
cmpq $0x20, %rax
je 0x1f50
movb $0x0, (%r14,%rax)
incq %rax
jmp 0x1f40
movq %r12, %rdi
callq 0x10d0
cmpl $0x9, %eax
jg 0x1f68
movq %r14, %rdi
movq %r12, %rsi
movq %rax, %rdx
jmp 0x1f71
pushq $0xa
popq %rdx
movq %r14, %rdi
movq %r12, %rsi
callq 0x1050
movq 0x30(%rbx), %rax
movb -0x1(%rax,%rbp), %al
movb %al, 0xb(%r14)
xorl %eax, %eax
movl 0x10(%rsp), %ebp
cmpb $0x43, %bpl
cmovel %eax, %r15d
movb %r13b, 0x10(%r14)
movb %r15b, 0x11(%r14)
movq 0x48(%rbx), %rdi
movl 0xc(%rbx), %esi
callq 0x1b66
movq %rax, 0x48(%rbx)
cmpl $0x0, 0x60(%rbx)
jne 0x208a
movslq 0xc(%rbx), %rdi
callq 0x1160
movq %rax, %r15
movsbl %bpl, %edi
callq 0x209e
movl 0x8(%rbx), %r12d
movq %r15, %rdx
movq 0x18(%rsp), %rcx
addq %rcx, %rdx
movq %rdx, 0x10(%rsp)
movl %r13d, %edx
movq %rdx, 0x20(%rsp)
leaq -0x1(%r12), %rbp
imulq %rcx, %rbp
addq 0x8(%rsp), %rbp
movzbl %al, %eax
movl %eax, 0x8(%rsp)
movq 0x18(%rsp), %r14
pushq $0x1
popq %r13
testl %r12d, %r12d
jle 0x206a
decq %r12
movq (%rbx), %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x1180
movq (%rbx), %rcx
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x10a0
movq 0x10(%rsp), %rdi
movl 0x8(%rsp), %esi
movq 0x20(%rsp), %rdx
callq 0x10f0
movslq 0xc(%rbx), %rax
imulq %r12, %rax
movslq 0x10(%rbx), %rsi
addq %rax, %rsi
movq (%rbx), %rdi
xorl %edx, %edx
callq 0x1180
movslq 0xc(%rbx), %rsi
movq (%rbx), %rcx
movq %r15, %rdi
movq %r13, %rdx
callq 0x10a0
subq %r14, %rbp
jmp 0x2001
movq %r15, %rdi
callq 0x1030
movl $0x1, 0x60(%rbx)
movq %rbx, %rdi
callq 0x1508
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x40(%rbx)
movl 0x14(%rbx), %eax
decl %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq