// File: /workspace/github_c_makefile_star2_O3/barabo[P]advanced-shell-history/src/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->h>=0 || pDbFd->pInode->bProcessLock==1 );
  assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );

  mask = (1<<(ofst+n)) - (1<<ofst);
  assert( n>1 || mask==(1<<ofst) );
  sqlite3_mutex_enter(pShmNode->mutex);
  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(pShmNode, 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(pShmNode, 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(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n);
      if( rc==SQLITE_OK ){
        assert( (p->sharedMask & mask)==0 );
        p->exclMask |= mask;
      }
    }
  }
  sqlite3_mutex_leave(pShmNode->mutex);
  OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
           p->id, getpid(), p->sharedMask, p->exclMask));
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x20, %rsp
movl %ecx, %eax
leal (%rdx,%rsi), %ecx
movl $0x1, %r8d
shll %cl, %r8d
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %esi, %ecx
shll %cl, %ebx
movq 0x40(%rdi), %r14
movq (%r14), %rcx
addl %r8d, %ebx
testb $0x1, %al
jne 0x18802
movq 0x38(%rcx), %rdi
testb $0x4, %al
jne 0x18824
testq %rdi, %rdi
je 0x187b7
movl $0x5, %eax
movzwl 0x14(%rdi), %r8d
testl %r8d, %ebx
jne 0x18868
movzwl 0x12(%rdi), %r8d
testl %r8d, %ebx
jne 0x18868
movq 0x8(%rdi), %rdi
testq %rdi, %rdi
jne 0x18792
movl 0x18(%rcx), %edi
testl %edi, %edi
js 0x187fb
addl $0x78, %esi
xorps %xmm0, %xmm0
leaq -0x30(%rbp), %rax
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movw $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 *0x7c10e(%rip)          # 0x94900
cmpl $-0x1, %eax
je 0x188f2
orw %bx, 0x14(%r14)
jmp 0x18866
movq 0x38(%rcx), %rax
testq %rax, %rax
je 0x18854
xorl %edi, %edi
cmpq %r14, %rax
je 0x18816
orw 0x12(%rax), %di
movq 0x8(%rax), %rax
testq %rax, %rax
jne 0x1880d
movzwl %di, %eax
jmp 0x18856
xorl %eax, %eax
testq %rdi, %rdi
je 0x18849
movzwl 0x14(%rdi), %r8d
testl %r8d, %ebx
jne 0x188f2
orw 0x12(%rdi), %ax
movq 0x8(%rdi), %rdi
testq %rdi, %rdi
jne 0x1882b
movzwl %ax, %eax
testl %eax, %ebx
je 0x188b3
orw %bx, 0x12(%r14)
jmp 0x18866
xorl %eax, %eax
testl %eax, %ebx
je 0x18871
notl %ebx
andw %bx, 0x14(%r14)
andw %bx, 0x12(%r14)
xorl %eax, %eax
addq $0x20, %rsp
popq %rbx
popq %r14
popq %rbp
retq
movl 0x18(%rcx), %edi
testl %edi, %edi
js 0x1885a
addl $0x78, %esi
xorps %xmm0, %xmm0
leaq -0x30(%rbp), %rax
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movw $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 *0x7c054(%rip)          # 0x94900
cmpl $-0x1, %eax
jne 0x1885a
jmp 0x188f2
movl 0x18(%rcx), %edi
testl %edi, %edi
js 0x1884d
addl $0x78, %esi
movslq %esi, %rcx
xorps %xmm0, %xmm0
leaq -0x30(%rbp), %rax
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movq %rcx, 0x8(%rax)
movslq %edx, %rcx
movq %rcx, 0x10(%rax)
movl $0x6, %esi
movq %rax, %rdx
xorl %eax, %eax
callq *0x7c017(%rip)          # 0x94900
cmpl $-0x1, %eax
jne 0x1884d
movl $0x5, %eax
jmp 0x18868