// File: /workspace/github_c_makefile_star2_O0/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
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 0x3d8f80
movl %eax, -0x48(%rbp)
movq -0x30(%rbp), %rax
cmpq $0x0, 0x40(%rax)
jne 0x3d72bd
movq -0x30(%rbp), %rdi
callq 0x3d8fc0
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x3d72bb
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3d764c
jmp 0x3d72bd
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
cmpb $0x0, 0x23(%rax)
je 0x3d7301
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x3d92b0
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x3d72f9
jmp 0x3d75fb
movq -0x40(%rbp), %rax
movb $0x0, 0x23(%rax)
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 0x3d75f9
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 0x3d745c
movq 0x2689f4(%rip), %rax    # 0x63fd40
movq -0x40(%rbp), %rcx
movl 0x18(%rcx), %edi
leaq -0xf0(%rbp), %rsi
callq *%rax
cmpl $0x0, %eax
je 0x3d736d
movl $0x130a, -0x44(%rbp)    # imm = 0x130A
jmp 0x3d75fb
movq -0xc0(%rbp), %rax
movslq -0x5c(%rbp), %rcx
cmpq %rcx, %rax
jge 0x3d745a
cmpl $0x0, -0x1c(%rbp)
jne 0x3d738c
jmp 0x3d75fb
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 0x3d7456
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 0x1c6232(%rip), %rdx    # 0x59d628
movl $0x1, %ecx
leaq -0xf8(%rbp), %r8
callq 0x3d8420
cmpl $0x1, %eax
je 0x3d7440
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rdx
movl $0x130a, %edi           # imm = 0x130A
leaq 0x1dc7a2(%rip), %rsi    # 0x5b3bd0
movl $0x9590, %ecx           # imm = 0x9590
callq 0x3d8070
movl %eax, -0x44(%rbp)
jmp 0x3d75fb
jmp 0x3d7442
movl -0xf4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf4(%rbp)
jmp 0x3d73a3
jmp 0x3d7458
jmp 0x3d745a
jmp 0x3d745c
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rdi
movslq -0x4c(%rbp), %rax
shlq $0x3, %rax
movl %eax, %esi
callq 0x3c08c0
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x3d748a
movl $0xc0a, -0x44(%rbp)     # imm = 0xC0A
jmp 0x3d75fb
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 0x3d75f7
movl -0x18(%rbp), %eax
imull -0x48(%rbp), %eax
movl %eax, -0x104(%rbp)
movq -0x40(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jl 0x3d754a
movq 0x268a0f(%rip), %rax    # 0x63fed8
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 0x3d7548
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rdx
movl $0x150a, %edi           # imm = 0x150A
leaq 0x16f088(%rip), %rsi    # 0x5465be
movl $0x95ab, %ecx           # imm = 0x95AB
callq 0x3d8070
movl %eax, -0x44(%rbp)
jmp 0x3d75fb
jmp 0x3d7588
movslq -0x104(%rbp), %rdi
callq 0x3c07c0
movq %rax, -0x110(%rbp)
cmpq $0x0, -0x110(%rbp)
jne 0x3d7573
movl $0x7, -0x44(%rbp)
jmp 0x3d75fb
movq -0x110(%rbp), %rdi
movslq -0x104(%rbp), %rdx
xorl %esi, %esi
callq 0x19360
movl $0x0, -0x108(%rbp)
movl -0x108(%rbp), %eax
cmpl -0x48(%rbp), %eax
jge 0x3d75e1
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 0x3d7592
movl -0x48(%rbp), %edx
movq -0x40(%rbp), %rax
movzwl 0x20(%rax), %ecx
addl %edx, %ecx
movw %cx, 0x20(%rax)
jmp 0x3d7496
jmp 0x3d75f9
jmp 0x3d75fb
movq -0x40(%rbp), %rax
movzwl 0x20(%rax), %eax
cmpl -0x14(%rbp), %eax
jle 0x3d7621
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 0x3d762c
movq -0x28(%rbp), %rax
movq $0x0, (%rax)
movq -0x40(%rbp), %rax
movzbl 0x22(%rax), %eax
cmpl $0x0, %eax
je 0x3d7646
cmpl $0x0, -0x44(%rbp)
jne 0x3d7646
movl $0x8, -0x44(%rbp)
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopl (%rax,%rax)