// File: WojciechMula[P]utf16utf8/include/scalar_utf16.h
// Function: void utf16::decode<sse_convert_utf16_to_utf8(unsigned short const*, unsigned long, unsigned char*)::$_0, sse_convert_utf16_to_utf8(unsigned short const*, unsigned long, unsigned char*)::$_1>(unsigned short const*, unsigned long, sse_convert_utf16_to_utf8(unsigned short const*, unsigned long, unsigned char*)::$_0, sse_convert_utf16_to_utf8(unsigned short const*, unsigned long, unsigned char*)::$_1)
void decode(const uint16_t* codepoints, size_t size, CONSUMER consumer, ERROR_HANDLER error_handler) {
        assert(codepoints != nullptr);

        const uint16_t* curr = codepoints;
        const uint16_t* end = codepoints + size;

        // RFC2781, chapter 2.2
        while (curr != end) {
            const uint16_t W1 = *curr;
            curr += 1;

            if (W1 < 0xd800 || W1 > 0xdfff) { // fast path, code point is equal to character's value
                consumer(W1);
                continue;
            }

            if (W1 > 0xdbff) { // W1 must be in range 0xd800 .. 0xdbff
                error_handler(codepoints, curr, Error::high_surrogate_out_of_range);
                continue;
            }

            if (curr == end) { // required the next word, but we're already at the end of data
                error_handler(codepoints, curr, Error::missing_low_surrogate);
                break;
            }

            const uint16_t W2 = *curr;
            if (W2 < 0xdc00 || W2 > 0xdfff) { // W2 = 0xdc00 .. 0xdfff
                error_handler(codepoints, curr, Error::low_surrogate_out_of_range);
            } else {
                const uint32_t hi = W1 & 0x3ff; // take lower 10 bits of W1 and W2
                const uint32_t lo = W2 & 0x3ff;
                const uint32_t tmp = lo | (hi << 10); // build a 20-bit temporary value U'

                consumer(tmp + 0x10000);
            }

            curr += 1;
        }
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdx, -0x8(%rbp)
movq %rcx, -0x18(%rbp)
movq %r8, -0x10(%rbp)
movq %rdi, -0x20(%rbp)
movq %rsi, -0x28(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x3c15
jmp 0x3c34
leaq 0x15324(%rip), %rdi     # 0x18f40
leaq 0x15333(%rip), %rsi     # 0x18f56
movl $0x13, %edx
leaq 0x1533e(%rip), %rcx     # 0x18f6d
callq 0x2130
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x38(%rbp), %rax
je 0x3d5f
movq -0x30(%rbp), %rax
movw (%rax), %ax
movw %ax, -0x3a(%rbp)
movq -0x30(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x30(%rbp)
movzwl -0x3a(%rbp), %eax
cmpl $0xd800, %eax           # imm = 0xD800
jl 0x3c89
movzwl -0x3a(%rbp), %eax
cmpl $0xdfff, %eax           # imm = 0xDFFF
jle 0x3c98
movzwl -0x3a(%rbp), %esi
leaq -0x8(%rbp), %rdi
callq 0x6f80
jmp 0x3c4e
movzwl -0x3a(%rbp), %eax
cmpl $0xdbff, %eax           # imm = 0xDBFF
jle 0x3cb8
movq -0x20(%rbp), %rsi
movq -0x30(%rbp), %rdx
leaq -0x18(%rbp), %rdi
xorl %ecx, %ecx
callq 0x6fc0
jmp 0x3c4e
movq -0x30(%rbp), %rax
cmpq -0x38(%rbp), %rax
jne 0x3cdd
movq -0x20(%rbp), %rsi
movq -0x30(%rbp), %rdx
leaq -0x18(%rbp), %rdi
movl $0x2, %ecx
callq 0x6fc0
jmp 0x3d5f
movq -0x30(%rbp), %rax
movw (%rax), %ax
movw %ax, -0x3c(%rbp)
movzwl -0x3c(%rbp), %eax
cmpl $0xdc00, %eax           # imm = 0xDC00
jl 0x3cfe
movzwl -0x3c(%rbp), %eax
cmpl $0xdfff, %eax           # imm = 0xDFFF
jle 0x3d16
movq -0x20(%rbp), %rsi
movq -0x30(%rbp), %rdx
leaq -0x18(%rbp), %rdi
movl $0x1, %ecx
callq 0x6fc0
jmp 0x3d4e
movzwl -0x3a(%rbp), %eax
andl $0x3ff, %eax            # imm = 0x3FF
movl %eax, -0x40(%rbp)
movzwl -0x3c(%rbp), %eax
andl $0x3ff, %eax            # imm = 0x3FF
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
movl -0x40(%rbp), %ecx
shll $0xa, %ecx
orl %ecx, %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %esi
addl $0x10000, %esi          # imm = 0x10000
leaq -0x8(%rbp), %rdi
callq 0x6f80
movq -0x30(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x30(%rbp)
jmp 0x3c4e
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)