#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 */

/* Variables and functions */
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int UCS2UTF16(unsigned int ucs, char *dest) {
  char *p = dest;
  if (ucs <= 0xFFFF) {
    if (!p)
      return 2;
    *p++ = ucs;
    *p++ = 0;
  } else if (ucs <= 0x10FFFF) {
    if (!p)
      return 4;
    ucs -= 0x10000;
    unsigned short us = (ucs & 0x3FF) | 0xDC00;
    *p++ = us >> 8;
    *p++ = us & 0xFF;
    us = (ucs >> 10) | 0xD800;
    *p++ = us >> 8;
    *p++ = us & 0xFF;
  } else {
    assert(0);
  }
  return p - dest;
}


x86 assembly (clang, -O0):
<UCS2UTF16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0xffff,-0x8(%rbp)
ja     1178 <UCS2UTF16+0x68>
cmpq   $0x0,-0x18(%rbp)
jne    114b <UCS2UTF16+0x3b>
movl   $0x2,-0x4(%rbp)
jmp    1252 <UCS2UTF16+0x142>
mov    -0x8(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x0,(%rax)
jmp    1244 <UCS2UTF16+0x134>
cmpl   $0x10ffff,-0x8(%rbp)
ja     1238 <UCS2UTF16+0x128>
cmpq   $0x0,-0x18(%rbp)
jne    119c <UCS2UTF16+0x8c>
movl   $0x4,-0x4(%rbp)
jmp    1252 <UCS2UTF16+0x142>
mov    -0x8(%rbp),%eax
sub    $0x10000,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
and    $0x3ff,%eax
or     $0xdc00,%eax
mov    %ax,-0x1a(%rbp)
movzwl -0x1a(%rbp),%eax
sar    $0x8,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
movzwl -0x1a(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%eax
shr    $0xa,%eax
or     $0xd800,%eax
mov    %ax,-0x1a(%rbp)
movzwl -0x1a(%rbp),%eax
sar    $0x8,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
movzwl -0x1a(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    123f <UCS2UTF16+0x12f>
xor    %edi,%edi
call   1030 <assert@plt>
jmp    1244 <UCS2UTF16+0x134>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<UCS2UTF16>:
push   %rbx
mov    %rsi,%rbx
cmp    $0xffff,%edi
ja     112e <UCS2UTF16+0x1e>
test   %rbx,%rbx
je     1176 <UCS2UTF16+0x66>
mov    %dil,(%rbx)
lea    0x2(%rbx),%rax
movb   $0x0,0x1(%rbx)
jmp    1172 <UCS2UTF16+0x62>
cmp    $0x10ffff,%edi
ja     1168 <UCS2UTF16+0x58>
test   %rbx,%rbx
je     117d <UCS2UTF16+0x6d>
lea    0xff0000(%rdi),%eax
mov    %edi,%ecx
shr    $0x8,%ecx
and    $0x3,%cl
or     $0xdc,%cl
mov    %cl,(%rbx)
mov    %dil,0x1(%rbx)
mov    %eax,%ecx
shr    $0xa,%ecx
shr    $0x12,%eax
or     $0xd8,%al
mov    %al,0x2(%rbx)
lea    0x4(%rbx),%rax
mov    %cl,0x3(%rbx)
jmp    1172 <UCS2UTF16+0x62>
xor    %edi,%edi
call   1030 <assert@plt>
mov    %rbx,%rax
sub    %ebx,%eax
pop    %rbx
ret
mov    $0x2,%eax
pop    %rbx
ret
mov    $0x4,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @UCS2UTF16(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i16
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
store i8* %8, i8** %6
%9 = load i32, i32* %4
%10 = icmp ule i32 %9, 65535
br i1 %10, label %11, label %22
11: ; preds = %2
%12 = load i8*, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %15, label %14
14: ; preds = %11
store i32 2, i32* %3
br label %74
15: ; preds = %11
%16 = load i32, i32* %4
%17 = trunc i32 %16 to i8
%18 = load i8*, i8** %6
%19 = getelementptr inbounds i8, i8* %18, i32 1
store i8* %19, i8** %6
store i8 %17, i8* %18
%20 = load i8*, i8** %6
%21 = getelementptr inbounds i8, i8* %20, i32 1
store i8* %21, i8** %6
store i8 0, i8* %20
br label %67
22: ; preds = %2
%23 = load i32, i32* %4
%24 = icmp ule i32 %23, 1114111
br i1 %24, label %25, label %64
25: ; preds = %22
%26 = load i8*, i8** %6
%27 = icmp ne i8* %26, null
br i1 %27, label %29, label %28
28: ; preds = %25
store i32 4, i32* %3
br label %74
29: ; preds = %25
%30 = load i32, i32* %4
%31 = sub i32 %30, 65536
store i32 %31, i32* %4
%32 = load i32, i32* %4
%33 = and i32 %32, 1023
%34 = or i32 %33, 56320
%35 = trunc i32 %34 to i16
store i16 %35, i16* %7
%36 = load i16, i16* %7
%37 = zext i16 %36 to i32
%38 = ashr i32 %37, 8
%39 = trunc i32 %38 to i8
%40 = load i8*, i8** %6
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %6
store i8 %39, i8* %40
%42 = load i16, i16* %7
%43 = zext i16 %42 to i32
%44 = and i32 %43, 255
%45 = trunc i32 %44 to i8
%46 = load i8*, i8** %6
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %6
store i8 %45, i8* %46
%48 = load i32, i32* %4
%49 = lshr i32 %48, 10
%50 = or i32 %49, 55296
%51 = trunc i32 %50 to i16
store i16 %51, i16* %7
%52 = load i16, i16* %7
%53 = zext i16 %52 to i32
%54 = ashr i32 %53, 8
%55 = trunc i32 %54 to i8
%56 = load i8*, i8** %6
%57 = getelementptr inbounds i8, i8* %56, i32 1
store i8* %57, i8** %6
store i8 %55, i8* %56
%58 = load i16, i16* %7
%59 = zext i16 %58 to i32
%60 = and i32 %59, 255
%61 = trunc i32 %60 to i8
%62 = load i8*, i8** %6
%63 = getelementptr inbounds i8, i8* %62, i32 1
store i8* %63, i8** %6
store i8 %61, i8* %62
br label %66
64: ; preds = %22
%65 = call i32 @assert(i32 0)
br label %66
66: ; preds = %64, %29
br label %67
67: ; preds = %66, %15
%68 = load i8*, i8** %6
%69 = load i8*, i8** %5
%70 = ptrtoint i8* %68 to i64
%71 = ptrtoint i8* %69 to i64
%72 = sub i64 %70, %71
%73 = trunc i64 %72 to i32
store i32 %73, i32* %3
br label %74
74: ; preds = %67, %28, %14
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
define i32 @UCS2UTF16(i32 %0, i8* %1) {
%3 = icmp ult i32 %0, 65536
br i1 %3, label %4, label %10
4: ; preds = %2
%5 = icmp eq i8* %1, null
br i1 %5, label %38, label %6
6: ; preds = %4
%7 = trunc i32 %0 to i8
%8 = getelementptr inbounds i8, i8* %1, i64 1
store i8 %7, i8* %1
%9 = getelementptr inbounds i8, i8* %1, i64 2
store i8 0, i8* %8
br label %32
10: ; preds = %2
%11 = icmp ult i32 %0, 1114112
br i1 %11, label %12, label %30
12: ; preds = %10
%13 = icmp eq i8* %1, null
br i1 %13, label %38, label %14
14: ; preds = %12
%15 = add nuw nsw i32 %0, 16711680
%16 = lshr i32 %0, 8
%17 = trunc i32 %16 to i8
%18 = and i8 %17, 3
%19 = or i8 %18, -36
%20 = getelementptr inbounds i8, i8* %1, i64 1
store i8 %19, i8* %1
%21 = trunc i32 %0 to i8
%22 = getelementptr inbounds i8, i8* %1, i64 2
store i8 %21, i8* %20
%23 = lshr i32 %15, 10
%24 = lshr i32 %15, 18
%25 = trunc i32 %24 to i8
%26 = or i8 %25, -40
%27 = getelementptr inbounds i8, i8* %1, i64 3
store i8 %26, i8* %22
%28 = trunc i32 %23 to i8
%29 = getelementptr inbounds i8, i8* %1, i64 4
store i8 %28, i8* %27
br label %32
30: ; preds = %10
%31 = tail call i32 @assert(i32 0)
br label %32
32: ; preds = %14, %30, %6
%33 = phi i8* [ %9, %6 ], [ %29, %14 ], [ %1, %30 ]
%34 = ptrtoint i8* %33 to i64
%35 = ptrtoint i8* %1 to i64
%36 = sub i64 %34, %35
%37 = trunc i64 %36 to i32
br label %38
38: ; preds = %12, %4, %32
%39 = phi i32 [ %37, %32 ], [ 2, %4 ], [ 4, %12 ]
ret i32 %39
}