// File: /workspace/github_c_makefile_star2_O0/ukontainer[P]sqlite-bench/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->h, 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);
      if( rc==SQLITE_OK && robust_ftruncate(pShmNode->h, 0) ){
        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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x34(%rbp)
movw $0x0, -0x2e(%rbp)
movq $0x80, -0x28(%rbp)
movq $0x1, -0x20(%rbp)
movw $0x1, -0x30(%rbp)
movq 0x1b0266(%rip), %rcx    # 0x5ce660
movq -0x10(%rbp), %rax
movl 0x18(%rax), %edi
movl $0x5, %esi
leaq -0x30(%rbp), %rdx
movb $0x0, %al
callq *%rcx
cmpl $0x0, %eax
je 0x41e41f
movl $0xf0a, -0x34(%rbp)     # imm = 0xF0A
jmp 0x41e4af
movswl -0x30(%rbp), %eax
cmpl $0x2, %eax
jne 0x41e49b
movq -0x10(%rbp), %rax
cmpb $0x0, 0x22(%rax)
je 0x41e443
movq -0x10(%rbp), %rax
movb $0x1, 0x23(%rax)
movl $0x508, -0x34(%rbp)     # imm = 0x508
jmp 0x41e499
movq -0x8(%rbp), %rdi
movl $0x1, %ecx
movl $0x80, %edx
movl %ecx, %esi
callq 0x41e640
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x41e497
movq -0x10(%rbp), %rax
movl 0x18(%rax), %edi
xorl %eax, %eax
movl %eax, %esi
callq 0x41d5a0
cmpl $0x0, %eax
je 0x41e497
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdx
movl $0x120a, %edi           # imm = 0x120A
leaq 0x160000(%rip), %rsi    # 0x57e48a
movl $0x8eb8, %ecx           # imm = 0x8EB8
callq 0x41d140
movl %eax, -0x34(%rbp)
jmp 0x41e499
jmp 0x41e4ad
movswl -0x30(%rbp), %eax
cmpl $0x1, %eax
jne 0x41e4ab
movl $0x5, -0x34(%rbp)
jmp 0x41e4ad
jmp 0x41e4af
cmpl $0x0, -0x34(%rbp)
jne 0x41e4cd
movq -0x8(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
movl $0x1, %ecx
callq 0x41e640
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)