// File: WojciechMula[P]utf16utf8/include/reference.h
// Function: sprint_unicode(unsigned char*, unsigned int)
static inline uint8_t *sprint_unicode(uint8_t *str, uint32_t c)	// str must be able to hold 1 to 5 bytes and will be null-terminated by this function
{
	const uint8_t m6 = 63;
	const uint8_t	c10x	= 0x80,
	      		c110x	= 0xC0,
	      		c1110x	= 0xE0,
	      		c11110x	= 0xF0;

	if (c < 0x0080)
	{
		str[0] = uint8_t(c);
		if (c > 0)
			str[1] = '\0';
	}
	else if (c < 0x0800)
	{
		str[1] = uint8_t((c & m6) | c10x);
		c >>= 6;
		str[0] = uint8_t(c | c110x);
		str[2] = '\0';
	}
	else if (c < 0x10000)
	{
		str[2] = uint8_t((c & m6) | c10x);
		c >>= 6;
		str[1] = uint8_t((c & m6) | c10x);
		c >>= 6;
		str[0] = uint8_t(c | c1110x);
		str[3] = '\0';
	}
	else if (c < 0x200000)
	{
		str[3] = uint8_t((c & m6) | c10x);
		c >>= 6;
		str[2] = uint8_t((c & m6) | c10x);
		c >>= 6;
		str[1] = uint8_t((c & m6) | c10x);
		c >>= 6;
		str[0] = uint8_t(c | c11110x);
		str[4] = '\0';
	}
	else
		str[0] = '\0';		// Unicode character doesn't map to UTF-8

	return str;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movb $0x3f, -0xd(%rbp)
movb $-0x80, -0xe(%rbp)
movb $-0x40, -0xf(%rbp)
movb $-0x20, -0x10(%rbp)
movb $-0x10, -0x11(%rbp)
cmpl $0x80, -0xc(%rbp)
jae 0xda96
movl -0xc(%rbp), %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, (%rax)
cmpl $0x0, -0xc(%rbp)
jbe 0xda91
movq -0x8(%rbp), %rax
movb $0x0, 0x1(%rax)
jmp 0xdbc0
cmpl $0x800, -0xc(%rbp)      # imm = 0x800
jae 0xdad9
movl -0xc(%rbp), %eax
andl $0x3f, %eax
orl $0x80, %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, 0x1(%rax)
movl -0xc(%rbp), %eax
shrl $0x6, %eax
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %eax
orl $0xc0, %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, (%rax)
movq -0x8(%rbp), %rax
movb $0x0, 0x2(%rax)
jmp 0xdbbe
cmpl $0x10000, -0xc(%rbp)    # imm = 0x10000
jae 0xdb39
movl -0xc(%rbp), %eax
andl $0x3f, %eax
orl $0x80, %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, 0x2(%rax)
movl -0xc(%rbp), %eax
shrl $0x6, %eax
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %eax
andl $0x3f, %eax
orl $0x80, %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, 0x1(%rax)
movl -0xc(%rbp), %eax
shrl $0x6, %eax
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %eax
orl $0xe0, %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, (%rax)
movq -0x8(%rbp), %rax
movb $0x0, 0x3(%rax)
jmp 0xdbbc
cmpl $0x200000, -0xc(%rbp)   # imm = 0x200000
jae 0xdbb3
movl -0xc(%rbp), %eax
andl $0x3f, %eax
orl $0x80, %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, 0x3(%rax)
movl -0xc(%rbp), %eax
shrl $0x6, %eax
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %eax
andl $0x3f, %eax
orl $0x80, %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, 0x2(%rax)
movl -0xc(%rbp), %eax
shrl $0x6, %eax
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %eax
andl $0x3f, %eax
orl $0x80, %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, 0x1(%rax)
movl -0xc(%rbp), %eax
shrl $0x6, %eax
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %eax
orl $0xf0, %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, (%rax)
movq -0x8(%rbp), %rax
movb $0x0, 0x4(%rax)
jmp 0xdbba
movq -0x8(%rbp), %rax
movb $0x0, (%rax)
jmp 0xdbbc
jmp 0xdbbe
jmp 0xdbc0
movq -0x8(%rbp), %rax
popq %rbp
retq
nopw %cs:(%rax,%rax)