// File: halsafar[P]libtmx-parser/./libs/tinyxml2/tinyxml2.cpp
// Function: tinyxml2::XMLUtil::ConvertUTF32ToUTF8(unsigned long, char*, int*)
void XMLUtil::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length )
{
    const unsigned long BYTE_MASK = 0xBF;
    const unsigned long BYTE_MARK = 0x80;
    const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };

    if (input < 0x80) {
        *length = 1;
    }
    else if ( input < 0x800 ) {
        *length = 2;
    }
    else if ( input < 0x10000 ) {
        *length = 3;
    }
    else if ( input < 0x200000 ) {
        *length = 4;
    }
    else {
        *length = 0;    // This code won't covert this correctly anyway.
        return;
    }

    output += *length;

    // Scary scary fall throughs.
    switch (*length) {
        case 4:
            --output;
            *output = (char)((input | BYTE_MARK) & BYTE_MASK);
            input >>= 6;
        case 3:
            --output;
            *output = (char)((input | BYTE_MARK) & BYTE_MASK);
            input >>= 6;
        case 2:
            --output;
            *output = (char)((input | BYTE_MARK) & BYTE_MASK);
            input >>= 6;
        case 1:
            --output;
            *output = (char)(input | FIRST_BYTE_MARK[*length]);
        default:
            break;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0xbf, -0x20(%rbp)
movq $0x80, -0x28(%rbp)
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
movl $0x38, %edx
callq 0x4170
movq $0xc0, -0x50(%rbp)
movq $0xe0, -0x48(%rbp)
movq $0xf0, -0x40(%rbp)
movq $0xf8, -0x38(%rbp)
movq $0xfc, -0x30(%rbp)
cmpq $0x80, -0x8(%rbp)
jae 0x1a212
movq -0x18(%rbp), %rax
movl $0x1, (%rax)
jmp 0x1a269
cmpq $0x800, -0x8(%rbp)      # imm = 0x800
jae 0x1a228
movq -0x18(%rbp), %rax
movl $0x2, (%rax)
jmp 0x1a267
cmpq $0x10000, -0x8(%rbp)    # imm = 0x10000
jae 0x1a23e
movq -0x18(%rbp), %rax
movl $0x3, (%rax)
jmp 0x1a265
cmpq $0x200000, -0x8(%rbp)   # imm = 0x200000
jae 0x1a254
movq -0x18(%rbp), %rax
movl $0x4, (%rax)
jmp 0x1a263
movq -0x18(%rbp), %rax
movl $0x0, (%rax)
jmp 0x1a35c
jmp 0x1a265
jmp 0x1a267
jmp 0x1a269
movq -0x18(%rbp), %rax
movslq (%rax), %rcx
movq -0x10(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x10(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x68(%rbp)
subl $0x3, %eax
ja 0x1a35a
movq -0x68(%rbp), %rax
leaq 0x86ab(%rip), %rcx      # 0x22948
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x10(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x10(%rbp)
movq -0x8(%rbp), %rax
orq $0x80, %rax
andq $0xbf, %rax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, (%rax)
movq -0x8(%rbp), %rax
shrq $0x6, %rax
movq %rax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x10(%rbp)
movq -0x8(%rbp), %rax
orq $0x80, %rax
andq $0xbf, %rax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, (%rax)
movq -0x8(%rbp), %rax
shrq $0x6, %rax
movq %rax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x10(%rbp)
movq -0x8(%rbp), %rax
orq $0x80, %rax
andq $0xbf, %rax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, (%rax)
movq -0x8(%rbp), %rax
shrq $0x6, %rax
movq %rax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq (%rcx), %rcx
orq -0x60(%rbp,%rcx,8), %rax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, (%rax)
jmp 0x1a35c
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)