char *to_utf8(unsigned int uc) {
  static char utfbuffer[4];
  int count = 0;
  if (uc < 0x80) {
    utfbuffer[0] = uc;
    count = 1;
  } else {
    if (uc < 0x800) {
      utfbuffer[count++] = 0xC0 | (uc >> 6);
    } else {
      utfbuffer[count++] = 0xE0 | (uc >> 12);
      utfbuffer[count++] = 0x80 | ((uc >> 6) & 0x3F);
    }
    utfbuffer[count++] = 0x80 | (uc & 0x3F);
  }
  utfbuffer[count] = 0;
  return utfbuffer;
}


x86 assembly (clang, -O0):
<to_utf8>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x80,-0x4(%rbp)
jae    1130 <to_utf8+0x30>
mov    -0x4(%rbp),%eax
mov    %al,0x2efd(%rip)
movl   $0x1,-0x8(%rbp)
jmp    11d9 <to_utf8+0xd9>
cmpl   $0x800,-0x4(%rbp)
jae    1167 <to_utf8+0x67>
mov    -0x4(%rbp),%eax
shr    $0x6,%eax
or     $0xc0,%eax
mov    %al,%dl
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x8(%rbp)
movslq %eax,%rcx
lea    0x2ec2(%rip),%rax
mov    %dl,(%rax,%rcx,1)
jmp    11b4 <to_utf8+0xb4>
mov    -0x4(%rbp),%eax
shr    $0xc,%eax
or     $0xe0,%eax
mov    %al,%dl
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x8(%rbp)
movslq %eax,%rcx
lea    0x2e98(%rip),%rax
mov    %dl,(%rax,%rcx,1)
mov    -0x4(%rbp),%eax
shr    $0x6,%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %al,%dl
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x8(%rbp)
movslq %eax,%rcx
lea    0x2e70(%rip),%rax
mov    %dl,(%rax,%rcx,1)
mov    -0x4(%rbp),%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %al,%dl
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x8(%rbp)
movslq %eax,%rcx
lea    0x2e4b(%rip),%rax
mov    %dl,(%rax,%rcx,1)
movslq -0x8(%rbp),%rcx
lea    0x2e3d(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2e32(%rip),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<to_utf8>:
cmp    $0x7f,%edi
ja     1113 <to_utf8+0x13>
mov    %dil,0x2f15(%rip)
mov    $0x1,%ecx
jmp    1166 <to_utf8+0x66>
mov    %edi,%eax
cmp    $0x7ff,%edi
ja     112f <to_utf8+0x2f>
shr    $0x6,%eax
or     $0xc0,%al
mov    %al,0x2ef9(%rip)
mov    $0x1,%ecx
jmp    114e <to_utf8+0x4e>
shr    $0xc,%eax
or     $0xe0,%al
mov    %al,0x2ee7(%rip)
mov    %edi,%eax
shr    $0x6,%eax
and    $0x3f,%al
or     $0x80,%al
mov    %al,0x2ed9(%rip)
mov    $0x2,%ecx
and    $0x3f,%dil
or     $0x80,%dil
mov    %ecx,%eax
add    $0x1,%ecx
lea    0x2ebf(%rip),%rdx
mov    %dil,(%rax,%rdx,1)
lea    0x2eb4(%rip),%rax
movb   $0x0,(%rcx,%rax,1)
ret

LLVM IR (-O0):
@to_utf8.utfbuffer = internal global [4 x i8] zeroinitializer
define i8* @to_utf8(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
%4 = load i32, i32* %2
%5 = icmp ult i32 %4, 128
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i32, i32* %2
%8 = trunc i32 %7 to i8
store i8 %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @to_utf8.utfbuffer, i64 0, i64 0)
store i32 1, i32* %3
br label %48
9: ; preds = %1
%10 = load i32, i32* %2
%11 = icmp ult i32 %10, 2048
br i1 %11, label %12, label %21
12: ; preds = %9
%13 = load i32, i32* %2
%14 = lshr i32 %13, 6
%15 = or i32 192, %14
%16 = trunc i32 %15 to i8
%17 = load i32, i32* %3
%18 = add nsw i32 %17, 1
store i32 %18, i32* %3
%19 = sext i32 %17 to i64
%20 = getelementptr inbounds [4 x i8], [4 x i8]* @to_utf8.utfbuffer, i64 0, i64 %19
store i8 %16, i8* %20
br label %39
21: ; preds = %9
%22 = load i32, i32* %2
%23 = lshr i32 %22, 12
%24 = or i32 224, %23
%25 = trunc i32 %24 to i8
%26 = load i32, i32* %3
%27 = add nsw i32 %26, 1
store i32 %27, i32* %3
%28 = sext i32 %26 to i64
%29 = getelementptr inbounds [4 x i8], [4 x i8]* @to_utf8.utfbuffer, i64 0, i64 %28
store i8 %25, i8* %29
%30 = load i32, i32* %2
%31 = lshr i32 %30, 6
%32 = and i32 %31, 63
%33 = or i32 128, %32
%34 = trunc i32 %33 to i8
%35 = load i32, i32* %3
%36 = add nsw i32 %35, 1
store i32 %36, i32* %3
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds [4 x i8], [4 x i8]* @to_utf8.utfbuffer, i64 0, i64 %37
store i8 %34, i8* %38
br label %39
39: ; preds = %21, %12
%40 = load i32, i32* %2
%41 = and i32 %40, 63
%42 = or i32 128, %41
%43 = trunc i32 %42 to i8
%44 = load i32, i32* %3
%45 = add nsw i32 %44, 1
store i32 %45, i32* %3
%46 = sext i32 %44 to i64
%47 = getelementptr inbounds [4 x i8], [4 x i8]* @to_utf8.utfbuffer, i64 0, i64 %46
store i8 %43, i8* %47
br label %48
48: ; preds = %39, %6
%49 = load i32, i32* %3
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [4 x i8], [4 x i8]* @to_utf8.utfbuffer, i64 0, i64 %50
store i8 0, i8* %51
ret i8* getelementptr inbounds ([4 x i8], [4 x i8]* @to_utf8.utfbuffer, i64 0, i64 0)
}

