// File: /workspace/github_c_makefile_star2_O1/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 $0x18, %rsp
xorl %ebp, %ebp
testl %esi, %esi
js 0x2e52
movl %esi, %r13d
movq %rdi, %rbx
cmpl %esi, 0x14(%rdi)
jle 0x2e52
movq %rbx, %rdi
callq 0x16fc
testl %eax, %eax
je 0x2e52
movslq 0xc(%rbx), %r12
movslq 0x10(%rbx), %r15
movq 0x18(%rbx), %rax
movl %r13d, %ecx
movslq (%rax,%rcx,4), %rax
movq %rax, 0x8(%rsp)
movq 0x20(%rbx), %rax
movslq (%rax,%rcx,4), %r14
leal 0x1(%r13), %ebp
movl 0x14(%rbx), %ecx
cmpl %ecx, %ebp
jge 0x2cd0
movq %rbp, %rax
movq 0x18(%rbx), %rcx
movl (%rcx,%rax,4), %edx
subl %r14d, %edx
leal -0x1(%rax), %esi
movl %edx, (%rcx,%rsi,4)
movq 0x20(%rbx), %rcx
movl (%rcx,%rax,4), %edx
movl %edx, (%rcx,%rsi,4)
movq 0x28(%rbx), %rcx
movl (%rcx,%rax,4), %edx
movl %edx, (%rcx,%rsi,4)
movq 0x30(%rbx), %rcx
movb (%rcx,%rax), %dl
movb %dl, (%rcx,%rsi)
incq %rax
movl 0x14(%rbx), %ecx
cmpl %eax, %ecx
jg 0x2c98
leal -0x1(%rcx), %eax
movl %eax, 0x14(%rbx)
movq 0x18(%rbx), %rdi
leal -0x4(,%rcx,4), %esi
callq 0x1c08
movq %rax, 0x18(%rbx)
movq 0x20(%rbx), %rdi
movl 0x14(%rbx), %esi
shll $0x2, %esi
callq 0x1c08
movq %rax, 0x20(%rbx)
movq 0x28(%rbx), %rdi
movl 0x14(%rbx), %esi
shll $0x2, %esi
callq 0x1c08
movq %rax, 0x28(%rbx)
movq 0x30(%rbx), %rdi
movl 0x14(%rbx), %esi
callq 0x1c08
movq %rax, 0x30(%rbx)
addl $-0x20, 0x10(%rbx)
subl %r14d, 0xc(%rbx)
movq 0x38(%rbx), %rax
movslq %r13d, %rcx
shll $0x5, %r13d
addq %rax, %r13
shll $0x5, %ebp
addq %rax, %rbp
movslq 0x14(%rbx), %rdx
subq %rcx, %rdx
shlq $0x5, %rdx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x11a0
movq 0x38(%rbx), %rdi
movl 0x14(%rbx), %esi
shll $0x5, %esi
callq 0x1c08
movq %rax, 0x38(%rbx)
movq 0x48(%rbx), %rdi
movl 0xc(%rbx), %esi
callq 0x1c08
movq %rax, 0x48(%rbx)
cmpl $0x0, 0x60(%rbx)
je 0x2d85
cmpl $0x0, 0x8(%rbx)
je 0x2e4d
movl $0x1, 0x60(%rbx)
movq %rbx, %rdi
callq 0x1590
movq %r12, %rdi
callq 0x1160
movq %rax, %r13
cmpl $0x0, 0x8(%rbx)
jle 0x2e3c
movq %r13, %rbp
movq 0x8(%rsp), %rax
addq %rax, %rbp
addq %r14, %rbp
addl %r14d, %eax
movl %r12d, %ecx
subl %eax, %ecx
movslq %ecx, %rax
movq %rax, 0x10(%rsp)
xorl %r14d, %r14d
movq (%rbx), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x1180
movq (%rbx), %rcx
movl $0x1, %edx
movq %r13, %rdi
movq %r12, %rsi
callq 0x10a0
movslq 0xc(%rbx), %rax
imulq %r14, %rax
movslq 0x10(%rbx), %rsi
addq %rax, %rsi
movq (%rbx), %rdi
xorl %edx, %edx
callq 0x1180
movq (%rbx), %rcx
movl $0x1, %edx
movq %r13, %rdi
movq 0x8(%rsp), %rsi
callq 0x11e0
movq (%rbx), %rcx
movl $0x1, %edx
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
callq 0x11e0
incq %r14
movslq 0x8(%rbx), %rax
addq %r12, %r15
cmpq %rax, %r14
jl 0x2dca
movq %r13, %rdi
callq 0x1030
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x40(%rbx)
movl $0x1, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq