#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;

/* Variables and functions */
int /*<<< orphan*/ issurrogate(int);
size_t u16tou8(uint8_t *buf, uint16_t c) {
  if (c < 0x0080) {
    buf[0] = c;
    return 1;
  } else if (c < 0x0800) {
    buf[0] = 0xc0 | ((c >> 6) & 0x1f);
    buf[1] = 0x80 | (c & 0x3f);
    return 2;
  } else if (!issurrogate(c)) {
    buf[0] = 0xe0 | ((c >> 12) & 0xf);
    buf[1] = 0x80 | ((c >> 6) & 0x3f);
    buf[2] = 0x80 | (c & 0x3f);
    return 3;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<u16tou8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x80,-0x14(%rbp)
jge    1142 <u16tou8+0x32>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movq   $0x1,-0x8(%rbp)
jmp    11e8 <u16tou8+0xd8>
cmpl   $0x800,-0x14(%rbp)
jge    1184 <u16tou8+0x74>
mov    -0x14(%rbp),%ecx
sar    $0x6,%ecx
and    $0x1f,%ecx
or     $0xc0,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
movq   $0x2,-0x8(%rbp)
jmp    11e8 <u16tou8+0xd8>
mov    -0x14(%rbp),%edi
call   1030 <issurrogate@plt>
cmp    $0x0,%eax
jne    11e0 <u16tou8+0xd0>
mov    -0x14(%rbp),%ecx
sar    $0xc,%ecx
and    $0xf,%ecx
or     $0xe0,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%ecx
sar    $0x6,%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x14(%rbp),%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
movq   $0x3,-0x8(%rbp)
jmp    11e8 <u16tou8+0xd8>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<u16tou8>:
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
cmp    $0x7f,%esi
jg     1128 <u16tou8+0x18>
mov    %ebx,(%r14)
mov    $0x1,%eax
jmp    1193 <u16tou8+0x83>
cmp    $0x7ff,%ebx
ja     1151 <u16tou8+0x41>
mov    %ebx,%eax
shr    $0x6,%eax
or     $0xc0,%eax
mov    %eax,(%r14)
and    $0x3f,%ebx
or     $0x80,%ebx
mov    %ebx,0x4(%r14)
mov    $0x2,%eax
jmp    1193 <u16tou8+0x83>
mov    %ebx,%edi
call   1030 <issurrogate@plt>
test   %eax,%eax
je     1160 <u16tou8+0x50>
xor    %eax,%eax
jmp    1193 <u16tou8+0x83>
mov    %ebx,%eax
shr    $0xc,%eax
and    $0xf,%eax
or     $0xe0,%eax
mov    %eax,(%r14)
mov    %ebx,%eax
shr    $0x6,%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %eax,0x4(%r14)
and    $0x3f,%ebx
or     $0x80,%ebx
mov    %ebx,0x8(%r14)
mov    $0x3,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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