// File: /workspace/github_c_makefile_star2_O3/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: unixShmMap
static int unixShmMap(
  sqlite3_file *fd,               /* Handle open on database file */
  int iRegion,                    /* Region to retrieve */
  int szRegion,                   /* Size of regions */
  int bExtend,                    /* True to extend file if necessary */
  void volatile **pp              /* OUT: Mapped memory */
){
  unixFile *pDbFd = (unixFile*)fd;
  unixShm *p;
  unixShmNode *pShmNode;
  int rc = SQLITE_OK;
  int nShmPerMap = unixShmRegionPerMap();
  int nReqRegion;

  /* If the shared-memory file has not yet been opened, open it now. */
  if( pDbFd->pShm==0 ){
    rc = unixOpenSharedMemory(pDbFd);
    if( rc!=SQLITE_OK ) return rc;
  }

  p = pDbFd->pShm;
  pShmNode = p->pShmNode;
  sqlite3_mutex_enter(pShmNode->pShmMutex);
  if( pShmNode->isUnlocked ){
    rc = unixLockSharedMemory(pDbFd, pShmNode);
    if( rc!=SQLITE_OK ) goto shmpage_out;
    pShmNode->isUnlocked = 0;
  }
  assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
  assert( pShmNode->pInode==pDbFd->pInode );
  assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
  assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );

  /* Minimum number of regions required to be mapped. */
  nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;

  if( pShmNode->nRegion<nReqRegion ){
    char **apNew;                      /* New apRegion[] array */
    int nByte = nReqRegion*szRegion;   /* Minimum required file size */
    struct stat sStat;                 /* Used by fstat() */

    pShmNode->szRegion = szRegion;

    if( pShmNode->hShm>=0 ){
      /* The requested region is not mapped into this processes address space.
      ** Check to see if it has been allocated (i.e. if the wal-index file is
      ** large enough to contain the requested region).
      */
      if( osFstat(pShmNode->hShm, &sStat) ){
        rc = SQLITE_IOERR_SHMSIZE;
        goto shmpage_out;
      }

      if( sStat.st_size<nByte ){
        /* The requested memory region does not exist. If bExtend is set to
        ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
        */
        if( !bExtend ){
          goto shmpage_out;
        }

        /* Alternatively, if bExtend is true, extend the file. Do this by
        ** writing a single byte to the end of each (OS) page being
        ** allocated or extended. Technically, we need only write to the
        ** last page in order to extend the file. But writing to all new
        ** pages forces the OS to allocate them immediately, which reduces
        ** the chances of SIGBUS while accessing the mapped region later on.
        */
        else{
          static const int pgsz = 4096;
          int iPg;

          /* Write to the last byte of each newly allocated or extended page */
          assert( (nByte % pgsz)==0 );
          for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){
            int x = 0;
            if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){
              const char *zFile = pShmNode->zFilename;
              rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile);
              goto shmpage_out;
            }
          }
        }
      }
    }

    /* Map the requested memory region into this processes address space. */
    apNew = (char **)sqlite3_realloc(
        pShmNode->apRegion, nReqRegion*sizeof(char *)
    );
    if( !apNew ){
      rc = SQLITE_IOERR_NOMEM_BKPT;
      goto shmpage_out;
    }
    pShmNode->apRegion = apNew;
    while( pShmNode->nRegion<nReqRegion ){
      int nMap = szRegion*nShmPerMap;
      int i;
      void *pMem;
      if( pShmNode->hShm>=0 ){
        pMem = osMmap(0, nMap,
            pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
            MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion
        );
        if( pMem==MAP_FAILED ){
          rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
          goto shmpage_out;
        }
      }else{
        pMem = sqlite3_malloc64(nMap);
        if( pMem==0 ){
          rc = SQLITE_NOMEM_BKPT;
          goto shmpage_out;
        }
        memset(pMem, 0, nMap);
      }

      for(i=0; i<nShmPerMap; i++){
        pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
      }
      pShmNode->nRegion += nShmPerMap;
    }
  }

shmpage_out:
  if( pShmNode->nRegion>iRegion ){
    *pp = pShmNode->apRegion[iRegion];
  }else{
    *pp = 0;
  }
  if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
  sqlite3_mutex_leave(pShmNode->pShmMutex);
  return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %r8, %r15
movl %ecx, %r12d
movl %edx, 0x10(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdi, %r13
callq *0x238563(%rip)         # 0x4bbd30
movl %eax, %ecx
shrl $0xf, %ecx
cmpl $0x8000, %eax           # imm = 0x8000
movl $0x1, %r14d
cmovgel %ecx, %r14d
movq 0x40(%r13), %rax
testq %rax, %rax
je 0x283922
movq %r15, 0x20(%rsp)
movq (%rax), %r15
cmpb $0x0, 0x23(%r15)
movq 0x28(%rsp), %rbx
je 0x28381c
movq %r13, %rdi
movq %r15, %rsi
callq 0x28474b
movl %eax, %ebp
testl %eax, %eax
jne 0x28390c
movb $0x0, 0x23(%r15)
leal (%r14,%rbx), %r13d
movl %r13d, %eax
cltd
idivl %r14d
subl %edx, %r13d
movzwl 0x20(%r15), %eax
xorl %ebp, %ebp
cmpl %eax, %r13d
jle 0x28390c
movl 0x10(%rsp), %eax
movl %eax, 0x1c(%r15)
movl 0x18(%r15), %edi
testl %edi, %edi
js 0x2838f1
leaq 0x38(%rsp), %rsi
callq *0x2382f7(%rip)         # 0x4bbb50
movl $0x130a, %ebp           # imm = 0x130A
testl %eax, %eax
jne 0x283907
movl %r13d, %eax
imull 0x10(%rsp), %eax
movq 0x68(%rsp), %rdx
movslq %eax, %rcx
cmpq %rcx, %rdx
jge 0x2838f1
testl %r12d, %r12d
je 0x283b3a
leaq 0xfff(%rdx), %rcx
testq %rdx, %rdx
cmovnsq %rdx, %rcx
sarq $0xc, %rcx
leal 0xfff(%rax), %edx
testl %eax, %eax
cmovnsl %eax, %edx
sarl $0xc, %edx
cmpl %ecx, %edx
jle 0x2838f1
movslq %ecx, %rbx
movslq %edx, %r12
subq %rbx, %r12
shlq $0xc, %rbx
orq $0xfff, %rbx            # imm = 0xFFF
leaq 0x34(%rsp), %rbp
movl 0x18(%r15), %edi
movq %rbx, %rsi
leaq 0x1468a1(%rip), %rdx    # 0x3ca170
movl $0x1, %ecx
movq %rbp, %r8
callq 0x2843c9
cmpl $0x1, %eax
jne 0x283bd9
addq $0x1000, %rbx           # imm = 0x1000
decq %r12
jne 0x2838c1
movq 0x28(%r15), %rbx
callq 0x2725a7
movl $0xc0a, %ebp            # imm = 0xC0A
testl %eax, %eax
je 0x2839b5
movq 0x28(%rsp), %rbx
movzwl 0x20(%r15), %eax
cmpl %ebx, %eax
jle 0x283985
movq 0x28(%r15), %rax
movslq %ebx, %rcx
movq (%rax,%rcx,8), %rax
jmp 0x283987
callq 0x2725a7
movl $0x7, %ebx
testl %eax, %eax
jne 0x2839a1
movq %r15, 0x20(%rsp)
movl $0x18, %edi
callq 0x272ae6
testq %rax, %rax
je 0x2839a1
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq 0x10(%r13), %rbp
movq 0x38(%rbp), %r15
xorl %ebx, %ebx
testq %r15, %r15
je 0x283ac2
movq %r15, (%rax)
incl 0x30(%r15)
movq %rax, 0x40(%r13)
movq 0x38(%r15), %rcx
movq %rcx, 0x8(%rax)
movq %rax, 0x38(%r15)
testl %ebx, %ebx
jne 0x2839a1
jmp 0x2837f6
xorl %eax, %eax
movq 0x20(%rsp), %rcx
movq %rax, (%rcx)
cmpb $0x0, 0x22(%r15)
movl $0x8, %ebx
cmovel %ebp, %ebx
testl %ebp, %ebp
cmovnel %ebp, %ebx
movl %ebx, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leal (,%r13,8), %esi
xorl %eax, %eax
testl %esi, %esi
cmovlel %eax, %esi
movq %rbx, %rdi
callq 0x272c63
testq %rax, %rax
je 0x283907
movq %rax, 0x28(%r15)
movzwl 0x20(%r15), %eax
movl $0x0, %ebp
cmpl %eax, %r13d
jle 0x283907
movl %r14d, %ecx
movl 0x10(%rsp), %edx
imull %edx, %ecx
movslq %ecx, %rcx
movq %rcx, 0x8(%rsp)
movslq %edx, %rbx
movl %r14d, %ecx
movq %rcx, 0x10(%rsp)
movl 0x18(%r15), %r8d
testl %r8d, %r8d
js 0x283a4b
xorl %ecx, %ecx
cmpb $0x0, 0x22(%r15)
sete %cl
leal 0x1(,%rcx,2), %edx
movzwl %ax, %r9d
imulq %rbx, %r9
xorl %edi, %edi
movq 0x8(%rsp), %rsi
movl $0x1, %ecx
callq *0x2382ab(%rip)         # 0x4bbce8
movq %rax, %r12
cmpq $-0x1, %rax
jne 0x283a82
jmp 0x283bf5
callq 0x2725a7
movl $0x7, %ebp
testl %eax, %eax
jne 0x283907
movq 0x8(%rsp), %rdi
callq 0x272ae6
testq %rax, %rax
je 0x283907
movq %rax, %r12
movq %rax, %rdi
xorl %esi, %esi
movq 0x8(%rsp), %rdx
callq 0x173a0
movzwl 0x20(%r15), %eax
testl %r14d, %r14d
jle 0x283aa7
movq 0x10(%rsp), %rcx
movq %rax, %rdx
movq 0x28(%r15), %rsi
movq %r12, (%rsi,%rdx,8)
incq %rdx
addq %rbx, %r12
decq %rcx
jne 0x283a94
addl %r14d, %eax
movw %ax, 0x20(%r15)
movzwl %ax, %ecx
xorl %ebp, %ebp
cmpl %ecx, %r13d
jg 0x283a09
jmp 0x283907
movq %rax, 0x8(%rsp)
movq 0x38(%r13), %r15
movl 0x18(%r13), %edi
leaq 0x38(%rsp), %rsi
callq *0x238076(%rip)         # 0x4bbb50
movl $0x70a, %ebx            # imm = 0x70A
testl %eax, %eax
jne 0x283afd
movq %r15, %rdi
callq 0x172f0
movq %rax, (%rsp)
callq 0x2725a7
movl $0x7, %ebx
testl %eax, %eax
je 0x283b41
movq %r13, %rdi
callq 0x284893
cmpl $0x0, 0x237b54(%rip)    # 0x4bb660
movq 0x8(%rsp), %r14
je 0x283b2c
movq %r14, %rdi
callq *0x237b7c(%rip)         # 0x4bb698
cltq
subq %rax, 0x23afd3(%rip)    # 0x4beaf8
decq 0x23b014(%rip)          # 0x4beb40
movq %r14, %rdi
callq *0x237b53(%rip)         # 0x4bb688
jmp 0x2839a1
xorl %ebp, %ebp
jmp 0x283907
movslq (%rsp), %rdi
addq $0x46, %rdi
movq %rdi, 0x18(%rsp)
callq 0x272ae6
testq %rax, %rax
je 0x283afd
movq %r15, %rcx
movq %rax, %r15
movq (%rsp), %rax
addl $0x6, %eax
movq %rax, (%rsp)
xorl %ebx, %ebx
movq %r15, %rdi
xorl %esi, %esi
movq 0x18(%rsp), %rdx
movq %rcx, 0x18(%rsp)
callq 0x173a0
movq %r15, %rsi
addq $0x40, %rsi
movq %rsi, 0x10(%r15)
leaq 0x15207d(%rip), %rdx    # 0x3d5c0e
movq (%rsp), %rdi
movq %rsi, (%rsp)
movq 0x18(%rsp), %rcx
xorl %eax, %eax
callq 0x274c41
movl $0xffffffff, 0x18(%r15) # imm = 0xFFFFFFFF
movq 0x10(%r13), %rax
movq %r15, 0x38(%rax)
movq %rax, (%r15)
cmpb $0x0, 0x237aa5(%rip)    # 0x4bb664
je 0x283bc9
movq $0x8, 0x8(%r15)
cmpb $0x0, 0x21(%rbp)
je 0x283c19
movq 0x8(%rsp), %rax
jmp 0x283965
movq 0x10(%r15), %rdx
leaq 0x15ab71(%rip), %rsi    # 0x3de755
movl $0x130a, %ebp           # imm = 0x130A
movl $0x130a, %edi           # imm = 0x130A
movl $0x9590, %ecx           # imm = 0x9590
jmp 0x283c0f
movq 0x10(%r15), %rdx
leaq 0xf078f(%rip), %rsi     # 0x37438f
movl $0x150a, %ebp           # imm = 0x150A
movl $0x150a, %edi           # imm = 0x150A
movl $0x95ab, %ecx           # imm = 0x95AB
callq 0x2842dc
jmp 0x283907
movq 0x38(%r13), %rdi
leaq 0x151ff1(%rip), %rsi    # 0x3d5c15
xorl %edx, %edx
callq 0x2823f2
testl %eax, %eax
je 0x283c39
movl 0x18(%r15), %ebx
movq (%rsp), %rbp
jmp 0x283c59
movl $0x1ff, %edx            # imm = 0x1FF
andl 0x50(%rsp), %edx
movq (%rsp), %rbp
movq %rbp, %rdi
movl $0x20042, %esi          # imm = 0x20042
callq 0x284201
movl %eax, %ebx
movl %eax, 0x18(%r15)
testl %ebx, %ebx
jns 0x283c82
movl $0x1ff, %edx            # imm = 0x1FF
andl 0x50(%rsp), %edx
movq %rbp, %rdi
movl $0x20000, %esi          # imm = 0x20000
callq 0x284201
movl %eax, 0x18(%r15)
testl %eax, %eax
js 0x283cc9
movl %eax, %ebx
movb $0x1, 0x22(%r15)
movl 0x54(%rsp), %ebp
movl 0x58(%rsp), %eax
movl %eax, (%rsp)
callq *0x23803d(%rip)         # 0x4bbcd0
testl %eax, %eax
jne 0x283ca4
movl %ebx, %edi
movl %ebp, %esi
movl (%rsp), %edx
callq *0x238014(%rip)         # 0x4bbcb8
movq %r13, %rdi
movq %r15, %rsi
callq 0x28474b
movl %eax, %ebx
cmpl $0x508, %eax            # imm = 0x508
je 0x283bcf
testl %ebx, %ebx
je 0x283bcf
jmp 0x283afd
leaq 0x151efb(%rip), %rsi    # 0x3d5bcb
leaq 0x151ee3(%rip), %rdx    # 0x3d5bba
leaq 0x151d53(%rip), %r8     # 0x3d5a31
movl $0xe, %ebx
movl $0xe, %edi
movl $0x9500, %ecx           # imm = 0x9500
xorl %eax, %eax
callq 0x274d08
leaq 0xe6ae9(%rip), %rsi     # 0x36a7e4
movl $0xe, %edi
movq %rbp, %rdx
movl $0x9500, %ecx           # imm = 0x9500
callq 0x2842dc
jmp 0x283afd