// File: /workspace/github_c_makefile_star2_O2/skeeto[P]flappy/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %ecx, %r15d
movl %edx, %r8d
movl %esi, %r9d
leal (%r8,%r9), %ecx
pushq $0x1
popq %r12
shll %cl, %r12d
pushq $-0x1
popq %r14
movl %r9d, %ecx
shll %cl, %r14d
movq 0x40(%rdi), %r13
movq (%r13), %rbx
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x17abe
movq %rbx, (%rsp)
movq %r8, %rbx
movq %r9, %rbp
callq *0x68874(%rip)          # 0x80328
movq %rbp, %r9
movq %rbx, %r8
movq (%rsp), %rbx
addl %r14d, %r12d
testb $0x1, %r15b
jne 0x17aff
leaq 0x38(%rbx), %rax
testb $0x4, %r15b
jne 0x17b43
pushq $0x5
popq %rbp
movq (%rax), %rax
testq %rax, %rax
je 0x17b8d
movzwl 0x14(%rax), %ecx
testl %ecx, %r12d
jne 0x17bc9
movzwl 0x12(%rax), %ecx
addq $0x8, %rax
testl %ecx, %r12d
je 0x17ad4
jmp 0x17bc9
leaq 0x38(%rbx), %rcx
xorl %eax, %eax
movq (%rcx), %rcx
testq %rcx, %rcx
je 0x17b1c
cmpq %r13, %rcx
je 0x17b16
orw 0x12(%rcx), %ax
addq $0x8, %rcx
jmp 0x17b05
movzwl %ax, %eax
testl %eax, %r12d
je 0x17b63
notl %r12d
movd 0x12(%r13), %xmm0
movd %r12d, %xmm1
pshuflw $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0,4,5,6,7]
pand %xmm0, %xmm1
movd %xmm1, 0x12(%r13)
jmp 0x17bab
xorl %ecx, %ecx
pushq $0x5
popq %rbp
movq (%rax), %rax
testq %rax, %rax
je 0x17b7e
movzwl 0x14(%rax), %edx
testl %edx, %r12d
jne 0x17bc9
orw 0x12(%rax), %cx
addq $0x8, %rax
jmp 0x17b48
addl $0x78, %r9d
pushq $0x2
popq %rsi
movq %rbx, %rdi
movl %r9d, %edx
movl %r8d, %ecx
callq 0x1857e
testl %eax, %eax
jne 0x17bc7
jmp 0x17b24
movzwl %cx, %eax
testl %eax, %r12d
je 0x17baf
orw %r12w, 0x12(%r13)
jmp 0x17bab
addl $0x78, %r9d
pushq $0x1
popq %rsi
movq %rbx, %rdi
movl %r9d, %edx
movl %r8d, %ecx
callq 0x1857e
testl %eax, %eax
jne 0x17bc7
orw %r12w, 0x14(%r13)
xorl %ebp, %ebp
jmp 0x17bc9
addl $0x78, %r9d
movq %rbx, %rdi
xorl %esi, %esi
movl %r9d, %edx
movl %r8d, %ecx
callq 0x1857e
testl %eax, %eax
je 0x17b86
movl %eax, %ebp
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x17bd8
callq *0x68760(%rip)          # 0x80338
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq