// File: /workspace/github_c_makefile_star2_O1/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, 0x10(%rsp)
movl %edx, %ebp
movq %rsi, 0x38(%rsp)
movq %rdi, %rbx
callq *0x239aae(%rip)         # 0x4c6d30
movl %eax, %ecx
shrl $0xf, %ecx
cmpl $0x8000, %eax           # imm = 0x8000
movl $0x1, %eax
cmovgel %ecx, %eax
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x40(%rbx)
je 0x28d327
movq 0x40(%rbx), %rax
movq (%rax), %r15
cmpb $0x0, 0x23(%r15)
movq 0x38(%rsp), %r12
je 0x28d2d1
movq %rbx, %rdi
movq %r15, %rsi
callq 0x28e2c1
movl %eax, %ebx
testl %eax, %eax
jne 0x28d79b
movb $0x0, 0x23(%r15)
movq 0x28(%rsp), %rcx
leal (%rcx,%r12), %r13d
movl %r13d, %eax
cltd
idivl %ecx
subl %edx, %r13d
movzwl 0x20(%r15), %eax
xorl %ebx, %ebx
cmpl %eax, %r13d
jle 0x28d79b
movl %ebp, 0x1c(%r15)
movl 0x18(%r15), %edi
xorl %ebx, %ebx
testl %edi, %edi
js 0x28d652
leaq 0x48(%rsp), %rsi
callq *0x239840(%rip)         # 0x4c6b50
movl $0x2, %ecx
testl %eax, %eax
je 0x28d3c6
movl $0x130a, %ebx           # imm = 0x130A
jmp 0x28d791
callq 0x27bed9
testl %eax, %eax
je 0x28d335
xorl %r15d, %r15d
jmp 0x28d342
movl $0x18, %edi
callq 0x27c381
movq %rax, %r15
testq %r15, %r15
je 0x28d3bb
movl %ebp, 0x8(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
movq $0x0, 0x10(%r15)
movq 0x10(%rbx), %rbp
movq 0x38(%rbp), %r13
xorl %r12d, %r12d
testq %r13, %r13
jne 0x28d39b
movq 0x38(%rbx), %r12
movl 0x18(%rbx), %edi
leaq 0x48(%rsp), %rsi
callq *0x2397d4(%rip)         # 0x4c6b50
testl %eax, %eax
je 0x28d4c3
movl $0x70a, %r12d           # imm = 0x70A
xorl %r13d, %r13d
xorl %eax, %eax
movl 0x8(%rsp), %ebp
testb %al, %al
je 0x28d4a5
movq %r13, (%r15)
incl 0x30(%r13)
movq %r15, 0x40(%rbx)
movq 0x38(%r13), %rax
movq %rax, 0x8(%r15)
movq %r15, 0x38(%r13)
movl 0x8(%rsp), %ebp
jmp 0x28d4b5
movl $0x7, %r12d
jmp 0x28d4b5
movl %r13d, %eax
imull %ebp, %eax
movq 0x78(%rsp), %rdx
movslq %eax, %rsi
cmpq %rsi, %rdx
jge 0x28d652
cmpl $0x0, 0x10(%rsp)
je 0x28d63f
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 0x28d646
movl %ebp, 0x8(%rsp)
movq %r14, 0x10(%rsp)
movslq %ecx, %r12
movslq %edx, %r14
subq %r12, %r14
shlq $0xc, %r12
orq $0xfff, %r12            # imm = 0xFFF
xorl %ebx, %ebx
movl $0x0, 0x34(%rsp)
movl 0x18(%r15), %edi
movq %r12, %rsi
leaq 0x14644b(%rip), %rdx    # 0x3d3890
movl $0x1, %ecx
leaq 0x34(%rsp), %r8
callq 0x28df1b
movl %eax, %ebp
movl $0x0, %ecx
cmpl $0x1, %eax
je 0x28d484
movq 0x10(%r15), %rdx
movl $0x130a, %ebx           # imm = 0x130A
movl $0x130a, %edi           # imm = 0x130A
leaq 0x15a9e0(%rip), %rsi    # 0x3e7e55
movl $0x9590, %ecx           # imm = 0x9590
callq 0x28de48
movl $0x2, %ecx
cmpl $0x1, %ebp
jne 0x28d497
addq $0x1000, %r12           # imm = 0x1000
decq %r14
jne 0x28d42f
xorl %ecx, %ecx
movq 0x10(%rsp), %r14
movl 0x8(%rsp), %ebp
jmp 0x28d64a
movq %rbx, %rdi
callq 0x28e42d
movq %r15, %rdi
callq 0x27c490
testl %r12d, %r12d
jne 0x28d7cc
jmp 0x28d2a4
movq %r12, %rdi
callq 0x172f0
movq %rax, 0x20(%rsp)
movslq %eax, %r13
addq $0x46, %r13
callq 0x27bed9
testl %eax, %eax
movq %r13, 0x18(%rsp)
je 0x28d4ea
xorl %r13d, %r13d
jmp 0x28d4f5
movq %r13, %rdi
callq 0x27c381
movq %rax, %r13
testq %r13, %r13
je 0x28d634
movq 0x20(%rsp), %rax
addl $0x6, %eax
movq %rax, 0x20(%rsp)
movq %r12, 0x40(%rsp)
xorl %r12d, %r12d
movq %r13, %rdi
xorl %esi, %esi
movq 0x18(%rsp), %rdx
callq 0x173a0
leaq 0x40(%r13), %rsi
movq %rsi, 0x10(%r13)
leaq 0x151e31(%rip), %rdx    # 0x3df362
movq 0x20(%rsp), %rdi
movq %rsi, 0x18(%rsp)
movq 0x40(%rsp), %rcx
xorl %eax, %eax
callq 0x27e570
movl $0xffffffff, 0x18(%r13) # imm = 0xFFFFFFFF
movq 0x10(%rbx), %rax
movq %r13, 0x38(%rax)
movq %rax, (%r13)
cmpb $0x0, 0x239102(%rip)    # 0x4c6664
je 0x28d56c
movq $0x8, 0x8(%r13)
movb $0x1, %al
cmpb $0x0, 0x21(%rbp)
jne 0x28d38f
movq 0x38(%rbx), %rdi
leaq 0x151de6(%rip), %rsi    # 0x3df369
xorl %edx, %edx
callq 0x28bf29
testl %eax, %eax
movl 0x8(%rsp), %ebp
movq 0x18(%rsp), %r12
jne 0x28d5b1
movl $0x1ff, %edx            # imm = 0x1FF
andl 0x60(%rsp), %edx
movq %r12, %rdi
movl $0x20042, %esi          # imm = 0x20042
callq 0x28dd65
movl %eax, 0x18(%r13)
cmpl $0x0, 0x18(%r13)
jns 0x28d5df
movl $0x1ff, %edx            # imm = 0x1FF
andl 0x60(%rsp), %edx
movq %r12, %rdi
movl $0x20000, %esi          # imm = 0x20000
callq 0x28dd65
movl %eax, 0x18(%r13)
testl %eax, %eax
js 0x28d7e3
movb $0x1, 0x22(%r13)
movl 0x18(%r13), %eax
movl %eax, 0x20(%rsp)
movl 0x64(%rsp), %r12d
movl 0x68(%rsp), %eax
movl %eax, 0x18(%rsp)
callq *0x2396d6(%rip)         # 0x4c6cd0
testl %eax, %eax
jne 0x28d60f
movl 0x20(%rsp), %edi
movl %r12d, %esi
movl 0x18(%rsp), %edx
callq *0x2396a9(%rip)         # 0x4c6cb8
movq %rbx, %rdi
movq %r13, %rsi
callq 0x28e2c1
movl %eax, %r12d
cmpl $0x508, %eax            # imm = 0x508
je 0x28d62d
testl %r12d, %r12d
jne 0x28d82a
movb $0x1, %al
jmp 0x28d393
movl $0x7, %r12d
jmp 0x28d38a
xorl %ebx, %ebx
jmp 0x28d791
xorl %ecx, %ecx
xorl %ebx, %ebx
testl %ecx, %ecx
jne 0x28d791
movq 0x28(%r15), %r12
callq 0x27bed9
movl %eax, %ecx
xorl %eax, %eax
testl %ecx, %ecx
jne 0x28d67b
leal (,%r13,8), %ecx
testl %ecx, %ecx
cmovgl %ecx, %eax
movq %r12, %rdi
movq %rax, %rsi
callq 0x27c4f3
testq %rax, %rax
je 0x28d783
movq %rax, 0x28(%r15)
movq 0x28(%rsp), %rcx
movl %ecx, %eax
imull %ebp, %eax
cltq
movq %rax, 0x10(%rsp)
movslq %ebp, %r12
movl %ecx, %eax
movq %rax, 0x8(%rsp)
movzwl 0x20(%r15), %r9d
cmpl %r9d, %r13d
jle 0x28d78f
movl 0x18(%r15), %r8d
testl %r8d, %r8d
js 0x28d70b
xorl %eax, %eax
cmpb $0x0, 0x22(%r15)
sete %al
leal 0x1(,%rax,2), %edx
imulq %r12, %r9
xorl %edi, %edi
movq 0x10(%rsp), %rsi
movl $0x1, %ecx
callq *0x239607(%rip)         # 0x4c6ce8
movq %rax, %rbp
cmpq $-0x1, %rax
jne 0x28d739
movq 0x10(%r15), %rdx
movl $0x150a, %ebx           # imm = 0x150A
movl $0x150a, %edi           # imm = 0x150A
leaq 0xeff10(%rip), %rsi     # 0x37d60f
movl $0x95ab, %ecx           # imm = 0x95AB
callq 0x28de48
jmp 0x28d772
callq 0x27bed9
testl %eax, %eax
je 0x28d718
xorl %ebp, %ebp
jmp 0x28d725
movq 0x10(%rsp), %rdi
callq 0x27c381
movq %rax, %rbp
testq %rbp, %rbp
je 0x28d76d
movq %rbp, %rdi
xorl %esi, %esi
movq 0x10(%rsp), %rdx
callq 0x173a0
cmpl $0x0, 0x28(%rsp)
jle 0x28d75d
movzwl 0x20(%r15), %eax
movq 0x8(%rsp), %rcx
movq 0x28(%r15), %rdx
movq %rbp, (%rdx,%rax,8)
incq %rax
addq %r12, %rbp
decq %rcx
jne 0x28d74a
movq 0x28(%rsp), %rax
addw %ax, 0x20(%r15)
xorl %ecx, %ecx
movb $0x1, %al
jmp 0x28d779
movl $0x7, %ebx
movl $0x2, %ecx
xorl %eax, %eax
testb %al, %al
jne 0x28d6a3
jmp 0x28d791
movl $0xc0a, %ebx            # imm = 0xC0A
movl $0x2, %ecx
jmp 0x28d791
xorl %ecx, %ecx
testb $0x1, %cl
movq 0x38(%rsp), %r12
jne 0x28d7e1
movzwl 0x20(%r15), %eax
cmpl %r12d, %eax
jle 0x28d7b2
movq 0x28(%r15), %rax
movslq %r12d, %rcx
movq (%rax,%rcx,8), %rax
jmp 0x28d7b4
xorl %eax, %eax
movq %rax, (%r14)
testl %ebx, %ebx
movl $0x8, %r12d
cmovnel %ebx, %r12d
cmpb $0x0, 0x22(%r15)
cmovel %ebx, %r12d
movl %r12d, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x28d7cc
leaq 0x151b35(%rip), %rsi    # 0x3df31f
leaq 0x151b1d(%rip), %rdx    # 0x3df30e
leaq 0x15198d(%rip), %r8     # 0x3df185
movl $0xe, %r12d
movl $0xe, %edi
movl $0x9500, %ecx           # imm = 0x9500
xorl %eax, %eax
callq 0x27e637
leaq 0xe621e(%rip), %rsi     # 0x373a34
movl $0xe, %edi
movq 0x18(%rsp), %rdx
movl $0x9500, %ecx           # imm = 0x9500
callq 0x28de48
xorl %eax, %eax
jmp 0x28d393