// File: /workspace/github_c_makefile_star2_O3/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 %rcx, %r12
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %rbx
movslq 0xc(%rdi), %rax
testq %rax, %rax
jle 0x286dc9
movslq %ebp, %rcx
addq %r12, %rcx
cmpq %rax, %rcx
jle 0x286dc9
leaq 0x20(%rsp), %rax
movl $0xb, %ecx
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)
movq $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 0x286e88
movl %eax, %r14d
movq (%rbx), %rax
testq %rax, %rax
je 0x286db4
movq %rbx, %rdi
callq *0x8(%rax)
leaq 0x20(%rsp), %rsi
movl $0xb, %ecx
movq %rbx, %rdi
rep  movsq (%rsi), %es:(%rdi)
jmp 0x286e6e
movq %r12, 0x10(%rsp)
movl %ebp, 0xc(%rsp)
testl %ebp, %ebp
jle 0x286e5f
leaq 0x18(%rbx), %rax
movq %rax, 0x18(%rsp)
movq 0x20(%rbx), %rax
movl 0xc(%rsp), %r13d
movq 0x28(%rbx), %rbp
movslq 0x8(%rbx), %rcx
cqto
idivq %rcx
movq %rdx, %r14
movl %ecx, %r12d
subl %r14d, %r12d
cmpl %r12d, %r13d
cmovll %r13d, %r12d
testq %rdx, %rdx
jne 0x286e36
leal 0x8(%rcx), %edi
callq 0x272ac7
testq %rax, %rax
je 0x286e80
testq %rbp, %rbp
cmoveq 0x18(%rsp), %rbp
movq $0x0, (%rax)
movq %rax, (%rbp)
movq %rax, 0x28(%rbx)
movq %rax, %rbp
leaq (%r14,%rbp), %rdi
addq $0x8, %rdi
movslq %r12d, %r14
movq %r15, %rsi
movq %r14, %rdx
callq 0x17660
addq %r14, %r15
movq 0x20(%rbx), %rax
addq %r14, %rax
movq %rax, 0x20(%rbx)
subl %r14d, %r13d
jg 0x286dec
movq 0x10(%rsp), %rax
addl 0xc(%rsp), %eax
movl %eax, 0x10(%rbx)
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xc0a, %r14d           # imm = 0xC0A
jmp 0x286e6e
movl %ebp, 0xc(%rsp)
movq %r12, 0x10(%rsp)
movq 0x38(%rsp), %rbp
testq %rbp, %rbp
je 0x286eef
movl 0x28(%rsp), %r12d
movq 0x40(%rsp), %rax
movq %rax, 0x18(%rsp)
xorl %r13d, %r13d
movslq %r12d, %r12
leaq (%r12,%r13), %rax
movq 0x18(%rsp), %rdx
movl %edx, %ecx
subl %r13d, %ecx
cmpq %rdx, %rax
cmovgl %ecx, %r12d
leaq 0x8(%rbp), %rsi
movq (%rbx), %rax
movq %rbx, %rdi
movl %r12d, %edx
movq %r13, %rcx
callq *0x18(%rax)
testl %eax, %eax
jne 0x286da3
movslq %r12d, %rax
addq %rax, %r13
movq (%rbp), %rbp
testq %rbp, %rbp
jne 0x286ead
leaq 0x20(%rsp), %rdi
callq 0x286f50
movq (%rbx), %rax
movq 0x18(%rax), %rax
movq %rbx, %rdi
movq %r15, %rsi
movl 0xc(%rsp), %edx
movq 0x10(%rsp), %rcx
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rax