// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: memjrnlWrite
static int memjrnlWrite(
  sqlite3_file *pJfd,    /* The journal file into which to write */
  const void *zBuf,      /* Take data to be written from here */
  int iAmt,              /* Number of bytes to write */
  sqlite_int64 iOfst     /* Begin writing at this offset into the file */
){
  MemJournal *p = (MemJournal *)pJfd;
  int nWrite = iAmt;
  u8 *zWrite = (u8 *)zBuf;

  /* If the file should be created now, create it and write the new data
  ** into the file on disk. */
  if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
    int rc = memjrnlCreateFile(p);
    if( rc==SQLITE_OK ){
      rc = sqlite3OsWrite(pJfd, zBuf, iAmt, iOfst);
    }
    return rc;
  }

  /* If the contents of this write should be stored in memory */
  else{
    /* An in-memory journal file should only ever be appended to. Random
    ** access writes are not required. The only exception to this is when
    ** the in-memory journal is being used by a connection using the
    ** atomic-write optimization. In this case the first 28 bytes of the
    ** journal file may be written as part of committing the transaction. */
    assert( iOfst==p->endpoint.iOffset || iOfst==0 );
#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
    if( iOfst==0 && p->pFirst ){
      assert( p->nChunkSize>iAmt );
      memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
    }else
#else
    assert( iOfst>0 || p->pFirst==0 );
#endif
    {
      while( nWrite>0 ){
        FileChunk *pChunk = p->endpoint.pChunk;
        int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
        int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset);

        if( iChunkOffset==0 ){
          /* New chunk is required to extend the file. */
          FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize));
          if( !pNew ){
            return SQLITE_IOERR_NOMEM_BKPT;
          }
          pNew->pNext = 0;
          if( pChunk ){
            assert( p->pFirst );
            pChunk->pNext = pNew;
          }else{
            assert( !p->pFirst );
            p->pFirst = pNew;
          }
          p->endpoint.pChunk = pNew;
        }

        memcpy((u8*)p->endpoint.pChunk->zChunk + iChunkOffset, zWrite, iSpace);
        zWrite += iSpace;
        nWrite -= iSpace;
        p->endpoint.iOffset += iSpace;
      }
      p->nSize = iAmt + iOfst;
    }
  }

  return SQLITE_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %r15
movq %rdi, %rbx
movslq 0xc(%rdi), %rax
testq %rax, %rax
movq %rcx, 0x18(%rsp)
movl %edx, 0xc(%rsp)
jle 0x230e97
movslq %edx, %rsi
addq %rcx, %rsi
cmpq %rax, %rsi
jle 0x230e97
pushq $0xb
popq %rcx
leaq 0x20(%rsp), %rax
movq %rax, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
xorps %xmm0, %xmm0
movups %xmm0, 0x40(%rbx)
movups %xmm0, 0x30(%rbx)
movups %xmm0, 0x20(%rbx)
movups %xmm0, 0x10(%rbx)
movups %xmm0, (%rbx)
andq $0x0, 0x50(%rbx)
movq 0x48(%rax), %rdi
movq 0x50(%rax), %rsi
movl $0x1087f7f, %ecx        # imm = 0x1087F7F
andl 0x40(%rax), %ecx
movq %rbx, %rdx
xorl %r8d, %r8d
callq *0x28(%rdi)
testl %eax, %eax
je 0x230f38
movl %eax, %ebp
movq (%rbx), %rax
testq %rax, %rax
je 0x230e84
movq %rbx, %rdi
callq *0x8(%rax)
pushq $0xb
popq %rcx
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
rep  movsq (%rsi), %es:(%rdi)
jmp 0x230f27
leaq 0x18(%rbx), %rax
movq %rax, 0x10(%rsp)
movl %edx, %ebp
testl %ebp, %ebp
jle 0x230f12
movq 0x20(%rbx), %rax
movq 0x28(%rbx), %r14
movslq 0x8(%rbx), %rcx
cqto
idivq %rcx
movq %rdx, %r12
movl %ecx, %r13d
subl %r12d, %r13d
cmpl %r13d, %ebp
cmovll %ebp, %r13d
testq %rdx, %rdx
jne 0x230ef0
leal 0x8(%rcx), %edi
callq 0x21f6c0
testq %rax, %rax
je 0x230f22
andq $0x0, (%rax)
testq %r14, %r14
cmoveq 0x10(%rsp), %r14
movq %rax, (%r14)
movq %rax, 0x28(%rbx)
movq %rax, %r14
leaq (%r14,%r12), %rdi
addq $0x8, %rdi
movslq %r13d, %r12
movq %r15, %rsi
movq %r12, %rdx
callq 0x17630
addq %r12, %r15
subl %r12d, %ebp
addq %r12, 0x20(%rbx)
jmp 0x230ea2
movq 0x18(%rsp), %rax
addl 0xc(%rsp), %eax
movl %eax, 0x10(%rbx)
xorl %ebp, %ebp
jmp 0x230f27
movl $0xc0a, %ebp            # imm = 0xC0A
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x28(%rsp), %r12d
movq 0x38(%rsp), %r14
movq 0x40(%rsp), %rax
movq %rax, 0x10(%rsp)
xorl %r13d, %r13d
testq %r14, %r14
je 0x230f92
movslq %r12d, %r12
leaq (%r12,%r13), %rax
movq 0x10(%rsp), %rdx
movl %edx, %ecx
subl %r13d, %ecx
cmpq %rdx, %rax
cmovgl %ecx, %r12d
leaq 0x8(%r14), %rsi
movq (%rbx), %rax
movq %rbx, %rdi
movl %r12d, %edx
movq %r13, %rcx
callq *0x18(%rax)
testl %eax, %eax
jne 0x230e74
movslq %r12d, %rax
addq %rax, %r13
movq (%r14), %r14
jmp 0x230f4f
leaq 0x20(%rsp), %rdi
callq 0x230ff0
movq (%rbx), %rax
movq 0x18(%rax), %rax
movq %rbx, %rdi
movq %r15, %rsi
movl 0xc(%rsp), %edx
movq 0x18(%rsp), %rcx
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rax