// File: /workspace/github_c_makefile_star2_O1/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 $0xd8, %rsp
movl %ecx, -0x4c(%rbp)
movl %edx, -0x2c(%rbp)
movl %esi, %r12d
movq %rdi, %r13
cmpq $0x0, 0x40(%rdi)
movq %r12, -0x40(%rbp)
je 0x182a6
movq 0x40(%r13), %rax
movq (%rax), %r13
movzwl 0x20(%r13), %eax
xorl %r14d, %r14d
cmpl %r12d, %eax
jg 0x18784
movq %r8, -0x48(%rbp)
leal 0x1(%r12), %ebx
movl -0x2c(%rbp), %eax
movl %eax, 0x1c(%r13)
movl 0x18(%r13), %edi
xorl %r14d, %r14d
testl %edi, %edi
js 0x18673
leaq -0x100(%rbp), %rsi
callq *0x7c642(%rip)          # 0x948d0
movl $0x2, %ecx
testl %eax, %eax
je 0x1833c
movl $0x130a, %r14d          # imm = 0x130A
jmp 0x1877b
movq %r8, -0x48(%rbp)
callq 0x9d1f
testl %eax, %eax
je 0x182b7
xorl %ebx, %ebx
jmp 0x182c4
movl $0x18, %edi
callq 0xa2a2
movq %rax, %rbx
testq %rbx, %rbx
je 0x18331
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
movq 0x10(%r13), %r14
movq 0x20(%r14), %r12
testq %r12, %r12
jne 0x184cb
movl 0x18(%r13), %edi
leaq -0x100(%rbp), %rsi
callq *0x7c5d7(%rip)          # 0x948d0
testl %eax, %eax
je 0x18308
cmpb $0x0, 0x15(%r14)
je 0x184ba
movq 0x38(%r13), %rdi
callq 0x8190
movq %rax, -0x38(%rbp)
movslq %eax, %r15
addq $0x46, %r15
callq 0x9d1f
testl %eax, %eax
je 0x1843d
xorl %r12d, %r12d
jmp 0x18448
movl $0x7, %r15d
jmp 0x184ec
movl %ebx, %eax
imull -0x2c(%rbp), %eax
movq -0xd0(%rbp), %rdx
movslq %eax, %rsi
cmpq %rsi, %rdx
jge 0x18673
cmpl $0x0, -0x4c(%rbp)
je 0x18510
leaq 0xfff(%rdx), %r15
testq %rdx, %rdx
cmovnsq %rdx, %r15
sarq $0xc, %r15
leal 0xfff(%rax), %r14d
testl %eax, %eax
cmovnsl %eax, %r14d
sarl $0xc, %r14d
xorl %ecx, %ecx
cmpl %r15d, %r14d
setg %r12b
jle 0x18663
shlq $0x20, %r15
movl 0x18(%r13), %edi
movq %r15, %rsi
sarq $0x14, %rsi
orq $0xfff, %rsi            # imm = 0xFFF
leaq 0x65fb2(%rip), %rdx     # 0x7e360
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0x18f86
cmpl $0x1, %eax
jne 0x18413
movslq %r14d, %r14
sarq $0x20, %r15
leaq 0x1(%r15), %r12
shlq $0xc, %r15
addq $0x1fff, %r15           # imm = 0x1FFF
cmpq %r12, %r14
je 0x1865d
movl 0x18(%r13), %edi
movq %r15, %rsi
leaq 0x65f73(%rip), %rdx     # 0x7e360
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0x18f86
incq %r12
addq $0x1000, %r15           # imm = 0x1000
cmpl $0x1, %eax
je 0x183d6
decq %r12
cmpq %r14, %r12
setl %r12b
movq 0x10(%r13), %rdx
leaq 0x61244(%rip), %rsi     # 0x79662
movl $0x130a, %r14d          # imm = 0x130A
movl $0x130a, %edi           # imm = 0x130A
movl $0x6b16, %ecx           # imm = 0x6B16
callq 0x18ea3
movl $0x2, %ecx
jmp 0x18666
movl %r15d, %edi
callq 0xa2a2
movq %rax, %r12
testq %r12, %r12
je 0x184b2
movq -0x38(%rbp), %rax
addl $0x6, %eax
movq %rax, -0x38(%rbp)
movq %r12, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x8200
leaq 0x40(%r12), %r15
movq %r15, 0x10(%r12)
movq 0x38(%r13), %rcx
leaq 0x612b8(%rip), %rdx     # 0x79732
movq -0x38(%rbp), %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0xb859
movl $0xffffffff, 0x18(%r12) # imm = 0xFFFFFFFF
movq 0x10(%r13), %rax
movq %r12, 0x20(%rax)
movq %rax, (%r12)
movq $0x8, 0x8(%r12)
cmpb $0x0, 0x15(%r14)
je 0x18518
movb $0x1, %r14b
jmp 0x184c6
movl $0x7, %r15d
jmp 0x184c0
movl $0x70a, %r15d           # imm = 0x70A
xorl %r12d, %r12d
xorl %r14d, %r14d
testb %r14b, %r14b
je 0x184fe
movq %r12, (%rbx)
incl 0x30(%r12)
movq %rbx, 0x40(%r13)
movq 0x38(%r12), %rax
movq %rax, 0x8(%rbx)
movq %rbx, 0x38(%r12)
xorl %r15d, %r15d
movq -0x40(%rbp), %r12
testl %r15d, %r15d
movq -0x48(%rbp), %r8
jne 0x187b6
jmp 0x1824a
movq %r13, %rdi
callq 0x19409
movq %rbx, %rdi
callq 0xa2e8
jmp 0x184e8
xorl %r14d, %r14d
jmp 0x1877b
movq %r15, %r14
movq 0x38(%r13), %rdi
leaq 0x61213(%rip), %rsi     # 0x79739
xorl %r15d, %r15d
xorl %edx, %edx
callq 0x16f19
testl %eax, %eax
je 0x1853c
movb $0x1, 0x22(%r12)
jmp 0x18542
movl $0x42, %r15d
movl $0x1ff, %edx            # imm = 0x1FF
andl -0xe8(%rbp), %edx
movq %r14, %rdi
movq %r14, -0x38(%rbp)
movl %r15d, %esi
callq 0x18df8
movl %eax, 0x18(%r12)
testl %eax, %eax
js 0x18616
movl -0xe4(%rbp), %esi
movl -0xe0(%rbp), %edx
movl %eax, %edi
callq *0x7c4bb(%rip)          # 0x94a38
movl 0x18(%r12), %edi
testl %edi, %edi
js 0x185c3
xorps %xmm0, %xmm0
leaq -0x70(%rbp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movl $0x1, (%rdx)
movq $0x80, 0x8(%rdx)
movq $0x1, 0x10(%rdx)
xorl %r15d, %r15d
movl $0x6, %esi
xorl %eax, %eax
callq *0x7c346(%rip)          # 0x94900
cmpl $-0x1, %eax
je 0x187d0
movl 0x18(%r12), %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %edi
xorl %esi, %esi
callq *0x7c312(%rip)          # 0x948e8
movl %eax, %r15d
testl %eax, %eax
jns 0x185e7
callq 0x8060
cmpl $0x4, (%rax)
je 0x185cb
testl %r15d, %r15d
je 0x187cd
leaq 0x6104c(%rip), %rsi     # 0x79643
movl $0x120a, %r15d          # imm = 0x120A
movl $0x120a, %edi           # imm = 0x120A
movq %r14, %rdx
movl $0x6a98, %ecx           # imm = 0x6A98
callq 0x18ea3
xorl %eax, %eax
jmp 0x187d2
leaq 0x610bf(%rip), %rsi     # 0x796dc
leaq 0x60f15(%rip), %rcx     # 0x79539
xorl %r14d, %r14d
movl $0xe, %r15d
movl $0xe, %edi
movl $0x6a88, %edx           # imm = 0x6A88
xorl %eax, %eax
callq 0xb8cc
leaq 0x60fdf(%rip), %rsi     # 0x79624
movl $0xe, %edi
movq -0x38(%rbp), %rdx
movl $0x6a88, %ecx           # imm = 0x6A88
callq 0x18ea3
jmp 0x184c6
setg %r12b
xorl %ecx, %ecx
xorl %r14d, %r14d
testb %r12b, %r12b
movq -0x40(%rbp), %r12
jne 0x1877b
movq 0x28(%r13), %r15
callq 0x9d1f
testl %eax, %eax
je 0x18684
xorl %eax, %eax
jmp 0x18691
shll $0x3, %ebx
movq %r15, %rdi
movl %ebx, %esi
callq 0xa37d
testq %rax, %rax
je 0x1876c
movq %rax, 0x28(%r13)
movslq -0x2c(%rbp), %rbx
leaq 0x61001(%rip), %r15     # 0x796aa
movzwl 0x20(%r13), %r9d
cmpl %r12d, %r9d
jg 0x18779
movl 0x18(%r13), %r8d
testl %r8d, %r8d
js 0x1870c
xorl %eax, %eax
cmpb $0x0, 0x22(%r13)
sete %al
leal 0x1(,%rax,2), %edx
imulq %rbx, %r9
xorl %edi, %edi
movq %rbx, %rsi
movl $0x1, %ecx
callq *0x7c36b(%rip)          # 0x94a50
movq %rax, %r12
cmpq $-0x1, %rax
jne 0x18737
movq 0x10(%r13), %rdx
movl $0x150a, %r14d          # imm = 0x150A
movl $0x150a, %edi           # imm = 0x150A
movq %r15, %rsi
movl $0x6b2f, %ecx           # imm = 0x6B2F
callq 0x18ea3
jmp 0x18757
callq 0x9d1f
testl %eax, %eax
je 0x1871a
xorl %r12d, %r12d
jmp 0x18725
movl -0x2c(%rbp), %edi
callq 0xa2a2
movq %rax, %r12
testq %r12, %r12
je 0x18751
movq %r12, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x8200
movq 0x28(%r13), %rax
movzwl 0x20(%r13), %ecx
movq %r12, (%rax,%rcx,8)
incl %ecx
movw %cx, 0x20(%r13)
xorl %ecx, %ecx
movb $0x1, %al
jmp 0x1875e
movl $0x7, %r14d
movl $0x2, %ecx
xorl %eax, %eax
testb %al, %al
movq -0x40(%rbp), %r12
jne 0x186a9
jmp 0x1877b
movl $0xc0a, %r14d           # imm = 0xC0A
movl $0x2, %ecx
jmp 0x1877b
xorl %ecx, %ecx
testb $0x1, %cl
movq -0x48(%rbp), %r8
jne 0x187cb
movzwl 0x20(%r13), %eax
cmpl %r12d, %eax
jle 0x1879b
movq 0x28(%r13), %rax
movslq %r12d, %rcx
movq (%rax,%rcx,8), %rax
jmp 0x1879d
xorl %eax, %eax
movq %rax, (%r8)
testl %r14d, %r14d
movl $0x8, %r15d
cmovnel %r14d, %r15d
cmpb $0x0, 0x22(%r13)
cmovel %r14d, %r15d
movl %r15d, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x187b6
xorl %r15d, %r15d
movb $0x1, %al
testb %al, %al
je 0x1881b
movl 0x18(%r12), %edi
testl %edi, %edi
js 0x18818
xorps %xmm0, %xmm0
leaq -0x70(%rbp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movq $0x80, 0x8(%rdx)
movq $0x1, 0x10(%rdx)
movl $0x6, %esi
xorl %eax, %eax
callq *0x7c0f6(%rip)          # 0x94900
xorl %ecx, %ecx
cmpl $-0x1, %eax
sete %cl
leal (%rcx,%rcx,4), %r15d
jmp 0x1881b
xorl %r15d, %r15d
testl %r15d, %r15d
sete %r14b
jmp 0x184c6