// File: /workspace/github_c_makefile_star2_O2/bmottershead[P]DBF2TSV/dbf.c
// Function: DBFAlterFieldDefn
int DBFAlterFieldDefn(DBFHandle psDBF, int iField, const char *pszFieldName,
                  char chType, int nWidth, int nDecimals) {
  int i;
  int iRecord;
  int nOffset;
  int nOldWidth;
  int nOldRecordLength;
  int nRecordOffset;
  char *pszFInfo;
  char chOldType;
  int bIsNULL;
  char chFieldFill;

  if (iField < 0 || iField >= psDBF->nFields)
    return FALSE;

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

  chFieldFill = DBFGetNullCharacter(chType);
  chOldType = psDBF->pachFieldType[iField];
  nOffset = psDBF->panFieldOffset[iField];
  nOldWidth = psDBF->panFieldSize[iField];
  nOldRecordLength = psDBF->nRecordLength;

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

  if (nWidth > 255)
    nWidth = 255;

  /* Assign the new field information fields. */
  psDBF->panFieldSize[iField] = nWidth;
  psDBF->panFieldDecimals[iField] = nDecimals;
  psDBF->pachFieldType[iField] = chType;

  /* Update the header information. */
  pszFInfo = psDBF->pszHeader + 32 * iField;
  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[iField];
  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;
  }

  /* Update offsets */
  if (nWidth != nOldWidth) {
    for (i = iField + 1; i < psDBF->nFields; i++)
      psDBF->panFieldOffset[i] += nWidth - nOldWidth;
    psDBF->nRecordLength += nWidth - nOldWidth;
    psDBF->pszCurrentRecord = (char *) SfRealloc(psDBF->pszCurrentRecord, psDBF->nRecordLength);
  }

  /* we're done if we're dealing with not yet created .dbf */
  if (psDBF->bNoHeader && psDBF->nRecords == 0)
    return TRUE;

  /* force update of header with new header and record length */
  psDBF->bNoHeader = TRUE;
  DBFUpdateHeader(psDBF);

  if (nWidth < nOldWidth || (nWidth == nOldWidth && chType != chOldType)) {
    char *pszRecord = (char *) malloc(sizeof(char) * nOldRecordLength);
    char *pszOldField = (char *) malloc(sizeof(char) * (nOldWidth + 1));

    pszOldField[nOldWidth] = 0;

    /* move records to their new positions */
    for (iRecord = 0; iRecord < psDBF->nRecords; iRecord++) {

      /* load record */
      nRecordOffset = nOldRecordLength * (unsigned long) iRecord + psDBF->nHeaderLength;
      fseek(psDBF->fp, nRecordOffset, 0);
      fread(pszRecord, nOldRecordLength, 1, psDBF->fp);
      memcpy(pszOldField, pszRecord + nOffset, nOldWidth);
      bIsNULL = DBFIsValueNULL(chOldType, pszOldField);

      if (nWidth != nOldWidth) {
        if ((chOldType == 'N' || chOldType == 'F') && pszOldField[0] == ' ') {
          /* Strip leading spaces when truncating a numeric field */
          memmove(pszRecord + nOffset,pszRecord + nOffset + nOldWidth - nWidth, nWidth);
        }
        if (nOffset + nOldWidth < nOldRecordLength) {
          memmove(pszRecord + nOffset + nWidth,
                  pszRecord + nOffset + nOldWidth,
                  nOldRecordLength - (nOffset + nOldWidth));
        }
      }

      /* Convert null value to the appropriate value of the new type */
      if (bIsNULL)
        memset(pszRecord + nOffset, chFieldFill, nWidth);

      /* write record */
      nRecordOffset = psDBF->nRecordLength * (unsigned long) iRecord + psDBF->nHeaderLength;
      fseek(psDBF->fp, nRecordOffset, 0);
      fwrite(pszRecord, psDBF->nRecordLength, 1,psDBF->fp);
    }

    free(pszRecord);
    free(pszOldField);
  } else if (nWidth > nOldWidth) {
    char *pszRecord = (char *) malloc(sizeof(char) * psDBF->nRecordLength);
    char *pszOldField = (char *) malloc(sizeof(char) * (nOldWidth + 1));

    pszOldField[nOldWidth] = 0;

    /* move records to their new positions */
    for (iRecord = psDBF->nRecords - 1; iRecord >= 0; iRecord--) {

      /* load record */
      nRecordOffset =  nOldRecordLength * (unsigned long) iRecord + psDBF->nHeaderLength;
      fseek(psDBF->fp, nRecordOffset, 0);
      fread(pszRecord, nOldRecordLength, 1, psDBF->fp);
      memcpy(pszOldField, pszRecord + nOffset, nOldWidth);
      bIsNULL = DBFIsValueNULL(chOldType, pszOldField);
      if (nOffset + nOldWidth < nOldRecordLength)
        memmove(pszRecord + nOffset + nWidth,pszRecord + nOffset + nOldWidth,
                nOldRecordLength - (nOffset + nOldWidth));

      /* Convert null value to the appropriate value of the new type */
      if (bIsNULL) {
        memset(pszRecord + nOffset, chFieldFill, nWidth);
      } else {
        if ((chOldType == 'N' || chOldType == 'F')) {
          /* Add leading spaces when expanding a numeric field */
          memmove(pszRecord + nOffset + nWidth - nOldWidth, pszRecord + nOffset, nOldWidth);
          memset(pszRecord + nOffset, ' ', nWidth - nOldWidth);
        } else {
          /* Add trailing spaces */
          memset(pszRecord + nOffset + nOldWidth, ' ', nWidth - nOldWidth);
        }
      }
      /* write record */
      nRecordOffset = psDBF->nRecordLength * (unsigned long) iRecord + psDBF->nHeaderLength;
      fseek(psDBF->fp, nRecordOffset, 0);
      fwrite(pszRecord, psDBF->nRecordLength, 1,psDBF->fp);
    }
    free(pszRecord);
    free(pszOldField);
  }
  psDBF->nCurrentRecord = -1;
  psDBF->bCurrentRecordModified = FALSE;
  return TRUE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
xorl %r12d, %r12d
testl %esi, %esi
js 0x34bc
movl %esi, %r14d
movq %rdi, %rbx
cmpl %esi, 0x14(%rdi)
jle 0x34bc
movl %r9d, %ebp
movl %r8d, %r13d
movq %rdx, %r15
movl %ecx, 0x10(%rsp)
movq %rbx, %rdi
callq 0x1672
testl %eax, %eax
je 0x34bc
movl 0x10(%rsp), %r12d
movsbl %r12b, %edi
callq 0x209e
movb %al, 0x1c(%rsp)
testl %r13d, %r13d
jle 0x309c
movq 0x30(%rbx), %rax
movl %r12d, %esi
movl %r14d, %r12d
movb (%rax,%r12), %cl
movb %cl, 0x7(%rsp)
movq 0x18(%rbx), %rcx
movq 0x20(%rbx), %rdx
movslq (%rcx,%r12,4), %rcx
movq %rcx, 0x8(%rsp)
movslq (%rdx,%r12,4), %rcx
movq %rcx, 0x20(%rsp)
movslq 0xc(%rbx), %rcx
movq %rcx, 0x28(%rsp)
movl $0xff, %ecx
cmpl %ecx, %r13d
cmovbl %r13d, %ecx
movl %ecx, %r13d
movl %ecx, (%rdx,%r12,4)
movq 0x28(%rbx), %rcx
movl %ebp, (%rcx,%r12,4)
movb %sil, (%rax,%r12)
shll $0x5, %r14d
addq 0x38(%rbx), %r14
xorl %eax, %eax
cmpq $0x20, %rax
je 0x3084
movb $0x0, (%r14,%rax)
incq %rax
jmp 0x3074
movq %r15, %rdi
callq 0x10d0
cmpl $0x9, %eax
jg 0x30a5
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
jmp 0x30ae
pushq $-0x1
popq %r12
jmp 0x34bc
pushq $0xa
popq %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x1050
xorl %eax, %eax
cmpb $0x43, 0x10(%rsp)
cmovel %eax, %ebp
movq 0x30(%rbx), %rax
movb (%rax,%r12), %al
movb %al, 0xb(%r14)
movb %r13b, 0x10(%r14)
movb %bpl, 0x11(%r14)
movq 0x20(%rsp), %r14
cmpl %r14d, %r13d
je 0x3107
movl %r13d, %esi
subl %r14d, %esi
incq %r12
cmpl %r12d, 0x14(%rbx)
jle 0x30f4
movq 0x18(%rbx), %rax
addl %esi, (%rax,%r12,4)
jmp 0x30e1
addl 0xc(%rbx), %esi
movl %esi, 0xc(%rbx)
movq 0x48(%rbx), %rdi
callq 0x1b66
movq %rax, 0x48(%rbx)
cmpl $0x0, 0x60(%rbx)
pushq $0x1
popq %r12
je 0x311b
cmpl $0x0, 0x8(%rbx)
je 0x34bc
movl $0x1, 0x60(%rbx)
movq %rbx, %rdi
callq 0x1508
cmpl %r14d, %r13d
jl 0x3312
cmpl %r14d, %r13d
setne %al
movl 0x10(%rsp), %ecx
cmpb %cl, 0x7(%rsp)
sete %cl
orb %al, %cl
je 0x3312
cmpl %r14d, %r13d
jle 0x34b3
movq 0x8(%rsp), %r12
movl %r12d, %r15d
movslq 0xc(%rbx), %rdi
callq 0x1160
movq %rax, %rbp
movq 0x20(%rsp), %rax
leaq 0x1(%rax), %rdi
callq 0x1160
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x38(%rsp)
movb $0x0, (%rcx,%rax)
movl 0x8(%rbx), %r14d
movq %rbp, 0x10(%rsp)
addq %rbp, %r12
movq 0x20(%rsp), %rax
leal (%r15,%rax), %edx
movq 0x20(%rsp), %r15
movl %r13d, %eax
movq %rax, 0x60(%rsp)
leaq (%r12,%rax), %rsi
movq %r12, 0x8(%rsp)
leaq (%r12,%r15), %rax
movq %rax, 0x40(%rsp)
movq 0x28(%rsp), %rcx
movl %ecx, %eax
movl %edx, 0x48(%rsp)
subl %edx, %eax
cltq
movq %rax, 0x50(%rsp)
subl %r15d, %r13d
movslq %r13d, %rax
movq %rax, 0x68(%rsp)
movq %rsi, 0x58(%rsp)
subq %r15, %rsi
movq %rsi, 0x70(%rsp)
leal -0x1(%r14), %edx
imull %ecx, %edx
pushq $0x1
popq %rbp
movb 0x7(%rsp), %al
movsbl %al, %ecx
movl %ecx, 0x30(%rsp)
andb $-0x9, %al
movb %al, 0x7(%rsp)
testl %r14d, %r14d
jle 0x349b
movl 0x10(%rbx), %eax
movl %edx, 0x34(%rsp)
addl %edx, %eax
movq (%rbx), %rdi
movslq %eax, %rsi
xorl %edx, %edx
callq 0x1180
movq (%rbx), %rcx
movq 0x10(%rsp), %rdi
movq 0x28(%rsp), %r13
movq %r13, %rsi
movq %rbp, %r12
movq %rbp, %rdx
callq 0x10a0
movq 0x38(%rsp), %rbp
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
movq %r15, %rdx
callq 0x1150
movl 0x30(%rsp), %edi
movq %rbp, %rsi
callq 0x224d
movl %eax, %r15d
cmpl 0x48(%rsp), %r13d
jle 0x327c
movq 0x58(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq 0x50(%rsp), %rdx
callq 0x11a0
testl %r15d, %r15d
movq %r12, %rbp
je 0x329f
movzbl 0x1c(%rsp), %esi
movq 0x8(%rsp), %rdi
movq 0x60(%rsp), %rdx
callq 0x10f0
movq 0x20(%rsp), %r15
jmp 0x32d7
cmpb $0x46, 0x7(%rsp)
movq 0x20(%rsp), %r15
jne 0x32c5
movq 0x70(%rsp), %rdi
movq 0x8(%rsp), %r13
movq %r13, %rsi
movq %r15, %rdx
callq 0x11a0
movq %r13, %rdi
jmp 0x32ca
movq 0x40(%rsp), %rdi
pushq $0x20
popq %rsi
movq 0x68(%rsp), %rdx
callq 0x10f0
decl %r14d
movl 0xc(%rbx), %eax
imull %r14d, %eax
addl 0x10(%rbx), %eax
movq (%rbx), %rdi
movslq %eax, %rsi
xorl %edx, %edx
callq 0x1180
movslq 0xc(%rbx), %rsi
movq (%rbx), %rcx
movq 0x10(%rsp), %rdi
movq %rbp, %rdx
callq 0x11e0
movl 0x34(%rsp), %edx
subl 0x28(%rsp), %edx
jmp 0x3203
movq 0x28(%rsp), %r15
movq %r15, %rdi
callq 0x1160
movq %rax, %r12
leaq 0x1(%r14), %rdi
callq 0x1160
movq %rax, 0x38(%rsp)
movb $0x0, (%rax,%r14)
movq %r12, 0x10(%rsp)
movq 0x8(%rsp), %rax
addq %rax, %r12
leaq (%r12,%r14), %rcx
movl %r13d, %edx
movq %rcx, 0x40(%rsp)
subq %rdx, %rcx
movq %rcx, 0x50(%rsp)
leal (%rax,%r14), %ecx
movq %r12, 0x8(%rsp)
movq %rdx, 0x48(%rsp)
leaq (%r12,%rdx), %rax
movq %rax, 0x60(%rsp)
movl %r15d, %eax
movl %ecx, 0x30(%rsp)
subl %ecx, %eax
cltq
movq %rax, 0x58(%rsp)
xorl %ebp, %ebp
pushq $0x1
popq %r14
movb 0x7(%rsp), %al
movsbl %al, %ecx
movl %ecx, 0x34(%rsp)
andb $-0x9, %al
movb %al, 0x7(%rsp)
movzbl 0x1c(%rsp), %eax
movl %eax, 0x1c(%rsp)
xorl %r15d, %r15d
cmpl 0x8(%rbx), %r15d
jge 0x349b
movl 0x10(%rbx), %eax
addl %ebp, %eax
movq (%rbx), %rdi
movslq %eax, %rsi
xorl %edx, %edx
callq 0x1180
movq (%rbx), %rcx
movq 0x10(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %r14, %rdx
callq 0x10a0
movq 0x38(%rsp), %r14
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movl %r13d, %r12d
movq 0x20(%rsp), %r13
movq %r13, %rdx
callq 0x1150
movl 0x34(%rsp), %edi
movq %r14, %rsi
callq 0x224d
movl %eax, %r14d
cmpl %r13d, %r12d
movl %r12d, %r13d
je 0x3448
cmpb $0x46, 0x7(%rsp)
jne 0x342a
movq 0x38(%rsp), %rax
cmpb $0x20, (%rax)
jne 0x342a
movq 0x8(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x48(%rsp), %rdx
callq 0x11a0
movl 0x30(%rsp), %eax
cmpl %eax, 0x28(%rsp)
jle 0x3448
movq 0x60(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq 0x58(%rsp), %rdx
callq 0x11a0
testl %r14d, %r14d
je 0x3460
movq 0x8(%rsp), %rdi
movl 0x1c(%rsp), %esi
movq 0x48(%rsp), %rdx
callq 0x10f0
movl 0xc(%rbx), %eax
imull %r15d, %eax
addl 0x10(%rbx), %eax
movq (%rbx), %rdi
movslq %eax, %rsi
xorl %edx, %edx
callq 0x1180
movslq 0xc(%rbx), %rsi
movq (%rbx), %rcx
movq 0x10(%rsp), %rdi
pushq $0x1
popq %r14
movq %r14, %rdx
callq 0x11e0
incl %r15d
addl 0x28(%rsp), %ebp
jmp 0x33a0
movq 0x10(%rsp), %rdi
callq 0x1030
movq 0x38(%rsp), %rdi
callq 0x1030
pushq $0x1
popq %r12
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x40(%rbx)
movl %r12d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq