// File: /workspace/github_c_makefile_star2_O3/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 $0xe8, %rsp
movl %ecx, %ebx
movl %esi, %r14d
movq 0x40(%rdi), %rax
testq %rax, %rax
je 0x1831a
movq (%rax), %r12
movzwl 0x20(%r12), %eax
xorl %r13d, %r13d
cmpl %r14d, %eax
jg 0x18590
movq %r8, -0x48(%rbp)
leal 0x1(%r14), %r15d
movl %edx, -0x2c(%rbp)
movl %edx, 0x1c(%r12)
movl 0x18(%r12), %edi
testl %edi, %edi
js 0x182ce
leaq -0x110(%rbp), %rsi
callq *0x7c697(%rip)          # 0x948d0
movl $0x130a, %r13d          # imm = 0x130A
testl %eax, %eax
jne 0x18586
movl %r15d, %eax
imull -0x2c(%rbp), %eax
movq -0xe0(%rbp), %rdx
movslq %eax, %rcx
cmpq %rcx, %rdx
jge 0x182ce
testl %ebx, %ebx
je 0x18312
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 0x182ce
movslq %ecx, %rbx
movslq %edx, %r13
subq %rbx, %r13
shlq $0xc, %rbx
orq $0xfff, %rbx            # imm = 0xFFF
movl 0x18(%r12), %edi
movq %rbx, %rsi
leaq 0x66104(%rip), %rdx     # 0x7e3b0
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0x18e15
cmpl $0x1, %eax
jne 0x18547
addq $0x1000, %rbx           # imm = 0x1000
decq %r13
jne 0x1829d
movq 0x28(%r12), %rbx
callq 0x9d86
movl $0xc0a, %r13d           # imm = 0xC0A
testl %eax, %eax
jne 0x18586
shll $0x3, %r15d
movq %rbx, %rdi
movl %r15d, %esi
callq 0xa45c
testq %rax, %rax
je 0x18586
movq %rax, 0x28(%r12)
movzwl 0x20(%r12), %eax
cmpl %r14d, %eax
jle 0x1848c
xorl %r13d, %r13d
jmp 0x18586
movq %rdi, %r13
movl %edx, -0x2c(%rbp)
movq %r8, -0x48(%rbp)
callq 0x9d86
movl $0x7, %r15d
testl %eax, %eax
jne 0x185bf
cmpl $0x0, 0x7c0c2(%rip)     # 0x94400
je 0x1835a
leaq -0x110(%rbp), %r12
movl $0x18, %edi
movq %r12, %rsi
callq 0x171f8
movq (%r12), %rax
jmp 0x18365
movl $0x18, %edi
callq *0x7c0bb(%rip)          # 0x94420
testq %rax, %rax
movl -0x2c(%rbp), %edx
je 0x185bf
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq 0x10(%r13), %r15
movq 0x20(%r15), %r12
testq %r12, %r12
je 0x183af
movq %r12, (%rax)
incl 0x30(%r12)
movq %rax, 0x40(%r13)
movq 0x38(%r12), %rcx
movq %rcx, 0x8(%rax)
movq %rax, 0x38(%r12)
movq -0x48(%rbp), %r8
jmp 0x181fd
movq %rax, -0x50(%rbp)
movl 0x18(%r13), %edi
leaq -0x110(%rbp), %rsi
callq *0x7c50c(%rip)          # 0x948d0
testl %eax, %eax
je 0x183d3
cmpb $0x0, 0x15(%r15)
je 0x1852e
movq %r15, -0x38(%rbp)
movq 0x38(%r13), %rdi
callq 0x8190
movq %rax, %r12
callq 0x9d86
movl $0x7, %r15d
testl %eax, %eax
jne 0x18534
movq %r12, -0x40(%rbp)
movslq %r12d, %rdi
addq $0x46, %rdi
movq %rdi, -0x58(%rbp)
callq 0xa38c
testq %rax, %rax
je 0x18534
movq %rax, %r12
movq -0x40(%rbp), %rax
addl $0x6, %eax
movq %rax, -0x40(%rbp)
movq %r12, %rdi
xorl %esi, %esi
movq -0x58(%rbp), %rdx
callq 0x8200
movq %r12, %r15
addq $0x40, %r15
movq %r15, 0x10(%r12)
movq 0x38(%r13), %rcx
leaq 0x612ec(%rip), %rdx     # 0x79732
movq -0x40(%rbp), %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0xb954
movl $0xffffffff, 0x18(%r12) # imm = 0xFFFFFFFF
movq 0x10(%r13), %rax
movq %r12, 0x20(%rax)
movq %rax, (%r12)
movq $0x8, 0x8(%r12)
movq -0x38(%rbp), %rax
cmpb $0x0, 0x15(%rax)
je 0x185d4
movl -0x2c(%rbp), %edx
movq -0x50(%rbp), %rax
jmp 0x1838c
movslq -0x2c(%rbp), %rbx
movl 0x18(%r12), %r8d
testl %r8d, %r8d
js 0x184d2
xorl %ecx, %ecx
cmpb $0x0, 0x22(%r12)
sete %cl
leal 0x1(,%rcx,2), %edx
movzwl %ax, %r9d
imulq %rbx, %r9
xorl %edi, %edi
movq %rbx, %rsi
movl $0x1, %ecx
callq *0x7c58c(%rip)          # 0x94a50
movq %rax, %r15
cmpq $-0x1, %rax
jne 0x18506
jmp 0x18565
callq 0x9d86
movl $0x7, %r13d
testl %eax, %eax
jne 0x18586
movl -0x2c(%rbp), %edi
callq 0xa38c
testq %rax, %rax
je 0x18586
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x8200
movq 0x28(%r12), %rcx
movzwl 0x20(%r12), %eax
movq %r15, (%rcx,%rax,8)
incl %eax
movw %ax, 0x20(%r12)
movzwl %ax, %ecx
xorl %r13d, %r13d
cmpl %r14d, %ecx
jbe 0x18490
jmp 0x18586
movl $0x70a, %r15d           # imm = 0x70A
movq %r13, %rdi
callq 0x19291
movq -0x50(%rbp), %rdi
callq 0xa3c7
jmp 0x185bf
movq 0x10(%r12), %rdx
leaq 0x6110f(%rip), %rsi     # 0x79662
movl $0x130a, %r13d          # imm = 0x130A
movl $0x130a, %edi           # imm = 0x130A
movl $0x6b16, %ecx           # imm = 0x6B16
jmp 0x18581
movq 0x10(%r12), %rdx
leaq 0x61139(%rip), %rsi     # 0x796aa
movl $0x150a, %r13d          # imm = 0x150A
movl $0x150a, %edi           # imm = 0x150A
movl $0x6b2f, %ecx           # imm = 0x6B2F
callq 0x18d3b
movzwl 0x20(%r12), %eax
movq -0x48(%rbp), %r8
cmpl %r14d, %eax
jle 0x185a3
movq 0x28(%r12), %rax
movslq %r14d, %rcx
movq (%rax,%rcx,8), %rax
jmp 0x185a5
xorl %eax, %eax
movq %rax, (%r8)
cmpb $0x0, 0x22(%r12)
movl $0x8, %r15d
cmovel %r13d, %r15d
testl %r13d, %r13d
cmovnel %r13d, %r15d
movl %r15d, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, -0x38(%rbp)
movq 0x38(%r13), %rdi
leaq 0x61156(%rip), %rsi     # 0x79739
xorl %r15d, %r15d
xorl %edx, %edx
callq 0x16f58
testl %eax, %eax
je 0x185f9
movb $0x1, 0x22(%r12)
jmp 0x185ff
movl $0x42, %r15d
movl $0x1ff, %edx            # imm = 0x1FF
andl -0xf8(%rbp), %edx
movq -0x38(%rbp), %rdi
movl %r15d, %esi
callq 0x18c99
movl %eax, 0x18(%r12)
testl %eax, %eax
js 0x18699
movl -0xf4(%rbp), %esi
movl -0xf0(%rbp), %edx
movl %eax, %edi
callq *0x7c405(%rip)          # 0x94a38
movl 0x18(%r12), %r15d
testl %r15d, %r15d
js 0x1867e
xorps %xmm0, %xmm0
leaq -0x80(%rbp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movw $0x1, (%rdx)
movq $0x80, 0x8(%rdx)
movq $0x1, 0x10(%rdx)
movl %r15d, %edi
movl $0x6, %esi
xorl %eax, %eax
callq *0x7c290(%rip)          # 0x94900
cmpl $-0x1, %eax
je 0x186fc
movl 0x18(%r12), %r15d
movl %r15d, %edi
xorl %esi, %esi
callq *0x7c25f(%rip)          # 0x948e8
testl %eax, %eax
jns 0x186d5
callq 0x8060
cmpl $0x4, (%rax)
je 0x1867e
jmp 0x186d7
leaq 0x6103c(%rip), %rsi     # 0x796dc
leaq 0x60e92(%rip), %rcx     # 0x79539
movl $0xe, %r15d
movl $0xe, %edi
movl $0x6a88, %edx           # imm = 0x6A88
xorl %eax, %eax
callq 0xb9c7
leaq 0x60f5f(%rip), %rsi     # 0x79624
movl $0xe, %edi
movq -0x38(%rbp), %rdx
movl $0x6a88, %ecx           # imm = 0x6A88
jmp 0x186f2
je 0x186fc
leaq 0x60f65(%rip), %rsi     # 0x79643
movl $0x120a, %r15d          # imm = 0x120A
movl $0x120a, %edi           # imm = 0x120A
movq -0x38(%rbp), %rdx
movl $0x6a98, %ecx           # imm = 0x6A98
callq 0x18d3b
jmp 0x18534
movl 0x18(%r12), %edi
testl %edi, %edi
js 0x18480
xorps %xmm0, %xmm0
leaq -0x80(%rbp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movq $0x80, 0x8(%rdx)
movq $0x1, 0x10(%rdx)
movl $0x6, %esi
xorl %eax, %eax
callq *0x7c1cc(%rip)          # 0x94900
cmpl $-0x1, %eax
jne 0x18480
movl $0x5, %r15d
jmp 0x18534