// File: /workspace/github_c_makefile_star2_O3/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: unixShmLock
static int unixShmLock(
  sqlite3_file *fd,          /* Database file holding the shared memory */
  int ofst,                  /* First lock to acquire or release */
  int n,                     /* Number of locks to acquire or release */
  int flags                  /* What to do with the lock */
){
  unixFile *pDbFd = (unixFile*)fd;      /* Connection holding shared memory */
  unixShm *p = pDbFd->pShm;             /* The shared memory being locked */
  unixShm *pX;                          /* For looping over all siblings */
  unixShmNode *pShmNode = p->pShmNode;  /* The underlying file iNode */
  int rc = SQLITE_OK;                   /* Result code */
  u16 mask;                             /* Mask of locks to take or release */

  assert( pShmNode==pDbFd->pInode->pShmNode );
  assert( pShmNode->pInode==pDbFd->pInode );
  assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
  assert( n>=1 );
  assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
       || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
  assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
  assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
  assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );

  /* Check that, if this to be a blocking lock, no locks that occur later
  ** in the following list than the lock being obtained are already held:
  **
  **   1. Checkpointer lock (ofst==1).
  **   2. Write lock (ofst==0).
  **   3. Read locks (ofst>=3 && ofst<SQLITE_SHM_NLOCK).
  **
  ** In other words, if this is a blocking lock, none of the locks that
  ** occur later in the above list than the lock being obtained may be
  ** held.
  **
  ** It is not permitted to block on the RECOVER lock.
  */
#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
  assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (
         (ofst!=2)                                   /* not RECOVER */
      && (ofst!=1 || (p->exclMask|p->sharedMask)==0)
      && (ofst!=0 || (p->exclMask|p->sharedMask)<3)
      && (ofst<3  || (p->exclMask|p->sharedMask)<(1<<ofst))
  ));
#endif

  mask = (1<<(ofst+n)) - (1<<ofst);
  assert( n>1 || mask==(1<<ofst) );
  sqlite3_mutex_enter(pShmNode->pShmMutex);
  if( flags & SQLITE_SHM_UNLOCK ){
    u16 allMask = 0; /* Mask of locks held by siblings */

    /* See if any siblings hold this same lock */
    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
      if( pX==p ) continue;
      assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
      allMask |= pX->sharedMask;
    }

    /* Unlock the system-level locks */
    if( (mask & allMask)==0 ){
      rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n);
    }else{
      rc = SQLITE_OK;
    }

    /* Undo the local locks */
    if( rc==SQLITE_OK ){
      p->exclMask &= ~mask;
      p->sharedMask &= ~mask;
    }
  }else if( flags & SQLITE_SHM_SHARED ){
    u16 allShared = 0;  /* Union of locks held by connections other than "p" */

    /* Find out which shared locks are already held by sibling connections.
    ** If any sibling already holds an exclusive lock, go ahead and return
    ** SQLITE_BUSY.
    */
    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
      if( (pX->exclMask & mask)!=0 ){
        rc = SQLITE_BUSY;
        break;
      }
      allShared |= pX->sharedMask;
    }

    /* Get shared locks at the system level, if necessary */
    if( rc==SQLITE_OK ){
      if( (allShared & mask)==0 ){
        rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n);
      }else{
        rc = SQLITE_OK;
      }
    }

    /* Get the local shared locks */
    if( rc==SQLITE_OK ){
      p->sharedMask |= mask;
    }
  }else{
    /* Make sure no sibling connections hold locks that will block this
    ** lock.  If any do, return SQLITE_BUSY right away.
    */
    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
      if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
        rc = SQLITE_BUSY;
        break;
      }
    }

    /* Get the exclusive locks at the system level.  Then if successful
    ** also mark the local connection as being locked.
    */
    if( rc==SQLITE_OK ){
      rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n);
      if( rc==SQLITE_OK ){
        assert( (p->sharedMask & mask)==0 );
        p->exclMask |= mask;
      }
    }
  }
  sqlite3_mutex_leave(pShmNode->pShmMutex);
  OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
           p->id, osGetpid(0), p->sharedMask, p->exclMask));
  return rc;
}

