// File: /workspace/github_c_makefile_star2_O1/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, %r14
movq %rdi, %rbx
leaq 0x28e0(%rip), %rsi      # 0x406c
movq %r14, %rdi
callq 0x1130
testl %eax, %eax
je 0x17ea
leaq 0x28cf(%rip), %rsi      # 0x406e
movq %r14, %rdi
callq 0x1130
testl %eax, %eax
je 0x17f1
leaq 0x286b(%rip), %rsi      # 0x401d
movq %r14, %rdi
callq 0x1130
testl %eax, %eax
je 0x17f1
leaq 0x28ac(%rip), %rsi      # 0x4071
movq %r14, %rdi
callq 0x1130
testl %eax, %eax
je 0x17f1
leaq 0x289d(%rip), %rsi      # 0x4075
movq %r14, %rdi
callq 0x1130
testl %eax, %eax
jne 0x19f0
jmp 0x17f1
leaq 0x282c(%rip), %r14      # 0x401d
leaq 0x2876(%rip), %rsi      # 0x406e
movq %r14, %rdi
callq 0x1130
testl %eax, %eax
leaq 0x2868(%rip), %r15      # 0x4071
cmovneq %r14, %r15
movq %rbx, %rdi
callq 0x10d0
leaq 0x5(%rax), %rdi
callq 0x1160
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1080
movq %r14, %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 0x186d
movl %ecx, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movzbl (%r14,%rsi), %esi
addl $-0x2e, %esi
cmpl $0x2e, %esi
ja 0x1849
btq %rsi, %rax
jae 0x1849
movl %ecx, %edx
movslq %edx, %rax
cmpb $0x2e, (%r14,%rax)
jne 0x187c
movb $0x0, (%r14,%rax)
movq %r14, %rdi
callq 0x10d0
leaq 0x5(%rax), %rdi
callq 0x1160
movq %rax, %r13
leaq 0x27e2(%rip), %rsi      # 0x4079
movq %rax, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x11d0
movl $0x1, %edi
movl $0x80, %esi
callq 0x1120
movq %rax, %rbx
movq %r13, %rdi
movq %r15, %rsi
callq 0x11b0
movq %rax, (%rbx)
testq %rax, %rax
jne 0x18eb
leaq 0x27b0(%rip), %rsi      # 0x4080
movq %r13, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x11d0
movq %r13, %rdi
movq %r15, %rsi
callq 0x11b0
movq %rax, (%rbx)
leaq 0x2795(%rip), %rsi      # 0x4087
movq %r13, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x11d0
leaq 0x2766(%rip), %rsi      # 0x406c
movq %r13, %rdi
callq 0x11b0
movq %rax, %r12
testq %rax, %rax
jne 0x193c
leaq 0x2771(%rip), %rsi      # 0x408e
movq %r13, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x11d0
leaq 0x273b(%rip), %rsi      # 0x406c
movq %r13, %rdi
callq 0x11b0
movq %rax, %r12
movq %r14, %rdi
callq 0x1030
movq %r13, %rdi
callq 0x1030
movq (%rbx), %r14
testq %r14, %r14
je 0x19db
movl $0x0, 0x60(%rbx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x40(%rbx)
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 0x19b4
movl 0x4(%r15), %eax
movl %eax, 0x8(%rbx)
movzwl 0x8(%r15), %ebp
movl %ebp, 0x10(%rbx)
movzwl 0xa(%r15), %eax
movl %eax, 0xc(%rbx)
movzbl 0x1d(%r15), %ecx
movl %ecx, 0x70(%rbx)
cmpl $0x1f, %ebp
ja 0x1a04
movq %r14, %rdi
callq 0x10c0
testq %r12, %r12
je 0x19c9
movq %r12, %rdi
callq 0x10c0
movq %r15, %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
jmp 0x19f0
movq %rbx, %rdi
callq 0x1030
testq %r12, %r12
je 0x19f0
movq %r12, %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 %ecx, 0x1c(%rsp)
leal -0x20(%rbp), %r13d
movl %r13d, %ecx
shrl $0x5, %ecx
movq %rcx, 0x20(%rsp)
movl %ecx, 0x14(%rbx)
movl %eax, %edi
callq 0x1160
movq %rax, 0x10(%rsp)
movq %rax, 0x48(%rbx)
movq $0x0, 0x78(%rbx)
testq %r12, %r12
movl %r13d, 0xc(%rsp)
je 0x1aa6
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 %r12, %rcx
callq 0x10a0
leaq 0x262e(%rip), %rsi      # 0x4095
movq %r15, %rdi
callq 0x1110
testq %rax, %rax
je 0x1a99
movq %rax, %r13
movb $0x0, (%r15,%rax)
incq %r13
movq %r13, %rdi
callq 0x1160
movq %rax, 0x78(%rbx)
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x1150
movq %r12, %rdi
callq 0x10c0
movl 0xc(%rsp), %r13d
cmpq $0x0, 0x78(%rbx)
jne 0x1ae9
cmpb $0x0, 0x1d(%r15)
je 0x1ae9
leaq 0x25dd(%rip), %rsi      # 0x4098
movq %r15, %rdi
movl 0x1c(%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
movl %ebp, %esi
callq 0x1c08
movq %rax, %r15
movq %rax, 0x38(%rbx)
movl $0x20, %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x1180
movl %r13d, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x10a0
cmpq $0x1, %rax
jne 0x1bee
movq 0x20(%rsp), %rbp
movl %ebp, %eax
movq %rax, 0x10(%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 0x10(%rsp), %rdi
callq 0x1160
movq %rax, 0x30(%rbx)
cmpl $0x20, 0xc(%rsp)
jb 0x19f2
cmpl $0x1, %ebp
adcl $0x0, %ebp
shll $0x2, %ebp
xorl %ecx, %ecx
movzbl 0xb(%r15,%rcx,8), %edx
orl $0x8, %edx
cmpl $0x4e, %edx
jne 0x1bab
movzbl 0x10(%r15,%rcx,8), %edx
movl %edx, (%r12,%rcx)
movzbl 0x11(%r15,%rcx,8), %edx
jmp 0x1bb7
movzbl 0x10(%r15,%rcx,8), %edx
movl %edx, (%r12,%rcx)
xorl %edx, %edx
movl %edx, (%r13,%rcx)
movb 0xb(%r15,%rcx,8), %dl
movb %dl, (%rax)
testq %rcx, %rcx
je 0x1bd4
movl -0x4(%r12,%rcx), %edx
addl -0x4(%r14,%rcx), %edx
jmp 0x1bd9
movl $0x1, %edx
movl %edx, (%r14,%rcx)
addq $0x4, %rcx
incq %rax
cmpq %rcx, %rbp
jne 0x1b8b
jmp 0x19f2
movq %r14, %rdi
callq 0x10c0
movq %r15, %rdi
callq 0x1030
movq 0x10(%rsp), %rdi
jmp 0x19cc