int _encoder_ucs2_to_utf8(unsigned short in, unsigned char *out) {
  if (in <= 0x007fu) {
    *out = in;
    return 0;
  } else if (in >= 0x0080u && in <= 0x07ffu) {
    *out = 0xc0 | (in >> 6);
    out++;
    *out = 0x80 | (in & (0xff >> 2));
    return 0;
  } else if (in >= 0x0800u) {
    *out = 0xe0 | (in >> 12);
    out++;
    *out = 0x80 | (in >> 6 & 0x003f);
    out++;
    *out = 0x80 | (in & (0xff >> 2));
    return 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<_encoder_ucs2_to_utf8>:
push   %rbp
mov    %rsp,%rbp
mov    %di,%ax
mov    %ax,-0x6(%rbp)
mov    %rsi,-0x10(%rbp)
movzwl -0x6(%rbp),%eax
cmp    $0x7f,%eax
ja     1134 <_encoder_ucs2_to_utf8+0x34>
mov    -0x6(%rbp),%ax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1215 <_encoder_ucs2_to_utf8+0x115>
movzwl -0x6(%rbp),%eax
cmp    $0x80,%eax
jb     1192 <_encoder_ucs2_to_utf8+0x92>
movzwl -0x6(%rbp),%eax
cmp    $0x7ff,%eax
ja     1192 <_encoder_ucs2_to_utf8+0x92>
movzwl -0x6(%rbp),%eax
sar    $0x6,%eax
or     $0xc0,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
movzwl -0x6(%rbp),%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1215 <_encoder_ucs2_to_utf8+0x115>
movzwl -0x6(%rbp),%eax
cmp    $0x800,%eax
jb     1204 <_encoder_ucs2_to_utf8+0x104>
movzwl -0x6(%rbp),%eax
sar    $0xc,%eax
or     $0xe0,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
movzwl -0x6(%rbp),%eax
sar    $0x6,%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
movzwl -0x6(%rbp),%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1215 <_encoder_ucs2_to_utf8+0x115>
jmp    1209 <_encoder_ucs2_to_utf8+0x109>
jmp    120e <_encoder_ucs2_to_utf8+0x10e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<_encoder_ucs2_to_utf8>:
cmp    $0x7f,%di
ja     110c <_encoder_ucs2_to_utf8+0xc>
mov    %dil,(%rsi)
xor    %eax,%eax
ret
movzwl %di,%eax
cmp    $0x7ff,%eax
ja     112e <_encoder_ucs2_to_utf8+0x2e>
mov    %edi,%eax
shr    $0x6,%eax
or     $0xc0,%al
mov    %al,(%rsi)
and    $0x3f,%dil
or     $0x80,%dil
mov    %dil,0x1(%rsi)
xor    %eax,%eax
ret
mov    %eax,%ecx
shr    $0xc,%ecx
or     $0xe0,%cl
mov    %cl,(%rsi)
shr    $0x6,%eax
and    $0x3f,%al
or     $0x80,%al
mov    %al,0x1(%rsi)
and    $0x3f,%dil
or     $0x80,%dil
mov    %dil,0x2(%rsi)
xor    %eax,%eax
ret

LLVM IR (-O0):
define i32 @_encoder_ucs2_to_utf8(i16 zeroext %0, i8* %1) {
%3 = alloca i32
%4 = alloca i16
%5 = alloca i8*
store i16 %0, i16* %4
store i8* %1, i8** %5
%6 = load i16, i16* %4
%7 = zext i16 %6 to i32
%8 = icmp ule i32 %7, 127
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i16, i16* %4
%11 = trunc i16 %10 to i8
%12 = load i8*, i8** %5
store i8 %11, i8* %12
store i32 0, i32* %3
br label %67
13: ; preds = %2
%14 = load i16, i16* %4
%15 = zext i16 %14 to i32
%16 = icmp uge i32 %15, 128
br i1 %16, label %17, label %36
17: ; preds = %13
%18 = load i16, i16* %4
%19 = zext i16 %18 to i32
%20 = icmp ule i32 %19, 2047
br i1 %20, label %21, label %36
21: ; preds = %17
%22 = load i16, i16* %4
%23 = zext i16 %22 to i32
%24 = ashr i32 %23, 6
%25 = or i32 192, %24
%26 = trunc i32 %25 to i8
%27 = load i8*, i8** %5
store i8 %26, i8* %27
%28 = load i8*, i8** %5
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %5
%30 = load i16, i16* %4
%31 = zext i16 %30 to i32
%32 = and i32 %31, 63
%33 = or i32 128, %32
%34 = trunc i32 %33 to i8
%35 = load i8*, i8** %5
store i8 %34, i8* %35
store i32 0, i32* %3
br label %67
36: ; preds = %17, %13
%37 = load i16, i16* %4
%38 = zext i16 %37 to i32
%39 = icmp uge i32 %38, 2048
br i1 %39, label %40, label %64
40: ; preds = %36
%41 = load i16, i16* %4
%42 = zext i16 %41 to i32
%43 = ashr i32 %42, 12
%44 = or i32 224, %43
%45 = trunc i32 %44 to i8
%46 = load i8*, i8** %5
store i8 %45, i8* %46
%47 = load i8*, i8** %5
%48 = getelementptr inbounds i8, i8* %47, i32 1
store i8* %48, i8** %5
%49 = load i16, i16* %4
%50 = zext i16 %49 to i32
%51 = ashr i32 %50, 6
%52 = and i32 %51, 63
%53 = or i32 128, %52
%54 = trunc i32 %53 to i8
%55 = load i8*, i8** %5
store i8 %54, i8* %55
%56 = load i8*, i8** %5
%57 = getelementptr inbounds i8, i8* %56, i32 1
store i8* %57, i8** %5
%58 = load i16, i16* %4
%59 = zext i16 %58 to i32
%60 = and i32 %59, 63
%61 = or i32 128, %60
%62 = trunc i32 %61 to i8
%63 = load i8*, i8** %5
store i8 %62, i8* %63
store i32 0, i32* %3
br label %67
64: ; preds = %36
br label %65
65: ; preds = %64
br label %66
66: ; preds = %65
store i32 0, i32* %3
br label %67
67: ; preds = %66, %40, %21, %9
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
define i32 @_encoder_ucs2_to_utf8(i16 zeroext %0, i8* nocapture writeonly %1) {
%3 = icmp ult i16 %0, 128
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = trunc i16 %0 to i8
store i8 %5, i8* %1
br label %29
6: ; preds = %2
%7 = icmp ult i16 %0, 2048
br i1 %7, label %8, label %16
8: ; preds = %6
%9 = lshr i16 %0, 6
%10 = trunc i16 %9 to i8
%11 = or i8 %10, -64
store i8 %11, i8* %1
%12 = getelementptr inbounds i8, i8* %1, i64 1
%13 = trunc i16 %0 to i8
%14 = and i8 %13, 63
%15 = or i8 %14, -128
store i8 %15, i8* %12
br label %29
16: ; preds = %6
%17 = lshr i16 %0, 12
%18 = trunc i16 %17 to i8
%19 = or i8 %18, -32
store i8 %19, i8* %1
%20 = getelementptr inbounds i8, i8* %1, i64 1
%21 = lshr i16 %0, 6
%22 = trunc i16 %21 to i8
%23 = and i8 %22, 63
%24 = or i8 %23, -128
store i8 %24, i8* %20
%25 = getelementptr inbounds i8, i8* %1, i64 2
%26 = trunc i16 %0 to i8
%27 = and i8 %26, 63
%28 = or i8 %27, -128
store i8 %28, i8* %25
br label %29
29: ; preds = %16, %8, %4
ret i32 0
}