// File: /workspace/github_c_makefile_star2_O2/bmottershead[P]DBF2TSV/dbf.c
// Function: DBFReorderFields
int  DBFReorderFields(DBFHandle psDBF, int *panMap) {
  unsigned long nRecordOffset;
  int i, iRecord;
  int *panFieldOffsetNew;
  int *panFieldSizeNew;
  int *panFieldDecimalsNew;
  char *pachFieldTypeNew;
  char *pszHeaderNew;
  char *pszRecord;
  char *pszRecordNew;

  if (psDBF->nFields == 0)
    return TRUE;

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

  panFieldOffsetNew = (int *) malloc(sizeof(int) * psDBF->nFields);
  panFieldSizeNew = (int *) malloc(sizeof(int) * psDBF->nFields);
  panFieldDecimalsNew = (int *) malloc(sizeof(int) * psDBF->nFields);
  pachFieldTypeNew = (char *) malloc(sizeof(char) * psDBF->nFields);
  pszHeaderNew = (char *) malloc(sizeof(char) * 32 * psDBF->nFields);

  /* shuffle fields definitions */
  for (i = 0; i < psDBF->nFields; i++) {
    panFieldSizeNew[i] = psDBF->panFieldSize[panMap[i]];
    panFieldDecimalsNew[i] = psDBF->panFieldDecimals[panMap[i]];
    pachFieldTypeNew[i] = psDBF->pachFieldType[panMap[i]];
    memcpy(pszHeaderNew + i * 32, psDBF->pszHeader + panMap[i] * 32,  32);
  }
  panFieldOffsetNew[0] = 1;
  for (i = 1; i < psDBF->nFields; i++)
    panFieldOffsetNew[i] = panFieldOffsetNew[i - 1] + panFieldSizeNew[i - 1];
  free(psDBF->pszHeader);
  psDBF->pszHeader = pszHeaderNew;

  /* we're done if we're dealing with not yet created .dbf */
  if (!(psDBF->bNoHeader && psDBF->nRecords == 0)) {
    /* force update of header with new header and record length */
    psDBF->bNoHeader = TRUE;
    DBFUpdateHeader(psDBF);

    /* alloc record */
    pszRecord = (char *) malloc(sizeof(char) * psDBF->nRecordLength);
    pszRecordNew = (char *) malloc(sizeof(char) * psDBF->nRecordLength);

    /* shuffle fields in records */
    for (iRecord = 0; iRecord < psDBF->nRecords; iRecord++) {
      nRecordOffset = psDBF->nRecordLength * (unsigned long) iRecord + psDBF->nHeaderLength;

      /* load record */
      fseek(psDBF->fp, nRecordOffset, 0);
      fread(pszRecord, psDBF->nRecordLength, 1,psDBF->fp);
      pszRecordNew[0] = pszRecord[0];
      for (i = 0; i < psDBF->nFields; i++) {
        memcpy(pszRecordNew + panFieldOffsetNew[i],pszRecord + psDBF->panFieldOffset[panMap[i]],
               psDBF->panFieldSize[panMap[i]]);
      }

      /* write record */
      fseek(psDBF->fp, nRecordOffset, 0);
      fwrite(pszRecordNew, psDBF->nRecordLength, 1, psDBF->fp);
    }

    /* free record */
    free(pszRecord);
    free(pszRecordNew);
  }

  free(psDBF->panFieldOffset);
  free(psDBF->panFieldSize);
  free(psDBF->panFieldDecimals);
  free(psDBF->pachFieldType);
  psDBF->panFieldOffset = panFieldOffsetNew;
  psDBF->panFieldSize = panFieldSizeNew;
  psDBF->panFieldDecimals = panFieldDecimalsNew;
  psDBF->pachFieldType = pachFieldTypeNew;
  psDBF->nCurrentRecord = -1;
  psDBF->bCurrentRecordModified = FALSE;
  return TRUE;
}

