// File: /workspace/github_c_makefile_star2_O1/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 $0xe8, %rsp
movq %r8, -0x70(%rbp)
movl %ecx, -0x48(%rbp)
movl %edx, %r12d
movl %esi, %ebx
movq %rdi, %r13
callq *0x76b9d(%rip)          # 0x939e0
movl %eax, %ecx
shrl $0xf, %ecx
cmpl $0x8000, %eax           # imm = 0x8000
movl $0x1, %eax
cmovgel %ecx, %eax
movq %rax, -0x58(%rbp)
cmpq $0x0, 0x40(%r13)
movq %rbx, -0x50(%rbp)
je 0x1cecf
movq 0x40(%r13), %rax
movq (%rax), %r14
movq 0x8(%r14), %rdi
testq %rdi, %rdi
je 0x1ce7a
callq *0x7652e(%rip)          # 0x933a8
movq -0x58(%rbp), %rcx
addl %ecx, %ebx
movl %ebx, %eax
cltd
idivl %ecx
subl %edx, %ebx
movzwl 0x20(%r14), %eax
xorl %edx, %edx
cmpl %eax, %ebx
jle 0x1d2fc
movl %r12d, 0x1c(%r14)
movl 0x18(%r14), %edi
movl $0x0, -0x30(%rbp)
testl %edi, %edi
js 0x1d18a
leaq -0x100(%rbp), %rsi
callq *0x76946(%rip)          # 0x93800
movl $0x2, %ecx
testl %eax, %eax
je 0x1cf28
movl $0x130a, -0x30(%rbp)    # imm = 0x130A
jmp 0x1d2f4
callq 0xa080
testl %eax, %eax
je 0x1cedc
xorl %ebx, %ebx
jmp 0x1cee9
movl $0x18, %edi
callq 0xbf9b
movq %rax, %rbx
testq %rbx, %rbx
je 0x1cf19
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
cmpl $0x0, 0x76421(%rip)     # 0x93324
je 0x1d034
movl $0xb, %edi
callq *0x76484(%rip)          # 0x93398
jmp 0x1d036
movl $0x7, %r15d
movq -0x50(%rbp), %rbx
jmp 0x1d0ee
movl %ebx, %eax
imull %r12d, %eax
movq -0xd0(%rbp), %rdx
movslq %eax, %rsi
cmpq %rsi, %rdx
jge 0x1d18a
cmpl $0x0, -0x48(%rbp)
je 0x1d155
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 0x1d179
movslq %ecx, %r15
movslq %edx, %r13
subq %r15, %r13
shlq $0xc, %r15
orq $0xfff, %r15            # imm = 0xFFF
movl $0x0, -0x30(%rbp)
movl %r12d, -0x38(%rbp)
movl $0x0, -0x5c(%rbp)
movl 0x18(%r14), %edi
movq %r15, %rsi
leaq 0x630ea(%rip), %rdx     # 0x80091
movl $0x1, %ecx
leaq -0x5c(%rbp), %r8
callq 0x1dd7a
movl $0x0, %ecx
cmpl $0x1, %eax
je 0x1d014
movq 0x10(%r14), %r12
movl %eax, -0x48(%rbp)
callq 0xa0b0
movl (%rax), %ecx
testq %r12, %r12
leaq 0x630ba(%rip), %rax     # 0x80091
cmoveq %rax, %r12
movq %rax, (%rsp)
movl $0x130a, -0x30(%rbp)    # imm = 0x130A
movl $0x130a, %edi           # imm = 0x130A
leaq 0x60375(%rip), %rsi     # 0x7d367
movl $0x7ae8, %edx           # imm = 0x7AE8
leaq 0x602bc(%rip), %r8      # 0x7d2ba
movq %r12, %r9
movl -0x38(%rbp), %r12d
xorl %eax, %eax
callq 0xa4b0
movl -0x48(%rbp), %eax
movl $0x2, %ecx
cmpl $0x1, %eax
jne 0x1d182
addq $0x1000, %r15           # imm = 0x1000
decq %r13
jne 0x1cf92
xorl %ecx, %ecx
jmp 0x1d182
xorl %eax, %eax
movl %r12d, -0x38(%rbp)
testq %rax, %rax
je 0x1d048
movq %rax, %rdi
callq *0x76360(%rip)          # 0x933a8
movq 0x10(%r13), %r14
movq 0x20(%r14), %r12
testq %r12, %r12
jne 0x1d083
movq 0x38(%r13), %r15
movl 0x18(%r13), %edi
leaq -0x100(%rbp), %rsi
callq *0x76796(%rip)          # 0x93800
testl %eax, %eax
je 0x1d125
movl $0x70a, %r15d           # imm = 0x70A
xorl %r12d, %r12d
xorl %r14d, %r14d
testb %r14b, %r14b
je 0x1d0fc
movq %r12, (%rbx)
incl 0x30(%r12)
movq %rbx, 0x40(%r13)
cmpl $0x0, 0x7628e(%rip)     # 0x93324
je 0x1d0a5
movl $0xb, %edi
callq *0x762f5(%rip)          # 0x93398
jmp 0x1d0a7
xorl %eax, %eax
testq %rax, %rax
je 0x1d0b5
movq %rax, %rdi
callq *0x76303(%rip)          # 0x933b8
movq 0x8(%r12), %rdi
testq %rdi, %rdi
je 0x1d0c5
callq *0x762e3(%rip)          # 0x933a8
movq 0x38(%r12), %rax
movq %rax, 0x8(%rbx)
movq %rbx, 0x38(%r12)
movq 0x8(%r12), %rdi
xorl %r15d, %r15d
testq %rdi, %rdi
movq -0x50(%rbp), %rbx
movl -0x38(%rbp), %r12d
je 0x1d0ee
callq *0x762ca(%rip)          # 0x933b8
testl %r15d, %r15d
jne 0x1d345
jmp 0x1ce64
movq %r13, %rdi
callq 0x1e1ba
movq %rbx, %rdi
callq 0xa620
cmpl $0x0, 0x76211(%rip)     # 0x93324
je 0x1d161
movl $0xb, %edi
callq *0x76278(%rip)          # 0x93398
movq %rax, %rdi
jmp 0x1d163
movq %r15, %rdi
callq 0xa190
movq %rax, -0x30(%rbp)
movslq %eax, %r12
addq $0x46, %r12
callq 0xa080
testl %eax, %eax
movq %r15, -0x68(%rbp)
movq %r12, -0x40(%rbp)
je 0x1d35a
xorl %r12d, %r12d
jmp 0x1d365
movl $0x0, -0x30(%rbp)
jmp 0x1d2f4
xorl %edi, %edi
movq -0x50(%rbp), %rbx
movl -0x38(%rbp), %r12d
testq %rdi, %rdi
jne 0x1d0e8
jmp 0x1d0ee
xorl %ecx, %ecx
movl $0x0, -0x30(%rbp)
testl %ecx, %ecx
jne 0x1d2f4
movq 0x28(%r14), %r15
callq 0xa080
movl %eax, %ecx
xorl %eax, %eax
testl %ecx, %ecx
jne 0x1d1b2
leal (,%rbx,8), %ecx
testl %ecx, %ecx
cmovgl %ecx, %eax
movq %r15, %rdi
movq %rax, %rsi
callq 0xc1a4
testq %rax, %rax
je 0x1d2e4
movq %rax, 0x28(%r14)
movslq %r12d, %r15
movq -0x58(%rbp), %rax
imull %eax, %r12d
movslq %r12d, %rcx
movq %rcx, -0x48(%rbp)
movl %eax, %eax
movq %rax, -0x38(%rbp)
movzwl 0x20(%r14), %r9d
cmpl %r9d, %ebx
jle 0x1d2f2
movl 0x18(%r14), %r8d
testl %r8d, %r8d
js 0x1d26d
xorl %eax, %eax
cmpb $0x0, 0x22(%r14)
sete %al
leal 0x1(,%rax,2), %edx
imulq %r15, %r9
xorl %edi, %edi
movq -0x48(%rbp), %rsi
movl $0x1, %ecx
callq *0x76784(%rip)          # 0x93998
movq %rax, %r13
cmpq $-0x1, %rax
jne 0x1d298
movq 0x10(%r14), %r13
callq 0xa0b0
movl (%rax), %ecx
testq %r13, %r13
leaq 0x62e5f(%rip), %rax     # 0x80091
cmoveq %rax, %r13
movq %rax, (%rsp)
xorl %r12d, %r12d
movl $0x150a, -0x30(%rbp)    # imm = 0x150A
movl $0x150a, %edi           # imm = 0x150A
leaq 0x60117(%rip), %rsi     # 0x7d367
movl $0x7b03, %edx           # imm = 0x7B03
leaq 0x600ae(%rip), %r8      # 0x7d30a
movq %r13, %r9
xorl %eax, %eax
callq 0xa4b0
movl $0x2, %ecx
jmp 0x1d2c8
callq 0xa080
testl %eax, %eax
je 0x1d27b
xorl %r13d, %r13d
jmp 0x1d286
movq %r15, %rdi
callq 0xbf9b
movq %rax, %r13
testq %r13, %r13
je 0x1d2d3
movq %r13, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0xa210
cmpl $0x0, -0x58(%rbp)
jle 0x1d2ba
movzwl 0x20(%r14), %eax
movq -0x38(%rbp), %rcx
movq 0x28(%r14), %rdx
movq %r13, (%rdx,%rax,8)
incq %rax
addq %r15, %r13
decq %rcx
jne 0x1d2a7
movq -0x58(%rbp), %rax
addw %ax, 0x20(%r14)
xorl %ecx, %ecx
movb $0x1, %r12b
testb %r12b, %r12b
jne 0x1d1d7
jmp 0x1d2f4
movl $0x7, -0x30(%rbp)
movl $0x2, %ecx
xorl %r12d, %r12d
jmp 0x1d2c8
movl $0xc0a, -0x30(%rbp)     # imm = 0xC0A
movl $0x2, %ecx
jmp 0x1d2f4
xorl %ecx, %ecx
testb $0x1, %cl
movl -0x30(%rbp), %edx
jne 0x1d345
movzwl 0x20(%r14), %eax
movq -0x50(%rbp), %rcx
cmpl %ecx, %eax
jle 0x1d316
movq 0x28(%r14), %rax
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
jmp 0x1d318
xorl %eax, %eax
movq -0x70(%rbp), %rcx
movq %rax, (%rcx)
testl %edx, %edx
movl $0x8, %r15d
cmovnel %edx, %r15d
cmpb $0x0, 0x22(%r14)
cmovel %edx, %r15d
movq 0x8(%r14), %rdi
testq %rdi, %rdi
je 0x1d345
callq *0x76075(%rip)          # 0x933b8
jmp 0x1d345
movl %r15d, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0xbf9b
movq %rax, %r12
movl $0x7, %r15d
testq %r12, %r12
je 0x1d078
movq -0x30(%rbp), %rax
addl $0x6, %eax
movq %rax, -0x30(%rbp)
movq %r12, %rdi
xorl %esi, %esi
movq -0x40(%rbp), %rdx
callq 0xa210
leaq 0x40(%r12), %rsi
movq %rsi, 0x10(%r12)
leaq 0x6004b(%rip), %rdx     # 0x7d3e9
movq -0x30(%rbp), %rdi
movq %rsi, -0x40(%rbp)
movq -0x68(%rbp), %rcx
xorl %eax, %eax
callq 0xa340
movl $0xffffffff, 0x18(%r12) # imm = 0xFFFFFFFF
movq 0x10(%r13), %rax
movq %r12, 0x20(%rax)
movq %rax, (%r12)
callq 0xa080
testl %eax, %eax
je 0x1d3d3
xorl %eax, %eax
jmp 0x1d3db
xorl %edi, %edi
callq *0x75fbd(%rip)          # 0x93398
movq %rax, 0x8(%r12)
testq %rax, %rax
je 0x1d07b
cmpb $0x0, 0x15(%r14)
je 0x1d3f8
movb $0x1, %r14b
jmp 0x1d07e
movq 0x38(%r13), %rdi
leaq 0x5ffed(%rip), %rsi     # 0x7d3f0
xorl %r15d, %r15d
xorl %edx, %edx
callq 0xa0f0
testl %eax, %eax
je 0x1d419
movb $0x1, 0x22(%r12)
jmp 0x1d41f
movl $0x42, %r15d
movq -0x40(%rbp), %rdi
movl $0x1ff, %edx            # imm = 0x1FF
andl -0xe8(%rbp), %edx
movl %r15d, %esi
callq 0x1db69
movl %eax, 0x18(%r12)
testl %eax, %eax
js 0x1d491
movl %eax, %r15d
movl -0xe4(%rbp), %r14d
movl -0xe0(%rbp), %eax
movl %eax, -0x30(%rbp)
callq *0x76528(%rip)          # 0x93980
testl %eax, %eax
jne 0x1d46b
movl %r15d, %edi
movl %r14d, %esi
movl -0x30(%rbp), %edx
callq *0x764fd(%rip)          # 0x93968
movq %r13, %rdi
movl $0x1, %esi
movl $0x80, %edx
movl $0x1, %ecx
callq 0x1e158
movb $0x1, %r14b
testl %eax, %eax
je 0x1d4d8
xorl %r15d, %r15d
jmp 0x1d524
leaq 0x5feee(%rip), %rsi     # 0x7d386
leaq 0x5fcde(%rip), %rcx     # 0x7d17d
xorl %r14d, %r14d
movl $0xe, %r15d
movl $0xe, %edi
movl $0x7a54, %edx           # imm = 0x7A54
xorl %eax, %eax
callq 0xa4b0
leaq 0x5fdbc(%rip), %rsi     # 0x7d27c
movl $0xe, %edi
movq -0x40(%rbp), %rdx
movl $0x7a54, %ecx           # imm = 0x7A54
callq 0x1dc63
jmp 0x1d07e
movl 0x18(%r12), %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %edi
xorl %esi, %esi
callq *0x7632d(%rip)          # 0x93818
movl %eax, %r15d
testl %eax, %eax
jns 0x1d4fc
callq 0xa0b0
cmpl $0x4, (%rax)
je 0x1d4e0
testl %r15d, %r15d
je 0x1d489
leaq 0x5fd93(%rip), %rsi     # 0x7d29b
movl $0x120a, %r15d          # imm = 0x120A
movl $0x120a, %edi           # imm = 0x120A
movq -0x40(%rbp), %rdx
movl $0x7a64, %ecx           # imm = 0x7A64
callq 0x1dc63
xorl %r14d, %r14d
testb %r14b, %r14b
je 0x1d540
movq %r13, %rdi
xorl %esi, %esi
movl $0x80, %edx
movl $0x1, %ecx
callq 0x1e158
movl %eax, %r15d
testl %r15d, %r15d
sete %r14b
jmp 0x1d07e