// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movb %cl, %al
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movb %al, -0x21(%rbp)
movl %r8d, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
cmpl $0x0, -0x14(%rbp)
jl 0x46d5
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jl 0x46e1
movl $0x0, -0x4(%rbp)
jmp 0x4d8e
movq -0x10(%rbp), %rdi
callq 0x17b0
cmpl $0x0, %eax
jne 0x46fb
movl $0x0, -0x4(%rbp)
jmp 0x4d8e
movsbl -0x21(%rbp), %edi
callq 0x2940
movb %al, -0x59(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x14(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x51(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x14(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x14(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x40(%rbp)
cmpl $0x1, -0x28(%rbp)
jge 0x4759
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4d8e
cmpl $0xff, -0x28(%rbp)
jle 0x4769
movl $0xff, -0x28(%rbp)
movl -0x28(%rbp), %edx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x14(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x2c(%rbp), %edx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x14(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movb -0x21(%rbp), %dl
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x14(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movl -0x14(%rbp), %ecx
shll $0x5, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movl $0x0, -0x30(%rbp)
cmpl $0x20, -0x30(%rbp)
jge 0x47db
movq -0x50(%rbp), %rax
movslq -0x30(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x47be
movq -0x20(%rbp), %rdi
callq 0x10c0
cmpl $0xa, %eax
jge 0x4820
movq -0x50(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x20(%rbp), %rdi
callq 0x10c0
movq -0x90(%rbp), %rdi
movq -0x88(%rbp), %rsi
movq %rax, %rdx
callq 0x1040
jmp 0x4832
movq -0x50(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl $0xa, %edx
callq 0x1040
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x14(%rbp), %rcx
movb (%rax,%rcx), %cl
movq -0x50(%rbp), %rax
movb %cl, 0xb(%rax)
movsbl -0x21(%rbp), %eax
cmpl $0x43, %eax
jne 0x487b
movl -0x28(%rbp), %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movb %dl, %cl
movq -0x50(%rbp), %rax
movb %cl, 0x10(%rax)
movl -0x28(%rbp), %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movb %al, %cl
movq -0x50(%rbp), %rax
movb %cl, 0x11(%rax)
jmp 0x4893
movl -0x28(%rbp), %eax
movb %al, %cl
movq -0x50(%rbp), %rax
movb %cl, 0x10(%rax)
movl -0x2c(%rbp), %eax
movb %al, %cl
movq -0x50(%rbp), %rax
movb %cl, 0x11(%rax)
movl -0x28(%rbp), %eax
cmpl -0x3c(%rbp), %eax
je 0x4902
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jge 0x48d3
movl -0x28(%rbp), %edx
subl -0x3c(%rbp), %edx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x30(%rbp), %rcx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x48a4
movl -0x28(%rbp), %ecx
subl -0x3c(%rbp), %ecx
movq -0x10(%rbp), %rax
addl 0xc(%rax), %ecx
movl %ecx, 0xc(%rax)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x10(%rbp), %rax
movl 0xc(%rax), %esi
callq 0x1ff0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x60(%rax)
je 0x4922
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jne 0x4922
movl $0x1, -0x4(%rbp)
jmp 0x4d8e
movq -0x10(%rbp), %rax
movl $0x1, 0x60(%rax)
movq -0x10(%rbp), %rdi
callq 0x1570
movl -0x28(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jl 0x495a
movl -0x28(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jne 0x4b46
movsbl -0x21(%rbp), %eax
movsbl -0x51(%rbp), %ecx
cmpl %ecx, %eax
je 0x4b46
movslq -0x40(%rbp), %rdi
shlq $0x0, %rdi
callq 0x1140
movq %rax, -0x68(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdi
shlq $0x0, %rdi
callq 0x1140
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x4b2f
movslq -0x40(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq %rcx, %rax
movq -0x10(%rbp), %rcx
movslq 0x10(%rcx), %rcx
addq %rcx, %rax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movslq -0x44(%rbp), %rsi
xorl %edx, %edx
callq 0x1160
movq -0x68(%rbp), %rdi
movslq -0x40(%rbp), %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movl $0x1, %edx
callq 0x1090
movq -0x70(%rbp), %rdi
movq -0x68(%rbp), %rsi
movslq -0x38(%rbp), %rax
addq %rax, %rsi
movslq -0x3c(%rbp), %rdx
callq 0x1130
movb -0x51(%rbp), %al
movq -0x70(%rbp), %rsi
movsbl %al, %edi
callq 0x2d60
movl %eax, -0x58(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x3c(%rbp), %eax
je 0x4ab3
movsbl -0x51(%rbp), %eax
cmpl $0x4e, %eax
je 0x4a31
movsbl -0x51(%rbp), %eax
cmpl $0x46, %eax
jne 0x4a6f
movq -0x70(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x4a6f
movq -0x68(%rbp), %rdi
movslq -0x38(%rbp), %rax
addq %rax, %rdi
movq -0x68(%rbp), %rsi
movslq -0x38(%rbp), %rax
addq %rax, %rsi
movslq -0x3c(%rbp), %rax
addq %rax, %rsi
movslq -0x28(%rbp), %rcx
xorl %eax, %eax
subq %rcx, %rax
addq %rax, %rsi
movslq -0x28(%rbp), %rdx
callq 0x1180
movl -0x38(%rbp), %eax
addl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jge 0x4ab1
movq -0x68(%rbp), %rdi
movslq -0x38(%rbp), %rax
addq %rax, %rdi
movslq -0x28(%rbp), %rax
addq %rax, %rdi
movq -0x68(%rbp), %rsi
movslq -0x38(%rbp), %rax
addq %rax, %rsi
movslq -0x3c(%rbp), %rax
addq %rax, %rsi
movl -0x40(%rbp), %eax
movl -0x38(%rbp), %ecx
addl -0x3c(%rbp), %ecx
subl %ecx, %eax
movslq %eax, %rdx
callq 0x1180
jmp 0x4ab3
cmpl $0x0, -0x58(%rbp)
je 0x4ad4
movq -0x68(%rbp), %rdi
movslq -0x38(%rbp), %rax
addq %rax, %rdi
movsbl -0x59(%rbp), %eax
movslq -0x28(%rbp), %rdx
movzbl %al, %esi
callq 0x10e0
movq -0x10(%rbp), %rax
movslq 0xc(%rax), %rax
movslq -0x34(%rbp), %rcx
imulq %rcx, %rax
movq -0x10(%rbp), %rcx
movslq 0x10(%rcx), %rcx
addq %rcx, %rax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movslq -0x44(%rbp), %rsi
xorl %edx, %edx
callq 0x1160
movq -0x68(%rbp), %rdi
movq -0x10(%rbp), %rax
movslq 0xc(%rax), %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movl $0x1, %edx
callq 0x11c0
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x4994
movq -0x68(%rbp), %rdi
callq 0x1030
movq -0x70(%rbp), %rdi
callq 0x1030
jmp 0x4d71
movl -0x28(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jle 0x4d6f
movq -0x10(%rbp), %rax
movslq 0xc(%rax), %rdi
shlq $0x0, %rdi
callq 0x1140
movq %rax, -0x78(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdi
shlq $0x0, %rdi
callq 0x1140
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
subl $0x1, %eax
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jl 0x4d5d
movslq -0x40(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq %rcx, %rax
movq -0x10(%rbp), %rcx
movslq 0x10(%rcx), %rcx
addq %rcx, %rax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movslq -0x44(%rbp), %rsi
xorl %edx, %edx
callq 0x1160
movq -0x78(%rbp), %rdi
movslq -0x40(%rbp), %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movl $0x1, %edx
callq 0x1090
movq -0x80(%rbp), %rdi
movq -0x78(%rbp), %rsi
movslq -0x38(%rbp), %rax
addq %rax, %rsi
movslq -0x3c(%rbp), %rdx
callq 0x1130
movb -0x51(%rbp), %al
movq -0x80(%rbp), %rsi
movsbl %al, %edi
callq 0x2d60
movl %eax, -0x58(%rbp)
movl -0x38(%rbp), %eax
addl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jge 0x4c51
movq -0x78(%rbp), %rdi
movslq -0x38(%rbp), %rax
addq %rax, %rdi
movslq -0x28(%rbp), %rax
addq %rax, %rdi
movq -0x78(%rbp), %rsi
movslq -0x38(%rbp), %rax
addq %rax, %rsi
movslq -0x3c(%rbp), %rax
addq %rax, %rsi
movl -0x40(%rbp), %eax
movl -0x38(%rbp), %ecx
addl -0x3c(%rbp), %ecx
subl %ecx, %eax
movslq %eax, %rdx
callq 0x1180
cmpl $0x0, -0x58(%rbp)
je 0x4c77
movq -0x78(%rbp), %rdi
movslq -0x38(%rbp), %rax
addq %rax, %rdi
movsbl -0x59(%rbp), %eax
movslq -0x28(%rbp), %rdx
movzbl %al, %esi
callq 0x10e0
jmp 0x4d02
movsbl -0x51(%rbp), %eax
cmpl $0x4e, %eax
je 0x4c89
movsbl -0x51(%rbp), %eax
cmpl $0x46, %eax
jne 0x4cdb
movq -0x78(%rbp), %rdi
movslq -0x38(%rbp), %rax
addq %rax, %rdi
movslq -0x28(%rbp), %rax
addq %rax, %rdi
movslq -0x3c(%rbp), %rcx
xorl %eax, %eax
subq %rcx, %rax
addq %rax, %rdi
movq -0x78(%rbp), %rsi
movslq -0x38(%rbp), %rax
addq %rax, %rsi
movslq -0x3c(%rbp), %rdx
callq 0x1180
movq -0x78(%rbp), %rdi
movslq -0x38(%rbp), %rax
addq %rax, %rdi
movl -0x28(%rbp), %eax
subl -0x3c(%rbp), %eax
movslq %eax, %rdx
movl $0x20, %esi
callq 0x10e0
jmp 0x4d00
movq -0x78(%rbp), %rdi
movslq -0x38(%rbp), %rax
addq %rax, %rdi
movslq -0x3c(%rbp), %rax
addq %rax, %rdi
movl -0x28(%rbp), %eax
subl -0x3c(%rbp), %eax
movslq %eax, %rdx
movl $0x20, %esi
callq 0x10e0
jmp 0x4d02
movq -0x10(%rbp), %rax
movslq 0xc(%rax), %rax
movslq -0x34(%rbp), %rcx
imulq %rcx, %rax
movq -0x10(%rbp), %rcx
movslq 0x10(%rcx), %rcx
addq %rcx, %rax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movslq -0x44(%rbp), %rsi
xorl %edx, %edx
callq 0x1160
movq -0x78(%rbp), %rdi
movq -0x10(%rbp), %rax
movslq 0xc(%rax), %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movl $0x1, %edx
callq 0x11c0
movl -0x34(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x4b96
movq -0x78(%rbp), %rdi
callq 0x1030
movq -0x80(%rbp), %rdi
callq 0x1030
jmp 0x4d71
movq -0x10(%rbp), %rax
movl $0xffffffff, 0x40(%rax) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rax
movl $0x0, 0x44(%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq