// File: /workspace/github_c_makefile_star2_O2/bmottershead[P]DBF2TSV/dbf.c
// Function: DBFOpen
DBFHandle  DBFOpen(const char *pszFilename, const char *pszAccess) {
  DBFHandle psDBF;
  FILE* pfCPG;
  unsigned char *pabyBuf;
  int nFields, nHeadLen, iField, i;
  char *pszBasename, *pszFullname;
  int nBufSize = 500;

  /* We only allow the access strings "rb" and "r+". */
  if (strcmp(pszAccess, "r") != 0 && strcmp(pszAccess, "r+") != 0
      && strcmp(pszAccess, "rb") != 0 && strcmp(pszAccess, "rb+") != 0
      && strcmp(pszAccess, "r+b") != 0)
    return (NULL);

  if (strcmp(pszAccess, "r") == 0)
    pszAccess = "rb";
  if (strcmp(pszAccess, "r+") == 0)
    pszAccess = "rb+";

  /* Compute the base (layer) name.  If there is any extension */
  /* on the passed in filename we will strip it off. */
  pszBasename = (char *) malloc(strlen(pszFilename) + 5);
  strcpy(pszBasename, pszFilename);
  for (i = strlen(pszBasename) - 1;
       i > 0 && pszBasename[i] != '.' && pszBasename[i] != '/'
         && pszBasename[i] != '\\'; i--) {
  }

  if (pszBasename[i] == '.')
    pszBasename[i] = '\0';

  pszFullname = (char *) malloc(strlen(pszBasename) + 5);
  sprintf(pszFullname, "%s.dbf", pszBasename);
  psDBF = (DBFHandle) calloc(1, sizeof(DBFInfo));
  psDBF->fp = fopen(pszFullname, pszAccess);
  if (psDBF->fp == NULL) {
    sprintf(pszFullname, "%s.DBF", pszBasename);
    psDBF->fp = fopen(pszFullname, pszAccess);
  }
  sprintf(pszFullname, "%s.cpg", pszBasename);
  pfCPG = fopen(pszFullname, "r");
  if (pfCPG == NULL) {
    sprintf(pszFullname, "%s.CPG", pszBasename);
    pfCPG = fopen(pszFullname, "r");
  }

  free(pszBasename);
  free(pszFullname);

  if (psDBF->fp == NULL) {
    free(psDBF);
    if (pfCPG)
      fclose(pfCPG);
    return (NULL);
  }

  psDBF->bNoHeader = FALSE;
  psDBF->nCurrentRecord = -1;
  psDBF->bCurrentRecordModified = FALSE;

  /* Read Table Header info */
  pabyBuf = (unsigned char *) malloc(nBufSize);
  if (fread(pabyBuf, 32, 1, psDBF->fp) != 1) {
    fclose(psDBF->fp);
    if (pfCPG)
      fclose(pfCPG);
    free(pabyBuf);
    free(psDBF);
    return NULL;
  }

  psDBF->nRecords =
    pabyBuf[4] + pabyBuf[5] * 256 + pabyBuf[6] * 256 * 256 +
    pabyBuf[7] * 256 * 256 * 256;

  psDBF->nHeaderLength = nHeadLen = pabyBuf[8] + pabyBuf[9] * 256;
  psDBF->nRecordLength = pabyBuf[10] + pabyBuf[11] * 256;
  psDBF->iLanguageDriver = pabyBuf[29];

  if (nHeadLen < 32) {
    fclose(psDBF->fp);
    if (pfCPG)
      fclose(pfCPG);
    free(pabyBuf);
    free(psDBF);
    return NULL;
  }

  psDBF->nFields = nFields = (nHeadLen - 32) / 32;
  psDBF->pszCurrentRecord = (char *) malloc(psDBF->nRecordLength);

  /* Figure out the code page from the LDID and CPG */
  psDBF->pszCodePage = NULL;
  if (pfCPG) {
    size_t n;
    memset(pabyBuf, 0, nBufSize);
    fread(pabyBuf, nBufSize - 1, 1, pfCPG);
    n = strcspn((char *) pabyBuf, "\n\r");
    if (n > 0) {
      pabyBuf[n] = '\0';
      psDBF->pszCodePage = (char *) malloc(n + 1);
      memcpy(psDBF->pszCodePage, pabyBuf, n + 1);
    }
    fclose(pfCPG);
  }
  if (psDBF->pszCodePage == NULL && pabyBuf[29] != 0) {
    sprintf((char *) pabyBuf, "LDID/%d", psDBF->iLanguageDriver);
    psDBF->pszCodePage = (char *) malloc(strlen((char *) pabyBuf) + 1);
    strcpy(psDBF->pszCodePage, (char *) pabyBuf);
  }


  /* Read in Field Definitions */
  pabyBuf = (unsigned char *) SfRealloc(pabyBuf, nHeadLen);
  psDBF->pszHeader = (char *) pabyBuf;

  fseek(psDBF->fp, 32, 0);
  if (fread(pabyBuf, nHeadLen - 32, 1, psDBF->fp) != 1) {
    fclose(psDBF->fp);
    free(pabyBuf);
    free(psDBF->pszCurrentRecord);
    free(psDBF);
    return NULL;
  }

  psDBF->panFieldOffset = (int *) malloc(sizeof(int) * nFields);
  psDBF->panFieldSize = (int *) malloc(sizeof(int) * nFields);
  psDBF->panFieldDecimals = (int *) malloc(sizeof(int) * nFields);
  psDBF->pachFieldType = (char *) malloc(sizeof(char) * nFields);

  for (iField = 0; iField < nFields; iField++) {
    unsigned char *pabyFInfo;

    pabyFInfo = pabyBuf + iField * 32;
    if (pabyFInfo[11] == 'N' || pabyFInfo[11] == 'F') {
      psDBF->panFieldSize[iField] = pabyFInfo[16];
      psDBF->panFieldDecimals[iField] = pabyFInfo[17];
    } else {
      psDBF->panFieldSize[iField] = pabyFInfo[16];
      psDBF->panFieldDecimals[iField] = 0;
    }
    psDBF->pachFieldType[iField] = (char) pabyFInfo[11];
    if (iField == 0)
      psDBF->panFieldOffset[iField] = 1;
    else
      psDBF->panFieldOffset[iField] =
        psDBF->panFieldOffset[iField - 1] +
        psDBF->panFieldSize[iField - 1];
  }

  return (psDBF);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r15
movq %rdi, %rbx
cmpb $0x72, (%rsi)
jne 0x1745
cmpb $0x0, 0x1(%r15)
je 0x1771
cmpb $0x2b, 0x1(%r15)
jne 0x1737
cmpb $0x0, 0x2(%r15)
je 0x1778
cmpb $0x62, 0x1(%r15)
jne 0x1745
cmpb $0x0, 0x2(%r15)
je 0x1778
leaq 0x2922(%rip), %rsi      # 0x406e
movq %r15, %rdi
callq 0x1130
testl %eax, %eax
je 0x1778
leaq 0x2913(%rip), %rsi      # 0x4072
movq %r15, %rdi
callq 0x1130
testl %eax, %eax
jne 0x196c
jmp 0x1778
leaq 0x28a5(%rip), %r15      # 0x401d
cmpb $0x72, (%r15)
jne 0x1795
cmpb $0x2b, 0x1(%r15)
jne 0x1795
cmpb $0x0, 0x2(%r15)
leaq 0x28dd(%rip), %rax      # 0x406e
cmoveq %rax, %r15
movq %rbx, %rdi
callq 0x10d0
leaq 0x5(%rax), %rdi
callq 0x1160
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1080
movq %r12, %rdi
callq 0x10d0
movl %eax, %ecx
decl %eax
movl %eax, %edx
sarl $0x1f, %edx
andl %eax, %edx
movabsq $0x400000000003, %rax   # imm = 0x400000000003
decq %rcx
testl %ecx, %ecx
jle 0x17f5
movl %ecx, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movzbl (%r12,%rsi), %esi
addl $-0x2e, %esi
cmpl $0x2e, %esi
ja 0x17d1
btq %rsi, %rax
jae 0x17d1
movl %ecx, %edx
movslq %edx, %rax
cmpb $0x2e, (%r12,%rax)
jne 0x1804
movb $0x0, (%r12,%rax)
movq %r12, %rdi
callq 0x10d0
leaq 0x5(%rax), %rdi
callq 0x1160
movq %rax, %rbp
leaq 0x2857(%rip), %rsi      # 0x4076
movq %rax, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x11d0
pushq $0x1
popq %rdi
movl $0x80, %esi
callq 0x1120
movq %rax, %rbx
movq %rbp, %rdi
movq %r15, %rsi
callq 0x11b0
movq %rax, %r14
movq %rax, (%rbx)
testq %rax, %rax
jne 0x1877
leaq 0x2824(%rip), %rsi      # 0x407d
movq %rbp, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x11d0
movq %rbp, %rdi
movq %r15, %rsi
callq 0x11b0
movq %rax, %r14
movq %rax, (%rbx)
leaq 0x2806(%rip), %rsi      # 0x4084
movq %rbp, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x11d0
leaq 0x27da(%rip), %rsi      # 0x406c
movq %rbp, %rdi
callq 0x11b0
movq %rax, %r13
testq %rax, %rax
jne 0x18c8
leaq 0x27e2(%rip), %rsi      # 0x408b
movq %rbp, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x11d0
leaq 0x27af(%rip), %rsi      # 0x406c
movq %rbp, %rdi
callq 0x11b0
movq %rax, %r13
movq %r12, %rdi
callq 0x1030
movq %rbp, %rdi
callq 0x1030
testq %r14, %r14
je 0x1957
orl $-0x1, 0x40(%rbx)
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x1160
movq %rax, %r12
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rdi
movq %r14, %rcx
callq 0x10a0
cmpq $0x1, %rax
jne 0x1930
movl 0x4(%r12), %eax
movl %eax, 0x8(%rbx)
movzwl 0x8(%r12), %ecx
movl %ecx, 0x10(%rbx)
movzwl 0xa(%r12), %eax
movl %eax, 0xc(%rbx)
movb 0x1d(%r12), %bpl
movzbl %bpl, %edx
movl %edx, 0x70(%rbx)
cmpl $0x1f, %ecx
ja 0x1980
movq %r14, %rdi
callq 0x10c0
testq %r13, %r13
je 0x1945
movq %r13, %rdi
callq 0x10c0
movq %r12, %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
jmp 0x196c
movq %rbx, %rdi
callq 0x1030
testq %r13, %r13
je 0x196c
movq %r13, %rdi
callq 0x10c0
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %edx, 0xc(%rsp)
movq %rcx, 0x20(%rsp)
addl $-0x20, %ecx
movl %ecx, (%rsp)
movl %ecx, %r15d
shrl $0x5, %r15d
movl %r15d, 0x14(%rbx)
movl %eax, %edi
callq 0x1160
movq %rax, 0x18(%rsp)
movq %rax, 0x48(%rbx)
testq %r13, %r13
je 0x1a2e
movl $0x1f4, %edx            # imm = 0x1F4
movq %r12, %rdi
xorl %esi, %esi
callq 0x10f0
pushq $0x1
popq %rdx
movl $0x1f3, %esi            # imm = 0x1F3
movq %r12, %rdi
movq %r13, %rcx
callq 0x10a0
leaq 0x26ba(%rip), %rsi      # 0x4092
movq %r12, %rdi
callq 0x1110
testq %rax, %rax
je 0x1a21
movq %rax, %rbp
movb $0x0, (%r12,%rax)
incq %rbp
movq %rbp, %rdi
callq 0x1160
movq %rax, 0x10(%rsp)
movq %rax, 0x78(%rbx)
movq %rax, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x1150
movq %r13, %rdi
callq 0x10c0
cmpq $0x0, 0x10(%rsp)
jne 0x1a68
jmp 0x1a29
movq %r13, %rdi
callq 0x10c0
movb 0x1d(%r12), %bpl
testb %bpl, %bpl
je 0x1a68
leaq 0x265b(%rip), %rsi      # 0x4095
movq %r12, %rdi
movl 0xc(%rsp), %edx
xorl %eax, %eax
callq 0x11d0
movq %r12, %rdi
callq 0x10d0
leaq 0x1(%rax), %rdi
callq 0x1160
movq %rax, 0x78(%rbx)
movq %rax, %rdi
movq %r12, %rsi
callq 0x1080
movq %r12, %rdi
movq 0x20(%rsp), %rsi
callq 0x1b66
movq %rax, %r12
movq %rax, 0x38(%rbx)
pushq $0x20
popq %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x1180
movl (%rsp), %esi
pushq $0x1
popq %rdx
movq %r12, %rdi
movq %r14, %rcx
callq 0x10a0
cmpq $0x1, %rax
jne 0x1b4c
movl %r15d, %eax
movq %rax, (%rsp)
shll $0x2, %r15d
movq %r15, %rdi
callq 0x1160
movq %rax, %r14
movq %rax, 0x18(%rbx)
movq %r15, %rdi
callq 0x1160
movq %rax, %r13
movq %rax, 0x20(%rbx)
movq %r15, %rdi
callq 0x1160
movq %rax, %rbp
movq %rax, 0x28(%rbx)
movq (%rsp), %rdi
callq 0x1160
movq %rax, 0x30(%rbx)
xorl %ecx, %ecx
pushq $0x1
popq %rdx
cmpq %rcx, %r15
je 0x196e
movzbl 0xb(%r12,%rcx,8), %esi
movl %esi, %edi
orl $0x8, %edi
cmpl $0x4e, %edi
jne 0x1b1a
movzbl 0x10(%r12,%rcx,8), %edi
movl %edi, (%r13,%rcx)
movzbl 0x11(%r12,%rcx,8), %edi
jmp 0x1b27
movzbl 0x10(%r12,%rcx,8), %edi
movl %edi, (%r13,%rcx)
xorl %edi, %edi
movl %edi, (%rbp,%rcx)
movb %sil, (%rax)
movl %edx, %esi
testq %rcx, %rcx
je 0x1b3f
movl -0x4(%r13,%rcx), %esi
addl -0x4(%r14,%rcx), %esi
movl %esi, (%r14,%rcx)
addq $0x4, %rcx
incq %rax
jmp 0x1aee
movq %r14, %rdi
callq 0x10c0
movq %r12, %rdi
callq 0x1030
movq 0x18(%rsp), %rdi
jmp 0x1948