// File: /workspace/github_c_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl $0x1, %ebp
cmpl $0x0, 0x14(%rdi)
je 0x3131
movq %rsi, %rbx
movq %rdi, %r14
callq 0x16fc
testl %eax, %eax
je 0x2f7c
movq %rbx, 0x28(%rsp)
movslq 0x14(%r14), %rbx
leaq (,%rbx,4), %r12
movq %r12, %rdi
callq 0x1160
movq %rax, %r15
movq %r12, %rdi
callq 0x1160
movq %rax, 0x10(%rsp)
movq %r12, %rdi
callq 0x1160
movq %rax, 0x20(%rsp)
movq %rbx, %rdi
callq 0x1160
movq %rax, %r13
movq %rbx, %rdi
shlq $0x5, %rdi
callq 0x1160
testq %rbx, %rbx
movq %r15, 0x18(%rsp)
movq %rax, 0x8(%rsp)
jle 0x2f83
movq 0x20(%r14), %r15
movq 0x28(%r14), %rcx
movq 0x30(%r14), %rdx
movq 0x38(%r14), %rsi
xorl %edi, %edi
movq %rax, %r8
movq 0x10(%rsp), %r12
movq 0x20(%rsp), %r11
movq %r13, %rbp
movq 0x28(%rsp), %r13
movslq (%r13,%rdi,4), %r9
movl (%r15,%r9,4), %r10d
movl %r10d, (%r12,%rdi,4)
movl (%rcx,%r9,4), %r10d
movl %r10d, (%r11,%rdi,4)
movb (%rdx,%r9), %r10b
movb %r10b, (%rbp,%rdi)
shlq $0x5, %r9
movups (%rsi,%r9), %xmm0
movups 0x10(%rsi,%r9), %xmm1
movups %xmm1, 0x10(%r8)
movups %xmm0, (%r8)
incq %rdi
addq $0x20, %r8
cmpq %rdi, %rbx
jne 0x2f04
movq 0x18(%rsp), %r15
movl $0x1, (%r15)
cmpl $0x1, %ebx
movq %rbp, %r13
movl $0x1, %ebp
je 0x2f8f
decq %rbx
movl $0x1, %eax
xorl %ecx, %ecx
addl (%r12,%rcx,4), %eax
movl %eax, 0x4(%r15,%rcx,4)
incq %rcx
cmpq %rcx, %rbx
jne 0x2f69
jmp 0x2f8f
xorl %ebp, %ebp
jmp 0x3131
movl $0x1, (%r15)
movq 0x10(%rsp), %r12
movq 0x38(%r14), %rdi
callq 0x1030
movq 0x8(%rsp), %rax
movq %rax, 0x38(%r14)
cmpl $0x0, 0x60(%r14)
je 0x2fb3
cmpl $0x0, 0x8(%r14)
je 0x30ef
movq %r13, 0x40(%rsp)
movl $0x1, 0x60(%r14)
movq %r14, %rdi
callq 0x1590
movslq 0xc(%r14), %rbx
movq %rbx, %rdi
callq 0x1160
movq %rax, %r15
movq %rbx, %rdi
callq 0x1160
movq %rax, %rbx
cmpl $0x0, 0x8(%r14)
jle 0x30cb
xorl %ecx, %ecx
movq %r14, 0x30(%rsp)
movq %rbx, 0x8(%rsp)
movq %r15, 0x38(%rsp)
movq 0x18(%rsp), %r13
movslq 0xc(%r14), %rax
movq %rcx, 0x50(%rsp)
imulq %rcx, %rax
movslq 0x10(%r14), %rsi
addq %rax, %rsi
movq (%r14), %rdi
movq %rsi, 0x48(%rsp)
xorl %edx, %edx
callq 0x1180
movslq 0xc(%r14), %rsi
movq (%r14), %rcx
movl $0x1, %edx
movq %r15, %rdi
callq 0x10a0
movb (%r15), %al
movb %al, (%rbx)
movslq 0x14(%r14), %r12
testq %r12, %r12
movq 0x28(%rsp), %rbx
jle 0x3084
movq 0x30(%rsp), %rax
movq 0x18(%rax), %r14
movq 0x20(%rax), %r15
xorl %ebp, %ebp
movslq (%r13,%rbp,4), %rdi
addq 0x8(%rsp), %rdi
movslq (%rbx,%rbp,4), %rax
movslq (%r14,%rax,4), %rsi
addq 0x38(%rsp), %rsi
movslq (%r15,%rax,4), %rdx
callq 0x1150
incq %rbp
cmpq %rbp, %r12
jne 0x305c
movq 0x30(%rsp), %r14
movq (%r14), %rdi
movq 0x48(%rsp), %rsi
xorl %edx, %edx
callq 0x1180
movslq 0xc(%r14), %rsi
movq (%r14), %rcx
movl $0x1, %edx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x11e0
movq 0x50(%rsp), %rcx
incq %rcx
movslq 0x8(%r14), %rax
cmpq %rax, %rcx
movq 0x38(%rsp), %r15
jl 0x3003
movq %r15, %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
movl $0x1, %ebp
movq 0x18(%rsp), %r15
movq 0x10(%rsp), %r12
movq 0x40(%rsp), %r13
movq 0x18(%r14), %rdi
callq 0x1030
movq 0x20(%r14), %rdi
callq 0x1030
movq 0x28(%r14), %rdi
callq 0x1030
movq 0x30(%r14), %rdi
callq 0x1030
movq %r15, 0x18(%r14)
movq %r12, 0x20(%r14)
movq 0x20(%rsp), %rax
movq %rax, 0x28(%r14)
movq %r13, 0x30(%r14)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x40(%r14)
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq