// File: /workspace/github_c_makefile_star2_O0/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
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x44(%rbp)
callq 0x24760
movl %eax, -0x48(%rbp)
movq -0x30(%rbp), %rax
cmpq $0x0, 0x40(%rax)
jne 0x22b0d
movq -0x30(%rbp), %rdi
callq 0x247a0
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x22b0b
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x22e83
jmp 0x22b0d
movq -0x30(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0xb550
movl -0x14(%rbp), %eax
addl -0x48(%rbp), %eax
cltd
idivl -0x48(%rbp)
imull -0x48(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x40(%rbp), %rax
movzwl 0x20(%rax), %eax
cmpl -0x4c(%rbp), %eax
jge 0x22e23
movl -0x4c(%rbp), %eax
imull -0x18(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x18(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x40(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jl 0x22c8c
movq 0x9ce24(%rip), %rax     # 0xbf9a0
movq -0x40(%rbp), %rcx
movl 0x18(%rcx), %edi
leaq -0xf0(%rbp), %rsi
callq *%rax
cmpl $0x0, %eax
je 0x22b9d
movl $0x130a, -0x44(%rbp)    # imm = 0x130A
jmp 0x22e25
movq -0xc0(%rbp), %rax
movslq -0x5c(%rbp), %rcx
cmpq %rcx, %rax
jge 0x22c8a
cmpl $0x0, -0x1c(%rbp)
jne 0x22bbc
jmp 0x22e25
movq -0xc0(%rbp), %rax
movl $0x1000, %ecx           # imm = 0x1000
cqto
idivq %rcx
movl %eax, -0xf4(%rbp)
movl -0xf4(%rbp), %eax
movl %eax, -0x114(%rbp)
movl -0x5c(%rbp), %eax
movl $0x1000, %ecx           # imm = 0x1000
cltd
idivl %ecx
movl %eax, %ecx
movl -0x114(%rbp), %eax
cmpl %ecx, %eax
jge 0x22c86
movl $0x0, -0xf8(%rbp)
movq -0x40(%rbp), %rax
movl 0x18(%rax), %edi
movl -0xf4(%rbp), %eax
shll $0xc, %eax
addl $0x1000, %eax           # imm = 0x1000
subl $0x1, %eax
movslq %eax, %rsi
leaq 0x85db9(%rip), %rdx     # 0xa89df
movl $0x1, %ecx
leaq -0xf8(%rbp), %r8
callq 0x23c60
cmpl $0x1, %eax
je 0x22c70
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rdx
movl $0x130a, %edi           # imm = 0x130A
leaq 0x82f95(%rip), %rsi     # 0xa5bf3
movl $0x7ae8, %ecx           # imm = 0x7AE8
callq 0x23900
movl %eax, -0x44(%rbp)
jmp 0x22e25
jmp 0x22c72
movl -0xf4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf4(%rbp)
jmp 0x22bd3
jmp 0x22c88
jmp 0x22c8a
jmp 0x22c8c
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rdi
movslq -0x4c(%rbp), %rax
shlq $0x3, %rax
movl %eax, %esi
callq 0xb7b0
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x22cba
movl $0xc0a, -0x44(%rbp)     # imm = 0xC0A
jmp 0x22e25
movq -0x58(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x40(%rbp), %rax
movzwl 0x20(%rax), %eax
cmpl -0x4c(%rbp), %eax
jge 0x22e21
movl -0x18(%rbp), %eax
imull -0x48(%rbp), %eax
movl %eax, -0x104(%rbp)
movq -0x40(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jl 0x22d7a
movq 0x9ce3f(%rip), %rax     # 0xbfb38
movslq -0x104(%rbp), %rsi
movq -0x40(%rbp), %rcx
movzbl 0x22(%rcx), %edi
movl $0x3, %edx
movl $0x1, %ecx
cmpl $0x0, %edi
cmovnel %ecx, %edx
movq -0x40(%rbp), %rcx
movl 0x18(%rcx), %r8d
movslq -0x18(%rbp), %r9
movq -0x40(%rbp), %rcx
movzwl 0x20(%rcx), %ecx
imulq %rcx, %r9
xorl %ecx, %ecx
movl %ecx, %edi
movl $0x1, %ecx
callq *%rax
movq %rax, -0x110(%rbp)
movq $-0x1, %rax
cmpq %rax, -0x110(%rbp)
jne 0x22d78
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rdx
movl $0x150a, %edi           # imm = 0x150A
leaq 0x82edd(%rip), %rsi     # 0xa5c43
movl $0x7b03, %ecx           # imm = 0x7B03
callq 0x23900
movl %eax, -0x44(%rbp)
jmp 0x22e25
jmp 0x22db2
movslq -0x18(%rbp), %rdi
callq 0xb270
movq %rax, -0x110(%rbp)
cmpq $0x0, -0x110(%rbp)
jne 0x22da0
movl $0x7, -0x44(%rbp)
jmp 0x22e25
movq -0x110(%rbp), %rdi
movslq -0x18(%rbp), %rdx
xorl %esi, %esi
callq 0xb2b0
movl $0x0, -0x108(%rbp)
movl -0x108(%rbp), %eax
cmpl -0x48(%rbp), %eax
jge 0x22e0b
movq -0x110(%rbp), %rdx
movl -0x18(%rbp), %eax
imull -0x108(%rbp), %eax
cltq
addq %rax, %rdx
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x40(%rbp), %rcx
movzwl 0x20(%rcx), %ecx
addl -0x108(%rbp), %ecx
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x108(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x108(%rbp)
jmp 0x22dbc
movl -0x48(%rbp), %edx
movq -0x40(%rbp), %rax
movzwl 0x20(%rax), %ecx
addl %edx, %ecx
movw %cx, 0x20(%rax)
jmp 0x22cc6
jmp 0x22e23
jmp 0x22e25
movq -0x40(%rbp), %rax
movzwl 0x20(%rax), %eax
cmpl -0x14(%rbp), %eax
jle 0x22e4b
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
jmp 0x22e56
movq -0x28(%rbp), %rax
movq $0x0, (%rax)
movq -0x40(%rbp), %rax
movzbl 0x22(%rax), %eax
cmpl $0x0, %eax
je 0x22e70
cmpl $0x0, -0x44(%rbp)
jne 0x22e70
movl $0x8, -0x44(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0xb4f0
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nop