// File: /workspace/github_c_makefile_star2_O0/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: posixUnlock
static int _posixUnlock(unqlite_file *id, int eFileLock, int handleNFSUnlock){
  unixFile *pFile = (unixFile*)id;
  unixInodeInfo *pInode;
  struct flock lock;
  int rc = UNQLITE_OK;
  int h;
  int tErrno;                      /* Error code from system call errors */

   if( pFile->eFileLock<=eFileLock ){
    return UNQLITE_OK;
  }
  unixEnterMutex();
  
  h = pFile->h;
  pInode = pFile->pInode;
  
  if( pFile->eFileLock>SHARED_LOCK ){
    /* downgrading to a shared lock on NFS involves clearing the write lock
    ** before establishing the readlock - to avoid a race condition we downgrade
    ** the lock in 2 blocks, so that part of the range will be covered by a 
    ** write lock until the rest is covered by a read lock:
    **  1:   [WWWWW]
    **  2:   [....W]
    **  3:   [RRRRW]
    **  4:   [RRRR.]
    */
    if( eFileLock==SHARED_LOCK ){
      if( handleNFSUnlock ){
        off_t divSize = SHARED_SIZE - 1;
        
        lock.l_type = F_UNLCK;
        lock.l_whence = SEEK_SET;
        lock.l_start = SHARED_FIRST;
        lock.l_len = divSize;
        if( fcntl(h, F_SETLK, &lock)==(-1) ){
          tErrno = errno;
		  rc = unqliteErrorFromPosixError(tErrno, UNQLITE_LOCKERR);
          if( IS_LOCK_ERROR(rc) ){
            pFile->lastErrno = tErrno;
          }
          goto end_unlock;
        }
        lock.l_type = F_RDLCK;
        lock.l_whence = SEEK_SET;
        lock.l_start = SHARED_FIRST;
        lock.l_len = divSize;
        if( fcntl(h, F_SETLK, &lock)==(-1) ){
          tErrno = errno;
		  rc = unqliteErrorFromPosixError(tErrno, UNQLITE_LOCKERR);
          if( IS_LOCK_ERROR(rc) ){
            pFile->lastErrno = tErrno;
          }
          goto end_unlock;
        }
        lock.l_type = F_UNLCK;
        lock.l_whence = SEEK_SET;
        lock.l_start = SHARED_FIRST+divSize;
        lock.l_len = SHARED_SIZE-divSize;
        if( fcntl(h, F_SETLK, &lock)==(-1) ){
          tErrno = errno;
		  rc = unqliteErrorFromPosixError(tErrno, UNQLITE_LOCKERR);
          if( IS_LOCK_ERROR(rc) ){
            pFile->lastErrno = tErrno;
          }
          goto end_unlock;
        }
      }else{
        lock.l_type = F_RDLCK;
        lock.l_whence = SEEK_SET;
        lock.l_start = SHARED_FIRST;
        lock.l_len = SHARED_SIZE;
        if( fcntl(h, F_SETLK, &lock)==(-1) ){
          tErrno = errno;
		  rc = unqliteErrorFromPosixError(tErrno, UNQLITE_LOCKERR);
          if( IS_LOCK_ERROR(rc) ){
            pFile->lastErrno = tErrno;
          }
          goto end_unlock;
        }
      }
    }
    lock.l_type = F_UNLCK;
    lock.l_whence = SEEK_SET;
    lock.l_start = PENDING_BYTE;
    lock.l_len = 2L;
    if( fcntl(h, F_SETLK, &lock)!=(-1) ){
      pInode->eFileLock = SHARED_LOCK;
    }else{
      tErrno = errno;
	  rc = unqliteErrorFromPosixError(tErrno, UNQLITE_LOCKERR);
      if( IS_LOCK_ERROR(rc) ){
        pFile->lastErrno = tErrno;
      }
      goto end_unlock;
    }
  }
  if( eFileLock==NO_LOCK ){
    /* Decrement the shared lock counter.  Release the lock using an
    ** OS call only when all threads in this same process have released
    ** the lock.
    */
    pInode->nShared--;
    if( pInode->nShared==0 ){
      lock.l_type = F_UNLCK;
      lock.l_whence = SEEK_SET;
      lock.l_start = lock.l_len = 0L;
      
      if( fcntl(h, F_SETLK, &lock)!=(-1) ){
        pInode->eFileLock = NO_LOCK;
      }else{
        tErrno = errno;
		rc = unqliteErrorFromPosixError(tErrno, UNQLITE_LOCKERR);
        if( IS_LOCK_ERROR(rc) ){
          pFile->lastErrno = tErrno;
        }
        pInode->eFileLock = NO_LOCK;
        pFile->eFileLock = NO_LOCK;
      }
    }

    /* Decrement the count of locks against this same file.  When the
    ** count reaches zero, close any other file descriptors whose close
    ** was deferred because of outstanding locks.
    */
    pInode->nLock--;
 
    if( pInode->nLock==0 ){
      int rc2 = closePendingFds(pFile);
      if( rc==UNQLITE_OK ){
        rc = rc2;
      }
    }
  }
	
end_unlock:

  unixLeaveMutex();
  
  if( rc==UNQLITE_OK ) pFile->eFileLock = eFileLock;
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x4c(%rbp)
movq -0x20(%rbp), %rax
movzbl 0x18(%rax), %eax
cmpl -0x14(%rbp), %eax
jg 0x5d03a
movl $0x0, -0x4(%rbp)
jmp 0x5d3ac
callq 0x5bec0
movq -0x20(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movzbl 0x18(%rax), %eax
cmpl $0x1, %eax
jle 0x5d2af
cmpl $0x1, -0x14(%rbp)
jne 0x5d237
cmpl $0x0, -0x18(%rbp)
je 0x5d1ca
movq $0x1fd, -0x60(%rbp)     # imm = 0x1FD
movw $0x2, -0x48(%rbp)
movw $0x0, -0x46(%rbp)
movq $0x40000002, -0x40(%rbp) # imm = 0x40000002
movq -0x60(%rbp), %rax
movq %rax, -0x38(%rbp)
movl -0x50(%rbp), %edi
movl $0x6, %esi
leaq -0x48(%rbp), %rdx
movb $0x0, %al
callq 0xd230
cmpl $-0x1, %eax
jne 0x5d0eb
callq 0xdcc0
movl (%rax), %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %edi
movl $0xffffffb4, %esi       # imm = 0xFFFFFFB4
callq 0x5cf90
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0x5d0e6
cmpl $-0xe, -0x4c(%rbp)
je 0x5d0e6
movl -0x54(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x1c(%rax)
jmp 0x5d38f
movw $0x0, -0x48(%rbp)
movw $0x0, -0x46(%rbp)
movq $0x40000002, -0x40(%rbp) # imm = 0x40000002
movq -0x60(%rbp), %rax
movq %rax, -0x38(%rbp)
movl -0x50(%rbp), %edi
movl $0x6, %esi
leaq -0x48(%rbp), %rdx
movb $0x0, %al
callq 0xd230
cmpl $-0x1, %eax
jne 0x5d154
callq 0xdcc0
movl (%rax), %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %edi
movl $0xffffffb4, %esi       # imm = 0xFFFFFFB4
callq 0x5cf90
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0x5d14f
cmpl $-0xe, -0x4c(%rbp)
je 0x5d14f
movl -0x54(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x1c(%rax)
jmp 0x5d38f
movw $0x2, -0x48(%rbp)
movw $0x0, -0x46(%rbp)
movq -0x60(%rbp), %rax
addq $0x40000002, %rax       # imm = 0x40000002
movq %rax, -0x40(%rbp)
movl $0x1fe, %eax            # imm = 0x1FE
subq -0x60(%rbp), %rax
movq %rax, -0x38(%rbp)
movl -0x50(%rbp), %edi
movl $0x6, %esi
leaq -0x48(%rbp), %rdx
movb $0x0, %al
callq 0xd230
cmpl $-0x1, %eax
jne 0x5d1c8
callq 0xdcc0
movl (%rax), %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %edi
movl $0xffffffb4, %esi       # imm = 0xFFFFFFB4
callq 0x5cf90
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0x5d1c3
cmpl $-0xe, -0x4c(%rbp)
je 0x5d1c3
movl -0x54(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x1c(%rax)
jmp 0x5d38f
jmp 0x5d235
movw $0x0, -0x48(%rbp)
movw $0x0, -0x46(%rbp)
movq $0x40000002, -0x40(%rbp) # imm = 0x40000002
movq $0x1fe, -0x38(%rbp)     # imm = 0x1FE
movl -0x50(%rbp), %edi
movl $0x6, %esi
leaq -0x48(%rbp), %rdx
movb $0x0, %al
callq 0xd230
cmpl $-0x1, %eax
jne 0x5d233
callq 0xdcc0
movl (%rax), %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %edi
movl $0xffffffb4, %esi       # imm = 0xFFFFFFB4
callq 0x5cf90
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0x5d22e
cmpl $-0xe, -0x4c(%rbp)
je 0x5d22e
movl -0x54(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x1c(%rax)
jmp 0x5d38f
jmp 0x5d235
jmp 0x5d237
movw $0x2, -0x48(%rbp)
movw $0x0, -0x46(%rbp)
movq $0x40000000, -0x40(%rbp) # imm = 0x40000000
movq $0x2, -0x38(%rbp)
movl -0x50(%rbp), %edi
movl $0x6, %esi
leaq -0x48(%rbp), %rdx
movb $0x0, %al
callq 0xd230
cmpl $-0x1, %eax
je 0x5d278
movq -0x28(%rbp), %rax
movl $0x1, 0x14(%rax)
jmp 0x5d2ad
callq 0xdcc0
movl (%rax), %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %edi
movl $0xffffffb4, %esi       # imm = 0xFFFFFFB4
callq 0x5cf90
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0x5d2a8
cmpl $-0xe, -0x4c(%rbp)
je 0x5d2a8
movl -0x54(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x1c(%rax)
jmp 0x5d38f
jmp 0x5d2af
cmpl $0x0, -0x14(%rbp)
jne 0x5d38d
movq -0x28(%rbp), %rax
movl 0x10(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x10(%rax)
movq -0x28(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x5d35a
movw $0x2, -0x48(%rbp)
movw $0x0, -0x46(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movl -0x50(%rbp), %edi
movl $0x6, %esi
leaq -0x48(%rbp), %rdx
movb $0x0, %al
callq 0xd230
cmpl $-0x1, %eax
je 0x5d315
movq -0x28(%rbp), %rax
movl $0x0, 0x14(%rax)
jmp 0x5d358
callq 0xdcc0
movl (%rax), %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %edi
movl $0xffffffb4, %esi       # imm = 0xFFFFFFB4
callq 0x5cf90
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0x5d345
cmpl $-0xe, -0x4c(%rbp)
je 0x5d345
movl -0x54(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x28(%rbp), %rax
movl $0x0, 0x14(%rax)
movq -0x20(%rbp), %rax
movb $0x0, 0x18(%rax)
jmp 0x5d35a
movq -0x28(%rbp), %rax
movl 0x1c(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x1c(%rax)
movq -0x28(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x5d38b
movq -0x20(%rbp), %rdi
callq 0x5cd60
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x4c(%rbp)
jne 0x5d389
movl -0x64(%rbp), %eax
movl %eax, -0x4c(%rbp)
jmp 0x5d38b
jmp 0x5d38d
jmp 0x5d38f
callq 0x5bf10
cmpl $0x0, -0x4c(%rbp)
jne 0x5d3a6
movl -0x14(%rbp), %eax
movb %al, %cl
movq -0x20(%rbp), %rax
movb %cl, 0x18(%rax)
movl -0x4c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)