// File: /workspace/github_c_makefile_star2_O1/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, %r12d
movq %rdx, %rbx
movq %rsi, %r14
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movl %ecx, %r15d
andl $0x4, %r15d
movl %ecx, %eax
andl $0x2, %eax
movq %rax, -0x38(%rbp)
movq $-0x4, %rax
movl $0x0, 0x4(%rbx,%rax)
addq $0x4, %rax
cmpq $0x44, %rax
jb 0x33d5a
leaq -0xe0(%rbp), %rsi
movq %r14, %rdi
callq 0xc110
testl %eax, %eax
movq %r14, -0x40(%rbp)
movl %r12d, -0x30(%rbp)
je 0x33d8c
xorl %r13d, %r13d
jmp 0x33df2
movl $0x4, 0x62ae2(%rip)     # 0x96878
leaq 0x62ab3(%rip), %rdi     # 0x96850
callq 0xc330
movq 0x6332f(%rip), %rax     # 0x970d8
testq %rax, %rax
je 0x33dd9
movq -0xe0(%rbp), %rcx
movq -0xd8(%rbp), %rdx
xorl %r13d, %r13d
cmpq %rcx, (%rax)
jne 0x33dce
cmpq %rdx, 0x8(%rax)
je 0x33ff7
movq 0x28(%rax), %rax
testq %rax, %rax
jne 0x33dbf
jmp 0x33ddc
xorl %r13d, %r13d
movl $0x4, 0x62a92(%rip)     # 0x96878
leaq 0x62a63(%rip), %rdi     # 0x96850
callq 0xc2d0
movl %r15d, %r12d
testq %r13, %r13
je 0x33e00
movl (%r13), %r14d
jmp 0x33e29
leaq 0x628b9(%rip), %rdi     # 0x966c0
movl $0x10, %esi
callq 0x343ed
movq %rax, %r13
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x340c7
movq %r13, 0x28(%rbx)
testl %r14d, %r14d
movq -0x40(%rbp), %r13
movl %r12d, %r15d
movl -0x30(%rbp), %r12d
jns 0x33e7e
movl %r15d, %eax
shll $0x4, %eax
movq -0x38(%rbp), %rdx
leal (%rax,%rdx), %ecx
testb $0x8, %r12b
leal 0x20080(%rax,%rdx), %esi
cmovel %ecx, %esi
testb $0x10, %r12b
movl $0x1a4, %eax            # imm = 0x1A4
movl $0x180, %edx            # imm = 0x180
cmovel %eax, %edx
movq %r13, %rdi
xorl %eax, %eax
callq 0xc510
movl %eax, %r14d
testl %eax, %eax
js 0x33fe7
movq 0x28(%rbx), %rax
testq %rax, %rax
je 0x33e8e
movl %r14d, (%rax)
movl %r12d, 0x4(%rax)
testb $0x10, %r12b
je 0x33e9c
movq %r13, %rdi
callq 0xc2c0
testl %r15d, %r15d
je 0x33eb9
leaq -0x2c(%rbp), %rsi
movq %r13, %rdi
callq 0x3433b
testl %eax, %eax
je 0x33eb9
movl %eax, %r15d
jmp 0x3409e
movl %r14d, %edi
movl $0x1, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0xc1b0
orl $0x1, %eax
movl %r14d, %edi
movl $0x2, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0xc1b0
movl -0x2c(%rbp), %r12d
movl %r14d, 0x10(%rbx)
movl %r12d, 0x14(%rbx)
movl $0x0, 0x30(%rbx)
movq %r13, 0x38(%rbx)
movl $0x4, 0x62979(%rip)     # 0x96878
leaq 0x6294a(%rip), %rdi     # 0x96850
callq 0xc330
movl 0x10(%rbx), %edi
leaq -0xe0(%rbp), %rsi
callq 0xc9b0
testl %eax, %eax
je 0x33f3f
callq 0xcb20
movl (%rax), %eax
movl %eax, 0x1c(%rbx)
cmpl $0x4b, %eax
movl $0xffffffef, %eax       # imm = 0xFFFFFFEF
movl $0xfffffffe, %r15d      # imm = 0xFFFFFFFE
cmovel %eax, %r15d
jmp 0x34057
movups -0xe0(%rbp), %xmm0
movaps %xmm0, -0x50(%rbp)
movq 0x63187(%rip), %r15     # 0x970d8
testq %r15, %r15
je 0x33f7b
leaq -0x50(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
movl $0x10, %edx
callq 0x34ebe
testl %eax, %eax
je 0x34044
movq 0x28(%r15), %r15
testq %r15, %r15
jne 0x33f5a
leaq 0x6273e(%rip), %rdi     # 0x966c0
movl $0x38, %esi
callq 0x343ed
testq %rax, %rax
je 0x34051
movq %rax, %r15
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
xorl %r13d, %r13d
movq %r13, 0x30(%rax)
leaq -0x50(%rbp), %rdi
movq %rax, %rsi
movl $0x10, %edx
callq 0x34fc5
movl $0x1, 0x18(%r15)
movq 0x6310b(%rip), %rax     # 0x970d8
movq %rax, 0x28(%r15)
movq %r13, 0x30(%r15)
testq %rax, %rax
je 0x33fde
movq %r15, 0x30(%rax)
movq %r15, 0x630f3(%rip)     # 0x970d8
jmp 0x34048
sarl $0x1f, %r14d
andl $-0x2, %r14d
movl %r14d, %r15d
jmp 0x340b2
movq 0x20(%rax), %r13
testq %r13, %r13
sete %cl
je 0x33ddc
cmpl %r12d, 0x4(%r13)
jne 0x34013
addq $0x20, %rax
jmp 0x34030
movq %r13, %rax
movq 0x8(%r13), %r13
testq %r13, %r13
sete %cl
je 0x33ddc
cmpl %r12d, 0x4(%r13)
jne 0x34013
addq $0x8, %rax
testb %cl, %cl
jne 0x33ddc
movq 0x8(%r13), %rcx
movq %rcx, (%rax)
jmp 0x33ddc
incl 0x18(%r15)
movq %r15, 0x8(%rbx)
xorl %r15d, %r15d
jmp 0x34057
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testl %r15d, %r15d
je 0x3406a
movl %r14d, %edi
callq 0xc2b0
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl $0x4, 0x62804(%rip)     # 0x96878
leaq 0x627d5(%rip), %rdi     # 0x96850
callq 0xc2d0
movl $0x0, 0x1c(%rbx)
testl %r15d, %r15d
je 0x340a8
testl %r12d, %r12d
js 0x34099
movl %r12d, %edi
callq 0xc2b0
testl %r14d, %r14d
js 0x340b2
movl %r14d, %edi
callq 0xc2b0
jmp 0x340b2
leaq 0x5ec21(%rip), %rax     # 0x92cd0
movq %rax, (%rbx)
testl %r15d, %r15d
je 0x340c7
movq 0x28(%rbx), %rsi
leaq 0x625fe(%rip), %rdi     # 0x966c0
callq 0x3503f
movl %r15d, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq