// File: /workspace/github_c_makefile_star2_O1/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 %r12
pushq %rbx
movl %esi, %ebp
movq %rdi, %rbx
cmpl $0x1, %esi
je 0x29b8e0
cmpb $0x1, 0xa(%rbx)
je 0x29b8e0
movq %rbx, %rdi
callq 0x281d9d
movl %eax, %ecx
movl $0x7, %eax
testl %ecx, %ecx
jne 0x29be27
movslq 0xc(%rbx), %rax
andq $-0x2, %rax
testl %eax, %eax
jle 0x29b8d7
movq 0x10(%rbx), %rcx
addq %rcx, %rax
rolw $0x8, (%rcx)
addq $0x2, %rcx
cmpq %rax, %rcx
jb 0x29b8ca
movb %bpl, 0xa(%rbx)
jmp 0x29be25
movslq 0xc(%rbx), %rax
cmpb $0x1, %bpl
jne 0x29b8fb
andq $-0x2, %rax
movl %eax, 0xc(%rbx)
leaq 0x1(,%rax,2), %rsi
jmp 0x29b903
leaq 0x2(,%rax,2), %rsi
movq 0x10(%rbx), %r15
movq 0x28(%rbx), %rdi
movslq 0xc(%rbx), %r12
testq %rdi, %rdi
je 0x29b91b
callq 0x28c518
jmp 0x29b923
movq %rsi, %rdi
callq 0x27c381
movq %rax, %r14
testq %rax, %rax
je 0x29ba6d
movq %r12, %rax
addq %r15, %rax
movzbl 0xa(%rbx), %ecx
cmpl $0x1, %ecx
je 0x29ba77
cmpl $0x2, %ecx
jne 0x29bb7a
movq %r14, %rcx
testl %r12d, %r12d
jle 0x29bc95
movq %r14, %rcx
movzbl (%r15), %esi
leaq 0x2(%r15), %rdi
movzbl 0x1(%r15), %r8d
movl %r8d, %edx
shll $0x8, %edx
orl %esi, %edx
andb $-0x8, %r8b
cmpb $-0x28, %r8b
sete %r8b
cmpq %rax, %rdi
setb %r9b
andb %r8b, %r9b
cmpb $0x1, %r9b
jne 0x29b9bf
movzbl 0x2(%r15), %edi
movzbl 0x3(%r15), %r8d
addq $0x4, %r15
andl $0x3, %r8d
shll $0x8, %r8d
shll $0xa, %esi
movzwl %si, %esi
shll $0xa, %edx
andl $0xf0000, %edx          # imm = 0xF0000
orl %esi, %edx
orl %edi, %edx
addl %r8d, %edx
addl $0x10000, %edx          # imm = 0x10000
jmp 0x29b9c2
movq %rdi, %r15
cmpl $0x7f, %edx
ja 0x29b9d1
movb %dl, (%rcx)
incq %rcx
jmp 0x29ba5f
movl %edx, %esi
cmpl $0x7ff, %edx            # imm = 0x7FF
ja 0x29b9f4
shrl $0x6, %esi
orb $-0x40, %sil
movb %sil, (%rcx)
andb $0x3f, %dl
orb $-0x80, %dl
movb %dl, 0x1(%rcx)
addq $0x2, %rcx
jmp 0x29ba5f
cmpl $0xffff, %edx           # imm = 0xFFFF
ja 0x29ba26
shrl $0xc, %esi
orb $-0x20, %sil
movb %sil, (%rcx)
movl %edx, %esi
shrl $0x6, %esi
andb $0x3f, %sil
orb $-0x80, %sil
movb %sil, 0x1(%rcx)
andb $0x3f, %dl
orb $-0x80, %dl
movb %dl, 0x2(%rcx)
addq $0x3, %rcx
jmp 0x29ba5f
shrl $0x12, %esi
orb $-0x10, %sil
movb %sil, (%rcx)
movl %edx, %esi
shrl $0xc, %esi
andb $0x3f, %sil
orb $-0x80, %sil
movb %sil, 0x1(%rcx)
movl %edx, %esi
shrl $0x6, %esi
andb $0x3f, %sil
orb $-0x80, %sil
movb %sil, 0x2(%rcx)
andb $0x3f, %dl
orb $-0x80, %dl
movb %dl, 0x3(%rcx)
addq $0x4, %rcx
cmpq %rax, %r15
jb 0x29b95a
jmp 0x29bc95
movl $0x7, %eax
jmp 0x29be27
movq %r14, %rcx
cmpb $0x2, %bpl
jne 0x29bca2
testl %r12d, %r12d
jle 0x29bda4
leaq 0x13f80c(%rip), %rdx    # 0x3db2a0
movl $0xfffd, %esi           # imm = 0xFFFD
movq %r14, %rcx
movzbl (%r15), %edi
incq %r15
cmpl $0xc0, %edi
jb 0x29bb0e
addl $0xffffff40, %edi       # imm = 0xFFFFFF40
movzbl (%rdi,%rdx), %r8d
cmpq %rax, %r15
je 0x29bade
movzbl (%r15), %edi
movl %edi, %r9d
andl $-0x40, %r9d
cmpl $0x80, %r9d
jne 0x29bade
shll $0x6, %r8d
incq %r15
andl $0x3f, %edi
orl %edi, %r8d
jmp 0x29bab6
movl %r8d, %edi
andl $0xfffff800, %edi       # imm = 0xFFFFF800
movl %r8d, %r9d
andl $-0x2, %r9d
cmpl $0xd800, %edi           # imm = 0xD800
movl %r8d, %edi
cmovel %esi, %edi
cmpl $0x80, %r8d
cmovbl %esi, %edi
cmpl $0xfffe, %r9d           # imm = 0xFFFE
cmovel %esi, %edi
cmpl $0xffff, %edi           # imm = 0xFFFF
ja 0x29bb1f
movw %di, (%rcx)
addq $0x2, %rcx
jmp 0x29bb6c
movl %edi, %r8d
shrl $0xa, %r8d
andl $0x3f, %r8d
leal -0x10000(%rdi), %r9d
movl %r9d, %r10d
shrl $0xa, %r10d
andl $0xc0, %r10d
orl %r8d, %r10d
movb %r10b, (%rcx)
shrl $0x12, %r9d
andb $0x3, %r9b
orb $-0x28, %r9b
movb %r9b, 0x1(%rcx)
movb %dil, 0x2(%rcx)
shrl $0x8, %edi
andb $0x3, %dil
orb $-0x24, %dil
movb %dil, 0x3(%rcx)
addq $0x4, %rcx
cmpq %rax, %r15
jb 0x29ba9c
jmp 0x29bda4
movq %r14, %rcx
testl %r12d, %r12d
jle 0x29bc95
movq %r14, %rcx
movzbl (%r15), %r8d
movl %r8d, %edx
shll $0x8, %edx
leaq 0x2(%r15), %rdi
movzbl 0x1(%r15), %esi
orl %esi, %edx
andb $-0x8, %r8b
cmpb $-0x28, %r8b
sete %r8b
cmpq %rax, %rdi
setb %r9b
andb %r8b, %r9b
cmpb $0x1, %r9b
jne 0x29bbec
movzbl 0x2(%r15), %edi
movzbl 0x3(%r15), %r8d
addq $0x4, %r15
andl $0x3, %edi
shll $0x8, %edi
shll $0xa, %esi
movzwl %si, %esi
shll $0xa, %edx
andl $0xf0000, %edx          # imm = 0xF0000
orl %esi, %edx
orl %r8d, %edx
addl %edi, %edx
addl $0x10000, %edx          # imm = 0x10000
jmp 0x29bbef
movq %rdi, %r15
cmpl $0x7f, %edx
ja 0x29bbfe
movb %dl, (%rcx)
incq %rcx
jmp 0x29bc8c
movl %edx, %esi
cmpl $0x7ff, %edx            # imm = 0x7FF
ja 0x29bc21
shrl $0x6, %esi
orb $-0x40, %sil
movb %sil, (%rcx)
andb $0x3f, %dl
orb $-0x80, %dl
movb %dl, 0x1(%rcx)
addq $0x2, %rcx
jmp 0x29bc8c
cmpl $0xffff, %edx           # imm = 0xFFFF
ja 0x29bc53
shrl $0xc, %esi
orb $-0x20, %sil
movb %sil, (%rcx)
movl %edx, %esi
shrl $0x6, %esi
andb $0x3f, %sil
orb $-0x80, %sil
movb %sil, 0x1(%rcx)
andb $0x3f, %dl
orb $-0x80, %dl
movb %dl, 0x2(%rcx)
addq $0x3, %rcx
jmp 0x29bc8c
shrl $0x12, %esi
orb $-0x10, %sil
movb %sil, (%rcx)
movl %edx, %esi
shrl $0xc, %esi
andb $0x3f, %sil
orb $-0x80, %sil
movb %sil, 0x1(%rcx)
movl %edx, %esi
shrl $0x6, %esi
andb $0x3f, %sil
orb $-0x80, %sil
movb %sil, 0x2(%rcx)
andb $0x3f, %dl
orb $-0x80, %dl
movb %dl, 0x3(%rcx)
addq $0x4, %rcx
cmpq %rax, %r15
jb 0x29bb89
movl %ecx, %eax
subl %r14d, %eax
movl %eax, 0xc(%rbx)
jmp 0x29bdb2
testl %r12d, %r12d
jle 0x29bda4
leaq 0x13f5ee(%rip), %rdi    # 0x3db2a0
movl $0xfffd, %esi           # imm = 0xFFFD
movq %r14, %rcx
movzbl (%r15), %edx
incq %r15
cmpl $0xc0, %edx
jb 0x29bd2c
addl $0xffffff40, %edx       # imm = 0xFFFFFF40
movzbl (%rdx,%rdi), %r8d
cmpq %rax, %r15
je 0x29bcfc
movzbl (%r15), %edx
movl %edx, %r9d
andl $-0x40, %r9d
cmpl $0x80, %r9d
jne 0x29bcfc
shll $0x6, %r8d
incq %r15
andl $0x3f, %edx
orl %edx, %r8d
jmp 0x29bcd4
movl %r8d, %edx
andl $0xfffff800, %edx       # imm = 0xFFFFF800
movl %r8d, %r9d
andl $-0x2, %r9d
cmpl $0xd800, %edx           # imm = 0xD800
movl %r8d, %edx
cmovel %esi, %edx
cmpl $0x80, %r8d
cmovbl %esi, %edx
cmpl $0xfffe, %r9d           # imm = 0xFFFE
cmovel %esi, %edx
cmpl $0xffff, %edx           # imm = 0xFFFF
ja 0x29bd42
leaq 0x1(%rcx), %r8
movb %dh, (%rcx)
movl $0x2, %r9d
jmp 0x29bd95
leal -0x10000(%rdx), %r8d
movl %r8d, %r9d
shrl $0x12, %r9d
andb $0x3, %r9b
orb $-0x28, %r9b
movb %r9b, (%rcx)
movl %edx, %r9d
shrl $0xa, %r9d
andl $0x3f, %r9d
shrl $0xa, %r8d
andl $0xc0, %r8d
orl %r9d, %r8d
movb %r8b, 0x1(%rcx)
movl %edx, %r9d
shrl $0x8, %r9d
andb $0x3, %r9b
orb $-0x24, %r9b
leaq 0x3(%rcx), %r8
movb %r9b, 0x2(%rcx)
movl $0x4, %r9d
addq %r9, %rcx
movb %dl, (%r8)
cmpq %rax, %r15
jb 0x29bcba
movl %ecx, %eax
subl %r14d, %eax
movl %eax, 0xc(%rbx)
movb $0x0, (%rcx)
incq %rcx
movb $0x0, (%rcx)
movzwl 0x8(%rbx), %eax
movl %eax, %r15d
andl $0x803d, %r15d          # imm = 0x803D
orl $0x202, %r15d           # imm = 0x202
testl $0x2400, %eax           # imm = 0x2400
jne 0x29bdd7
cmpl $0x0, 0x20(%rbx)
je 0x29bddf
movq %rbx, %rdi
callq 0x29b45b
movw %r15w, 0x8(%rbx)
movb %bpl, 0xa(%rbx)
movq %r14, 0x10(%rbx)
movq %r14, 0x18(%rbx)
movq 0x28(%rbx), %rcx
testq %rcx, %rcx
je 0x29be19
cmpq %r14, 0x1c0(%rcx)
jbe 0x29be19
movl $0x80, %eax
cmpq %r14, 0x1b0(%rcx)
jbe 0x29be22
cmpq %r14, 0x1b8(%rcx)
jbe 0x29be30
movq %r14, %rdi
callq *0x22a876(%rip)         # 0x4c6698
movl %eax, 0x20(%rbx)
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movzwl 0x176(%rcx), %eax
jmp 0x29be22