// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movl %ecx, %r13d
orl $-0x1, -0x30(%rbp)
movq %rdx, %rbx
movq %rsi, %r15
movl %ecx, %r14d
andl $0x4, %r14d
pushq $0x48
popq %rsi
movq %rdx, %rdi
callq 0x27aea
leaq -0xe0(%rbp), %rsi
movq %r15, -0x38(%rbp)
movq %r15, %rdi
callq 0xc120
testl %eax, %eax
je 0x2c4a5
pushq $0x10
popq %rdi
callq 0x2c912
testq %rax, %rax
je 0x2c4db
leaq 0x28(%rbx), %rcx
movq %rcx, -0x40(%rbp)
movq %rax, 0x28(%rbx)
testl %r14d, %r14d
sete -0x2c(%rbp)
movl %r13d, %eax
andl $0x2, %eax
shll $0x4, %r14d
leal (%r14,%rax), %ecx
testb $0x8, %r13b
leal 0x20080(%r14,%rax), %esi
cmovel %ecx, %esi
testb $0x10, %r13b
movl $0x1a4, %eax            # imm = 0x1A4
movl $0x180, %edx            # imm = 0x180
cmovel %eax, %edx
movq -0x38(%rbp), %rdi
xorl %eax, %eax
callq 0xc510
testl %eax, %eax
js 0x2c4e4
movl %eax, %r15d
movq -0x40(%rbp), %rax
movq (%rax), %r12
testq %r12, %r12
jne 0x2c541
jmp 0x2c54a
callq 0x2c9e3
movq -0xe0(%rbp), %rcx
movq -0xd8(%rbp), %rdx
leaq 0x5bc39(%rip), %rax     # 0x880f8
xorl %r12d, %r12d
movq (%rax), %rax
testq %rax, %rax
je 0x2c513
cmpq %rcx, (%rax)
jne 0x2c4d5
cmpq %rdx, 0x8(%rax)
je 0x2c4ed
addq $0x28, %rax
jmp 0x2c4c2
pushq $-0x1
popq %r13
jmp 0x2c6f1
pushq $-0x2
popq %r13
jmp 0x2c6e5
addq $0x20, %rax
movq (%rax), %r12
testq %r12, %r12
je 0x2c510
movq %rax, %rcx
leaq 0x8(%r12), %rax
cmpl %r13d, 0x4(%r12)
jne 0x2c4f1
movq (%rax), %rax
movq %rax, (%rcx)
jmp 0x2c513
xorl %r12d, %r12d
callq 0x2c9fe
testq %r12, %r12
je 0x2c42c
movl (%r12), %r15d
leaq 0x28(%rbx), %rax
movq %rax, -0x40(%rbp)
movq %r12, 0x28(%rbx)
testl %r14d, %r14d
sete -0x2c(%rbp)
testl %r15d, %r15d
js 0x2c450
movl %r15d, (%r12)
movl %r13d, 0x4(%r12)
testb $0x10, %r13b
je 0x2c559
movq -0x38(%rbp), %rdi
callq 0xc2c0
cmpb $0x0, -0x2c(%rbp)
jne 0x2c580
leaq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdi
callq 0x2c925
testl %eax, %eax
je 0x2c580
movl %eax, %r13d
movl %r15d, %edi
callq 0xc2b0
jmp 0x2c6e5
pushq $0x1
popq %rsi
movl %r15d, %edi
xorl %edx, %edx
xorl %eax, %eax
callq 0xc1d0
orl $0x1, %eax
pushq $0x2
popq %rsi
movl %r15d, %edi
movl %eax, %edx
xorl %eax, %eax
callq 0xc1d0
movl -0x30(%rbp), %eax
movl %r15d, 0x10(%rbx)
movl %eax, -0x2c(%rbp)
movl %eax, 0x14(%rbx)
andl $0x0, 0x30(%rbx)
movq -0x38(%rbp), %rax
movq %rax, 0x38(%rbx)
callq 0x2c9e3
movl 0x10(%rbx), %edi
leaq -0xe0(%rbp), %rsi
callq 0xc9e0
testl %eax, %eax
je 0x2c601
callq 0xcb50
movl (%rax), %eax
movl %eax, 0x1c(%rbx)
cmpl $0x4b, %eax
pushq $-0x11
popq %rax
pushq $-0x2
popq %r13
cmovel %eax, %r13d
pushq $-0x1
popq %r12
movl %r15d, %edi
callq 0xc2b0
xorl %r14d, %r14d
movl %r12d, %r15d
jmp 0x2c6ad
leaq -0x50(%rbp), %r12
pushq $0x10
popq %r13
movq %r12, %rdi
movl %r13d, %esi
callq 0x27aea
movups -0xe0(%rbp), %xmm0
movups %xmm0, (%r12)
leaq 0x5bad1(%rip), %r14     # 0x880f8
movq (%r14), %r14
testq %r14, %r14
je 0x2c647
movq %r12, %rdi
movq %r14, %rsi
movl %r13d, %edx
callq 0x2d30e
testl %eax, %eax
je 0x2c69f
addq $0x28, %r14
jmp 0x2c627
pushq $0x38
popq %rdi
callq 0x2c912
testq %rax, %rax
je 0x2c70b
movq %rax, %r14
pushq $0x38
popq %rsi
movq %rax, %rdi
callq 0x27aea
leaq -0x50(%rbp), %rdi
pushq $0x10
popq %rdx
movq %r14, %rsi
callq 0x2d3b0
movl $0x1, 0x18(%r14)
movq 0x5ba74(%rip), %rax     # 0x880f8
movq %rax, 0x28(%r14)
andq $0x0, 0x30(%r14)
testq %rax, %rax
je 0x2c696
movq %r14, 0x30(%rax)
movq %r14, 0x5ba5b(%rip)     # 0x880f8
jmp 0x2c6a3
incl 0x18(%r14)
movq %r14, 0x8(%rbx)
xorl %r13d, %r13d
movb $0x1, %r14b
callq 0x2c9fe
andl $0x0, 0x1c(%rbx)
testb %r14b, %r14b
je 0x2c6c7
leaq 0x5761e(%rip), %rax     # 0x83ce0
movq %rax, (%rbx)
jmp 0x2c6e0
movl -0x2c(%rbp), %edi
testl %edi, %edi
js 0x2c6d3
callq 0xc2b0
testl %r15d, %r15d
js 0x2c6e0
movl %r15d, %edi
callq 0xc2b0
testl %r13d, %r13d
je 0x2c706
movq -0x40(%rbp), %rax
movq (%rax), %rdi
callq 0x2c9cf
movl %r13d, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r13d, %r13d
jmp 0x2c6f1
pushq $-0x1
popq %r13
jmp 0x2c5ea