#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 uint8_t;
typedef int uint16_t;
typedef int ucs2;

/* Variables and functions */
int ucs2utf(uint16_t ucs2, uint8_t *utf8) {
  if ((ucs2 & (~0x7F)) == 0) {
    *utf8 = ucs2;
    return 1;
  }
  if ((ucs2 & 0xF800) == 0) {
    *utf8++ = 0xC0 | (ucs2 >> 6);
    *utf8++ = 0x80 | (ucs2 & 0x3F);
    return 2;
  } else {
    *utf8++ = 0xE0 | (ucs2 >> 12);
    *utf8++ = 0x80 | ((ucs2 >> 6) & 0x3F);
    *utf8++ = 0x80 | ((ucs2)&0x3F);
    return 3;
  }
}


x86 assembly (clang, -O0):
<ucs2utf>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%eax
and    $0xffffff80,%eax
cmp    $0x0,%eax
jne    112f <ucs2utf+0x2f>
mov    -0x8(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    11e7 <ucs2utf+0xe7>
mov    -0x8(%rbp),%eax
and    $0xf800,%eax
cmp    $0x0,%eax
jne    1186 <ucs2utf+0x86>
mov    -0x8(%rbp),%ecx
sar    $0x6,%ecx
or     $0xc0,%ecx
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x10(%rbp)
mov    %ecx,(%rax)
mov    -0x8(%rbp),%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x10(%rbp)
mov    %ecx,(%rax)
movl   $0x2,-0x4(%rbp)
jmp    11e7 <ucs2utf+0xe7>
mov    -0x8(%rbp),%ecx
sar    $0xc,%ecx
or     $0xe0,%ecx
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x10(%rbp)
mov    %ecx,(%rax)
mov    -0x8(%rbp),%ecx
sar    $0x6,%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x10(%rbp)
mov    %ecx,(%rax)
mov    -0x8(%rbp),%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x10(%rbp)
mov    %ecx,(%rax)
movl   $0x3,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<ucs2utf>:
cmp    $0x7f,%edi
ja     110d <ucs2utf+0xd>
mov    %edi,(%rsi)
mov    $0x1,%eax
ret
mov    %edi,%eax
test   $0xf800,%edi
je     1143 <ucs2utf+0x43>
sar    $0xc,%eax
or     $0xe0,%eax
mov    %eax,(%rsi)
mov    %edi,%eax
shr    $0x6,%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %eax,0x4(%rsi)
and    $0x3f,%edi
or     $0x80,%edi
mov    %edi,0x8(%rsi)
mov    $0x3,%eax
ret
sar    $0x6,%eax
or     $0xc0,%eax
mov    %eax,(%rsi)
and    $0x3f,%edi
or     $0x80,%edi
mov    %edi,0x4(%rsi)
mov    $0x2,%eax
ret

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

LLVM IR (-O2):
define i32 @ucs2utf(i32 %0, i32* nocapture writeonly %1) {
%3 = icmp ult i32 %0, 128
br i1 %3, label %4, label %5
4: ; preds = %2
store i32 %0, i32* %1
br label %24
5: ; preds = %2
%6 = and i32 %0, 63488
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %5
%9 = ashr i32 %0, 6
%10 = or i32 %9, 192
%11 = getelementptr inbounds i32, i32* %1, i64 1
store i32 %10, i32* %1
%12 = and i32 %0, 63
%13 = or i32 %12, 128
store i32 %13, i32* %11
br label %24
14: ; preds = %5
%15 = ashr i32 %0, 12
%16 = or i32 %15, 224
%17 = getelementptr inbounds i32, i32* %1, i64 1
store i32 %16, i32* %1
%18 = lshr i32 %0, 6
%19 = and i32 %18, 63
%20 = or i32 %19, 128
%21 = getelementptr inbounds i32, i32* %1, i64 2
store i32 %20, i32* %17
%22 = and i32 %0, 63
%23 = or i32 %22, 128
store i32 %23, i32* %21
br label %24
24: ; preds = %14, %8, %4
%25 = phi i32 [ 1, %4 ], [ 2, %8 ], [ 3, %14 ]
ret i32 %25
}