// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x1f4, -0x54(%rbp)     # imm = 0x1F4
movq -0x18(%rbp), %rdi
leaq 0x381a(%rip), %rsi      # 0x50dc
callq 0x1110
cmpl $0x0, %eax
je 0x192d
movq -0x18(%rbp), %rdi
leaq 0x3807(%rip), %rsi      # 0x50de
callq 0x1110
cmpl $0x0, %eax
je 0x192d
movq -0x18(%rbp), %rdi
leaq 0x3731(%rip), %rsi      # 0x501d
callq 0x1110
cmpl $0x0, %eax
je 0x192d
movq -0x18(%rbp), %rdi
leaq 0x37e0(%rip), %rsi      # 0x50e1
callq 0x1110
cmpl $0x0, %eax
je 0x192d
movq -0x18(%rbp), %rdi
leaq 0x37cf(%rip), %rsi      # 0x50e5
callq 0x1110
cmpl $0x0, %eax
je 0x192d
movq $0x0, -0x8(%rbp)
jmp 0x1fde
movq -0x18(%rbp), %rdi
leaq 0x37a4(%rip), %rsi      # 0x50dc
callq 0x1110
cmpl $0x0, %eax
jne 0x194d
leaq 0x36d4(%rip), %rax      # 0x501d
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x3786(%rip), %rsi      # 0x50de
callq 0x1110
cmpl $0x0, %eax
jne 0x196d
leaq 0x3778(%rip), %rax      # 0x50e1
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rdi
callq 0x10c0
movq %rax, %rdi
addq $0x5, %rdi
callq 0x1140
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1070
movq -0x48(%rbp), %rdi
callq 0x10c0
subq $0x1, %rax
movl %eax, -0x40(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x40(%rbp)
movb %al, -0x69(%rbp)
jle 0x19ef
movq -0x48(%rbp), %rax
movslq -0x40(%rbp), %rcx
movsbl (%rax,%rcx), %ecx
xorl %eax, %eax
cmpl $0x2e, %ecx
movb %al, -0x69(%rbp)
je 0x19ef
movq -0x48(%rbp), %rax
movslq -0x40(%rbp), %rcx
movsbl (%rax,%rcx), %ecx
xorl %eax, %eax
cmpl $0x2f, %ecx
movb %al, -0x69(%rbp)
je 0x19ef
movq -0x48(%rbp), %rax
movslq -0x40(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x5c, %eax
setne %al
movb %al, -0x69(%rbp)
movb -0x69(%rbp), %al
testb $0x1, %al
jne 0x19f8
jmp 0x1a05
jmp 0x19fa
movl -0x40(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x19a3
movq -0x48(%rbp), %rax
movslq -0x40(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2e, %eax
jne 0x1a22
movq -0x48(%rbp), %rax
movslq -0x40(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x48(%rbp), %rdi
callq 0x10c0
movq %rax, %rdi
addq $0x5, %rdi
callq 0x1140
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rdx
leaq 0x369f(%rip), %rsi      # 0x50e9
movb $0x0, %al
callq 0x11b0
movl $0x1, %edi
movl $0x80, %esi
callq 0x1100
movq %rax, -0x20(%rbp)
movq -0x50(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x1190
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x1ab2
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rdx
leaq 0x365c(%rip), %rsi      # 0x50f0
movb $0x0, %al
callq 0x11b0
movq -0x50(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x1190
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rdx
leaq 0x3636(%rip), %rsi      # 0x50f7
movb $0x0, %al
callq 0x11b0
movq -0x50(%rbp), %rdi
leaq 0x3609(%rip), %rsi      # 0x50dc
callq 0x1190
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x1b0d
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rdx
leaq 0x360c(%rip), %rsi      # 0x50fe
movb $0x0, %al
callq 0x11b0
movq -0x50(%rbp), %rdi
leaq 0x35d8(%rip), %rsi      # 0x50dc
callq 0x1190
movq %rax, -0x28(%rbp)
movq -0x48(%rbp), %rdi
callq 0x1030
movq -0x50(%rbp), %rdi
callq 0x1030
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x1b4f
movq -0x20(%rbp), %rdi
callq 0x1030
cmpq $0x0, -0x28(%rbp)
je 0x1b42
movq -0x28(%rbp), %rdi
callq 0x10b0
movq $0x0, -0x8(%rbp)
jmp 0x1fde
movq -0x20(%rbp), %rax
movl $0x0, 0x60(%rax)
movq -0x20(%rbp), %rax
movl $0xffffffff, 0x40(%rax) # imm = 0xFFFFFFFF
movq -0x20(%rbp), %rax
movl $0x0, 0x44(%rax)
movslq -0x54(%rbp), %rdi
callq 0x1140
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rcx
movl $0x20, %esi
movl $0x1, %edx
callq 0x1090
cmpq $0x1, %rax
je 0x1bd8
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x10b0
cmpq $0x0, -0x28(%rbp)
je 0x1bb9
movq -0x28(%rbp), %rdi
callq 0x10b0
movq -0x30(%rbp), %rdi
callq 0x1030
movq -0x20(%rbp), %rdi
callq 0x1030
movq $0x0, -0x8(%rbp)
jmp 0x1fde
movq -0x30(%rbp), %rax
movzbl 0x4(%rax), %ecx
movq -0x30(%rbp), %rax
movzbl 0x5(%rax), %eax
shll $0x8, %eax
addl %eax, %ecx
movq -0x30(%rbp), %rax
movzbl 0x6(%rax), %eax
shll $0x8, %eax
shll $0x8, %eax
addl %eax, %ecx
movq -0x30(%rbp), %rax
movzbl 0x7(%rax), %eax
shll $0x8, %eax
shll $0x8, %eax
shll $0x8, %eax
addl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x30(%rbp), %rax
movzbl 0x8(%rax), %ecx
movq -0x30(%rbp), %rax
movzbl 0x9(%rax), %eax
shll $0x8, %eax
addl %eax, %ecx
movl %ecx, -0x38(%rbp)
movq -0x20(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x30(%rbp), %rax
movzbl 0xa(%rax), %ecx
movq -0x30(%rbp), %rax
movzbl 0xb(%rax), %eax
shll $0x8, %eax
addl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x30(%rbp), %rax
movzbl 0x1d(%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x70(%rax)
cmpl $0x20, -0x38(%rbp)
jge 0x1ca2
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x10b0
cmpq $0x0, -0x28(%rbp)
je 0x1c83
movq -0x28(%rbp), %rdi
callq 0x10b0
movq -0x30(%rbp), %rdi
callq 0x1030
movq -0x20(%rbp), %rdi
callq 0x1030
movq $0x0, -0x8(%rbp)
jmp 0x1fde
movl -0x38(%rbp), %eax
subl $0x20, %eax
movl $0x20, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl %ecx, -0x34(%rbp)
movq -0x20(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x20(%rbp), %rax
movslq 0xc(%rax), %rdi
callq 0x1140
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x78(%rax)
cmpq $0x0, -0x28(%rbp)
je 0x1d76
movq -0x30(%rbp), %rdi
movslq -0x54(%rbp), %rdx
xorl %esi, %esi
callq 0x10e0
movq -0x30(%rbp), %rdi
movl -0x54(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
movq -0x28(%rbp), %rcx
movl $0x1, %edx
callq 0x1090
movq -0x30(%rbp), %rdi
leaq 0x33e5(%rip), %rsi      # 0x5105
callq 0x10f0
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jbe 0x1d6d
movq -0x30(%rbp), %rax
movq -0x60(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x60(%rbp), %rdi
addq $0x1, %rdi
callq 0x1140
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x78(%rax)
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq $0x1, %rdx
callq 0x1130
movq -0x28(%rbp), %rdi
callq 0x10b0
movq -0x20(%rbp), %rax
cmpq $0x0, 0x78(%rax)
jne 0x1dd8
movq -0x30(%rbp), %rax
movzbl 0x1d(%rax), %eax
cmpl $0x0, %eax
je 0x1dd8
movq -0x30(%rbp), %rdi
movq -0x20(%rbp), %rax
movl 0x70(%rax), %edx
leaq 0x3368(%rip), %rsi      # 0x5108
movb $0x0, %al
callq 0x11b0
movq -0x30(%rbp), %rdi
callq 0x10c0
movq %rax, %rdi
addq $0x1, %rdi
callq 0x1140
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x78(%rax)
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x1070
movq -0x30(%rbp), %rdi
movl -0x38(%rbp), %esi
callq 0x1ff0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x20(%rbp), %rax
movq (%rax), %rdi
movl $0x20, %esi
xorl %edx, %edx
callq 0x1160
movq -0x30(%rbp), %rdi
movl -0x38(%rbp), %eax
subl $0x20, %eax
movslq %eax, %rsi
movq -0x20(%rbp), %rax
movq (%rax), %rcx
movl $0x1, %edx
callq 0x1090
cmpq $0x1, %rax
je 0x1e63
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x10b0
movq -0x30(%rbp), %rdi
callq 0x1030
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rdi
callq 0x1030
movq -0x20(%rbp), %rdi
callq 0x1030
movq $0x0, -0x8(%rbp)
jmp 0x1fde
movslq -0x34(%rbp), %rdi
shlq $0x2, %rdi
callq 0x1140
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x18(%rax)
movslq -0x34(%rbp), %rdi
shlq $0x2, %rdi
callq 0x1140
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x20(%rax)
movslq -0x34(%rbp), %rdi
shlq $0x2, %rdi
callq 0x1140
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x28(%rax)
movslq -0x34(%rbp), %rdi
shlq $0x0, %rdi
callq 0x1140
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x30(%rax)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x1fd6
movq -0x30(%rbp), %rax
movl -0x3c(%rbp), %ecx
shll $0x5, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movzbl 0xb(%rax), %eax
cmpl $0x4e, %eax
je 0x1f04
movq -0x68(%rbp), %rax
movzbl 0xb(%rax), %eax
cmpl $0x46, %eax
jne 0x1f34
movq -0x68(%rbp), %rax
movzbl 0x10(%rax), %edx
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x3c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x68(%rbp), %rax
movzbl 0x11(%rax), %edx
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x3c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x1f5e
movq -0x68(%rbp), %rax
movzbl 0x10(%rax), %edx
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x3c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x3c(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movq -0x68(%rbp), %rax
movb 0xb(%rax), %dl
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x3c(%rbp), %rcx
movb %dl, (%rax,%rcx)
cmpl $0x0, -0x3c(%rbp)
jne 0x1f8f
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x3c(%rbp), %rcx
movl $0x1, (%rax,%rcx,4)
jmp 0x1fc6
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x3c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x3c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
addl (%rax,%rcx,4), %edx
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x3c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x1fc8
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1eca
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)