// File: /workspace/github_c_makefile_star2_O0/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: unixOpen
static int unixOpen(
  unqlite_vfs *pVfs,           /* The VFS for which this is the xOpen method */
  const char *zPath,           /* Pathname of file to be opened */
  unqlite_file *pFile,         /* The file descriptor to be filled in */
  unsigned int flags           /* Input flags to control the opening */
){
  unixFile *p = (unixFile *)pFile;
  int fd = -1;                   /* File descriptor returned by open() */
  int dirfd = -1;                /* Directory file descriptor */
  int openFlags = 0;             /* Flags to pass to open() */
  int noLock;                    /* True to omit locking primitives */
  int rc = UNQLITE_OK;            /* Function Return Code */
  UnixUnusedFd *pUnused;
  int isExclusive  = (flags & UNQLITE_OPEN_EXCLUSIVE);
  int isDelete     = (flags & UNQLITE_OPEN_TEMP_DB);
  int isCreate     = (flags & UNQLITE_OPEN_CREATE);
  int isReadonly   = (flags & UNQLITE_OPEN_READONLY);
  int isReadWrite  = (flags & UNQLITE_OPEN_READWRITE);
  /* If creating a master or main-file journal, this function will open
  ** a file-descriptor on the directory too. The first time unixSync()
  ** is called the directory file descriptor will be fsync()ed and close()d.
  */
  int isOpenDirectory = isCreate ;
  const char *zName = zPath;

  SyZero(p,sizeof(unixFile));
  
  pUnused = findReusableFd(zName, flags);
  if( pUnused ){
	  fd = pUnused->fd;
  }else{
	  pUnused = (UnixUnusedFd *)unqlite_malloc(sizeof(*pUnused));
      if( !pUnused ){
        return UNQLITE_NOMEM;
      }
  }
  p->pUnused = pUnused;
  
  /* Determine the value of the flags parameter passed to POSIX function
  ** open(). These must be calculated even if open() is not called, as
  ** they may be stored as part of the file handle and used by the 
  ** 'conch file' locking functions later on.  */
  if( isReadonly )  openFlags |= O_RDONLY;
  if( isReadWrite ) openFlags |= O_RDWR;
  if( isCreate )    openFlags |= O_CREAT;
  if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
  openFlags |= (O_LARGEFILE|O_BINARY);

  if( fd<0 ){
    mode_t openMode;              /* Permissions to create file with */
    rc = findCreateFileMode(zName, flags, &openMode);
    if( rc!=UNQLITE_OK ){
      return rc;
    }
    fd = open(zName, openFlags, openMode);
    if( fd<0 ){
	  rc = UNQLITE_IOERR;
      goto open_finished;
    }
  }
  
  if( p->pUnused ){
    p->pUnused->fd = fd;
    p->pUnused->flags = flags;
  }

  if( isDelete ){
    unlink(zName);
  }

  if( isOpenDirectory ){
    rc = openDirectory(zPath, &dirfd);
    if( rc!=UNQLITE_OK ){
      /* It is safe to close fd at this point, because it is guaranteed not
      ** to be open on a database file. If it were open on a database file,
      ** it would not be safe to close as this would release any locks held
      ** on the file by this process.  */
      close(fd);             /* silently leak if fail, already in error */
      goto open_finished;
    }
  }

#ifdef FD_CLOEXEC
  fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
#endif

  noLock = 0;

#if defined(__APPLE__) 
  struct statfs fsInfo;
  if( fstatfs(fd, &fsInfo) == -1 ){
    ((unixFile*)pFile)->lastErrno = errno;
    if( dirfd>=0 ) close(dirfd); /* silently leak if fail, in error */
    close(fd); /* silently leak if fail, in error */
    return UNQLITE_IOERR;
  }
  if (0 == SyStrncmp("msdos", fsInfo.f_fstypename, 5)) {
    ((unixFile*)pFile)->fsFlags |= UNQLITE_FSFLAGS_IS_MSDOS;
  }
#endif
  
  rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
open_finished:
  if( rc!=UNQLITE_OK ){
    unqlite_free(p->pUnused);
  }
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0xffffffff, -0x34(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x24(%rbp), %eax
andl $0x8, %eax
movl %eax, -0x54(%rbp)
movl -0x24(%rbp), %eax
andl $0x10, %eax
movl %eax, -0x58(%rbp)
movl -0x24(%rbp), %eax
andl $0x4, %eax
movl %eax, -0x5c(%rbp)
movl -0x24(%rbp), %eax
andl $0x1, %eax
movl %eax, -0x60(%rbp)
movl -0x24(%rbp), %eax
andl $0x2, %eax
movl %eax, -0x64(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, -0x68(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x30(%rbp), %rdi
movl $0x48, %esi
callq 0x54280
movq -0x70(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x5baa0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x5b5a9
movq -0x50(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x34(%rbp)
jmp 0x5b5cc
movl $0x10, %edi
callq 0x5bc10
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x5b5ca
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5b73d
jmp 0x5b5cc
movq -0x50(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x28(%rax)
cmpl $0x0, -0x60(%rbp)
je 0x5b5e7
movl -0x3c(%rbp), %eax
orl $0x0, %eax
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x64(%rbp)
je 0x5b5f6
movl -0x3c(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x5c(%rbp)
je 0x5b605
movl -0x3c(%rbp), %eax
orl $0x40, %eax
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0x5b616
movl -0x3c(%rbp), %eax
orl $0x20080, %eax          # imm = 0x20080
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
orl $0x0, %eax
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x34(%rbp)
jge 0x5b671
movq -0x70(%rbp), %rdi
movl -0x24(%rbp), %esi
leaq -0x74(%rbp), %rdx
callq 0x5bc40
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x5b649
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x5b73d
movq -0x70(%rbp), %rdi
movl -0x3c(%rbp), %esi
movl -0x74(%rbp), %edx
movb $0x0, %al
callq 0xd600
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jge 0x5b66f
movl $0xfffffffe, -0x44(%rbp) # imm = 0xFFFFFFFE
jmp 0x5b724
jmp 0x5b671
movq -0x30(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x5b697
movl -0x34(%rbp), %ecx
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rax
movl %ecx, (%rax)
movl -0x24(%rbp), %ecx
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rax
movl %ecx, 0x4(%rax)
cmpl $0x0, -0x58(%rbp)
je 0x5b6a6
movq -0x70(%rbp), %rdi
callq 0xd360
cmpl $0x0, -0x68(%rbp)
je 0x5b6ce
movq -0x18(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x5bc90
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x5b6cc
movl -0x34(%rbp), %edi
callq 0xd350
jmp 0x5b724
jmp 0x5b6ce
movl -0x34(%rbp), %eax
movl %eax, -0x78(%rbp)
movl -0x34(%rbp), %edi
movl $0x1, %esi
xorl %edx, %edx
movb $0x0, %al
callq 0xd230
movl -0x78(%rbp), %edi
movl %eax, %edx
orl $0x1, %edx
movl $0x2, %esi
movb $0x0, %al
callq 0xd230
movl $0x0, -0x40(%rbp)
movq -0x10(%rbp), %rdi
movl -0x34(%rbp), %esi
movl -0x38(%rbp), %edx
movq -0x20(%rbp), %rcx
movq -0x18(%rbp), %r8
movl -0x40(%rbp), %r9d
movl -0x58(%rbp), %eax
movl %eax, (%rsp)
callq 0x5bd90
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x5b737
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x5be90
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)