// Assembly:
pushq %rbp
pushq %rbx
subq $0x28, %rsp
movl %ecx, %eax
leal (%rdx,%rsi), %ecx
movl $0x1, %r8d
shll %cl, %r8d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %esi, %ecx
shll %cl, %ebp
movq 0x40(%rdi), %rbx
movq (%rbx), %rcx
addl %r8d, %ebp
testb $0x1, %al
jne 0x283dc6
movq 0x38(%rcx), %rcx
testb $0x4, %al
jne 0x283de8
testq %rcx, %rcx
je 0x283d7c
movl $0x5, %eax
movzwl 0x14(%rcx), %r8d
testl %r8d, %ebp
jne 0x283e38
movzwl 0x12(%rcx), %r8d
testl %r8d, %ebp
jne 0x283e38
movq 0x8(%rcx), %rcx
testq %rcx, %rcx
jne 0x283d57
movq 0x10(%rdi), %rax
movq 0x38(%rax), %rax
movl 0x18(%rax), %edi
testl %edi, %edi
js 0x283dc0
addl $0x78, %esi
leaq 0x8(%rsp), %rax
movl $0x1, (%rax)
movslq %esi, %rcx
movq %rcx, 0x8(%rax)
movslq %edx, %rcx
movq %rcx, 0x10(%rax)
movl $0x6, %esi
movq %rax, %rdx
xorl %eax, %eax
callq *0x237dc9(%rip)         # 0x4bbb80
cmpl $-0x1, %eax
je 0x283ec5
orw %bp, 0x14(%rbx)
jmp 0x283e36
movq 0x38(%rcx), %rax
testq %rax, %rax
je 0x283e17
xorl %ecx, %ecx
cmpq %rbx, %rax
je 0x283dda
orw 0x12(%rax), %cx
movq 0x8(%rax), %rax
testq %rax, %rax
jne 0x283dd1
movzwl %cx, %eax
jmp 0x283e19
xorl %eax, %eax
testq %rcx, %rcx
je 0x283e0d
movzwl 0x14(%rcx), %r8d
testl %r8d, %ebp
jne 0x283ec5
orw 0x12(%rcx), %ax
movq 0x8(%rcx), %rcx
testq %rcx, %rcx
jne 0x283def
movzwl %ax, %eax
testl %eax, %ebp
je 0x283e81
orw %bp, 0x12(%rbx)
jmp 0x283e36
xorl %eax, %eax
testl %eax, %ebp
je 0x283e3f
notl %ebp
movd 0x12(%rbx), %xmm0
movd %ebp, %xmm1
pshuflw $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0,4,5,6,7]
pand %xmm0, %xmm1
movd %xmm1, 0x12(%rbx)
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %rbp
retq
movq 0x10(%rdi), %rax
movq 0x38(%rax), %rax
movl 0x18(%rax), %edi
testl %edi, %edi
js 0x283e1d
addl $0x78, %esi
leaq 0x8(%rsp), %rax
movl $0x2, (%rax)
movslq %esi, %rcx
movq %rcx, 0x8(%rax)
movslq %edx, %rcx
movq %rcx, 0x10(%rax)
movl $0x6, %esi
movq %rax, %rdx
xorl %eax, %eax
callq *0x237d06(%rip)         # 0x4bbb80
cmpl $-0x1, %eax
jne 0x283e1d
jmp 0x283ec5
movq 0x10(%rdi), %rax
movq 0x38(%rax), %rax
movl 0x18(%rax), %edi
testl %edi, %edi
js 0x283e11
addl $0x78, %esi
leaq 0x8(%rsp), %rax
movl $0x0, (%rax)
movslq %esi, %rcx
movq %rcx, 0x8(%rax)
movslq %edx, %rcx
movq %rcx, 0x10(%rax)
movl $0x6, %esi
movq %rax, %rdx
xorl %eax, %eax
callq *0x237cc4(%rip)         # 0x4bbb80
cmpl $-0x1, %eax
jne 0x283e11
movl $0x5, %eax
jmp 0x283e38