// File: /workspace/github_c_makefile_star2_O1/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 0x1d21
movl %ecx, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movzbl (%r14,%rsi), %esi
addl $-0x2e, %esi
cmpl $0x2e, %esi
ja 0x1cfd
btq %rsi, %rax
jae 0x1cfd
movl %ecx, %edx
movslq %edx, %rax
cmpb $0x2e, (%r14,%rax)
jne 0x1d30
movb $0x0, (%r14,%rax)
movq %r14, %rdi
callq 0x10d0
leaq 0x5(%rax), %rdi
callq 0x1160
movq %rax, %r15
leaq 0x232e(%rip), %rsi      # 0x4079
xorl %r13d, %r13d
movq %rax, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x11d0
leaq 0x2346(%rip), %rsi      # 0x40a8
movq %r15, %rdi
callq 0x11b0
testq %rax, %rax
je 0x1ed9
movq %rax, %r12
xorl %r13d, %r13d
xorl %edi, %edi
movq %rax, %rsi
callq 0x1100
movq %r12, %rdi
callq 0x10c0
leaq 0x22df(%rip), %rsi      # 0x4071
movq %r15, %rdi
callq 0x11b0
testq %rax, %rax
je 0x1ed9
movq %rax, %r12
leaq 0x22da(%rip), %rsi      # 0x4087
movq %r15, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x11d0
testq %rbx, %rbx
je 0x1e2d
leaq 0x22e5(%rip), %rsi      # 0x40ab
movl $0x5, %edx
movq %rbx, %rdi
callq 0x1070
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x1df2
leaq 0x5(%rbx), %rdi
callq 0x11c0
cmpl $0x100, %eax            # imm = 0x100
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmovll %eax, %ebp
testl %ebp, %ebp
jns 0x1e32
leaq 0x22b4(%rip), %rsi      # 0x40b1
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 0x1e32
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rbx, %rbx
je 0x1e3b
testl %ebp, %ebp
js 0x1e43
movq %r15, %rdi
callq 0x1060
movq %r14, %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movl $0x1, %edi
movl $0x80, %esi
callq 0x1120
movq %rax, %r13
xorl %eax, %eax
testl %ebp, %ebp
cmovlel %eax, %ebp
movq %r12, (%r13)
movl %eax, 0x8(%r13)
movl %eax, 0x14(%r13)
movabsq $0x2100000001, %rcx     # imm = 0x2100000001
movq %rcx, 0xc(%r13)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%r13)
movups %xmm0, 0x28(%r13)
movq %rax, 0x38(%r13)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %rcx, 0x40(%r13)
movq %rax, 0x48(%r13)
movl $0x1, 0x60(%r13)
movl %ebp, 0x70(%r13)
movq %rax, 0x78(%r13)
testq %rbx, %rbx
je 0x1ed9
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