// File: /workspace/github_c_makefile_star2_O2/bmottershead[P]DBF2TSV/dbf.c
// Function: DBFCreateEx
DBFHandle  DBFCreateEx(const char *pszFilename, const char *pszCodePage) {
  DBFHandle psDBF;
  FILE* fp;
  char *pszFullname, *pszBasename;
  int i, ldid = -1;
  char chZero = '\0';

  /* 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--) {/* empty loop */ }
  if (pszBasename[i] == '.')
    pszBasename[i] = '\0';
  pszFullname = (char *) malloc(strlen(pszBasename) + 5);
  sprintf(pszFullname, "%s.dbf", pszBasename);

  /* Create the file. */
  fp = fopen(pszFullname, "wb");
  if (fp == NULL)
    return (NULL);
  fwrite(&chZero, 1, 1, fp);
  fclose(fp);

  fp = fopen(pszFullname, "rb+");
  if (fp == NULL)
    return (NULL);

  sprintf(pszFullname, "%s.cpg", pszBasename);
  if (pszCodePage != NULL) {
    if (strncmp(pszCodePage, "LDID/", 5) == 0) {
      ldid = atoi(pszCodePage + 5);
      if (ldid > 255)
        ldid = -1;  // don't use 0 to indicate out of range as LDID/0 is a valid one
    }
    if (ldid < 0) {
      FILE* fpCPG = fopen(pszFullname, "w");
      fwrite((char *) pszCodePage, strlen(pszCodePage), 1, fpCPG);
      fclose(fpCPG);
    }
  }
  if (pszCodePage == NULL || ldid >= 0)
    remove(pszFullname);
  free(pszBasename);
  free(pszFullname);

  /* Create the info structure. */
  psDBF = (DBFHandle) calloc(1, sizeof(DBFInfo));
  psDBF->fp = fp;
  psDBF->nRecords = 0;
  psDBF->nFields = 0;
  psDBF->nRecordLength = 1;
  psDBF->nHeaderLength = 33;
  psDBF->panFieldOffset = NULL;
  psDBF->panFieldSize = NULL;
  psDBF->panFieldDecimals = NULL;
  psDBF->pachFieldType = NULL;
  psDBF->pszHeader = NULL;
  psDBF->nCurrentRecord = -1;
  psDBF->bCurrentRecordModified = FALSE;
  psDBF->pszCurrentRecord = NULL;
  psDBF->bNoHeader = TRUE;
  psDBF->iLanguageDriver = ldid > 0 ? ldid : 0;
  psDBF->pszCodePage = NULL;
  if (pszCodePage) {
    psDBF->pszCodePage = (char *) malloc(strlen(pszCodePage) + 1);
    strcpy(psDBF->pszCodePage, pszCodePage);
  }
  return (psDBF);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r15
callq 0x10d0
leaq 0x5(%rax), %rdi
callq 0x1160
movq %rax, %r14
movq %rax, %rdi
movq %r15, %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 0x1c7f
movl %ecx, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movzbl (%r14,%rsi), %esi
addl $-0x2e, %esi
cmpl $0x2e, %esi
ja 0x1c5b
btq %rsi, %rax
jae 0x1c5b
movl %ecx, %edx
movslq %edx, %rax
cmpb $0x2e, (%r14,%rax)
jne 0x1c8e
movb $0x0, (%r14,%rax)
movq %r14, %rdi
callq 0x10d0
leaq 0x5(%rax), %rdi
callq 0x1160
movq %rax, %r15
leaq 0x23cd(%rip), %rsi      # 0x4076
xorl %r13d, %r13d
movq %rax, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x11d0
leaq 0x23e5(%rip), %rsi      # 0x40a5
movq %r15, %rdi
callq 0x11b0
testq %rax, %rax
je 0x1e01
movq %rax, %r12
xorl %r13d, %r13d
xorl %edi, %edi
movq %rax, %rsi
callq 0x1100
movq %r12, %rdi
callq 0x10c0
leaq 0x237e(%rip), %rsi      # 0x406e
movq %r15, %rdi
callq 0x11b0
testq %rax, %rax
je 0x1e01
movq %rax, %r12
leaq 0x2379(%rip), %rsi      # 0x4084
movq %r15, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x11d0
testq %rbx, %rbx
je 0x1d84
leaq 0x2384(%rip), %rsi      # 0x40a8
pushq $0x5
popq %rdx
movq %rbx, %rdi
callq 0x1070
pushq $-0x1
popq %rbp
testl %eax, %eax
jne 0x1d4f
leaq 0x5(%rbx), %rdi
callq 0x11c0
cmpl $0xff, %eax
jg 0x1d4f
movl %eax, %r13d
movl %eax, %ebp
testl %eax, %eax
jns 0x1d88
leaq 0x2358(%rip), %rsi      # 0x40ae
movq %r15, %rdi
callq 0x11b0
movq %rax, %r13
movq %rbx, %rdi
callq 0x10d0
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %rax, %rsi
movq %r13, %rcx
callq 0x11e0
movq %r13, %rdi
callq 0x10c0
jmp 0x1d93
pushq $-0x1
popq %r13
movq %r15, %rdi
callq 0x1060
movl %r13d, %ebp
movq %r14, %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
pushq $0x1
popq %rdi
movl $0x80, %esi
callq 0x1120
movq %rax, %r13
orl $-0x1, 0x40(%rax)
xorl %eax, %eax
testl %ebp, %ebp
cmovgl %ebp, %eax
movq %r12, (%r13)
movabsq $0x2100000001, %rcx     # imm = 0x2100000001
movq %rcx, 0xc(%r13)
movl $0x1, 0x60(%r13)
movl %eax, 0x70(%r13)
testq %rbx, %rbx
je 0x1e01
movq %rbx, %rdi
callq 0x10d0
leaq 0x1(%rax), %rdi
callq 0x1160
movq %rax, 0x78(%r13)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1080
movq %r13, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq