// File: /workspace/github_c_makefile_star2_O1/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: unixLockSharedMemory
static int unixLockSharedMemory(unixFile *pDbFd, unixShmNode *pShmNode){
  struct flock lock;
  int rc = SQLITE_OK;

  /* Use F_GETLK to determine the locks other processes are holding
  ** on the DMS byte. If it indicates that another process is holding
  ** a SHARED lock, then this process may also take a SHARED lock
  ** and proceed with opening the *-shm file.
  **
  ** Or, if no other process is holding any lock, then this process
  ** is the first to open it. In this case take an EXCLUSIVE lock on the
  ** DMS byte and truncate the *-shm file to zero bytes in size. Then
  ** downgrade to a SHARED lock on the DMS byte.
  **
  ** If another process is holding an EXCLUSIVE lock on the DMS byte,
  ** return SQLITE_BUSY to the caller (it will try again). An earlier
  ** version of this code attempted the SHARED lock at this point. But
  ** this introduced a subtle race condition: if the process holding
  ** EXCLUSIVE failed just before truncating the *-shm file, then this
  ** process might open and use the *-shm file without truncating it.
  ** And if the *-shm file has been corrupted by a power failure or
  ** system crash, the database itself may also become corrupt.  */
  lock.l_whence = SEEK_SET;
  lock.l_start = UNIX_SHM_DMS;
  lock.l_len = 1;
  lock.l_type = F_WRLCK;
  if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) {
    rc = SQLITE_IOERR_LOCK;
  }else if( lock.l_type==F_UNLCK ){
    if( pShmNode->isReadonly ){
      pShmNode->isUnlocked = 1;
      rc = SQLITE_READONLY_CANTINIT;
    }else{
      rc = unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1);
      /* The first connection to attach must truncate the -shm file.  We
      ** truncate to 3 bytes (an arbitrary small number, less than the
      ** -shm header size) rather than 0 as a system debugging aid, to
      ** help detect if a -shm file truncation is legitimate or is the work
      ** or a rogue process. */
      if( rc==SQLITE_OK && robust_ftruncate(pShmNode->hShm, 3) ){
        rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename);
      }
    }
  }else if( lock.l_type==F_WRLCK ){
    rc = SQLITE_BUSY;
  }

  if( rc==SQLITE_OK ){
    assert( lock.l_type==F_UNLCK || lock.l_type==F_RDLCK );
    rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1);
  }
  return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x8(%rsp), %rdx
movq $0x80, 0x8(%rdx)
movq $0x1, 0x10(%rdx)
movl $0x1, (%rdx)
movl 0x18(%rsi), %edi
movl $0x5, %esi
xorl %eax, %eax
callq *0x238884(%rip)         # 0x4c6b80
movl $0xf0a, %ebp            # imm = 0xF0A
testl %eax, %eax
je 0x28e34d
testl %ebp, %ebp
jne 0x28e376
movq 0x10(%rbx), %rax
movq 0x38(%rax), %rax
movl 0x18(%rax), %edi
testl %edi, %edi
js 0x28e374
leaq 0x28(%rsp), %rdx
movl $0x0, (%rdx)
movq $0x80, 0x8(%rdx)
movq $0x1, 0x10(%rdx)
movl $0x6, %esi
xorl %eax, %eax
callq *0x238840(%rip)         # 0x4c6b80
xorl %ecx, %ecx
cmpl $-0x1, %eax
sete %cl
leal (%rcx,%rcx,4), %ebp
jmp 0x28e376
movzwl 0x8(%rsp), %eax
cmpl $0x1, %eax
je 0x28e383
movl $0x0, %ebp
cmpl $0x2, %eax
jne 0x28e305
cmpb $0x0, 0x22(%r14)
je 0x28e38d
movb $0x1, 0x23(%r14)
movl $0x508, %ebp            # imm = 0x508
jmp 0x28e305
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movl $0x5, %ebp
jmp 0x28e305
movq 0x10(%rbx), %rax
movq 0x38(%rax), %rax
movl 0x18(%rax), %edi
testl %edi, %edi
js 0x28e3d1
leaq 0x28(%rsp), %rdx
movl $0x1, (%rdx)
movq $0x80, 0x8(%rdx)
movq $0x1, 0x10(%rdx)
movl $0x6, %esi
xorl %eax, %eax
callq *0x2387bc(%rip)         # 0x4c6b80
xorl %ecx, %ecx
cmpl $-0x1, %eax
sete %cl
leal (%rcx,%rcx,4), %ebp
jmp 0x28e3d3
xorl %ebp, %ebp
testl %ebp, %ebp
jne 0x28e305
movl 0x18(%r14), %ebp
movl $0x3, %esi
movl %ebp, %edi
callq *0x23877c(%rip)         # 0x4c6b68
movl %eax, %r15d
testl %eax, %eax
jns 0x28e3fd
callq 0x17060
cmpl $0x4, (%rax)
je 0x28e3df
testl %r15d, %r15d
je 0x28e426
movq 0x10(%r14), %rdx
leaq 0x150e2c(%rip), %rsi    # 0x3df239
movl $0x120a, %ebp           # imm = 0x120A
movl $0x120a, %edi           # imm = 0x120A
movl $0x9483, %ecx           # imm = 0x9483
callq 0x28de48
jmp 0x28e305
xorl %ebp, %ebp
jmp 0x28e305