// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x14(%rax)
jne 0x42c6
movl $0x1, -0x4(%rbp)
jmp 0x4690
movq -0x10(%rbp), %rdi
callq 0x17b0
cmpl $0x0, %eax
jne 0x42e0
movl $0x0, -0x4(%rbp)
jmp 0x4690
movq -0x10(%rbp), %rax
movslq 0x14(%rax), %rdi
shlq $0x2, %rdi
callq 0x1140
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movslq 0x14(%rax), %rdi
shlq $0x2, %rdi
callq 0x1140
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movslq 0x14(%rax), %rdi
shlq $0x2, %rdi
callq 0x1140
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movslq 0x14(%rax), %rdi
shlq $0x0, %rdi
callq 0x1140
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movslq 0x14(%rax), %rdi
shlq $0x5, %rdi
callq 0x1140
movq %rax, -0x50(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jge 0x4420
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x18(%rbp), %rcx
movslq -0x24(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movl (%rax,%rcx,4), %edx
movq -0x38(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x18(%rbp), %rcx
movslq -0x24(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movl (%rax,%rcx,4), %edx
movq -0x40(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x18(%rbp), %rcx
movslq -0x24(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movb (%rax,%rcx), %dl
movq -0x48(%rbp), %rax
movslq -0x24(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x50(%rbp), %rax
movl -0x24(%rbp), %ecx
shll $0x5, %ecx
movslq %ecx, %rcx
movq -0x10(%rbp), %rdx
movq 0x38(%rdx), %rdx
movq -0x18(%rbp), %rsi
movslq -0x24(%rbp), %rdi
movl (%rsi,%rdi,4), %esi
shll $0x5, %esi
movslq %esi, %rsi
movq (%rdx,%rsi), %rdi
movq %rdi, (%rax,%rcx)
movq 0x8(%rdx,%rsi), %rdi
movq %rdi, 0x8(%rax,%rcx)
movq 0x10(%rdx,%rsi), %rdi
movq %rdi, 0x10(%rax,%rcx)
movq 0x18(%rdx,%rsi), %rdx
movq %rdx, 0x18(%rax,%rcx)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x4350
movq -0x30(%rbp), %rax
movl $0x1, (%rax)
movl $0x1, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jge 0x4473
movq -0x30(%rbp), %rax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
movq -0x38(%rbp), %rax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
addl (%rax,%rcx,4), %edx
movq -0x30(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x4431
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x1030
movq -0x50(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x60(%rax)
je 0x44a4
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x460f
movq -0x10(%rbp), %rax
movl $0x1, 0x60(%rax)
movq -0x10(%rbp), %rdi
callq 0x1570
movq -0x10(%rbp), %rax
movslq 0xc(%rax), %rdi
shlq $0x0, %rdi
callq 0x1140
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movslq 0xc(%rax), %rdi
shlq $0x0, %rdi
callq 0x1140
movq %rax, -0x60(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x45fd
movq -0x10(%rbp), %rax
movslq 0xc(%rax), %rax
movslq -0x28(%rbp), %rcx
imulq %rcx, %rax
movq -0x10(%rbp), %rcx
movslq 0x10(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rsi
xorl %edx, %edx
callq 0x1160
movq -0x58(%rbp), %rdi
movq -0x10(%rbp), %rax
movslq 0xc(%rax), %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movl $0x1, %edx
callq 0x1090
movq -0x58(%rbp), %rax
movb (%rax), %cl
movq -0x60(%rbp), %rax
movb %cl, (%rax)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jge 0x45c0
movq -0x60(%rbp), %rdi
movq -0x30(%rbp), %rax
movslq -0x24(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
addq %rax, %rdi
movq -0x58(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x18(%rbp), %rcx
movslq -0x24(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movslq (%rax,%rcx,4), %rax
addq %rax, %rsi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x18(%rbp), %rcx
movslq -0x24(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movslq (%rax,%rcx,4), %rdx
callq 0x1130
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x455a
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rsi
xorl %edx, %edx
callq 0x1160
movq -0x60(%rbp), %rdi
movq -0x10(%rbp), %rax
movslq 0xc(%rax), %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movl $0x1, %edx
callq 0x11c0
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x44e9
movq -0x58(%rbp), %rdi
callq 0x1030
movq -0x60(%rbp), %rdi
callq 0x1030
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x1030
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x1030
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x1030
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x1030
movq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x38(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x40(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x48(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
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 $0x60, %rsp
popq %rbp
retq
nopl (%rax)