// File: /workspace/github_c_makefile_star2_O3/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 $0x68, %rsp
xorl %ebp, %ebp
testl %esi, %esi
js 0x3658
movl %esi, %r12d
movq %rdi, %rbx
cmpl %esi, 0x14(%rdi)
jle 0x3658
movl %r9d, %r14d
movl %r8d, %r15d
movq %rdx, %r13
movl %ecx, 0x8(%rsp)
movq %rbx, %rdi
callq 0x16fc
testl %eax, %eax
je 0x3658
movl 0x8(%rsp), %edi
movsbl %dil, %eax
addl $-0x44, %eax
rorl %eax
leal (,%rax,8), %ecx
movabsq $0x2a3f20202a30, %rdx   # imm = 0x2A3F20202A30
shrq %cl, %rdx
cmpl $0x6, %eax
movl $0x20, %eax
cmovbl %edx, %eax
testl %r15d, %r15d
jle 0x3240
movl %eax, 0x4(%rsp)
movq 0x30(%rbx), %rax
movl %r12d, %ebp
movb (%rax,%rbp), %cl
movb %cl, (%rsp)
movq 0x18(%rbx), %rcx
movq 0x20(%rbx), %rdx
movslq (%rcx,%rbp,4), %rcx
movq %rcx, 0x18(%rsp)
movslq (%rdx,%rbp,4), %rcx
movq %rcx, 0x20(%rsp)
movl $0xff, %esi
cmpl %esi, %r15d
cmovbl %r15d, %esi
movslq 0xc(%rbx), %rcx
movq %rcx, 0x60(%rsp)
movl %esi, 0x14(%rsp)
movl %esi, (%rdx,%rbp,4)
movq 0x28(%rbx), %rcx
movl %r14d, (%rcx,%rbp,4)
movb %dil, (%rax,%rbp)
movq 0x38(%rbx), %rax
movl %r12d, %ecx
shll $0x5, %ecx
leaq (%rax,%rcx), %r15
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
movq %r13, %rdi
callq 0x10d0
cmpl $0x9, %eax
jg 0x324a
movq %r15, %rdi
movq %r13, %rsi
movq %rax, %rdx
jmp 0x3255
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x3658
movl $0xa, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x1050
movq 0x30(%rbx), %rax
movb (%rax,%rbp), %al
xorl %ecx, %ecx
cmpb $0x43, 0x8(%rsp)
cmovel %ecx, %r14d
movb %al, 0xb(%r15)
movl 0x14(%rsp), %ebp
movb %bpl, 0x10(%r15)
movb %r14b, 0x11(%r15)
cmpl 0x20(%rsp), %ebp
movq 0x60(%rsp), %r13
je 0x32c3
incl %r12d
movl %ebp, %esi
subl 0x20(%rsp), %esi
cmpl 0x14(%rbx), %r12d
jge 0x32b0
movl %r12d, %eax
shlq $0x2, %rax
addq 0x18(%rbx), %rax
addl %esi, (%rax)
addq $0x4, %rax
incl %r12d
cmpl %r12d, 0x14(%rbx)
jg 0x32a1
addl 0xc(%rbx), %esi
movl %esi, 0xc(%rbx)
movq 0x48(%rbx), %rdi
callq 0x1bf7
movq %rax, 0x48(%rbx)
cmpl $0x0, 0x60(%rbx)
je 0x32d3
cmpl $0x0, 0x8(%rbx)
je 0x3653
movl $0x1, 0x60(%rbx)
movq %rbx, %rdi
callq 0x1590
movq 0x20(%rsp), %r12
cmpl %r12d, %ebp
jl 0x34af
cmpl %r12d, %ebp
setne %al
movl 0x8(%rsp), %ecx
cmpb %cl, (%rsp)
sete %cl
orb %al, %cl
je 0x34af
cmpl %r12d, %ebp
jle 0x364a
movslq 0xc(%rbx), %rdi
callq 0x1160
movq %rax, %r15
leaq 0x1(%r12), %rdi
callq 0x1160
movq %rax, 0x28(%rsp)
movb $0x0, (%rax,%r12)
movl 0x8(%rbx), %r14d
testl %r14d, %r14d
jle 0x3638
movq %r15, %rcx
movq 0x18(%rsp), %rax
addq %rax, %rcx
leal (%rax,%r12), %edx
movl %ebp, %eax
movq %rax, 0x58(%rsp)
leaq (%rcx,%rax), %rsi
movq %rcx, 0x8(%rsp)
leaq (%rcx,%r12), %rax
movq %rax, 0x38(%rsp)
movl %r13d, %eax
movl %edx, 0x18(%rsp)
subl %edx, %eax
cltq
movq %rax, 0x48(%rsp)
subl %r12d, %ebp
movslq %ebp, %rax
movq %rax, 0x30(%rsp)
movq %rsi, 0x50(%rsp)
subq %r12, %rsi
movq %rsi, 0x40(%rsp)
leal 0x1(%r14), %ebp
decl %r14d
imull %r13d, %r14d
movb (%rsp), %al
movsbl %al, %ecx
movl %ecx, 0x14(%rsp)
andb $-0x9, %al
movb %al, (%rsp)
movzbl 0x4(%rsp), %eax
movl %eax, 0x4(%rsp)
movl 0x10(%rbx), %eax
addl %r14d, %eax
movq (%rbx), %rdi
movslq %eax, %rsi
xorl %edx, %edx
callq 0x1180
movq (%rbx), %rcx
movl $0x1, %edx
movq %r15, %r12
movq %r15, %rdi
movq %r13, %rsi
callq 0x10a0
movq 0x28(%rsp), %r15
movq %r15, %rdi
movq 0x8(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x1150
movl 0x14(%rsp), %edi
movq %r15, %rsi
callq 0x230a
movl %eax, %r15d
cmpl 0x18(%rsp), %r13d
jle 0x341b
movq 0x50(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x48(%rsp), %rdx
callq 0x11a0
testl %r15d, %r15d
je 0x3438
movq 0x8(%rsp), %rdi
movl 0x4(%rsp), %esi
movq 0x58(%rsp), %rdx
callq 0x10f0
movq %r12, %r15
jmp 0x3471
cmpb $0x46, (%rsp)
movq %r12, %r15
jne 0x345d
movq 0x40(%rsp), %rdi
movq 0x8(%rsp), %r12
movq %r12, %rsi
movq 0x20(%rsp), %rdx
callq 0x11a0
movq %r12, %rdi
jmp 0x3462
movq 0x38(%rsp), %rdi
movl $0x20, %esi
movq 0x30(%rsp), %rdx
callq 0x10f0
leal -0x2(%rbp), %eax
imull 0xc(%rbx), %eax
addl 0x10(%rbx), %eax
movq (%rbx), %rdi
movslq %eax, %rsi
xorl %edx, %edx
callq 0x1180
movslq 0xc(%rbx), %rsi
movq (%rbx), %rcx
movl $0x1, %edx
movq %r15, %rdi
callq 0x11e0
decl %ebp
subl %r13d, %r14d
cmpl $0x1, %ebp
ja 0x33b1
jmp 0x3638
movq %r13, %rdi
callq 0x1160
movq %rax, %r15
leaq 0x1(%r12), %rdi
callq 0x1160
movq %rax, 0x28(%rsp)
movb $0x0, (%rax,%r12)
cmpl $0x0, 0x8(%rbx)
jle 0x3638
movq %r15, %rdx
movq 0x18(%rsp), %rax
addq %rax, %rdx
movb (%rsp), %cl
movl %ecx, %esi
andb $-0x9, %sil
movb %sil, 0x38(%rsp)
movq %r12, %rsi
addq %rdx, %rsi
movl %ebp, %edi
movq %rsi, 0x50(%rsp)
subq %rdi, %rsi
movq %rsi, 0x40(%rsp)
leal (%rax,%r12), %esi
movq %rdx, 0x8(%rsp)
movq %rdi, 0x18(%rsp)
leaq (%rdx,%rdi), %rax
movq %rax, 0x48(%rsp)
movl %r13d, %eax
movl %esi, 0x58(%rsp)
subl %esi, %eax
cltq
movq %rax, 0x30(%rsp)
xorl %r12d, %r12d
movsbl %cl, %eax
movl %eax, (%rsp)
movzbl 0x4(%rsp), %eax
movl %eax, 0x4(%rsp)
xorl %r14d, %r14d
movl 0x10(%rbx), %eax
addl %r12d, %eax
movq (%rbx), %rdi
movslq %eax, %rsi
xorl %edx, %edx
callq 0x1180
movq (%rbx), %rcx
movl $0x1, %edx
movq %r15, %rbp
movq %r15, %rdi
movq %r13, %rsi
callq 0x10a0
movq 0x28(%rsp), %r15
movq %r15, %rdi
movq 0x8(%rsp), %rsi
movq 0x20(%rsp), %r13
movq %r13, %rdx
callq 0x1150
movl (%rsp), %edi
movq %r15, %rsi
callq 0x230a
movl %eax, %r15d
cmpl %r13d, 0x14(%rsp)
je 0x35dd
cmpb $0x46, 0x38(%rsp)
jne 0x35bf
movq 0x28(%rsp), %rax
cmpb $0x20, (%rax)
jne 0x35bf
movq 0x8(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x11a0
movl 0x58(%rsp), %eax
cmpl %eax, 0x60(%rsp)
jle 0x35dd
movq 0x48(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x11a0
testl %r15d, %r15d
je 0x35f5
movq 0x8(%rsp), %rdi
movl 0x4(%rsp), %esi
movq 0x18(%rsp), %rdx
callq 0x10f0
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
movl $0x1, %edx
movq %rbp, %r15
movq %rbp, %rdi
callq 0x11e0
incl %r14d
movq 0x60(%rsp), %r13
addl %r13d, %r12d
cmpl 0x8(%rbx), %r14d
jl 0x3542
movq %r15, %rdi
callq 0x1030
movq 0x28(%rsp), %rdi
callq 0x1030
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x40(%rbx)
movl $0x1, %ebp
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq