// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: sqlite3VdbeMemTranslate
int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
  sqlite3_int64 len;          /* Maximum length of output string in bytes */
  unsigned char *zOut;        /* Output buffer */
  unsigned char *zIn;         /* Input iterator */
  unsigned char *zTerm;       /* End of input */
  unsigned char *z;           /* Output iterator */
  unsigned int c;

  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
  assert( pMem->flags&MEM_Str );
  assert( pMem->enc!=desiredEnc );
  assert( pMem->enc!=0 );
  assert( pMem->n>=0 );

#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
  {
    StrAccum acc;
    char zBuf[1000];
    sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
    sqlite3VdbeMemPrettyPrint(pMem, &acc);
    fprintf(stderr, "INPUT:  %s\n", sqlite3StrAccumFinish(&acc));
  }
#endif

  /* If the translation is between UTF-16 little and big endian, then
  ** all that is required is to swap the byte order. This case is handled
  ** differently from the others.
  */
  if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
    u8 temp;
    int rc;
    rc = sqlite3VdbeMemMakeWriteable(pMem);
    if( rc!=SQLITE_OK ){
      assert( rc==SQLITE_NOMEM );
      return SQLITE_NOMEM_BKPT;
    }
    zIn = (u8*)pMem->z;
    zTerm = &zIn[pMem->n&~1];
    while( zIn<zTerm ){
      temp = *zIn;
      *zIn = *(zIn+1);
      zIn++;
      *zIn++ = temp;
    }
    pMem->enc = desiredEnc;
    goto translate_out;
  }

  /* Set len to the maximum number of bytes required in the output buffer. */
  if( desiredEnc==SQLITE_UTF8 ){
    /* When converting from UTF-16, the maximum growth results from
    ** translating a 2-byte character to a 4-byte UTF-8 character.
    ** A single byte is required for the output string
    ** nul-terminator.
    */
    pMem->n &= ~1;
    len = 2 * (sqlite3_int64)pMem->n + 1;
  }else{
    /* When converting from UTF-8 to UTF-16 the maximum growth is caused
    ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
    ** character. Two bytes are required in the output buffer for the
    ** nul-terminator.
    */
    len = 2 * (sqlite3_int64)pMem->n + 2;
  }

  /* Set zIn to point at the start of the input buffer and zTerm to point 1
  ** byte past the end.
  **
  ** Variable zOut is set to point at the output buffer, space obtained
  ** from sqlite3_malloc().
  */
  zIn = (u8*)pMem->z;
  zTerm = &zIn[pMem->n];
  zOut = sqlite3DbMallocRaw(pMem->db, len);
  if( !zOut ){
    return SQLITE_NOMEM_BKPT;
  }
  z = zOut;

  if( pMem->enc==SQLITE_UTF8 ){
    if( desiredEnc==SQLITE_UTF16LE ){
      /* UTF-8 -> UTF-16 Little-endian */
      while( zIn<zTerm ){
        READ_UTF8(zIn, zTerm, c);
        WRITE_UTF16LE(z, c);
      }
    }else{
      assert( desiredEnc==SQLITE_UTF16BE );
      /* UTF-8 -> UTF-16 Big-endian */
      while( zIn<zTerm ){
        READ_UTF8(zIn, zTerm, c);
        WRITE_UTF16BE(z, c);
      }
    }
    pMem->n = (int)(z - zOut);
    *z++ = 0;
  }else{
    assert( desiredEnc==SQLITE_UTF8 );
    if( pMem->enc==SQLITE_UTF16LE ){
      /* UTF-16 Little-endian -> UTF-8 */
      while( zIn<zTerm ){
        c = *(zIn++);
        c += (*(zIn++))<<8;
        if( c>=0xd800 && c<0xe000 ){
#ifdef SQLITE_REPLACE_INVALID_UTF
          if( c>=0xdc00 || zIn>=zTerm ){
            c = 0xfffd;
          }else{
            int c2 = *(zIn++);
            c2 += (*(zIn++))<<8;
            if( c2<0xdc00 || c2>=0xe000 ){
              zIn -= 2;
              c = 0xfffd;
            }else{
              c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000;
            }
          }
#else
          if( zIn<zTerm ){
            int c2 = (*zIn++);
            c2 += ((*zIn++)<<8);
            c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);
          }
#endif
        }
        WRITE_UTF8(z, c);
      }
    }else{
      /* UTF-16 Big-endian -> UTF-8 */
      while( zIn<zTerm ){
        c = (*(zIn++))<<8;
        c += *(zIn++);
        if( c>=0xd800 && c<0xe000 ){
#ifdef SQLITE_REPLACE_INVALID_UTF
          if( c>=0xdc00 || zIn>=zTerm ){
            c = 0xfffd;
          }else{
            int c2 = (*(zIn++))<<8;
            c2 += *(zIn++);
            if( c2<0xdc00 || c2>=0xe000 ){
              zIn -= 2;
              c = 0xfffd;
            }else{
              c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000;
            }
          }
#else
          if( zIn<zTerm ){
            int c2 = ((*zIn++)<<8);
            c2 += (*zIn++);
            c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);
          }
#endif
        }
        WRITE_UTF8(z, c);
      }
    }
    pMem->n = (int)(z - zOut);
  }
  *z = 0;
  assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );

  c = MEM_Str|MEM_Term|(pMem->flags&(MEM_AffMask|MEM_Subtype));
  sqlite3VdbeMemRelease(pMem);
  pMem->flags = c;
  pMem->enc = desiredEnc;
  pMem->z = (char*)zOut;
  pMem->zMalloc = pMem->z;
  pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z);

translate_out:
#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
  {
    StrAccum acc;
    char zBuf[1000];
    sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
    sqlite3VdbeMemPrettyPrint(pMem, &acc);
    fprintf(stderr, "OUTPUT: %s\n", sqlite3StrAccumFinish(&acc));
  }
#endif
  return SQLITE_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %ebp
movq %rdi, %rbx
pushq $0x7
popq %r15
cmpl $0x1, %esi
je 0x23a373
cmpb $0x1, 0xa(%rbx)
je 0x23a373
movq %rbx, %rdi
callq 0x2245b6
testl %eax, %eax
jne 0x23a829
movq 0x10(%rbx), %rax
movslq 0xc(%rbx), %rcx
andq $-0x2, %rcx
addq %rax, %rcx
cmpq %rcx, %rax
jae 0x23a4dc
rolw $0x8, (%rax)
addq $0x2, %rax
jmp 0x23a360
movslq 0xc(%rbx), %r12
cmpb $0x1, %bpl
jne 0x23a38f
andq $-0x2, %r12
movl %r12d, 0xc(%rbx)
leaq 0x1(,%r12,2), %rsi
jmp 0x23a397
leaq 0x2(,%r12,2), %rsi
movq 0x10(%rbx), %r13
movq 0x28(%rbx), %rdi
callq 0x22809f
testq %rax, %rax
je 0x23a829
movq %rax, %r14
addq %r13, %r12
movzbl 0xa(%rbx), %eax
cmpl $0x1, %eax
je 0x23a4e5
cmpl $0x2, %eax
jne 0x23a5cc
movq %r14, %rax
cmpq %r12, %r13
jae 0x23a6df
movb (%r13), %sil
movzbl %sil, %edi
leaq 0x2(%r13), %rcx
movzbl 0x1(%r13), %r8d
movl %r8d, %edx
shll $0x8, %edx
orl %edi, %edx
movl %r8d, %r9d
andb $-0x8, %r9b
cmpb $-0x28, %r9b
jne 0x23a474
cmpq %r12, %rcx
jae 0x23a474
movzbl 0x2(%r13), %ecx
movzbl 0x3(%r13), %esi
addq $0x4, %r13
andl $0x3, %esi
shll $0x8, %esi
shll $0xa, %edi
orl %ecx, %esi
orl %edi, %esi
andl $0xf000, %edi           # imm = 0xF000
shll $0xa, %edx
andl $0xf0000, %edx          # imm = 0xF0000
movl %edx, %r8d
addl $0x10000, %r8d          # imm = 0x10000
addl %edi, %edx
addl $0x10000, %edx          # imm = 0x10000
shrl $0x12, %r8d
orb $-0x10, %r8b
movb %r8b, (%rax)
shrl $0xc, %edx
andb $0x3f, %dl
orb $-0x80, %dl
movb %dl, 0x1(%rax)
shrl $0x6, %esi
andb $0x3f, %sil
orb $-0x80, %sil
movb %sil, 0x2(%rax)
andb $0x3f, %cl
orb $-0x80, %cl
movb %cl, 0x3(%rax)
addq $0x4, %rax
jmp 0x23a3cc
cmpl $0x7f, %edx
ja 0x23a487
movb %sil, (%rax)
incq %rax
movq %rcx, %r13
jmp 0x23a3cc
cmpb $0x7, %r8b
ja 0x23a4ad
shrl $0x6, %edx
orb $-0x40, %dl
movb %dl, (%rax)
andb $0x3f, %sil
orb $-0x80, %sil
movb %sil, 0x1(%rax)
addq $0x2, %rax
movq %rcx, %r13
jmp 0x23a3cc
shrb $0x4, %r8b
orb $-0x20, %r8b
movb %r8b, (%rax)
shrl $0x6, %edx
andb $0x3f, %dl
orb $-0x80, %dl
movb %dl, 0x1(%rax)
andb $0x3f, %sil
orb $-0x80, %sil
movb %sil, 0x2(%rax)
addq $0x3, %rax
movq %rcx, %r13
jmp 0x23a3cc
movb %bpl, 0xa(%rbx)
jmp 0x23a826
leaq 0x137a44(%rip), %rcx    # 0x371f30
cmpb $0x2, %bpl
jne 0x23a6ec
movq %r14, %rax
cmpq %r12, %r13
jae 0x23a7dc
movzbl (%r13), %esi
incq %r13
cmpl $0xc0, %esi
jb 0x23a577
addl $0xffffff40, %esi       # imm = 0xFFFFFF40
movzbl (%rsi,%rcx), %edx
cmpq %r12, %r13
je 0x23a540
movzbl (%r13), %esi
movl %esi, %edi
andl $-0x40, %edi
cmpl $0x80, %edi
jne 0x23a543
shll $0x6, %edx
incq %r13
andl $0x3f, %esi
orl %esi, %edx
jmp 0x23a51c
movq %r12, %r13
movl %edx, %edi
andl $-0x2, %edi
movl $0xfffd, %esi           # imm = 0xFFFD
cmpl $0xfffe, %edi           # imm = 0xFFFE
je 0x23a577
cmpl $0x80, %edx
jb 0x23a577
movl %edx, %edi
andl $0xfffff800, %edi       # imm = 0xFFFFF800
cmpl $0xd800, %edi           # imm = 0xD800
je 0x23a577
cmpl $0xffff, %edx           # imm = 0xFFFF
ja 0x23a583
movl %edx, %esi
movw %si, (%rax)
addq $0x2, %rax
jmp 0x23a4f9
movl %edx, %esi
shrl $0xa, %esi
andl $0x3f, %esi
leal -0x10000(%rdx), %edi
movl %edi, %r8d
shrl $0xa, %r8d
andl $0xc0, %r8d
orl %esi, %r8d
movb %r8b, (%rax)
shrl $0x12, %edi
andb $0x3, %dil
orb $-0x28, %dil
movb %dil, 0x1(%rax)
movb %dl, 0x2(%rax)
shrl $0x8, %edx
andb $0x3, %dl
orb $-0x24, %dl
movb %dl, 0x3(%rax)
addq $0x4, %rax
jmp 0x23a4f9
movq %r14, %rax
cmpq %r12, %r13
jae 0x23a6df
movzbl (%r13), %r8d
movl %r8d, %ecx
shll $0x8, %ecx
leaq 0x2(%r13), %rdx
movb 0x1(%r13), %sil
movzbl %sil, %edi
orl %edi, %ecx
movl %r8d, %r9d
andb $-0x8, %r9b
cmpb $-0x28, %r9b
jne 0x23a677
cmpq %r12, %rdx
jae 0x23a677
movzbl 0x2(%r13), %esi
movzbl 0x3(%r13), %edx
addq $0x4, %r13
andl $0x3, %esi
shll $0x8, %esi
shll $0xa, %edi
orl %edx, %esi
orl %edi, %esi
andl $0xf000, %edi           # imm = 0xF000
shll $0xa, %ecx
andl $0xf0000, %ecx          # imm = 0xF0000
movl %ecx, %r8d
addl $0x10000, %r8d          # imm = 0x10000
addl %edi, %ecx
addl $0x10000, %ecx          # imm = 0x10000
shrl $0x12, %r8d
orb $-0x10, %r8b
movb %r8b, (%rax)
shrl $0xc, %ecx
andb $0x3f, %cl
orb $-0x80, %cl
movb %cl, 0x1(%rax)
shrl $0x6, %esi
andb $0x3f, %sil
orb $-0x80, %sil
movb %sil, 0x2(%rax)
andb $0x3f, %dl
orb $-0x80, %dl
movb %dl, 0x3(%rax)
addq $0x4, %rax
jmp 0x23a5cf
cmpl $0x7f, %ecx
ja 0x23a68a
movb %sil, (%rax)
incq %rax
movq %rdx, %r13
jmp 0x23a5cf
cmpb $0x7, %r8b
ja 0x23a6b0
shrl $0x6, %ecx
orb $-0x40, %cl
movb %cl, (%rax)
andb $0x3f, %sil
orb $-0x80, %sil
movb %sil, 0x1(%rax)
addq $0x2, %rax
movq %rdx, %r13
jmp 0x23a5cf
shrb $0x4, %r8b
orb $-0x20, %r8b
movb %r8b, (%rax)
shrl $0x6, %ecx
andb $0x3f, %cl
orb $-0x80, %cl
movb %cl, 0x1(%rax)
andb $0x3f, %sil
orb $-0x80, %sil
movb %sil, 0x2(%rax)
addq $0x3, %rax
movq %rdx, %r13
jmp 0x23a5cf
movl %eax, %ecx
subl %r14d, %ecx
movl %ecx, 0xc(%rbx)
jmp 0x23a7ea
pushq $0x4
popq %r8
pushq $0x2
popq %rsi
movq %r14, %rax
cmpq %r12, %r13
jae 0x23a7dc
movzbl (%r13), %edx
incq %r13
cmpl $0xc0, %edx
jb 0x23a77d
addl $0xffffff40, %edx       # imm = 0xFFFFFF40
movzbl (%rdx,%rcx), %edi
cmpq %r12, %r13
je 0x23a740
movzbl (%r13), %edx
movl %edx, %r9d
andl $-0x40, %r9d
cmpl $0x80, %r9d
jne 0x23a743
shll $0x6, %edi
incq %r13
andl $0x3f, %edx
orl %edx, %edi
jmp 0x23a719
movq %r12, %r13
movl %edi, %r9d
andl $-0x2, %r9d
movl $0xfffd, %edx           # imm = 0xFFFD
cmpl $0xfffe, %r9d           # imm = 0xFFFE
je 0x23a77d
cmpl $0x80, %edi
jb 0x23a77d
movl %edi, %r9d
andl $0xfffff800, %r9d       # imm = 0xFFFFF800
cmpl $0xd800, %r9d           # imm = 0xD800
je 0x23a77d
cmpl $0xffff, %edi           # imm = 0xFFFF
ja 0x23a793
movl %edi, %edx
leaq 0x1(%rax), %r9
movb %dh, (%rax)
movl %edx, %edi
movq %rsi, %rdx
addq %rdx, %rax
movb %dil, (%r9)
jmp 0x23a6f6
leal -0x10000(%rdi), %edx
movl %edx, %r9d
shrl $0x12, %r9d
andb $0x3, %r9b
orb $-0x28, %r9b
movb %r9b, (%rax)
movl %edi, %r9d
shrl $0xa, %r9d
andl $0x3f, %r9d
shrl $0xa, %edx
andl $0xc0, %edx
orl %r9d, %edx
movb %dl, 0x1(%rax)
movl %edi, %edx
shrl $0x8, %edx
andb $0x3, %dl
orb $-0x24, %dl
leaq 0x3(%rax), %r9
movb %dl, 0x2(%rax)
movq %r8, %rdx
jmp 0x23a788
movl %eax, %ecx
subl %r14d, %ecx
movl %ecx, 0xc(%rbx)
movb $0x0, (%rax)
incq %rax
movb $0x0, (%rax)
movl $0xffff803d, %r15d      # imm = 0xFFFF803D
andl 0x8(%rbx), %r15d
orl $0x202, %r15d           # imm = 0x202
movq %rbx, %rdi
callq 0x224336
movw %r15w, 0x8(%rbx)
movb %bpl, 0xa(%rbx)
movq %r14, 0x10(%rbx)
movq %r14, 0x18(%rbx)
movq 0x28(%rbx), %rdi
movq %r14, %rsi
callq 0x22103d
movl %eax, 0x20(%rbx)
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq