// File: /workspace/github_c_makefile_star2_O0/barabo[P]advanced-shell-history/src/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;                         /* Result code */
  unixInodeInfo *pInode;          /* The inode of fd */
  char *zShmFilename;             /* 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_malloc( sizeof(*p) );
  if( p==0 ) return SQLITE_NOMEM;
  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.
  */
  unixEnterMutex();
  pInode = pDbFd->pInode;
  pShmNode = pInode->pShmNode;
  if( pShmNode==0 ){
    struct stat sStat;                 /* fstat() info for database file */

    /* 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) && pInode->bProcessLock==0 ){
      rc = SQLITE_IOERR_FSTAT;
      goto shm_open_err;
    }

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

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

      /* 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.
      */
      osFchown(pShmNode->h, sStat.st_uid, sStat.st_gid);
  
      /* Check to see if another process is holding the dead-man switch.
      ** If not, truncate the file to zero length. 
      */
      rc = SQLITE_OK;
      if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
        if( robust_ftruncate(pShmNode->h, 0) ){
          rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
        }
      }
      if( rc==SQLITE_OK ){
        rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1);
      }
      if( rc ) 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->mutex 
  ** mutex.
  */
  sqlite3_mutex_enter(pShmNode->mutex);
  p->pNext = pShmNode->pFirst;
  pShmNode->pFirst = p;
  sqlite3_mutex_leave(pShmNode->mutex);
  return SQLITE_OK;

  /* 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 $0x18, %edi
callq 0xa380
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x1e898
movl $0x7, -0x4(%rbp)
jmp 0x1eb75
movq -0x18(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x8200
callq 0x1d540
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x1eb0a
movq 0xb2089(%rip), %rax     # 0xd0960
movq -0x10(%rbp), %rcx
movl 0x18(%rcx), %edi
leaq -0xd0(%rbp), %rsi
callq *%rax
cmpl $0x0, %eax
je 0x1e905
movq -0x30(%rbp), %rax
movzbl 0x15(%rax), %eax
cmpl $0x0, %eax
jne 0x1e905
movl $0x70a, -0x24(%rbp)     # imm = 0x70A
jmp 0x1eb58
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x81b0
addl $0x6, %eax
movl %eax, -0x3c(%rbp)
movslq -0x3c(%rbp), %rax
addq $0x40, %rax
movl %eax, %edi
callq 0xa380
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x1e93e
movl $0x7, -0x24(%rbp)
jmp 0x1eb58
movq -0x20(%rbp), %rdi
movslq -0x3c(%rbp), %rdx
addq $0x40, %rdx
xorl %esi, %esi
callq 0x8200
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 -0x10(%rbp), %rax
movq 0x38(%rax), %rcx
leaq 0x8f8ae(%rip), %rdx     # 0xae229
movb $0x0, %al
callq 0xc540
movq -0x20(%rbp), %rax
movl $0xffffffff, 0x18(%rax) # imm = 0xFFFFFFFF
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movl $0x8, %ecx
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x1e9d0
movl $0x7, -0x24(%rbp)
jmp 0x1eb58
movq -0x30(%rbp), %rax
movzbl 0x15(%rax), %eax
cmpl $0x0, %eax
jne 0x1eb08
movl $0x42, -0xd4(%rbp)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
leaq 0x8f836(%rip), %rsi     # 0xae230
xorl %edx, %edx
callq 0x1b3c0
cmpl $0x0, %eax
je 0x1ea18
movl $0x0, -0xd4(%rbp)
movq -0x20(%rbp), %rax
movb $0x1, 0x22(%rax)
movq -0x38(%rbp), %rdi
movl -0xd4(%rbp), %esi
movl -0xb8(%rbp), %edx
andl $0x1ff, %edx            # imm = 0x1FF
callq 0x1d850
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jge 0x1ea6f
movl $0x6a88, %edi           # imm = 0x6A88
callq 0x1d9a0
movl %eax, %edi
movq -0x38(%rbp), %rdx
leaq 0x8f6be(%rip), %rsi     # 0xae11b
movl $0x6a88, %ecx           # imm = 0x6A88
callq 0x1d930
movl %eax, -0x24(%rbp)
jmp 0x1eb58
movq 0xb2052(%rip), %rax     # 0xd0ac8
movq -0x20(%rbp), %rcx
movl 0x18(%rcx), %edi
movl -0xb4(%rbp), %esi
movl -0xb0(%rbp), %edx
callq *%rax
movl $0x0, -0x24(%rbp)
movq -0x20(%rbp), %rdi
movl $0x1, %ecx
movl $0x80, %edx
movl %ecx, %esi
callq 0x1eb90
cmpl $0x0, %eax
jne 0x1eae0
movq -0x20(%rbp), %rax
movl 0x18(%rax), %edi
xorl %eax, %eax
movl %eax, %esi
callq 0x1dd60
cmpl $0x0, %eax
je 0x1eade
movq -0x38(%rbp), %rdx
movl $0x120a, %edi           # imm = 0x120A
leaq 0x8f669(%rip), %rsi     # 0xae13a
movl $0x6a98, %ecx           # imm = 0x6A98
callq 0x1d930
movl %eax, -0x24(%rbp)
jmp 0x1eae0
cmpl $0x0, -0x24(%rbp)
jne 0x1eafe
movq -0x20(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
movl $0x1, %ecx
callq 0x1eb90
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x1eb06
jmp 0x1eb58
jmp 0x1eb08
jmp 0x1eb0a
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 0x1d6c0
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 $0x0, -0x4(%rbp)
jmp 0x1eb75
movq -0x10(%rbp), %rdi
callq 0x1ec20
movq -0x18(%rbp), %rdi
callq 0xa420
callq 0x1d6c0
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)