// File: /workspace/github_c_makefile_star2_O3/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 0x17ad
cmpb $0x0, 0x1(%r15)
je 0x17d9
cmpb $0x2b, 0x1(%r15)
jne 0x179f
cmpb $0x0, 0x2(%r15)
je 0x17e0
cmpb $0x62, 0x1(%r15)
jne 0x17ad
cmpb $0x0, 0x2(%r15)
je 0x17e0
leaq 0x28ba(%rip), %rsi      # 0x406e
movq %r15, %rdi
callq 0x1130
testl %eax, %eax
je 0x17e0
leaq 0x28ab(%rip), %rsi      # 0x4072
movq %r15, %rdi
callq 0x1130
testl %eax, %eax
jne 0x19d9
jmp 0x17e0
leaq 0x283d(%rip), %r15      # 0x401d
cmpb $0x72, (%r15)
jne 0x17fd
cmpb $0x2b, 0x1(%r15)
jne 0x17fd
cmpb $0x0, 0x2(%r15)
leaq 0x2875(%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 0x185d
movl %ecx, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movzbl (%r12,%rsi), %esi
addl $-0x2e, %esi
cmpl $0x2e, %esi
ja 0x1839
btq %rsi, %rax
jae 0x1839
movl %ecx, %edx
movslq %edx, %rax
cmpb $0x2e, (%r12,%rax)
jne 0x186c
movb $0x0, (%r12,%rax)
movq %r12, %rdi
callq 0x10d0
leaq 0x5(%rax), %rdi
callq 0x1160
movq %rax, %rbp
leaq 0x27ef(%rip), %rsi      # 0x4076
movq %rax, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x11d0
movl $0x1, %edi
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 0x18e1
leaq 0x27ba(%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 0x279c(%rip), %rsi      # 0x4084
movq %rbp, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x11d0
leaq 0x2770(%rip), %rsi      # 0x406c
movq %rbp, %rdi
callq 0x11b0
movq %rax, %r13
testq %rax, %rax
jne 0x1932
leaq 0x2778(%rip), %rsi      # 0x408b
movq %rbp, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x11d0
leaq 0x2745(%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 0x19c4
movl $0xffffffff, 0x40(%rbx) # imm = 0xFFFFFFFF
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x1160
movq %rax, %r15
movl $0x20, %esi
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rcx
callq 0x10a0
cmpq $0x1, %rax
jne 0x199d
movl 0x4(%r15), %eax
movl %eax, 0x8(%rbx)
movzwl 0x8(%r15), %ecx
movl %ecx, 0x10(%rbx)
movzwl 0xa(%r15), %eax
movl %eax, 0xc(%rbx)
movb 0x1d(%r15), %r12b
movzbl %r12b, %edx
movl %edx, 0x70(%rbx)
cmpl $0x1f, %ecx
ja 0x19ed
movq %r14, %rdi
callq 0x10c0
testq %r13, %r13
je 0x19b2
movq %r13, %rdi
callq 0x10c0
movq %r15, %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
jmp 0x19d9
movq %rbx, %rdi
callq 0x1030
testq %r13, %r13
je 0x19d9
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, 0x14(%rsp)
movq %rcx, 0x8(%rsp)
addl $-0x20, %ecx
movl %ecx, 0x4(%rsp)
movl %ecx, %ebp
shrl $0x5, %ebp
movl %ebp, 0x14(%rbx)
movl %eax, %edi
callq 0x1160
movq %rax, 0x20(%rsp)
movq %rax, 0x48(%rbx)
testq %r13, %r13
je 0x1a9e
movl $0x1f4, %edx            # imm = 0x1F4
movq %r15, %rdi
xorl %esi, %esi
callq 0x10f0
movl $0x1f3, %esi            # imm = 0x1F3
movl $0x1, %edx
movq %r15, %rdi
movq %r13, %rcx
callq 0x10a0
leaq 0x2649(%rip), %rsi      # 0x4092
movq %r15, %rdi
callq 0x1110
testq %rax, %rax
je 0x1a92
movq %rax, %r12
movb $0x0, (%r15,%rax)
incq %r12
movq %r12, %rdi
callq 0x1160
movq %rax, 0x18(%rsp)
movq %rax, 0x78(%rbx)
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x1150
movq %r13, %rdi
callq 0x10c0
cmpq $0x0, 0x18(%rsp)
jne 0x1ad8
jmp 0x1a9a
movq %r13, %rdi
callq 0x10c0
movb 0x1d(%r15), %r12b
testb %r12b, %r12b
je 0x1ad8
leaq 0x25eb(%rip), %rsi      # 0x4095
movq %r15, %rdi
movl 0x14(%rsp), %edx
xorl %eax, %eax
callq 0x11d0
movq %r15, %rdi
callq 0x10d0
leaq 0x1(%rax), %rdi
callq 0x1160
movq %rax, 0x78(%rbx)
movq %rax, %rdi
movq %r15, %rsi
callq 0x1080
movq %r15, %rdi
movq 0x8(%rsp), %rsi
callq 0x1bf7
movq %rax, %r15
movq %rax, 0x38(%rbx)
movl $0x20, %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x1180
movl 0x4(%rsp), %r12d
movl %r12d, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x10a0
cmpq $0x1, %rax
jne 0x1bdd
movl %ebp, %eax
movq %rax, 0x8(%rsp)
leal (,%rbp,4), %r13d
movq %r13, %rdi
callq 0x1160
movq %rax, %r14
movq %rax, 0x18(%rbx)
movq %r13, %rdi
callq 0x1160
movq %rax, %r12
movq %rax, 0x20(%rbx)
movq %r13, %rdi
callq 0x1160
movq %rax, %r13
movq %rax, 0x28(%rbx)
movq 0x8(%rsp), %rdi
callq 0x1160
movq %rax, 0x30(%rbx)
cmpl $0x20, 0x4(%rsp)
jb 0x19db
cmpl $0x1, %ebp
adcl $0x0, %ebp
shll $0x2, %ebp
xorl %ecx, %ecx
movzbl 0xb(%r15,%rcx,8), %edx
movl %edx, %esi
orl $0x8, %esi
cmpl $0x4e, %esi
jne 0x1b9f
movzbl 0x10(%r15,%rcx,8), %esi
movl %esi, (%r12,%rcx)
movzbl 0x11(%r15,%rcx,8), %esi
jmp 0x1bab
movzbl 0x10(%r15,%rcx,8), %esi
movl %esi, (%r12,%rcx)
xorl %esi, %esi
movl %esi, (%r13,%rcx)
movb %dl, (%rax)
testq %rcx, %rcx
je 0x1bc3
movl -0x4(%r12,%rcx), %edx
addl -0x4(%r14,%rcx), %edx
jmp 0x1bc8
movl $0x1, %edx
movl %edx, (%r14,%rcx)
addq $0x4, %rcx
incq %rax
cmpq %rcx, %rbp
jne 0x1b7d
jmp 0x19db
movq %r14, %rdi
callq 0x10c0
movq %r15, %rdi
callq 0x1030
movq 0x20(%rsp), %rdi
jmp 0x19b5