LLVM IR (-O2):
@to_utf8.utfbuffer = internal global [4 x i8] zeroinitializer
define i8* @to_utf8(i32 %0) {
%2 = icmp ult i32 %0, 128
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = trunc i32 %0 to i8
store i8 %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @to_utf8.utfbuffer, i64 0, i64 0)
br label %28
5: ; preds = %1
%6 = icmp ult i32 %0, 2048
br i1 %6, label %7, label %11
7: ; preds = %5
%8 = lshr i32 %0, 6
%9 = trunc i32 %8 to i8
%10 = or i8 %9, -64
store i8 %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @to_utf8.utfbuffer, i64 0, i64 0)
br label %19
11: ; preds = %5
%12 = lshr i32 %0, 12
%13 = trunc i32 %12 to i8
%14 = or i8 %13, -32
store i8 %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @to_utf8.utfbuffer, i64 0, i64 0)
%15 = lshr i32 %0, 6
%16 = trunc i32 %15 to i8
%17 = and i8 %16, 63
%18 = or i8 %17, -128
store i8 %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @to_utf8.utfbuffer, i64 0, i64 1)
br label %19
19: ; preds = %11, %7
%20 = phi i32 [ 1, %7 ], [ 2, %11 ]
%21 = trunc i32 %0 to i8
%22 = and i8 %21, 63
%23 = or i8 %22, -128
%24 = add nuw nsw i32 %20, 1
%25 = zext i32 %20 to i64
%26 = getelementptr inbounds [4 x i8], [4 x i8]* @to_utf8.utfbuffer, i64 0, i64 %25
store i8 %23, i8* %26
%27 = zext i32 %24 to i64
br label %28
28: ; preds = %19, %3
%29 = phi i64 [ 1, %3 ], [ %27, %19 ]
%30 = getelementptr inbounds [4 x i8], [4 x i8]* @to_utf8.utfbuffer, i64 0, i64 %29
store i8 0, i8* %30
ret i8* getelementptr inbounds ([4 x i8], [4 x i8]* @to_utf8.utfbuffer, i64 0, i64 0)
}