// File: /workspace/github_c_makefile_star2_O1/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, %r13
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %r14
movslq 0xc(%rdi), %rax
testq %rax, %rax
jle 0x290a2a
movslq %ebp, %rcx
addq %r13, %rcx
cmpq %rax, %rcx
jle 0x290a2a
leaq 0xc(%r14), %rcx
movq (%r14), %rax
movq %rax, 0x50(%rsp)
movl 0x8(%r14), %eax
movl %eax, 0x1c(%rsp)
movl 0x8(%rcx), %eax
movl %eax, 0x38(%rsp)
movq %rcx, 0x58(%rsp)
movq (%rcx), %rax
movq %rax, 0x30(%rsp)
movq 0x18(%r14), %r12
movq 0x20(%r14), %rax
movq %rax, 0x20(%rsp)
movq 0x38(%r14), %rax
movq %rax, 0x70(%rsp)
movups 0x28(%r14), %xmm0
movaps %xmm0, 0x60(%rsp)
movl 0x40(%r14), %ecx
movl 0x44(%r14), %eax
movl %eax, 0x28(%rsp)
movq 0x48(%r14), %rdi
movq 0x50(%r14), %rsi
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%r14)
movups %xmm0, (%r14)
movups %xmm0, 0x10(%r14)
movups %xmm0, 0x30(%r14)
movups %xmm0, 0x40(%r14)
movq $0x0, 0x50(%r14)
movl %ecx, 0x2c(%rsp)
andl $0x1087f7f, %ecx        # imm = 0x1087F7F
movq %rsi, 0x40(%rsp)
movq %r14, %rdx
xorl %r8d, %r8d
movq %rdi, 0x48(%rsp)
callq *0x28(%rdi)
testl %eax, %eax
je 0x290ad8
movl %eax, %edx
jmp 0x290b77
movq %r13, 0x10(%rsp)
leaq 0x18(%r14), %rax
movq %rax, 0x20(%rsp)
movl %ebp, 0xc(%rsp)
testl %ebp, %ebp
jle 0x290aba
movq 0x20(%r14), %rax
movslq 0x8(%r14), %rcx
cqto
idivq %rcx
movq %rdx, %rbx
movl %ecx, %r13d
subl %ebx, %r13d
cmpl %r13d, %ebp
cmovll %ebp, %r13d
testq %rdx, %rdx
jne 0x290a8b
movq 0x28(%r14), %r12
leal 0x8(%rcx), %edi
callq 0x27c362
testq %rax, %rax
je 0x290ab6
movq $0x0, (%rax)
testq %r12, %r12
cmoveq 0x20(%rsp), %r12
movq %rax, (%r12)
movq %rax, 0x28(%r14)
movq 0x28(%r14), %rax
leaq (%rax,%rbx), %rdi
addq $0x8, %rdi
movslq %r13d, %rbx
movq %r15, %rsi
movq %rbx, %rdx
callq 0x17660
addq %rbx, %r15
subl %ebx, %ebp
addq %rbx, 0x20(%r14)
movb $0x1, %al
testb %al, %al
jne 0x290a3c
jmp 0x290ace
xorl %eax, %eax
jmp 0x290ab0
movq 0x10(%rsp), %rax
addl 0xc(%rsp), %eax
movl %eax, 0x10(%r14)
xorl %edx, %edx
jmp 0x290c0a
movl $0xc0a, %edx            # imm = 0xC0A
jmp 0x290c0a
testq %r12, %r12
movl $0x0, %edx
je 0x290b40
movl %ebp, 0xc(%rsp)
movq %r13, 0x10(%rsp)
xorl %ebx, %ebx
movq %r12, %r13
movl 0x1c(%rsp), %eax
movl %eax, %ebp
movslq %ebp, %rbp
leaq (%rbx,%rbp), %rax
movq 0x20(%rsp), %rdx
movl %edx, %ecx
subl %ebx, %ecx
cmpq %rdx, %rax
cmovgl %ecx, %ebp
leaq 0x8(%r13), %rsi
movq (%r14), %rax
movq %r14, %rdi
movl %ebp, %edx
movq %rbx, %rcx
callq *0x18(%rax)
testl %eax, %eax
jne 0x290b35
movslq %ebp, %rax
addq %rax, %rbx
movq (%r13), %r13
testq %r13, %r13
jne 0x290af6
xorl %edx, %edx
jmp 0x290b37
movl %eax, %edx
movq 0x10(%rsp), %r13
movl 0xc(%rsp), %ebp
xorl %eax, %eax
testl %edx, %edx
cmovneq %r12, %rax
testq %r12, %r12
je 0x290b74
testl %edx, %edx
jne 0x290b74
movq %rdx, 0x10(%rsp)
movq (%r12), %rbx
movq %r12, %rdi
callq 0x27c490
movq %rbx, %r12
testq %rbx, %rbx
jne 0x290b56
xorl %r12d, %r12d
movq 0x10(%rsp), %rdx
jmp 0x290b77
movq %rax, %r12
testl %edx, %edx
je 0x290c1b
leaq 0x28(%r14), %rbx
movq (%r14), %rax
testq %rax, %rax
je 0x290ba2
movq %r14, %rdi
movq %rdx, 0x10(%rsp)
callq *0x8(%rax)
movq 0x10(%rsp), %rdx
movq $0x0, (%r14)
movq 0x50(%rsp), %rax
movq %rax, (%r14)
movl 0x1c(%rsp), %eax
movl %eax, 0x8(%r14)
movl 0x38(%rsp), %eax
movq 0x58(%rsp), %rcx
movl %eax, 0x8(%rcx)
movq 0x30(%rsp), %rax
movq %rax, (%rcx)
movq %r12, 0x18(%r14)
movq 0x20(%rsp), %rax
movq %rax, 0x20(%r14)
movq 0x70(%rsp), %rax
movq %rax, 0x10(%rbx)
movaps 0x60(%rsp), %xmm0
movups %xmm0, (%rbx)
movl 0x2c(%rsp), %eax
movl %eax, 0x40(%r14)
movl 0x28(%rsp), %eax
movl %eax, 0x44(%r14)
movq 0x48(%rsp), %rax
movq %rax, 0x48(%r14)
movq 0x40(%rsp), %rax
movq %rax, 0x50(%r14)
testl %edx, %edx
je 0x290c1b
movl %edx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r14), %rax
movq 0x18(%rax), %rax
movq %r14, %rdi
movq %r15, %rsi
movl %ebp, %edx
movq %r13, %rcx
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rax