#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 */
scalar_t__ ishsurrogate(int);
scalar_t__ islsurrogate(int);
size_t u16stou8(uint8_t *buf, uint16_t hsg, uint16_t lsg) {
  if (ishsurrogate(hsg) && islsurrogate(lsg)) {
    hsg = hsg - 0xd800 + 0x40;
    lsg -= 0xdc00;
    buf[0] = 0xf0 | ((hsg >> 8) & 0x3);
    buf[1] = 0x80 | ((hsg >> 2) & 0x3f);
    buf[2] = 0x80 | (((hsg << 4) | ((lsg >> 6) & 0xf)) & 0x3f);
    buf[3] = 0x80 | (lsg & 0x3f);
    return 4;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<u16stou8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <ishsurrogate@plt>
cmp    $0x0,%rax
je     11db <u16stou8+0xbb>
mov    -0x18(%rbp),%edi
call   1040 <islsurrogate@plt>
cmp    $0x0,%rax
je     11db <u16stou8+0xbb>
mov    -0x14(%rbp),%eax
sub    $0xd800,%eax
add    $0x40,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
sub    $0xdc00,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%ecx
sar    $0x8,%ecx
and    $0x3,%ecx
or     $0xf0,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%ecx
sar    $0x2,%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x14(%rbp),%ecx
shl    $0x4,%ecx
mov    -0x18(%rbp),%eax
sar    $0x6,%eax
and    $0xf,%eax
or     %eax,%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x18(%rbp),%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0xc(%rax)
movq   $0x4,-0x8(%rbp)
jmp    11e3 <u16stou8+0xc3>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<u16stou8>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %esi,%ebx
mov    %rdi,%r14
mov    %esi,%edi
call   1030 <ishsurrogate@plt>
test   %rax,%rax
je     1197 <u16stou8+0x77>
mov    %ebp,%edi
call   1040 <islsurrogate@plt>
test   %rax,%rax
je     1197 <u16stou8+0x77>
add    $0xffff2840,%ebx
mov    %ebx,%eax
shr    $0x8,%eax
and    $0x3,%eax
or     $0xf0,%eax
mov    %eax,(%r14)
mov    %ebx,%eax
shr    $0x2,%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %eax,0x4(%r14)
shl    $0x4,%ebx
mov    %ebp,%eax
shr    $0x6,%eax
and    $0xf,%eax
and    $0x30,%ebx
add    %ebx,%eax
add    $0x80,%eax
mov    %eax,0x8(%r14)
and    $0x3f,%ebp
or     $0x80,%ebp
mov    %ebp,0xc(%r14)
mov    $0x4,%eax
jmp    1199 <u16stou8+0x79>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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