// File: /workspace/github_c_makefile_star2_O0/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: unixOpenSharedMemory
static int unixOpenSharedMemory(unixFile *pDbFd){
  struct unixShm *p = 0;          /* The connection to be opened */
  struct unixShmNode *pShmNode;   /* The underlying mmapped file */
  int rc = SQLITE_OK;             /* Result code */
  unixInodeInfo *pInode;          /* The inode of fd */
  char *zShm;             /* Name of the file used for SHM */
  int nShmFilename;               /* Size of the SHM filename in bytes */

  /* Allocate space for the new unixShm object. */
  p = sqlite3_malloc64( sizeof(*p) );
  if( p==0 ) return SQLITE_NOMEM_BKPT;
  memset(p, 0, sizeof(*p));
  assert( pDbFd->pShm==0 );

  /* Check to see if a unixShmNode object already exists. Reuse an existing
  ** one if present. Create a new one if necessary.
  */
  assert( unixFileMutexNotheld(pDbFd) );
  unixEnterMutex();
  pInode = pDbFd->pInode;
  pShmNode = pInode->pShmNode;
  if( pShmNode==0 ){
    struct stat sStat;                 /* fstat() info for database file */
#ifndef SQLITE_SHM_DIRECTORY
    const char *zBasePath = pDbFd->zPath;
#endif

    /* Call fstat() to figure out the permissions on the database file. If
    ** a new *-shm file is created, an attempt will be made to create it
    ** with the same permissions.
    */
    if( osFstat(pDbFd->h, &sStat) ){
      rc = SQLITE_IOERR_FSTAT;
      goto shm_open_err;
    }

#ifdef SQLITE_SHM_DIRECTORY
    nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
#else
    nShmFilename = 6 + (int)strlen(zBasePath);
#endif
    pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename );
    if( pShmNode==0 ){
      rc = SQLITE_NOMEM_BKPT;
      goto shm_open_err;
    }
    memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
    zShm = pShmNode->zFilename = (char*)&pShmNode[1];
#ifdef SQLITE_SHM_DIRECTORY
    sqlite3_snprintf(nShmFilename, zShm,
                     SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
                     (u32)sStat.st_ino, (u32)sStat.st_dev);
#else
    sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath);
    sqlite3FileSuffix3(pDbFd->zPath, zShm);
#endif
    pShmNode->hShm = -1;
    pDbFd->pInode->pShmNode = pShmNode;
    pShmNode->pInode = pDbFd->pInode;
    if( sqlite3GlobalConfig.bCoreMutex ){
      pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
      if( pShmNode->pShmMutex==0 ){
        rc = SQLITE_NOMEM_BKPT;
        goto shm_open_err;
      }
    }

    if( pInode->bProcessLock==0 ){
      if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
        pShmNode->hShm = robust_open(zShm, O_RDWR|O_CREAT|O_NOFOLLOW,
                                     (sStat.st_mode&0777));
      }
      if( pShmNode->hShm<0 ){
        pShmNode->hShm = robust_open(zShm, O_RDONLY|O_NOFOLLOW,
                                     (sStat.st_mode&0777));
        if( pShmNode->hShm<0 ){
          rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShm);
          goto shm_open_err;
        }
        pShmNode->isReadonly = 1;
      }

      /* If this process is running as root, make sure that the SHM file
      ** is owned by the same user that owns the original database.  Otherwise,
      ** the original owner will not be able to connect.
      */
      robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid);

      rc = unixLockSharedMemory(pDbFd, pShmNode);
      if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
    }
  }

  /* Make the new connection a child of the unixShmNode */
  p->pShmNode = pShmNode;
#ifdef SQLITE_DEBUG
  p->id = pShmNode->nextShmId++;
#endif
  pShmNode->nRef++;
  pDbFd->pShm = p;
  unixLeaveMutex();

  /* The reference count on pShmNode has already been incremented under
  ** the cover of the unixEnterMutex() mutex and the pointer from the
  ** new (struct unixShm) object to the pShmNode has been set. All that is
  ** left to do is to link the new object into the linked list starting
  ** at pShmNode->pFirst. This must be done while holding the
  ** pShmNode->pShmMutex.
  */
  sqlite3_mutex_enter(pShmNode->pShmMutex);
  p->pNext = pShmNode->pFirst;
  pShmNode->pFirst = p;
  sqlite3_mutex_leave(pShmNode->pShmMutex);
  return rc;

  /* Jump here on any error */
