// File: /workspace/github_c_makefile_star2_O1/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 %ebp, %ebp
testl %esi, %esi
js 0x366c
movl %esi, %r12d
movq %rdi, %rbx
cmpl %esi, 0x14(%rdi)
jle 0x366c
movl %r9d, %r14d
movl %r8d, %r15d
movq %rdx, %r13
movl %ecx, 0x10(%rsp)
movq %rbx, %rdi
callq 0x16fc
testl %eax, %eax
je 0x366c
movl 0x10(%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 0x3233
movl %eax, 0xc(%rsp)
movq 0x30(%rbx), %rax
movl %r12d, %ebp
movb (%rax,%rbp), %cl
movb %cl, 0x8(%rsp)
movq 0x18(%rbx), %rcx
movq 0x20(%rbx), %rdx
movslq (%rcx,%rbp,4), %rcx
movq %rcx, 0x20(%rsp)
movslq (%rdx,%rbp,4), %rcx
movq %rcx, 0x28(%rsp)
movl $0xff, %esi
cmpl %esi, %r15d
cmovll %r15d, %esi
movslq 0xc(%rbx), %rcx
movq %rcx, 0x68(%rsp)
movl %esi, 0x1c(%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 0x323d
movq %r15, %rdi
movq %r13, %rsi
movq %rax, %rdx
jmp 0x3248
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x366c
movl $0xa, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x1050
movq 0x30(%rbx), %rax
movb (%rax,%rbp), %al
movb %al, 0xb(%r15)
xorl %eax, %eax
cmpb $0x43, 0x10(%rsp)
movl 0x1c(%rsp), %ebp
movb %bpl, 0x10(%r15)
cmovel %eax, %r14d
movb %r14b, 0x11(%r15)
cmpl 0x28(%rsp), %ebp
movq 0x68(%rsp), %r13
je 0x32bc
incl %r12d
cmpl 0x14(%rbx), %r12d
jge 0x32a3
movl %ebp, %eax
subl 0x28(%rsp), %eax
movl %r12d, %ecx
shlq $0x2, %rcx
addq 0x18(%rbx), %rcx
addl %eax, (%rcx)
addq $0x4, %rcx
incl %r12d
cmpl %r12d, 0x14(%rbx)
jg 0x3294
movl %ebp, %esi
subl 0x28(%rsp), %esi
addl 0xc(%rbx), %esi
movl %esi, 0xc(%rbx)
movq 0x48(%rbx), %rdi
callq 0x1c08
movq %rax, 0x48(%rbx)
cmpl $0x0, 0x60(%rbx)
je 0x32cc
cmpl $0x0, 0x8(%rbx)
je 0x3667
movl $0x1, 0x60(%rbx)
movq %rbx, %rdi
callq 0x1590
movq 0x28(%rsp), %r12
cmpl %r12d, %ebp
jl 0x34c2
cmpl %r12d, %ebp
setne %al
movl 0x10(%rsp), %ecx
cmpb %cl, 0x8(%rsp)
sete %cl
orb %al, %cl
je 0x34c2
cmpl %r12d, %ebp
jle 0x365e
movslq 0xc(%rbx), %rdi
callq 0x1160
movq %rax, %r15
leaq 0x1(%r12), %rdi
callq 0x1160
movq %rax, 0x30(%rsp)
movb $0x0, (%rax,%r12)
movl 0x8(%rbx), %r14d
testl %r14d, %r14d
jle 0x364c
movq %r15, %rcx
movq 0x20(%rsp), %rax
addq %rax, %rcx
leal (%rax,%r12), %edx
movl %ebp, %eax
movq %rax, 0x60(%rsp)
leaq (%rcx,%rax), %rsi
movq %rcx, 0x10(%rsp)
leaq (%rcx,%r12), %rax
movq %rax, 0x38(%rsp)
movl %r13d, %eax
movl %edx, 0x20(%rsp)
subl %edx, %eax
cltq
movq %rax, 0x50(%rsp)
movl %ebp, %eax
subl %r12d, %eax
cltq
movq %rax, 0x48(%rsp)
movq %rsi, 0x58(%rsp)
subq %r12, %rsi
movq %rsi, 0x40(%rsp)
subl %r12d, %ebp
movslq %ebp, %rax
movq %rax, 0x70(%rsp)
leal 0x1(%r14), %ebp
decl %r14d
imull %r13d, %r14d
movb 0x8(%rsp), %al
movsbl %al, %ecx
movl %ecx, 0x1c(%rsp)
andb $-0x9, %al
movb %al, 0x8(%rsp)
movzbl 0xc(%rsp), %eax
movl %eax, 0xc(%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 0x30(%rsp), %r15
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x1150
movl 0x1c(%rsp), %edi
movq %r15, %rsi
callq 0x233a
movl %eax, %r15d
cmpl 0x20(%rsp), %r13d
jle 0x3423
movq 0x58(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x50(%rsp), %rdx
callq 0x11a0
testl %r15d, %r15d
je 0x3440
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %esi
movq 0x60(%rsp), %rdx
callq 0x10f0
movq %r12, %r15
jmp 0x3484
cmpb $0x46, 0x8(%rsp)
movq %r12, %r15
jne 0x3470
movq 0x40(%rsp), %rdi
movq 0x10(%rsp), %r12
movq %r12, %rsi
movq 0x28(%rsp), %rdx
callq 0x11a0
movq %r12, %rdi
movl $0x20, %esi
movq 0x70(%rsp), %rdx
jmp 0x347f
movq 0x38(%rsp), %rdi
movl $0x20, %esi
movq 0x48(%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
jg 0x33b9
jmp 0x364c
movq %r13, %rdi
callq 0x1160
movq %rax, %r15
leaq 0x1(%r12), %rdi
callq 0x1160
movq %rax, 0x30(%rsp)
movb $0x0, (%rax,%r12)
cmpl $0x0, 0x8(%rbx)
jle 0x364c
movq %r15, %rsi
movq 0x20(%rsp), %rdx
addq %rdx, %rsi
movb 0x8(%rsp), %cl
movl %ecx, %edi
andb $-0x9, %dil
movb %dil, 0x38(%rsp)
movq %r12, %rdi
addq %rsi, %rdi
movl %ebp, %eax
movq %rdi, 0x58(%rsp)
subq %rax, %rdi
movq %rdi, 0x40(%rsp)
addl %r12d, %edx
movq %rsi, 0x10(%rsp)
movq %rax, 0x20(%rsp)
addq %rsi, %rax
movq %rax, 0x50(%rsp)
movl %r13d, %eax
movl %edx, 0x60(%rsp)
subl %edx, %eax
cltq
movq %rax, 0x48(%rsp)
xorl %r12d, %r12d
movsbl %cl, %eax
movl %eax, 0x8(%rsp)
movzbl 0xc(%rsp), %eax
movl %eax, 0xc(%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 0x30(%rsp), %r15
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movq 0x28(%rsp), %r13
movq %r13, %rdx
callq 0x1150
movl 0x8(%rsp), %edi
movq %r15, %rsi
callq 0x233a
movl %eax, %r15d
cmpl %r13d, 0x1c(%rsp)
je 0x35f1
cmpb $0x46, 0x38(%rsp)
jne 0x35d3
movq 0x30(%rsp), %rax
cmpb $0x20, (%rax)
jne 0x35d3
movq 0x10(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x11a0
movl 0x60(%rsp), %eax
cmpl %eax, 0x68(%rsp)
jle 0x35f1
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq 0x48(%rsp), %rdx
callq 0x11a0
testl %r15d, %r15d
je 0x3609
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %esi
movq 0x20(%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 0x68(%rsp), %r13
addl %r13d, %r12d
cmpl 0x8(%rbx), %r14d
jl 0x3555
movq %r15, %rdi
callq 0x1030
movq 0x30(%rsp), %rdi
callq 0x1030
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x40(%rbx)
movl $0x1, %ebp
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq