// File: /workspace/github_c_makefile_star2_O1/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, %r13d
movq %rsi, %r12
movq %rdi, %rbx
callq 0x16fc
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %r14d, %r14d
jle 0x2197
testl %ecx, %ecx
je 0x2197
movl $0xff, %ebp
cmpl %ebp, %r14d
cmovll %r14d, %ebp
movslq 0xc(%rbx), %rax
movq %rax, 0x10(%rsp)
movslq 0x10(%rbx), %rax
movq %rax, 0x18(%rsp)
movl 0x14(%rbx), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x14(%rbx)
movq 0x18(%rbx), %rdi
leal 0x4(,%rax,4), %esi
callq 0x1c08
movq %rax, 0x18(%rbx)
movq 0x20(%rbx), %rdi
movl 0x14(%rbx), %esi
shll $0x2, %esi
callq 0x1c08
movq %rax, 0x20(%rbx)
movq 0x28(%rbx), %rdi
movl 0x14(%rbx), %esi
shll $0x2, %esi
callq 0x1c08
movq %rax, 0x28(%rbx)
movq 0x30(%rbx), %rdi
movl 0x14(%rbx), %esi
callq 0x1c08
movq %rax, 0x30(%rbx)
movl 0xc(%rbx), %ecx
movq 0x18(%rbx), %rdx
movslq 0x14(%rbx), %rsi
movl %ecx, -0x4(%rdx,%rsi,4)
addl %ebp, 0xc(%rbx)
movq 0x20(%rbx), %rcx
movslq 0x14(%rbx), %rdx
movl %ebp, -0x4(%rcx,%rdx,4)
movq 0x28(%rbx), %rcx
movslq 0x14(%rbx), %rdx
movl %r15d, -0x4(%rcx,%rdx,4)
movslq 0x14(%rbx), %rsi
movl %r13d, 0x8(%rsp)
movb %r13b, -0x1(%rax,%rsi)
addl $0x20, 0x10(%rbx)
movl $0x0, 0x64(%rbx)
movq 0x38(%rbx), %rdi
shll $0x5, %esi
callq 0x1c08
movq %rax, 0x38(%rbx)
movslq 0x14(%rbx), %r13
movl %r13d, %ecx
shll $0x5, %ecx
addl $-0x20, %ecx
movslq %ecx, %rcx
leaq (%rax,%rcx), %r14
xorps %xmm0, %xmm0
movups %xmm0, (%rax,%rcx)
movups %xmm0, 0x10(%rax,%rcx)
movq %r12, %rdi
callq 0x10d0
cmpl $0x9, %eax
jg 0x2044
movq %r14, %rdi
movq %r12, %rsi
movq %rax, %rdx
jmp 0x204f
movl $0xa, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x1050
movq 0x30(%rbx), %rax
movb -0x1(%rax,%r13), %al
movb %al, 0xb(%r14)
xorl %eax, %eax
movl 0x8(%rsp), %r12d
cmpb $0x43, %r12b
cmovel %eax, %r15d
movb %bpl, 0x10(%r14)
movb %r15b, 0x11(%r14)
movq 0x48(%rbx), %rdi
movl 0xc(%rbx), %esi
callq 0x1c08
movq %rax, 0x48(%rbx)
cmpl $0x0, 0x60(%rbx)
jne 0x2192
movslq 0xc(%rbx), %rdi
callq 0x1160
movsbl %r12b, %edx
addl $-0x44, %edx
rorl %edx
leal (,%rdx,8), %ecx
movabsq $0x2a3f20202a30, %rsi   # imm = 0x2A3F20202A30
shrq %cl, %rsi
movq %rax, %r15
cmpl $0x6, %edx
movl $0x20, %eax
cmovbl %esi, %eax
movslq 0x8(%rbx), %r12
testq %r12, %r12
movq 0x10(%rsp), %rcx
jle 0x2172
movq %r15, %rdx
addq %rcx, %rdx
movq %rdx, 0x8(%rsp)
movl %ebp, %edx
movq %rdx, 0x20(%rsp)
leaq 0x1(%r12), %rbp
decq %r12
imulq %rcx, %r12
addq 0x18(%rsp), %r12
movzbl %al, %r14d
leaq -0x2(%rbp), %r13
movq (%rbx), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x1180
movq (%rbx), %rcx
movl $0x1, %edx
movq %r15, %rdi
movq 0x10(%rsp), %rsi
callq 0x10a0
movq 0x8(%rsp), %rdi
movl %r14d, %esi
movq 0x20(%rsp), %rdx
callq 0x10f0
movslq 0xc(%rbx), %rax
imulq %r13, %rax
movslq 0x10(%rbx), %rsi
addq %rax, %rsi
movq (%rbx), %rdi
xorl %edx, %edx
callq 0x1180
movslq 0xc(%rbx), %rsi
movq (%rbx), %rcx
movl $0x1, %edx
movq %r15, %rdi
callq 0x10a0
decq %rbp
subq 0x10(%rsp), %r12
cmpq $0x1, %rbp
jg 0x20ff
movq %r15, %rdi
callq 0x1030
movl $0x1, 0x60(%rbx)
movq %rbx, %rdi
callq 0x1590
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