// File: /workspace/github_c_makefile_star2_O0/barabo[P]advanced-shell-history/src/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;

  /* 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 );

  if( pShmNode->nRegion<=iRegion ){
    char **apNew;                      /* New apRegion[] array */
    int nByte = (iRegion+1)*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++){
            if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, 0)!=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, (iRegion+1)*sizeof(char *)
    );
    if( !apNew ){
      rc = SQLITE_IOERR_NOMEM;
      goto shmpage_out;
    }
    pShmNode->apRegion = apNew;
    while(pShmNode->nRegion<=iRegion){
      void *pMem;
      if( pShmNode->h>=0 ){
        pMem = osMmap(0, szRegion,
            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_malloc(szRegion);
        if( pMem==0 ){
          rc = SQLITE_NOMEM;
          goto shmpage_out;
        }
        memset(pMem, 0, szRegion);
      }
      pShmNode->apRegion[pShmNode->nRegion] = pMem;
      pShmNode->nRegion++;
    }
  }

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 $0x110, %rsp            # imm = 0x110
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)
movq -0x30(%rbp), %rax
cmpq $0x0, 0x40(%rax)
jne 0x1cc35
movq -0x30(%rbp), %rdi
callq 0x1e860
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x1cc33
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x1cf25
jmp 0x1cc35
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
movzwl 0x20(%rax), %eax
cmpl -0x14(%rbp), %eax
jg 0x1ced2
movl -0x14(%rbp), %eax
addl $0x1, %eax
imull -0x18(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x18(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x40(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jl 0x1cd8d
movq 0xb3cd7(%rip), %rax     # 0xd0960
movq -0x40(%rbp), %rcx
movl 0x18(%rcx), %edi
leaq -0xe8(%rbp), %rsi
callq *%rax
cmpl $0x0, %eax
je 0x1ccaa
movl $0x130a, -0x44(%rbp)    # imm = 0x130A
jmp 0x1ced4
movq -0xb8(%rbp), %rax
movslq -0x54(%rbp), %rcx
cmpq %rcx, %rax
jge 0x1cd8b
cmpl $0x0, -0x1c(%rbp)
jne 0x1ccc9
jmp 0x1ced4
movq -0xb8(%rbp), %rax
movl $0x1000, %ecx           # imm = 0x1000
cqto
idivq %rcx
movl %eax, -0xec(%rbp)
movl -0xec(%rbp), %eax
movl %eax, -0x104(%rbp)
movl -0x54(%rbp), %eax
movl $0x1000, %ecx           # imm = 0x1000
cltd
idivl %ecx
movl %eax, %ecx
movl -0x104(%rbp), %eax
cmpl %ecx, %eax
jge 0x1cd87
movq -0x40(%rbp), %rax
movl 0x18(%rax), %edi
movl -0xec(%rbp), %eax
shll $0xc, %eax
addl $0x1000, %eax           # imm = 0x1000
subl $0x1, %eax
movslq %eax, %rsi
leaq 0x960cc(%rip), %rdx     # 0xb2df5
movl $0x1, %ecx
xorl %eax, %eax
movl %eax, %r8d
callq 0x1dc80
cmpl $0x1, %eax
je 0x1cd71
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rdx
movl $0x130a, %edi           # imm = 0x130A
leaq 0x913fa(%rip), %rsi     # 0xae159
movl $0x6b16, %ecx           # imm = 0x6B16
callq 0x1d930
movl %eax, -0x44(%rbp)
jmp 0x1ced4
jmp 0x1cd73
movl -0xec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xec(%rbp)
jmp 0x1cce0
jmp 0x1cd89
jmp 0x1cd8b
jmp 0x1cd8d
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rdi
movl -0x14(%rbp), %eax
addl $0x1, %eax
cltq
shlq $0x3, %rax
movl %eax, %esi
callq 0xa500
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x1cdbf
movl $0xc0a, -0x44(%rbp)     # imm = 0xC0A
jmp 0x1ced4
movq -0x50(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x40(%rbp), %rax
movzwl 0x20(%rax), %eax
cmpl -0x14(%rbp), %eax
jg 0x1ced0
movq -0x40(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jl 0x1ce6c
movq 0xb3cef(%rip), %rax     # 0xd0ae0
movslq -0x18(%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, -0x100(%rbp)
movq $-0x1, %rax
cmpq %rax, -0x100(%rbp)
jne 0x1ce6a
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rdx
movl $0x150a, %edi           # imm = 0x150A
leaq 0x91346(%rip), %rsi     # 0xae1a1
movl $0x6b2f, %ecx           # imm = 0x6B2F
callq 0x1d930
movl %eax, -0x44(%rbp)
jmp 0x1ced4
jmp 0x1cea0
movl -0x18(%rbp), %edi
callq 0xa380
movq %rax, -0x100(%rbp)
cmpq $0x0, -0x100(%rbp)
jne 0x1ce8e
movl $0x7, -0x44(%rbp)
jmp 0x1ced4
movq -0x100(%rbp), %rdi
movslq -0x18(%rbp), %rdx
xorl %esi, %esi
callq 0x8200
movq -0x100(%rbp), %rdx
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x40(%rbp), %rcx
movzwl 0x20(%rcx), %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x40(%rbp), %rax
movw 0x20(%rax), %cx
addw $0x1, %cx
movw %cx, 0x20(%rax)
jmp 0x1cdcb
jmp 0x1ced2
jmp 0x1ced4
movq -0x40(%rbp), %rax
movzwl 0x20(%rax), %eax
cmpl -0x14(%rbp), %eax
jle 0x1cefa
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 0x1cf05
movq -0x28(%rbp), %rax
movq $0x0, (%rax)
movq -0x40(%rbp), %rax
movzbl 0x22(%rax), %eax
cmpl $0x0, %eax
je 0x1cf1f
cmpl $0x0, -0x44(%rbp)
jne 0x1cf1f
movl $0x8, -0x44(%rbp)
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopw %cs:(%rax,%rax)