// File: /workspace/github_c_makefile_star2_O2/shifengbin[P]bishe/Output/../Persistence/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->mutex);
  assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
  assert( pShmNode->pInode==pDbFd->pInode );
  assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
  assert( pShmNode->h<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->h>=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->h, &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->h, 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;
      goto shmpage_out;
    }
    pShmNode->apRegion = apNew;
    while( pShmNode->nRegion<nReqRegion ){
      int nMap = szRegion*nShmPerMap;
      int i;
      void *pMem;
      if( pShmNode->h>=0 ){
        pMem = osMmap(0, nMap,
            pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, 
            MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
        );
        if( pMem==MAP_FAILED ){
          rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
          goto shmpage_out;
        }
      }else{
        pMem = sqlite3_malloc64(szRegion);
        if( pMem==0 ){
          rc = SQLITE_NOMEM;
          goto shmpage_out;
        }
        memset(pMem, 0, szRegion);
      }

      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->mutex);
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %r8, %r15
movl %ecx, -0x5c(%rbp)
movl %edx, -0x38(%rbp)
movl %esi, %r13d
movq %rdi, %r14
callq *0x691e6(%rip)          # 0x83ae0
movl %eax, %ecx
shrl $0xf, %ecx
cmpl $0x8000, %eax           # imm = 0x8000
pushq $0x1
popq %rax
cmovgel %ecx, %eax
movq %rax, -0x58(%rbp)
cmpq $0x0, 0x40(%r14)
je 0x1aa0d
movq 0x40(%r14), %rax
movq (%rax), %r12
movq 0x8(%r12), %rdi
testq %rdi, %rdi
je 0x1a930
callq *0x68b78(%rip)          # 0x834a8
movq -0x58(%rbp), %rcx
leal (%rcx,%r13), %ebx
movl %ebx, %eax
cltd
idivl %ecx
subl %edx, %ebx
movzwl 0x20(%r12), %eax
xorl %edx, %edx
cmpl %eax, %ebx
jle 0x1acda
movq %r13, -0x40(%rbp)
movq %r15, -0x50(%rbp)
movl -0x38(%rbp), %eax
movl %eax, 0x1c(%r12)
movl 0x18(%r12), %edi
testl %edi, %edi
js 0x1aa75
leaq -0x100(%rbp), %rsi
callq *0x68f87(%rip)          # 0x83900
movl $0x130a, %edx           # imm = 0x130A
testl %eax, %eax
jne 0x1accc
movl %ebx, %ecx
imull -0x38(%rbp), %ecx
movq -0xd0(%rbp), %rax
movslq %ecx, %rdx
cmpq %rdx, %rax
jge 0x1aa75
cmpl $0x0, -0x5c(%rbp)
je 0x1ab9d
movl $0x1000, %esi           # imm = 0x1000
cqto
idivq %rsi
movq %rax, %rsi
movl $0x1000, %edi           # imm = 0x1000
movl %ecx, %eax
cltd
idivl %edi
movslq %esi, %r13
movslq %eax, %r15
movq %r13, %r14
shlq $0xc, %r14
orq $0xfff, %r14            # imm = 0xFFF
cmpq %r15, %r13
jge 0x1aa75
movl 0x18(%r12), %edi
movq %r14, %rsi
leaq 0x546a5(%rip), %rdx     # 0x6f091
pushq $0x1
popq %rcx
leaq -0x6c(%rbp), %r8
callq 0x1b57d
cmpl $0x1, %eax
jne 0x1acac
incq %r13
addq $0x1000, %r14           # imm = 0x1000
jmp 0x1a9d4
pushq $0x18
popq %rdi
callq 0xa230
testq %rax, %rax
je 0x1ab57
movq %rax, %rbx
movq %r13, -0x40(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
callq 0x1b179
movq 0x10(%r14), %r13
movq 0x20(%r13), %r12
testq %r12, %r12
je 0x1ab60
movq %r12, (%rbx)
incl 0x30(%r12)
movq %rbx, 0x40(%r14)
callq 0x1b1f4
movq 0x8(%r12), %rdi
testq %rdi, %rdi
je 0x1abae
callq *0x68a3d(%rip)          # 0x834a8
movq 0x8(%r12), %rdi
jmp 0x1abb0
movq 0x28(%r12), %rdi
leal (,%rbx,8), %esi
callq 0xa6a0
testq %rax, %rax
je 0x1aba4
movq %rax, 0x28(%r12)
movl -0x38(%rbp), %eax
movslq %eax, %r15
movq -0x58(%rbp), %r13
imull %r13d, %eax
cltq
movq %rax, -0x38(%rbp)
xorl %eax, %eax
testl %r13d, %r13d
cmovlel %eax, %r13d
movzwl 0x20(%r12), %eax
movzwl %ax, %r9d
cmpl %r9d, %ebx
jle 0x1ab9d
movl 0x18(%r12), %r8d
testl %r8d, %r8d
js 0x1ab01
xorl %eax, %eax
cmpb $0x0, 0x22(%r12)
sete %al
leal 0x1(,%rax,2), %edx
imulq %r15, %r9
xorl %edi, %edi
movq -0x38(%rbp), %rsi
pushq $0x1
popq %rcx
callq *0x68fa5(%rip)          # 0x83a98
movq %rax, %r14
cmpq $-0x1, %rax
jne 0x1ab22
jmp 0x1abd6
movq %r15, %rdi
callq 0xa230
testq %rax, %rax
je 0x1aca5
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0xa270
xorl %eax, %eax
cmpq %rax, %r13
je 0x1ab42
movq 0x28(%r12), %rcx
movzwl 0x20(%r12), %edx
addl %eax, %edx
movq %r14, (%rcx,%rdx,8)
incq %rax
addq %r15, %r14
jmp 0x1ab24
movzwl 0x20(%r12), %eax
addw -0x58(%rbp), %ax
movw %ax, 0x20(%r12)
jmp 0x1aab7
pushq $0x7
popq %r15
jmp 0x1ad16
movq %r15, -0x50(%rbp)
movq 0x38(%r14), %r12
movl 0x18(%r14), %edi
leaq -0x100(%rbp), %rsi
callq *0x68d87(%rip)          # 0x83900
movl $0x70a, %r15d           # imm = 0x70A
testl %eax, %eax
je 0x1abfb
movq %r14, %rdi
callq 0x1b9d2
movq %rbx, %rdi
callq 0xa828
callq 0x1b1f4
jmp 0x1ad16
xorl %edx, %edx
jmp 0x1accc
movl $0xc0a, %edx            # imm = 0xC0A
jmp 0x1accc
xorl %edi, %edi
movq -0x40(%rbp), %r13
movq 0x38(%r12), %rax
movq %rax, 0x8(%rbx)
movq %rbx, 0x38(%r12)
testq %rdi, %rdi
je 0x1a919
callq *0x688e7(%rip)          # 0x834b8
jmp 0x1a919
movq 0x10(%r12), %rdx
leaq 0x51728(%rip), %rsi     # 0x6c30a
movl $0x150a, %edi           # imm = 0x150A
movl $0x7b03, %ecx           # imm = 0x7B03
callq 0x1b436
movl $0x150a, %edx           # imm = 0x150A
jmp 0x1accc
movq %r12, -0x48(%rbp)
movq %r12, %rdi
callq 0xa1c0
movq %rax, -0x30(%rbp)
movslq %eax, %rdi
addq $0x46, %rdi
movq %rdi, -0x68(%rbp)
callq 0xa230
pushq $0x7
popq %r15
testq %rax, %rax
je 0x1ab83
movq %rax, %r12
movq -0x30(%rbp), %rax
addl $0x6, %eax
movq %rax, -0x30(%rbp)
movq %r12, %rdi
xorl %esi, %esi
movq -0x68(%rbp), %rdx
callq 0xa270
movq %r12, %rsi
addq $0x40, %rsi
movq %rsi, 0x10(%r12)
leaq 0x51792(%rip), %rdx     # 0x6c3e9
movq -0x30(%rbp), %rdi
movq %rsi, -0x30(%rbp)
movq -0x48(%rbp), %rcx
xorl %eax, %eax
callq 0xa400
orl $-0x1, 0x18(%r12)
movq 0x10(%r14), %rax
movq %r12, 0x20(%rax)
movq %rax, (%r12)
xorl %edi, %edi
callq 0xa1b0
movq %rax, 0x8(%r12)
testq %rax, %rax
je 0x1ab83
cmpb $0x0, 0x15(%r13)
je 0x1ad2b
movq -0x50(%rbp), %r15
jmp 0x1aa46
pushq $0x7
popq %rax
movl %eax, %edx
jmp 0x1accc
movq 0x10(%r12), %rdx
leaq 0x51602(%rip), %rsi     # 0x6c2ba
movl $0x130a, %edi           # imm = 0x130A
movl $0x7ae8, %ecx           # imm = 0x7AE8
callq 0x1b436
movl $0x130a, %edx           # imm = 0x130A
movzwl 0x20(%r12), %eax
movq -0x50(%rbp), %r15
movq -0x40(%rbp), %r13
cmpl %r13d, %eax
jle 0x1aced
movq 0x28(%r12), %rax
movslq %r13d, %rcx
movq (%rax,%rcx,8), %rax
jmp 0x1acef
xorl %eax, %eax
movq %rax, (%r15)
cmpb $0x0, 0x22(%r12)
pushq $0x8
popq %r15
cmovel %edx, %r15d
testl %edx, %edx
cmovnel %edx, %r15d
movq 0x8(%r12), %rdi
testq %rdi, %rdi
je 0x1ad16
callq *0x687a2(%rip)          # 0x834b8
movl %r15d, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x38(%r14), %rdi
leaq 0x516ba(%rip), %rsi     # 0x6c3f0
xorl %r15d, %r15d
xorl %edx, %edx
callq 0xa100
testl %eax, %eax
je 0x1ad4c
movb $0x1, 0x22(%r12)
jmp 0x1ad50
pushq $0x42
popq %r15
movq -0x30(%rbp), %r13
movl $0x1ff, %edx            # imm = 0x1FF
andl -0xe8(%rbp), %edx
movq %r13, %rdi
movl %r15d, %esi
callq 0x1b349
movl %eax, 0x18(%r12)
testl %eax, %eax
js 0x1add9
movl %eax, %r15d
movl -0xe4(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0xe0(%rbp), %eax
movl %eax, -0x48(%rbp)
callq *0x68cf2(%rip)          # 0x83a80
testl %eax, %eax
jne 0x1ada1
movl %r15d, %edi
movl -0x30(%rbp), %esi
movl -0x48(%rbp), %edx
callq *0x68cc7(%rip)          # 0x83a68
pushq $0x1
popq %rcx
movq %r14, %rdi
movl %ecx, %esi
movl $0x80, %edx
callq 0x1b97a
testl %eax, %eax
je 0x1adfb
pushq $0x1
popq %rcx
movq %r14, %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x1b97a
testl %eax, %eax
je 0x1ac9c
movl %eax, %r15d
jmp 0x1ab83
movl $0x7a54, %edi           # imm = 0x7A54
callq 0x1b492
leaq 0x51492(%rip), %rsi     # 0x6c27c
pushq $0xe
popq %r15
movl %r15d, %edi
movq %r13, %rdx
movl $0x7a54, %ecx           # imm = 0x7A54
jmp 0x1ae25
movl 0x18(%r12), %edi
xorl %esi, %esi
callq 0x1b5f7
testl %eax, %eax
je 0x1adb7
leaq 0x51489(%rip), %rsi     # 0x6c29b
movl $0x120a, %r15d          # imm = 0x120A
movl $0x120a, %edi           # imm = 0x120A
movq %r13, %rdx
movl $0x7a64, %ecx           # imm = 0x7A64
callq 0x1b436
jmp 0x1ab83