// File: /workspace/github_c_makefile_star2_O3/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 0x1d10
movl %ecx, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movzbl (%r14,%rsi), %esi
addl $-0x2e, %esi
cmpl $0x2e, %esi
ja 0x1cec
btq %rsi, %rax
jae 0x1cec
movl %ecx, %edx
movslq %edx, %rax
cmpb $0x2e, (%r14,%rax)
jne 0x1d1f
movb $0x0, (%r14,%rax)
movq %r14, %rdi
callq 0x10d0
leaq 0x5(%rax), %rdi
callq 0x1160
movq %rax, %r15
leaq 0x233c(%rip), %rsi      # 0x4076
xorl %r13d, %r13d
movq %rax, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x11d0
leaq 0x2354(%rip), %rsi      # 0x40a5
movq %r15, %rdi
callq 0x11b0
testq %rax, %rax
je 0x1e9f
movq %rax, %r12
xorl %r13d, %r13d
xorl %edi, %edi
movq %rax, %rsi
callq 0x1100
movq %r12, %rdi
callq 0x10c0
leaq 0x22ed(%rip), %rsi      # 0x406e
movq %r15, %rdi
callq 0x11b0
testq %rax, %rax
je 0x1e9f
movq %rax, %r12
leaq 0x22e8(%rip), %rsi      # 0x4084
movq %r15, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x11d0
testq %rbx, %rbx
je 0x1e1b
leaq 0x22f3(%rip), %rsi      # 0x40a8
movl $0x5, %edx
movq %rbx, %rdi
callq 0x1070
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x1de4
leaq 0x5(%rbx), %rdi
callq 0x11c0
cmpl $0xff, %eax
jg 0x1de4
movl %eax, %r13d
movl %eax, %ebp
testl %eax, %eax
jns 0x1e21
leaq 0x22c3(%rip), %rsi      # 0x40ae
movq %r15, %rdi
callq 0x11b0
movq %rax, %r13
movq %rbx, %rdi
callq 0x10d0
movl $0x1, %edx
movq %rbx, %rdi
movq %rax, %rsi
movq %r13, %rcx
callq 0x11e0
movq %r13, %rdi
callq 0x10c0
jmp 0x1e2c
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq %r15, %rdi
callq 0x1060
movl %r13d, %ebp
movq %r14, %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movl $0x1, %edi
movl $0x80, %esi
callq 0x1120
xorl %ecx, %ecx
testl %ebp, %ebp
cmovgl %ebp, %ecx
movq %rax, %r13
movq %r12, (%rax)
movabsq $0x2100000001, %rax     # imm = 0x2100000001
movq %rax, 0xc(%r13)
movl $0xffffffff, 0x40(%r13) # imm = 0xFFFFFFFF
movl $0x1, 0x60(%r13)
movl %ecx, 0x70(%r13)
testq %rbx, %rbx
je 0x1e9f
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