#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int u_int16_t;

/* Variables and functions */
char *bs_utf16_to_utf8(u_int16_t uc, char *s) {
  if (uc < 0x80) {
    *s++ = uc;
  } else if (uc < 0x0800) {
    *s++ = ((uc >> 6) & 31) | 0xc0;
    *s++ = (uc & 63) | 0x80;
  } else {
    *s++ = ((uc >> 12) & 15) | 0xe0;
    *s++ = ((uc >> 6) & 63) | 0x80;
    *s++ = (uc & 63) | 0x80;
  }
  *s = 0;
  return s;
}


x86 assembly (clang, -O0):
<bs_utf16_to_utf8>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x80,-0x4(%rbp)
jge    1133 <bs_utf16_to_utf8+0x33>
mov    -0x4(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    11e9 <bs_utf16_to_utf8+0xe9>
cmpl   $0x800,-0x4(%rbp)
jge    1184 <bs_utf16_to_utf8+0x84>
mov    -0x4(%rbp),%eax
sar    $0x6,%eax
and    $0x1f,%eax
or     $0xc0,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x4(%rbp),%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    11e4 <bs_utf16_to_utf8+0xe4>
mov    -0x4(%rbp),%eax
sar    $0xc,%eax
and    $0xf,%eax
or     $0xe0,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x4(%rbp),%eax
sar    $0x6,%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x4(%rbp),%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    11e9 <bs_utf16_to_utf8+0xe9>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<bs_utf16_to_utf8>:
mov    %rsi,%rax
cmp    $0x7f,%edi
jg     1113 <bs_utf16_to_utf8+0x13>
mov    %dil,(%rax)
add    $0x1,%rax
movb   $0x0,(%rax)
ret
mov    %edi,%ecx
cmp    $0x7ff,%edi
ja     1139 <bs_utf16_to_utf8+0x39>
shr    $0x6,%ecx
or     $0xc0,%cl
mov    %cl,(%rax)
and    $0x3f,%dil
or     $0x80,%dil
mov    %dil,0x1(%rax)
add    $0x2,%rax
movb   $0x0,(%rax)
ret
shr    $0xc,%ecx
and    $0xf,%cl
or     $0xe0,%cl
mov    %cl,(%rax)
mov    %edi,%ecx
shr    $0x6,%ecx
and    $0x3f,%cl
or     $0x80,%cl
mov    %cl,0x1(%rax)
and    $0x3f,%dil
or     $0x80,%dil
mov    %dil,0x2(%rax)
add    $0x3,%rax
movb   $0x0,(%rax)
ret

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

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