// Assembly:
cmpl $0x0, 0x14(%rdi)
je 0x2f3b
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r13
movq %rdi, %rbx
callq 0x1672
testl %eax, %eax
je 0x2fa5
movslq 0x14(%rbx), %r12
leaq (,%r12,4), %r15
movq %r15, %rdi
callq 0x1160
movq %rax, 0x8(%rsp)
movq %r15, %rdi
callq 0x1160
movq %r15, %rdi
movq %rax, %r14
callq 0x1160
movq %rax, %r15
movq %r12, %rdi
callq 0x1160
movq %rax, %rbp
movq %r12, %rdi
shlq $0x5, %rdi
callq 0x1160
movq %rbp, %r8
movq %rax, %rbp
xorl %eax, %eax
testl %r12d, %r12d
movl $0x0, %ecx
cmovgl %r12d, %ecx
movq %rbp, %rdx
cmpq %rax, %rcx
je 0x2e03
movq 0x20(%rbx), %rsi
movslq (%r13,%rax,4), %rdi
movl (%rsi,%rdi,4), %esi
movl %esi, (%r14,%rax,4)
movq 0x28(%rbx), %rsi
movl (%rsi,%rdi,4), %esi
movl %esi, (%r15,%rax,4)
movq 0x30(%rbx), %rsi
movb (%rsi,%rdi), %sil
movb %sil, (%r8,%rax)
movq 0x38(%rbx), %rsi
shlq $0x5, %rdi
movups (%rsi,%rdi), %xmm0
movups 0x10(%rsi,%rdi), %xmm1
movups %xmm1, 0x10(%rdx)
movups %xmm0, (%rdx)
incq %rax
addq $0x20, %rdx
jmp 0x2db6
movq 0x8(%rsp), %rax
movl $0x1, (%rax)
pushq $0x1
popq %rax
pushq $0x1
popq %rcx
movq 0x8(%rsp), %rdx
cmpq %r12, %rax
jge 0x2e2b
addl -0x4(%r14,%rax,4), %ecx
movl %ecx, (%rdx,%rax,4)
incq %rax
jmp 0x2e19
movq %r8, 0x20(%rsp)
movq 0x38(%rbx), %rdi
callq 0x1030
movq %rbp, 0x38(%rbx)
cmpl $0x0, 0x60(%rbx)
je 0x2e4d
cmpl $0x0, 0x8(%rbx)
je 0x2f59
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movl $0x1, 0x60(%rbx)
movq %rbx, %rdi
callq 0x1508
movslq 0xc(%rbx), %r15
movq %r15, %rdi
callq 0x1160
movq %rax, %r12
movq %r15, %rdi
callq 0x1160
movq %rax, %rbp
xorl %ecx, %ecx
pushq $0x1
popq %r14
movslq 0x8(%rbx), %rax
cmpq %rax, %rcx
jge 0x2f3f
movslq 0xc(%rbx), %rax
movq %rcx, 0x30(%rsp)
imulq %rcx, %rax
movslq 0x10(%rbx), %rsi
addq %rax, %rsi
movq (%rbx), %rdi
movq %rsi, 0x28(%rsp)
xorl %edx, %edx
callq 0x1180
movslq 0xc(%rbx), %rsi
movq (%rbx), %rcx
movq %r12, %rdi
movq %r14, %rdx
callq 0x10a0
movb (%r12), %al
movb %al, (%rbp)
movslq 0x14(%rbx), %r15
xorl %r14d, %r14d
cmpq %r15, %r14
jge 0x2f09
movq 0x8(%rsp), %rax
movslq (%rax,%r14,4), %rdi
addq %rbp, %rdi
movslq (%r13,%r14,4), %rax
movq 0x18(%rbx), %rcx
movq 0x20(%rbx), %rdx
movslq (%rcx,%rax,4), %rsi
addq %r12, %rsi
movslq (%rdx,%rax,4), %rdx
callq 0x1150
incq %r14
jmp 0x2ed6
movq (%rbx), %rdi
movq 0x28(%rsp), %rsi
xorl %edx, %edx
callq 0x1180
movslq 0xc(%rbx), %rsi
movq (%rbx), %rcx
movq %rbp, %rdi
pushq $0x1
popq %r14
movq %r14, %rdx
callq 0x11e0
movq 0x30(%rsp), %rcx
incq %rcx
jmp 0x2e86
pushq $0x1
popq %rax
retq
movq %r12, %rdi
callq 0x1030
movq %rbp, %rdi
callq 0x1030
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %r15
movq 0x18(%rbx), %rdi
callq 0x1030
movq 0x20(%rbx), %rdi
callq 0x1030
movq 0x28(%rbx), %rdi
callq 0x1030
movq 0x30(%rbx), %rdi
callq 0x1030
movq 0x8(%rsp), %rax
movq %rax, 0x18(%rbx)
movq %r14, 0x20(%rbx)
movq %r15, 0x28(%rbx)
movq 0x20(%rsp), %rax
movq %rax, 0x30(%rbx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x40(%rbx)
pushq $0x1
popq %rax
jmp 0x2fa7
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq