// File: /workspace/github_c_makefile_star2_O3/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
cmpl $0x1, %esi
je 0x291c22
cmpb $0x1, 0xa(%rbx)
je 0x291c22
movq %rbx, %rdi
callq 0x27840a
movl %eax, %ecx
movl $0x7, %eax
testl %ecx, %ecx
jne 0x292134
movslq 0xc(%rbx), %rax
andq $-0x2, %rax
testl %eax, %eax
jle 0x291c19
movq 0x10(%rbx), %rcx
addq %rcx, %rax
rolw $0x8, (%rcx)
addq $0x2, %rcx
cmpq %rax, %rcx
jb 0x291c0c
movb %bpl, 0xa(%rbx)
jmp 0x292132
movl 0xc(%rbx), %r13d
movslq %r13d, %r15
cmpb $0x1, %bpl
jne 0x291c44
andq $-0x2, %r15
movl %r15d, 0xc(%rbx)
leaq 0x1(,%r15,2), %rsi
movl %r15d, %r13d
jmp 0x291c4c
leaq 0x2(,%r15,2), %rsi
movq 0x10(%rbx), %r12
movq 0x28(%rbx), %rdi
testq %rdi, %rdi
je 0x291c60
callq 0x282a35
jmp 0x291c68
movq %rsi, %rdi
callq 0x272ae6
movq %rax, %r14
testq %rax, %rax
je 0x291da4
addq %r12, %r15
movzbl 0xa(%rbx), %eax
cmpl $0x1, %eax
je 0x291dae
cmpl $0x2, %eax
jne 0x291ea2
movq %r14, %rax
testl %r13d, %r13d
jle 0x291fb4
movq %r14, %rax
movb (%r12), %sil
movzbl %sil, %edi
leaq 0x2(%r12), %rcx
movzbl 0x1(%r12), %r8d
movl %r8d, %edx
shll $0x8, %edx
orl %edi, %edx
movl %r8d, %r9d
andb $-0x8, %r9b
cmpb $-0x28, %r9b
jne 0x291d3f
cmpq %r15, %rcx
jae 0x291d3f
movzbl 0x2(%r12), %ecx
movzbl 0x3(%r12), %esi
addq $0x4, %r12
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
movq %r12, %rcx
jmp 0x291d93
cmpl $0x7f, %edx
ja 0x291d4c
movb %sil, (%rax)
incq %rax
jmp 0x291d93
cmpb $0x7, %r8b
ja 0x291d6c
shrl $0x6, %edx
orb $-0x40, %dl
movb %dl, (%rax)
andb $0x3f, %sil
orb $-0x80, %sil
movb %sil, 0x1(%rax)
addq $0x2, %rax
jmp 0x291d93
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, %r12
cmpq %r15, %rcx
jb 0x291c9c
jmp 0x291fb4
movl $0x7, %eax
jmp 0x292134
movq %r14, %rax
cmpb $0x2, %bpl
jne 0x291fc1
testl %r13d, %r13d
jle 0x2920b1
leaq 0x13fd55(%rip), %rcx    # 0x3d1b20
movq %r14, %rax
movzbl (%r12), %esi
incq %r12
cmpl $0xc0, %esi
jb 0x291e47
addl $0xffffff40, %esi       # imm = 0xFFFFFF40
movzbl (%rsi,%rcx), %edx
cmpq %r15, %r12
je 0x291e10
movzbl (%r12), %esi
movl %esi, %edi
andl $-0x40, %edi
cmpl $0x80, %edi
jne 0x291e13
movl %edx, %edi
shll $0x6, %edi
incq %r12
andl $0x3f, %esi
movl %esi, %edx
orl %edi, %edx
jmp 0x291de8
movq %r15, %r12
movl %edx, %edi
andl $-0x2, %edi
movl $0xfffd, %esi           # imm = 0xFFFD
cmpl $0xfffe, %edi           # imm = 0xFFFE
je 0x291e47
cmpl $0x80, %edx
jb 0x291e47
movl %edx, %edi
andl $0xfffff800, %edi       # imm = 0xFFFFF800
cmpl $0xd800, %edi           # imm = 0xD800
je 0x291e47
cmpl $0xffff, %edx           # imm = 0xFFFF
ja 0x291e5c
movl %edx, %esi
movw %si, (%rax)
addq $0x2, %rax
cmpq %r15, %r12
jb 0x291dce
jmp 0x2920b1
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 0x291e4e
movq %r14, %rax
testl %r13d, %r13d
jle 0x291fb4
movq %r14, %rax
movzbl (%r12), %r8d
movl %r8d, %edx
shll $0x8, %edx
leaq 0x2(%r12), %rcx
movb 0x1(%r12), %sil
movzbl %sil, %edi
orl %edi, %edx
movl %r8d, %r9d
andb $-0x8, %r9b
cmpb $-0x28, %r9b
jne 0x291f54
cmpq %r15, %rcx
jae 0x291f54
movzbl 0x2(%r12), %esi
movzbl 0x3(%r12), %ecx
addq $0x4, %r12
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
movq %r12, %rcx
jmp 0x291fa8
cmpl $0x7f, %edx
ja 0x291f61
movb %sil, (%rax)
incq %rax
jmp 0x291fa8
cmpb $0x7, %r8b
ja 0x291f81
shrl $0x6, %edx
orb $-0x40, %dl
movb %dl, (%rax)
andb $0x3f, %sil
orb $-0x80, %sil
movb %sil, 0x1(%rax)
addq $0x2, %rax
jmp 0x291fa8
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, %r12
cmpq %r15, %rcx
jb 0x291eb1
movl %eax, %ecx
subl %r14d, %ecx
movl %ecx, 0xc(%rbx)
jmp 0x2920bf
testl %r13d, %r13d
jle 0x2920b1
leaq 0x13fb4f(%rip), %rsi    # 0x3d1b20
movq %r14, %rax
movzbl (%r12), %ecx
incq %r12
cmpl $0xc0, %ecx
jb 0x29204d
addl $0xffffff40, %ecx       # imm = 0xFFFFFF40
movzbl (%rcx,%rsi), %edx
cmpq %r15, %r12
je 0x292016
movzbl (%r12), %ecx
movl %ecx, %edi
andl $-0x40, %edi
cmpl $0x80, %edi
jne 0x292019
movl %edx, %edi
shll $0x6, %edi
incq %r12
andl $0x3f, %ecx
movl %ecx, %edx
orl %edi, %edx
jmp 0x291fee
movq %r15, %r12
movl %edx, %edi
andl $-0x2, %edi
movl $0xfffd, %ecx           # imm = 0xFFFD
cmpl $0xfffe, %edi           # imm = 0xFFFE
je 0x29204d
cmpl $0x80, %edx
jb 0x29204d
movl %edx, %edi
andl $0xfffff800, %edi       # imm = 0xFFFFF800
cmpl $0xd800, %edi           # imm = 0xD800
je 0x29204d
cmpl $0xffff, %edx           # imm = 0xFFFF
ja 0x29206b
movl %edx, %ecx
leaq 0x1(%rax), %rdi
movb %ch, (%rax)
movl $0x2, %r8d
movl %ecx, %edx
addq %r8, %rax
movb %dl, (%rdi)
cmpq %r15, %r12
jb 0x291fd4
jmp 0x2920b1
leal -0x10000(%rdx), %ecx
movl %ecx, %edi
shrl $0x12, %edi
andb $0x3, %dil
orb $-0x28, %dil
movb %dil, (%rax)
movl %edx, %edi
shrl $0xa, %edi
andl $0x3f, %edi
shrl $0xa, %ecx
andl $0xc0, %ecx
orl %edi, %ecx
movb %cl, 0x1(%rax)
movl %edx, %ecx
shrl $0x8, %ecx
andb $0x3, %cl
orb $-0x24, %cl
leaq 0x3(%rax), %rdi
movb %cl, 0x2(%rax)
movl $0x4, %r8d
jmp 0x29205b
movl %eax, %ecx
subl %r14d, %ecx
movl %ecx, 0xc(%rbx)
movb $0x0, (%rax)
incq %rax
movb $0x0, (%rax)
movzwl 0x8(%rbx), %eax
movl %eax, %r15d
andl $0x803d, %r15d          # imm = 0x803D
orl $0x202, %r15d           # imm = 0x202
testl $0x2400, %eax           # imm = 0x2400
jne 0x2920e4
cmpl $0x0, 0x20(%rbx)
je 0x2920ec
movq %rbx, %rdi
callq 0x291775
movw %r15w, 0x8(%rbx)
movb %bpl, 0xa(%rbx)
movq %r14, 0x10(%rbx)
movq %r14, 0x18(%rbx)
movq 0x28(%rbx), %rcx
testq %rcx, %rcx
je 0x292126
cmpq %r14, 0x1c0(%rcx)
jbe 0x292126
movl $0x80, %eax
cmpq %r14, 0x1b0(%rcx)
jbe 0x29212f
cmpq %r14, 0x1b8(%rcx)
jbe 0x292143
movq %r14, %rdi
callq *0x229569(%rip)         # 0x4bb698
movl %eax, 0x20(%rbx)
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzwl 0x176(%rcx), %eax
jmp 0x29212f