// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r8, %r12
movl %edx, -0x2c(%rbp)
movl %esi, %r14d
movq 0x40(%rdi), %rax
testq %rax, %rax
je 0x1638a
movq (%rax), %r15
jmp 0x163db
movq %rdi, %rbx
movl %ecx, -0x40(%rbp)
pushq $0x18
popq %rdi
callq 0x9c1c
testq %rax, %rax
je 0x1656b
movq %rax, %r13
xorps %xmm0, %xmm0
andq $0x0, 0x10(%rax)
movups %xmm0, (%rax)
movq 0x10(%rbx), %rax
movq 0x20(%rax), %r15
testq %r15, %r15
je 0x16574
movq %r15, (%r13)
incl 0x30(%r15)
movq %r13, 0x40(%rbx)
movq 0x38(%r15), %rax
movq %rax, 0x8(%r13)
movq %r13, 0x38(%r15)
movl -0x40(%rbp), %ecx
movzwl 0x20(%r15), %eax
xorl %r13d, %r13d
cmpl %r14d, %eax
jg 0x16683
movq %r12, -0x48(%rbp)
leal 0x1(%r14), %ebx
movl -0x2c(%rbp), %eax
movl %eax, 0x1c(%r15)
movl 0x18(%r15), %edi
testl %edi, %edi
js 0x164c6
movl %ecx, %r12d
leaq -0xe0(%rbp), %rsi
callq *0x714b9(%rip)          # 0x878d0
movl $0x130a, %r13d          # imm = 0x130A
testl %eax, %eax
jne 0x1667a
movl %ebx, %ecx
imull -0x2c(%rbp), %ecx
movq -0xb0(%rbp), %rax
movslq %ecx, %rdx
cmpq %rdx, %rax
jge 0x164c6
testl %r12d, %r12d
je 0x16677
movl $0x1000, %esi           # imm = 0x1000
cqto
idivq %rsi
movq %rax, %rsi
movl $0x1000, %edi           # imm = 0x1000
movl %ecx, %eax
cltd
idivl %edi
movslq %esi, %r12
cltq
movq %rax, -0x40(%rbp)
movq %r12, %rsi
shlq $0xc, %rsi
orq $0xfff, %rsi            # imm = 0xFFF
decq %r12
incq %r12
cmpq -0x40(%rbp), %r12
jge 0x164c6
leaq 0x1000(%rsi), %r13
movl 0x18(%r15), %edi
leaq 0x59eb2(%rip), %rdx     # 0x70345
pushq $0x1
popq %rcx
xorl %r8d, %r8d
callq 0x16d9b
movq %r13, %rsi
cmpl $0x1, %eax
je 0x16478
movq 0x10(%r15), %rdx
leaq 0x551d1(%rip), %rsi     # 0x6b682
movl $0x130a, %r13d          # imm = 0x130A
movl $0x130a, %edi           # imm = 0x130A
movl $0x6b16, %ecx           # imm = 0x6B16
jmp 0x16654
movq 0x28(%r15), %rdi
shll $0x3, %ebx
movl %ebx, %esi
callq 0x9ce9
testq %rax, %rax
je 0x16631
movq %rax, 0x28(%r15)
movslq -0x2c(%rbp), %rbx
movzwl 0x20(%r15), %eax
xorl %r13d, %r13d
movzwl %ax, %r9d
cmpl %r14d, %r9d
jg 0x1667a
movl 0x18(%r15), %r8d
testl %r8d, %r8d
js 0x16534
xorl %eax, %eax
cmpb $0x0, 0x22(%r15)
sete %al
leal 0x1(,%rax,2), %edx
imulq %rbx, %r9
xorl %edi, %edi
movq %rbx, %rsi
pushq $0x1
popq %rcx
callq *0x7152a(%rip)          # 0x87a50
movq %rax, %r12
cmpq $-0x1, %rax
jne 0x16555
jmp 0x16639
movl -0x2c(%rbp), %edi
callq 0x9c1c
testq %rax, %rax
je 0x16664
movq %rax, %r12
movq %rax, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x81e0
movq 0x28(%r15), %rcx
movzwl 0x20(%r15), %eax
movq %r12, (%rcx,%rax,8)
incl %eax
movw %ax, 0x20(%r15)
jmp 0x164ed
pushq $0x7
popq %r12
jmp 0x166af
movq %rax, -0x50(%rbp)
movl 0x18(%rbx), %edi
leaq -0xe0(%rbp), %rsi
callq *0x71348(%rip)          # 0x878d0
testl %eax, %eax
je 0x1659a
movq -0x50(%rbp), %rax
cmpb $0x0, 0x15(%rax)
je 0x1666c
movq %r12, -0x48(%rbp)
movq 0x38(%rbx), %rdi
callq 0x8170
movq %rax, -0x38(%rbp)
movslq %eax, %r12
addq $0x46, %r12
movl %r12d, %edi
callq 0x9c1c
testq %rax, %rax
je 0x1665b
movq %rax, %r15
movq -0x38(%rbp), %rax
addl $0x6, %eax
movq %rax, -0x38(%rbp)
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x81e0
movq %r15, %r12
addq $0x40, %r12
movq %r12, 0x10(%r15)
movq 0x38(%rbx), %rcx
leaq 0x5515e(%rip), %rdx     # 0x6b752
movq -0x38(%rbp), %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0xaf26
orl $-0x1, 0x18(%r15)
movq 0x10(%rbx), %rax
movq %r15, 0x20(%rax)
movq %rax, (%r15)
movq $0x8, 0x8(%r15)
movq -0x50(%rbp), %rax
cmpb $0x0, 0x15(%rax)
je 0x166c4
movq -0x48(%rbp), %r12
jmp 0x163c0
movl $0xc0a, %r13d           # imm = 0xC0A
jmp 0x1667a
movq 0x10(%r15), %rdx
leaq 0x55086(%rip), %rsi     # 0x6b6ca
movl $0x150a, %r13d          # imm = 0x150A
movl $0x150a, %edi           # imm = 0x150A
movl $0x6b2f, %ecx           # imm = 0x6B2F
callq 0x16c4b
jmp 0x1667a
pushq $0x7
popq %r12
jmp 0x167a6
pushq $0x7
popq %rax
movl %eax, %r13d
jmp 0x1667a
movl $0x70a, %r12d           # imm = 0x70A
jmp 0x167a6
xorl %r13d, %r13d
movzwl 0x20(%r15), %eax
movq -0x48(%rbp), %r12
cmpl %r14d, %eax
jle 0x16695
movq 0x28(%r15), %rax
movslq %r14d, %rcx
movq (%rax,%rcx,8), %rax
jmp 0x16697
xorl %eax, %eax
movq %rax, (%r12)
cmpb $0x0, 0x22(%r15)
pushq $0x8
popq %r12
cmovel %r13d, %r12d
testl %r13d, %r13d
cmovnel %r13d, %r12d
movl %r12d, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, -0x38(%rbp)
movq 0x38(%rbx), %rdi
leaq 0x55086(%rip), %rsi     # 0x6b759
xorl %r12d, %r12d
xorl %edx, %edx
callq 0x1520c
testl %eax, %eax
je 0x166e8
movb $0x1, 0x22(%r15)
jmp 0x166ec
pushq $0x42
popq %r12
movl $0x1ff, %edx            # imm = 0x1FF
andl -0xc8(%rbp), %edx
movq -0x38(%rbp), %rdi
movl %r12d, %esi
callq 0x16ba9
movl %eax, 0x18(%r15)
testl %eax, %eax
js 0x16754
movl -0xc4(%rbp), %esi
movl -0xc0(%rbp), %edx
movl %eax, %edi
callq *0x71319(%rip)          # 0x87a38
pushq $0x1
popq %rcx
movq %r15, %rdi
movl %ecx, %esi
movl $0x80, %edx
callq 0x1723d
testl %eax, %eax
je 0x16777
pushq $0x1
popq %rcx
movq %r15, %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x1723d
testl %eax, %eax
je 0x16628
movl %eax, %r12d
jmp 0x167a6
movl $0x6a88, %edi           # imm = 0x6A88
callq 0x16cb0
leaq 0x54edf(%rip), %rsi     # 0x6b644
pushq $0xe
popq %r12
movl %r12d, %edi
movq -0x38(%rbp), %rdx
movl $0x6a88, %ecx           # imm = 0x6A88
jmp 0x167a1
movl 0x18(%r15), %edi
xorl %esi, %esi
callq 0x16e2d
testl %eax, %eax
je 0x16735
leaq 0x54ed6(%rip), %rsi     # 0x6b663
movl $0x120a, %r12d          # imm = 0x120A
movl $0x120a, %edi           # imm = 0x120A
movq -0x38(%rbp), %rdx
movl $0x6a98, %ecx           # imm = 0x6A98
callq 0x16c4b
movq %rbx, %rdi
callq 0x1728d
movq %r13, %rdi
callq 0x9c7b
jmp 0x166af