shm_open_err:
  unixShmPurge(pDbFd);       /* This call frees pShmNode if required */
  sqlite3_free(p);
  unixLeaveMutex();
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x18, %edi
callq 0x3c07c0
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x3d8fff
movl $0x7, -0x4(%rbp)
jmp 0x3d92a1
movq -0x18(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x19360
callq 0x3d7bd0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x3d9237
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0xd8(%rbp)
movq 0x266cf3(%rip), %rax    # 0x63fd40
movq -0x10(%rbp), %rcx
movl 0x18(%rcx), %edi
leaq -0xd0(%rbp), %rsi
callq *%rax
cmpl $0x0, %eax
je 0x3d906e
movl $0x70a, -0x24(%rbp)     # imm = 0x70A
jmp 0x3d9284
movq -0xd8(%rbp), %rdi
callq 0x192b0
addl $0x6, %eax
movl %eax, -0x3c(%rbp)
movslq -0x3c(%rbp), %rdi
addq $0x40, %rdi
callq 0x3c07c0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x3d90a4
movl $0x7, -0x24(%rbp)
jmp 0x3d9284
movq -0x20(%rbp), %rdi
movslq -0x3c(%rbp), %rdx
addq $0x40, %rdx
xorl %esi, %esi
callq 0x19360
movq -0x20(%rbp), %rax
addq $0x40, %rax
movq -0x20(%rbp), %rcx
movq %rax, 0x10(%rcx)
movq %rax, -0x38(%rbp)
movl -0x3c(%rbp), %edi
movq -0x38(%rbp), %rsi
movq -0xd8(%rbp), %rcx
leaq 0x1d1fad(%rip), %rdx    # 0x5ab08d
movb $0x0, %al
callq 0x3c38c0
movq -0x20(%rbp), %rax
movl $0xffffffff, 0x18(%rax) # imm = 0xFFFFFFFF
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x38(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
cmpb $0x0, 0x26673c(%rip)    # 0x63f854
je 0x3d9140
movq -0x20(%rbp), %rax
movl $0x8, %ecx
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x3d913e
movl $0x7, -0x24(%rbp)
jmp 0x3d9284
jmp 0x3d9140
movq -0x30(%rbp), %rax
movzbl 0x21(%rax), %eax
cmpl $0x0, %eax
jne 0x3d9235
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
leaq 0x1d1f34(%rip), %rsi    # 0x5ab094
xorl %edx, %edx
callq 0x3d55b0
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x3d9192
movq -0x38(%rbp), %rdi
movl -0xb8(%rbp), %edx
andl $0x1ff, %edx            # imm = 0x1FF
movl $0x20042, %esi          # imm = 0x20042
callq 0x3d7f50
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jge 0x3d91fa
movq -0x38(%rbp), %rdi
movl -0xb8(%rbp), %edx
andl $0x1ff, %edx            # imm = 0x1FF
movl $0x20000, %esi          # imm = 0x20000
callq 0x3d7f50
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jge 0x3d91f2
movl $0x9500, %edi           # imm = 0x9500
callq 0x3d80e0
movl %eax, %edi
movq -0x38(%rbp), %rdx
leaq 0x162ae9(%rip), %rsi    # 0x53bcc9
movl $0x9500, %ecx           # imm = 0x9500
callq 0x3d8070
movl %eax, -0x24(%rbp)
jmp 0x3d9284
movq -0x20(%rbp), %rax
movb $0x1, 0x22(%rax)
movq -0x20(%rbp), %rax
movl 0x18(%rax), %edi
movl -0xb4(%rbp), %esi
movl -0xb0(%rbp), %edx
callq 0x3d93d0
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x3d92b0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x3d9233
cmpl $0x508, -0x24(%rbp)     # imm = 0x508
je 0x3d9233
jmp 0x3d9284
jmp 0x3d9235
jmp 0x3d9237
movq -0x20(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movl 0x30(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x30(%rax)
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x40(%rax)
callq 0x3d7d50
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x38(%rax)
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3d92a1
movq -0x10(%rbp), %rdi
callq 0x3d9420
movq -0x18(%rbp), %rdi
callq 0x3c0800
callq 0x3d7d50
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
nopl (%rax)