// File: /workspace/github_c_makefile_star2_O2/bmottershead[P]DBF2TSV/dbf.c
// Function: DBFDeleteField
int  DBFDeleteField(DBFHandle psDBF, int iField) {
  int nOldRecordLength, nOldHeaderLength;
  int nDeletedFieldOffset, nDeletedFieldSize;
  unsigned long nRecordOffset;
  char *pszRecord;
  int i, iRecord;

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

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

  /* get information about field to be deleted */
  nOldRecordLength = psDBF->nRecordLength;
  nOldHeaderLength = psDBF->nHeaderLength;
  nDeletedFieldOffset = psDBF->panFieldOffset[iField];
  nDeletedFieldSize = psDBF->panFieldSize[iField];

  /* update fields info */
  for (i = iField + 1; i < psDBF->nFields; i++) {
    psDBF->panFieldOffset[i - 1] =
      psDBF->panFieldOffset[i] - nDeletedFieldSize;
    psDBF->panFieldSize[i - 1] = psDBF->panFieldSize[i];
    psDBF->panFieldDecimals[i - 1] = psDBF->panFieldDecimals[i];
    psDBF->pachFieldType[i - 1] = psDBF->pachFieldType[i];
  }

  /* resize fields arrays */
  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);

  /* update header information */
  psDBF->nHeaderLength -= 32;
  psDBF->nRecordLength -= nDeletedFieldSize;
  memmove(psDBF->pszHeader + iField * 32, psDBF->pszHeader + (iField + 1) * 32,
          sizeof(char) * (psDBF->nFields - iField) * 32);
  psDBF->pszHeader = (char *) SfRealloc(psDBF->pszHeader, psDBF->nFields * 32);

  /* update size of current record appropriately */
  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);

  /* shift records to their new positions */
  pszRecord = (char *) malloc(sizeof(char) * nOldRecordLength);
  for (iRecord = 0; iRecord < psDBF->nRecords; iRecord++) {
    /* load record */
    nRecordOffset = nOldRecordLength * (unsigned long) iRecord + nOldHeaderLength;
    fseek(psDBF->fp, nRecordOffset, 0);
    fread(pszRecord, nOldRecordLength, 1, psDBF->fp);

    /* move record in two steps */
    nRecordOffset = psDBF->nRecordLength * (unsigned long) iRecord + psDBF->nHeaderLength;
    fseek(psDBF->fp, nRecordOffset, 0);
    fwrite(pszRecord, nDeletedFieldOffset, 1, psDBF->fp);
    fwrite(pszRecord + nDeletedFieldOffset +  nDeletedFieldSize,
           nOldRecordLength - nDeletedFieldOffset -
           nDeletedFieldSize, 1, psDBF->fp);
  }

  /* free record */
  free(pszRecord);
  psDBF->nCurrentRecord = -1;
  psDBF->bCurrentRecordModified = FALSE;
  return TRUE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
xorl %ebp, %ebp
testl %esi, %esi
js 0x2d1d
movl %esi, %r13d
movq %rdi, %rbx
cmpl %esi, 0x14(%rdi)
jle 0x2d1d
movq %rbx, %rdi
callq 0x1672
testl %eax, %eax
je 0x2d1d
movslq 0xc(%rbx), %rax
movq %rax, 0x10(%rsp)
movslq 0x10(%rbx), %r15
movl %r13d, %eax
movq 0x18(%rbx), %rcx
movq 0x20(%rbx), %rdx
movslq (%rcx,%rax,4), %rcx
movq %rcx, 0x8(%rsp)
movslq (%rdx,%rax,4), %r14
incq %rax
movl 0x14(%rbx), %ecx
cmpl %eax, %ecx
jle 0x2b94
leaq -0x1(%rax), %rcx
movq 0x18(%rbx), %rdx
movl (%rdx,%rax,4), %esi
subl %r14d, %esi
movl %ecx, %ecx
movl %esi, (%rdx,%rcx,4)
movq 0x20(%rbx), %rdx
movl (%rdx,%rax,4), %esi
movl %esi, (%rdx,%rcx,4)
movq 0x28(%rbx), %rdx
movl (%rdx,%rax,4), %esi
movl %esi, (%rdx,%rcx,4)
movq 0x30(%rbx), %rdx
movb (%rdx,%rax), %sil
movb %sil, (%rdx,%rcx)
jmp 0x2b55
leal -0x1(%rcx), %eax
movl %eax, 0x14(%rbx)
movq 0x18(%rbx), %rdi
leal -0x4(,%rcx,4), %esi
callq 0x1b66
movq %rax, 0x18(%rbx)
movq 0x20(%rbx), %rdi
movl 0x14(%rbx), %esi
shll $0x2, %esi
callq 0x1b66
movq %rax, 0x20(%rbx)
movq 0x28(%rbx), %rdi
movl 0x14(%rbx), %esi
shll $0x2, %esi
callq 0x1b66
movq %rax, 0x28(%rbx)
movq 0x30(%rbx), %rdi
movl 0x14(%rbx), %esi
callq 0x1b66
movq %rax, 0x30(%rbx)
addl $-0x20, 0x10(%rbx)
subl %r14d, 0xc(%rbx)
movq 0x38(%rbx), %rax
movslq %r13d, %rcx
shll $0x5, %r13d
leaq (%rax,%r13), %rdi
addl $0x20, %r13d
addq %rax, %r13
movslq 0x14(%rbx), %rdx
subq %rcx, %rdx
shlq $0x5, %rdx
movq %r13, %rsi
callq 0x11a0
movq 0x38(%rbx), %rdi
movl 0x14(%rbx), %esi
shll $0x5, %esi
callq 0x1b66
movq %rax, 0x38(%rbx)
movq 0x48(%rbx), %rdi
movl 0xc(%rbx), %esi
callq 0x1b66
movq %rax, 0x48(%rbx)
cmpl $0x0, 0x60(%rbx)
pushq $0x1
popq %rbp
je 0x2c4b
cmpl $0x0, 0x8(%rbx)
je 0x2d1d
movl $0x1, 0x60(%rbx)
movq %rbx, %rdi
callq 0x1508
movq 0x10(%rsp), %r12
movq %r12, %rdi
callq 0x1160
movq %rax, %r13
movq %rax, %rcx
movq 0x8(%rsp), %rax
addq %rax, %rcx
addq %r14, %rcx
movq %rcx, 0x20(%rsp)
addl %r14d, %eax
movl %r12d, %ecx
subl %eax, %ecx
movslq %ecx, %rax
movq %rax, 0x18(%rsp)
xorl %r12d, %r12d
pushq $0x1
popq %r14
movq 0x10(%rsp), %rbp
movslq 0x8(%rbx), %rax
cmpq %rax, %r12
jge 0x2d09
movq (%rbx), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x1180
movq (%rbx), %rcx
movq %r13, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x10a0
movslq 0xc(%rbx), %rax
imulq %r12, %rax
movslq 0x10(%rbx), %rsi
addq %rax, %rsi
movq (%rbx), %rdi
xorl %edx, %edx
callq 0x1180
movq (%rbx), %rcx
movq %r13, %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0x11e0
movq (%rbx), %rcx
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %r14, %rdx
callq 0x11e0
incq %r12
addq %rbp, %r15
jmp 0x2c99
movq %r13, %rdi
callq 0x1030
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x40(%rbx)
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq