// File: /workspace/github_c_makefile_star2_O2/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 $0xd8, %rsp
movq %r8, %r14
movl %ecx, 0x34(%rsp)
movl %edx, %ebp
movl %esi, %r15d
movq %rdi, %r13
callq *0x20cf2e(%rip)         # 0x43ace0
movl %eax, %ecx
shrl $0xf, %ecx
cmpl $0x8000, %eax           # imm = 0x8000
pushq $0x1
popq %rax
cmovgel %ecx, %eax
movq %rax, 0x28(%rsp)
movq 0x40(%r13), %rax
testq %rax, %rax
je 0x22ded8
movl %ebp, 0x18(%rsp)
movq %r15, %rbp
movq (%rax), %r15
cmpb $0x0, 0x23(%r15)
je 0x22ddff
movq %r13, %rdi
movq %r15, %rsi
callq 0x22ec29
movl %eax, %edx
testl %eax, %eax
jne 0x22e18d
movb $0x0, 0x23(%r15)
movq 0x28(%rsp), %rcx
leal (%rcx,%rbp), %ebx
movl %ebx, %eax
cltd
idivl %ecx
subl %edx, %ebx
movzwl 0x20(%r15), %eax
xorl %edx, %edx
cmpl %eax, %ebx
jle 0x22e18d
movq %rbp, 0x8(%rsp)
movq %r14, 0x20(%rsp)
movl 0x18(%rsp), %ebp
movl %ebp, 0x1c(%r15)
movl 0x18(%r15), %edi
testl %edi, %edi
js 0x22df46
leaq 0x48(%rsp), %rsi
callq *0x20ccba(%rip)         # 0x43ab00
movl $0x130a, %edx           # imm = 0x130A
testl %eax, %eax
jne 0x22e183
movl %ebx, %ecx
imull %ebp, %ecx
movq 0x78(%rsp), %rax
movslq %ecx, %rdx
cmpq %rdx, %rax
jge 0x22df46
cmpl $0x0, 0x34(%rsp)
je 0x22e05e
movl $0x1000, %esi           # imm = 0x1000
cqto
idivq %rsi
movq %rax, %rsi
movl $0x1000, %edi           # imm = 0x1000
movl %ecx, %eax
cltd
idivl %edi
movslq %esi, %r14
movslq %eax, %r13
movq %r14, %r12
shlq $0xc, %r12
orq $0xfff, %r12            # imm = 0xFFF
cmpq %r13, %r14
jge 0x22df46
movl 0x18(%r15), %edi
movq %r12, %rsi
leaq 0x13c6ba(%rip), %rdx    # 0x36a570
pushq $0x1
popq %rcx
leaq 0x44(%rsp), %r8
callq 0x22e84a
cmpl $0x1, %eax
jne 0x22e164
incq %r14
addq $0x1000, %r12           # imm = 0x1000
jmp 0x22de9f
pushq $0x18
popq %rdi
callq 0x21f7df
testq %rax, %rax
je 0x22df3d
movq %rax, %rbx
movl %ebp, 0x18(%rsp)
movq %r15, %rbp
movq %r14, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq 0x10(%r13), %r14
movq 0x38(%r14), %r15
xorl %r12d, %r12d
testq %r15, %r15
je 0x22e02c
movq %r15, (%rbx)
incl 0x30(%r15)
movq %rbx, 0x40(%r13)
movq 0x38(%r15), %rax
movq %rax, 0x8(%rbx)
movq %rbx, 0x38(%r15)
testl %r12d, %r12d
movq 0x20(%rsp), %r14
jne 0x22e1bb
jmp 0x22ddde
pushq $0x7
popq %r12
jmp 0x22e1bb
movq 0x28(%r15), %rdi
leal (,%rbx,8), %esi
callq 0x21f830
testq %rax, %rax
je 0x22e065
movq %rax, 0x28(%r15)
movq 0x28(%rsp), %r12
movl %r12d, %eax
imull %ebp, %eax
movslq %eax, %rsi
movslq %ebp, %r14
xorl %eax, %eax
testl %r12d, %r12d
cmovlel %eax, %r12d
movzwl 0x20(%r15), %eax
movq %rsi, 0x18(%rsp)
movzwl %ax, %r9d
cmpl %r9d, %ebx
jle 0x22e05e
movl 0x18(%r15), %r8d
testl %r8d, %r8d
js 0x22dfcb
xorl %eax, %eax
cmpb $0x0, 0x22(%r15)
sete %al
leal 0x1(,%rax,2), %edx
imulq %r14, %r9
xorl %edi, %edi
pushq $0x1
popq %rcx
callq *0x20ccdb(%rip)         # 0x43ac98
movq %rax, %rbp
cmpq $-0x1, %rax
jne 0x22dff5
jmp 0x22e133
movq %rbx, %r13
movq %rsi, %rdi
movq %rsi, %rbx
callq 0x21f7df
testq %rax, %rax
je 0x22e154
movq %rax, %rbp
movq %rax, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x17380
movq %r13, %rbx
xorl %eax, %eax
movq 0x18(%rsp), %rsi
cmpq %rax, %r12
je 0x22e018
movq 0x28(%r15), %rcx
movzwl 0x20(%r15), %edx
addl %eax, %edx
movq %rbp, (%rcx,%rdx,8)
incq %rax
addq %r14, %rbp
jmp 0x22dffc
movzwl 0x20(%r15), %eax
addw 0x28(%rsp), %ax
movw %ax, 0x20(%r15)
jmp 0x22df87
movq 0x38(%r13), %r15
movl 0x18(%r13), %edi
leaq 0x48(%rsp), %rsi
callq *0x20cac1(%rip)         # 0x43ab00
movl $0x70a, %r12d           # imm = 0x70A
testl %eax, %eax
je 0x22e06f
movq %r13, %rdi
callq 0x22ed00
movq %rbx, %rdi
callq 0x21f7f9
jmp 0x22e1bb
xorl %edx, %edx
jmp 0x22e183
movl $0xc0a, %edx            # imm = 0xC0A
jmp 0x22e183
movq %r15, 0x8(%rsp)
movq %r15, %rdi
callq 0x172d0
movq %rax, %r12
movslq %r12d, %rdi
addq $0x46, %rdi
movq %rdi, 0x10(%rsp)
callq 0x21f7df
testq %rax, %rax
je 0x22e15b
movq %rax, %r15
addl $0x6, %r12d
movq %r12, 0x38(%rsp)
xorl %r12d, %r12d
movq %rax, %rdi
xorl %esi, %esi
movq 0x10(%rsp), %rdx
callq 0x17380
movq %r15, %rsi
addq $0x40, %rsi
movq %rsi, 0x10(%r15)
leaq 0x147f65(%rip), %rdx    # 0x37602e
movq 0x38(%rsp), %rdi
movq %rsi, 0x10(%rsp)
movq 0x8(%rsp), %rcx
xorl %eax, %eax
callq 0x221418
orl $-0x1, 0x18(%r15)
movq 0x10(%r13), %rax
movq %r15, 0x38(%rax)
movq %rax, (%r15)
cmpb $0x0, 0x20c51e(%rip)    # 0x43a614
je 0x22e100
movq $0x8, 0x8(%r15)
cmpb $0x0, 0x21(%r14)
jne 0x22df13
movq 0x38(%r13), %rdi
leaq 0x147f1f(%rip), %rsi    # 0x376035
xorl %edx, %edx
callq 0x22cd81
testl %eax, %eax
je 0x22e1d0
movl 0x18(%r15), %r12d
movq 0x10(%rsp), %r14
jmp 0x22e1f2
movq 0x10(%r15), %rdx
leaq 0xe6561(%rip), %rsi     # 0x31469f
movl $0x150a, %edi           # imm = 0x150A
movl $0x95ab, %ecx           # imm = 0x95AB
callq 0x22e701
movl $0x150a, %edx           # imm = 0x150A
jmp 0x22e183
pushq $0x7
popq %rax
movl %eax, %edx
jmp 0x22e183
pushq $0x7
popq %r12
jmp 0x22e049
movq 0x10(%r15), %rdx
leaq 0x1509b6(%rip), %rsi    # 0x37eb25
movl $0x130a, %edi           # imm = 0x130A
movl $0x9590, %ecx           # imm = 0x9590
callq 0x22e701
movl $0x130a, %edx           # imm = 0x130A
movq 0x20(%rsp), %r14
movq 0x8(%rsp), %rbp
movzwl 0x20(%r15), %eax
cmpl %ebp, %eax
jle 0x22e1a3
movq 0x28(%r15), %rax
movslq %ebp, %rcx
movq (%rax,%rcx,8), %rax
jmp 0x22e1a5
xorl %eax, %eax
movq %rax, (%r14)
cmpb $0x0, 0x22(%r15)
pushq $0x8
popq %r12
cmovel %edx, %r12d
testl %edx, %edx
cmovnel %edx, %r12d
movl %r12d, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1ff, %edx            # imm = 0x1FF
andl 0x60(%rsp), %edx
movq 0x10(%rsp), %r14
movq %r14, %rdi
movl $0x20042, %esi          # imm = 0x20042
callq 0x22e62a
movl %eax, %r12d
movl %eax, 0x18(%r15)
testl %r12d, %r12d
jns 0x22e21d
movl $0x1ff, %edx            # imm = 0x1FF
andl 0x60(%rsp), %edx
movq %r14, %rdi
movl $0x20000, %esi          # imm = 0x20000
callq 0x22e62a
movl %eax, 0x18(%r15)
testl %eax, %eax
js 0x22e26b
movl %eax, %r12d
movb $0x1, 0x22(%r15)
movl 0x64(%rsp), %r14d
movl 0x68(%rsp), %eax
movl %eax, 0x8(%rsp)
callq *0x20ca50(%rip)         # 0x43ac80
testl %eax, %eax
jne 0x22e244
movl %r12d, %edi
movl %r14d, %esi
movl 0x8(%rsp), %edx
callq *0x20ca24(%rip)         # 0x43ac68
movq %r13, %rdi
movq %r15, %rsi
callq 0x22ec29
movl %eax, %r12d
cmpl $0x508, %eax            # imm = 0x508
je 0x22df13
testl %r12d, %r12d
je 0x22df13
jmp 0x22e049
movl $0x9500, %edi           # imm = 0x9500
callq 0x22e760
movl %eax, %r12d
leaq 0xdc4c5(%rip), %rsi     # 0x30a744
movl %eax, %edi
movq %r14, %rdx
movl $0x9500, %ecx           # imm = 0x9500
callq 0x22e701
jmp 0x22e049