// File: /workspace/github_c_makefile_star2_O3/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 $0x108, %rsp            # imm = 0x108
movq %r8, %rbx
movl %ecx, %r12d
movl %edx, -0x48(%rbp)
movq %rsi, -0x60(%rbp)
movq %rdi, %r13
callq *0x74bda(%rip)          # 0x919f0
movl %eax, %ecx
shrl $0xf, %ecx
cmpl $0x8000, %eax           # imm = 0x8000
movl $0x1, %r15d
cmovgel %ecx, %r15d
cmpq $0x0, 0x40(%r13)
movq %r15, -0x58(%rbp)
je 0x1cf6e
movq 0x40(%r13), %rax
movq (%rax), %r14
movq 0x8(%r14), %rdi
testq %rdi, %rdi
je 0x1ce4f
callq *0x74569(%rip)          # 0x913b8
movq -0x60(%rbp), %rcx
leal (%r15,%rcx), %r13d
movl %r13d, %eax
cltd
idivl %r15d
subl %edx, %r13d
movzwl 0x20(%r14), %eax
xorl %r15d, %r15d
cmpl %eax, %r13d
jle 0x1cf56
movq %rbx, -0x50(%rbp)
movl -0x48(%rbp), %eax
movl %eax, 0x1c(%r14)
movl 0x18(%r14), %edi
testl %edi, %edi
js 0x1cf32
leaq -0x120(%rbp), %rsi
callq *0x7497a(%rip)          # 0x91810
movl $0x130a, %r15d          # imm = 0x130A
testl %eax, %eax
jne 0x1cf49
movl %r13d, %eax
imull -0x48(%rbp), %eax
movq -0xf0(%rbp), %rdx
movslq %eax, %rcx
cmpq %rcx, %rdx
jge 0x1cf32
testl %r12d, %r12d
je 0x1d22f
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 0x1cf32
movslq %ecx, %r12
movslq %edx, %r15
subq %r12, %r15
shlq $0xc, %r12
orq $0xfff, %r12            # imm = 0xFFF
leaq -0x90(%rbp), %rbx
movl 0x18(%r14), %edi
movq %r12, %rsi
leaq 0x61191(%rip), %rdx     # 0x7e0a1
movl $0x1, %ecx
movq %rbx, %r8
callq 0x1dd45
cmpl $0x1, %eax
jne 0x1d237
addq $0x1000, %r12           # imm = 0x1000
decq %r15
jne 0x1cf02
movq 0x28(%r14), %rbx
callq 0xa080
movl $0xc0a, %r15d           # imm = 0xC0A
testl %eax, %eax
je 0x1d09a
movzwl 0x20(%r14), %eax
movq -0x50(%rbp), %rbx
movq -0x60(%rbp), %rcx
cmpl %ecx, %eax
jle 0x1d05d
movq 0x28(%r14), %rax
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
jmp 0x1d05f
movq %rbx, %r15
callq 0xa080
movl $0x7, %ebx
testl %eax, %eax
jne 0x1d086
movl $0x18, %edi
callq 0xc0e2
testq %rax, %rax
je 0x1d086
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, -0x50(%rbp)
movq $0x0, 0x10(%rax)
cmpl $0x0, 0x74385(%rip)     # 0x91334
je 0x1cfca
movl $0xb, %edi
callq *0x743ec(%rip)          # 0x913a8
testq %rax, %rax
je 0x1cfca
movq %rax, %rdi
callq *0x743ee(%rip)          # 0x913b8
movq 0x10(%r13), %r14
movq 0x20(%r14), %rax
testq %rax, %rax
movq %r15, %rbx
je 0x1d1a3
movq -0x50(%rbp), %r14
movq %rax, (%r14)
movq %rax, %r15
incl 0x30(%rax)
movq %r14, 0x40(%r13)
cmpl $0x0, 0x7433e(%rip)     # 0x91334
je 0x1d011
movl $0xb, %edi
callq *0x743a5(%rip)          # 0x913a8
testq %rax, %rax
je 0x1d011
movq %rax, %rdi
callq *0x743b7(%rip)          # 0x913c8
movq %r15, %rcx
movq 0x8(%r15), %rdi
testq %rdi, %rdi
movq -0x58(%rbp), %r15
je 0x1d21e
movl %r12d, -0x40(%rbp)
movq %rcx, %r12
callq *0x74386(%rip)          # 0x913b8
movq 0x8(%r12), %rdi
movq 0x38(%r12), %rax
movq %rax, 0x8(%r14)
movq %r14, 0x38(%r12)
movl -0x40(%rbp), %r12d
testq %rdi, %rdi
je 0x1ce39
callq *0x74370(%rip)          # 0x913c8
jmp 0x1ce39
xorl %eax, %eax
movq %rax, (%rbx)
cmpb $0x0, 0x22(%r14)
movl $0x8, %ebx
cmovel %r15d, %ebx
testl %r15d, %r15d
cmovnel %r15d, %ebx
movq 0x8(%r14), %rdi
testq %rdi, %rdi
je 0x1d086
callq *0x74342(%rip)          # 0x913c8
movl %ebx, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leal (,%r13,8), %esi
xorl %eax, %eax
testl %esi, %esi
cmovlel %eax, %esi
movq %rbx, %rdi
callq 0xc2eb
testq %rax, %rax
je 0x1cf49
movq %rax, 0x28(%r14)
movzwl 0x20(%r14), %eax
movl $0x0, %r15d
cmpl %eax, %r13d
jle 0x1cf49
movl -0x48(%rbp), %edx
movslq %edx, %rbx
movq -0x58(%rbp), %rcx
imull %ecx, %edx
movslq %edx, %rdx
movq %rdx, -0x48(%rbp)
movl %ecx, %ecx
movq %rcx, -0x40(%rbp)
movl 0x18(%r14), %r8d
testl %r8d, %r8d
js 0x1d12d
xorl %ecx, %ecx
cmpb $0x0, 0x22(%r14)
sete %cl
leal 0x1(,%rcx,2), %edx
movzwl %ax, %r9d
imulq %rbx, %r9
xorl %edi, %edi
movq -0x48(%rbp), %rsi
movl $0x1, %ecx
callq *0x74889(%rip)          # 0x919a8
movq %rax, %r12
cmpq $-0x1, %rax
jne 0x1d161
jmp 0x1d274
callq 0xa080
movl $0x7, %r15d
testl %eax, %eax
jne 0x1cf49
movq %rbx, %rdi
callq 0xc0e2
testq %rax, %rax
je 0x1cf49
movq %rax, %r12
movq %rax, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0xa220
movzwl 0x20(%r14), %eax
movq -0x58(%rbp), %rdi
testl %edi, %edi
jle 0x1d188
movq -0x40(%rbp), %rcx
movq %rax, %rdx
movq 0x28(%r14), %rsi
movq %r12, (%rsi,%rdx,8)
incq %rdx
addq %rbx, %r12
decq %rcx
jne 0x1d175
addl %edi, %eax
movw %ax, 0x20(%r14)
movzwl %ax, %ecx
xorl %r15d, %r15d
cmpl %ecx, %r13d
jg 0x1d0ec
jmp 0x1cf49
movq 0x38(%r13), %rax
movq %rax, -0x40(%rbp)
movl 0x18(%r13), %edi
leaq -0x120(%rbp), %rsi
callq *0x74654(%rip)          # 0x91810
movl $0x70a, %ebx            # imm = 0x70A
testl %eax, %eax
jne 0x1d1e4
movq -0x40(%rbp), %rdi
callq 0xa190
movq %rax, -0x30(%rbp)
callq 0xa080
movl $0x7, %ebx
testl %eax, %eax
je 0x1d2be
movq %r13, %rdi
callq 0x1e118
movq -0x50(%rbp), %rdi
callq 0xa630
cmpl $0x0, 0x74138(%rip)     # 0x91334
je 0x1d086
movl $0xb, %edi
callq *0x7419b(%rip)          # 0x913a8
movq %rax, %rdi
testq %rax, %rax
jne 0x1d080
jmp 0x1d086
movq 0x38(%rcx), %rax
movq %rax, 0x8(%r14)
movq %r14, 0x38(%rcx)
jmp 0x1ce39
xorl %r15d, %r15d
jmp 0x1cf49
movq 0x10(%r14), %rbx
callq 0xa0b0
movl (%rax), %ecx
testq %rbx, %rbx
leaq 0x60e55(%rip), %rax     # 0x7e0a1
cmoveq %rax, %rbx
movq %rax, (%rsp)
leaq 0x5e11c(%rip), %rsi     # 0x7b377
leaq 0x5e068(%rip), %r8      # 0x7b2ca
movl $0x130a, %r15d          # imm = 0x130A
movl $0x130a, %edi           # imm = 0x130A
movl $0x7ae8, %edx           # imm = 0x7AE8
jmp 0x1d2af
movq 0x10(%r14), %rbx
callq 0xa0b0
movl (%rax), %ecx
testq %rbx, %rbx
leaq 0x60e18(%rip), %rax     # 0x7e0a1
cmoveq %rax, %rbx
movq %rax, (%rsp)
leaq 0x5e0df(%rip), %rsi     # 0x7b377
leaq 0x5e07b(%rip), %r8      # 0x7b31a
movl $0x150a, %r15d          # imm = 0x150A
movl $0x150a, %edi           # imm = 0x150A
movl $0x7b03, %edx           # imm = 0x7B03
movq %rbx, %r9
xorl %eax, %eax
callq 0xa4c0
jmp 0x1cf49
movslq -0x30(%rbp), %rdi
addq $0x46, %rdi
movq %rdi, -0x68(%rbp)
callq 0xc0e2
testq %rax, %rax
je 0x1d1e4
movq -0x30(%rbp), %rcx
addl $0x6, %ecx
movq %rcx, -0x30(%rbp)
movq %rax, %rdi
xorl %esi, %esi
movq -0x68(%rbp), %rdx
movq %rax, -0x38(%rbp)
callq 0xa220
movq -0x38(%rbp), %rax
movq %rax, %rsi
addq $0x40, %rsi
movq %rsi, 0x10(%rax)
leaq 0x5e0ee(%rip), %rdx     # 0x7b3f9
movq -0x30(%rbp), %rdi
movq %rsi, -0x30(%rbp)
movq -0x40(%rbp), %rcx
xorl %eax, %eax
callq 0xa350
movq -0x38(%rbp), %rcx
movl $0xffffffff, 0x18(%rcx) # imm = 0xFFFFFFFF
movq 0x10(%r13), %rax
movq %rcx, 0x20(%rax)
movq %rax, (%rcx)
callq 0xa080
testl %eax, %eax
je 0x1d34e
movq -0x38(%rbp), %rax
movq $0x0, 0x8(%rax)
jmp 0x1d1e4
xorl %edi, %edi
callq *0x74052(%rip)          # 0x913a8
movq -0x38(%rbp), %rcx
movq %rax, 0x8(%rcx)
testq %rax, %rax
je 0x1d1e4
movq %rcx, %rax
cmpb $0x0, 0x15(%r14)
je 0x1d379
movq %r15, %rbx
jmp 0x1cfde
movq 0x38(%r13), %rdi
leaq 0x5e07c(%rip), %rsi     # 0x7b400
xorl %ebx, %ebx
xorl %edx, %edx
callq 0xa0f0
testl %eax, %eax
je 0x1d39c
movq -0x38(%rbp), %r14
movb $0x1, 0x22(%r14)
jmp 0x1d3a5
movl $0x42, %ebx
movq -0x38(%rbp), %r14
movq -0x30(%rbp), %rdi
movl $0x1ff, %edx            # imm = 0x1FF
andl -0x108(%rbp), %edx
movl %ebx, %esi
callq 0x1db3d
movl %eax, 0x18(%r14)
testl %eax, %eax
js 0x1d45d
movl %eax, %ebx
movl -0x104(%rbp), %r14d
movl -0x100(%rbp), %eax
movl %eax, -0x40(%rbp)
callq *0x745b1(%rip)          # 0x91990
testl %eax, %eax
jne 0x1d3f1
movl %ebx, %edi
movl %r14d, %esi
movl -0x40(%rbp), %edx
callq *0x74587(%rip)          # 0x91978
movq 0x10(%r13), %rax
movq 0x20(%rax), %rax
movl 0x18(%rax), %edi
testl %edi, %edi
js 0x1d43c
xorps %xmm0, %xmm0
leaq -0x90(%rbp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movw $0x1, (%rdx)
movq $0x80, 0x8(%rdx)
movq $0x1, 0x10(%rdx)
movl $0x6, %esi
xorl %eax, %eax
callq *0x7440d(%rip)          # 0x91840
cmpl $-0x1, %eax
je 0x1d4be
movq -0x38(%rbp), %rax
movl 0x18(%rax), %ebx
movl %ebx, %edi
xorl %esi, %esi
callq *0x743db(%rip)          # 0x91828
testl %eax, %eax
jns 0x1d498
callq 0xa0b0
cmpl $0x4, (%rax)
je 0x1d443
jmp 0x1d49a
leaq 0x5df32(%rip), %rsi     # 0x7b396
leaq 0x5dd22(%rip), %rcx     # 0x7b18d
movl $0xe, %ebx
movl $0xe, %edi
movl $0x7a54, %edx           # imm = 0x7A54
xorl %eax, %eax
callq 0xa4c0
leaq 0x5de04(%rip), %rsi     # 0x7b28c
movl $0xe, %edi
movq -0x30(%rbp), %rdx
movl $0x7a54, %ecx           # imm = 0x7A54
jmp 0x1d4b4
je 0x1d4be
leaq 0x5de0a(%rip), %rsi     # 0x7b2ab
movl $0x120a, %ebx           # imm = 0x120A
movl $0x120a, %edi           # imm = 0x120A
movq -0x30(%rbp), %rdx
movl $0x7a64, %ecx           # imm = 0x7A64
callq 0x1dc2e
jmp 0x1d1e4
movq 0x10(%r13), %rax
movq 0x20(%rax), %rax
movl 0x18(%rax), %edi
testl %edi, %edi
js 0x1d500
xorps %xmm0, %xmm0
leaq -0x90(%rbp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movq $0x80, 0x8(%rdx)
movq $0x1, 0x10(%rdx)
movl $0x6, %esi
xorl %eax, %eax
callq *0x74345(%rip)          # 0x91840
cmpl $-0x1, %eax
je 0x1d509
movq -0x38(%rbp), %rax
jmp 0x1d371
movl $0x5, %ebx
jmp 0x1d1e4