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

/* Variables and functions */
int unicode_reverse_byte_order_utf16(int, int);
int unicode_write_utf16_with_endianness(uint32_t code_point, char *string,
                                        bool system_endianness) {
  if (code_point <= 0xFFFF) {
    *((uint16_t *)string) =
        unicode_reverse_byte_order_utf16(code_point, !system_endianness);
    return 2;
  } else {
    uint32_t de_aug = code_point - 0x10000;
    *((uint16_t *)string) = unicode_reverse_byte_order_utf16(
        (((uint16_t)(de_aug >> 10)) & 0x3FF) | 0xD800, !system_endianness);
    *((uint16_t *)&string[2]) = unicode_reverse_byte_order_utf16(
        (((uint16_t)de_aug) & 0x3FF) | 0xDC00, !system_endianness);
    return 4;
  }
}


x86 assembly (clang, -O0):
<unicode_write_utf16_with_endianness>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0xffff,-0x8(%rbp)
jg     1159 <unicode_write_utf16_with_endianness+0x49>
mov    -0x8(%rbp),%edi
cmpl   $0x0,-0x14(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%esi
call   1030 <unicode_reverse_byte_order_utf16@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x2,-0x4(%rbp)
jmp    11c3 <unicode_write_utf16_with_endianness+0xb3>
mov    -0x8(%rbp),%eax
sub    $0x10000,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
sar    $0xa,%edi
and    $0x3ff,%edi
or     $0xd800,%edi
cmpl   $0x0,-0x14(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%esi
call   1030 <unicode_reverse_byte_order_utf16@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%edi
and    $0x3ff,%edi
or     $0xdc00,%edi
cmpl   $0x0,-0x14(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%esi
call   1030 <unicode_reverse_byte_order_utf16@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x2(%rax)
movl   $0x4,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unicode_write_utf16_with_endianness>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebx
cmp    $0xffff,%edi
jg     1137 <unicode_write_utf16_with_endianness+0x27>
xor    %esi,%esi
test   %edx,%edx
sete   %sil
mov    %ebx,%edi
call   1030 <unicode_reverse_byte_order_utf16@plt>
mov    $0x2,%ecx
jmp    117c <unicode_write_utf16_with_endianness+0x6c>
lea    0xf0000(%rbx),%edi
shr    $0xa,%edi
and    $0x3ff,%edi
or     $0xd800,%edi
xor    %ebp,%ebp
test   %edx,%edx
sete   %bpl
mov    %ebp,%esi
call   1030 <unicode_reverse_byte_order_utf16@plt>
mov    %eax,(%r14)
and    $0x3ff,%ebx
or     $0xdc00,%ebx
mov    %ebx,%edi
mov    %ebp,%esi
call   1030 <unicode_reverse_byte_order_utf16@plt>
add    $0x2,%r14
mov    $0x4,%ecx
mov    %eax,(%r14)
mov    %ecx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @unicode_write_utf16_with_endianness(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i32, i32* %5
%10 = icmp sle i32 %9, 65535
br i1 %10, label %11, label %20
11: ; preds = %3
%12 = load i32, i32* %5
%13 = load i32, i32* %7
%14 = icmp ne i32 %13, 0
%15 = xor i1 %14, true
%16 = zext i1 %15 to i32
%17 = call i32 @unicode_reverse_byte_order_utf16(i32 %12, i32 %16)
%18 = load i8*, i8** %6
%19 = bitcast i8* %18 to i32*
store i32 %17, i32* %19
store i32 2, i32* %4
br label %45
20: ; preds = %3
%21 = load i32, i32* %5
%22 = sub nsw i32 %21, 65536
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = ashr i32 %23, 10
%25 = and i32 %24, 1023
%26 = or i32 %25, 55296
%27 = load i32, i32* %7
%28 = icmp ne i32 %27, 0
%29 = xor i1 %28, true
%30 = zext i1 %29 to i32
%31 = call i32 @unicode_reverse_byte_order_utf16(i32 %26, i32 %30)
%32 = load i8*, i8** %6
%33 = bitcast i8* %32 to i32*
store i32 %31, i32* %33
%34 = load i32, i32* %8
%35 = and i32 %34, 1023
%36 = or i32 %35, 56320
%37 = load i32, i32* %7
%38 = icmp ne i32 %37, 0
%39 = xor i1 %38, true
%40 = zext i1 %39 to i32
%41 = call i32 @unicode_reverse_byte_order_utf16(i32 %36, i32 %40)
%42 = load i8*, i8** %6
%43 = getelementptr inbounds i8, i8* %42, i64 2
%44 = bitcast i8* %43 to i32*
store i32 %41, i32* %44
store i32 4, i32* %4
br label %45
45: ; preds = %20, %11
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
define i32 @unicode_write_utf16_with_endianness(i32 %0, i8* nocapture writeonly %1, i32 %2) {
%4 = icmp slt i32 %0, 65536
br i1 %4, label %5, label %9
5: ; preds = %3
%6 = icmp eq i32 %2, 0
%7 = zext i1 %6 to i32
%8 = tail call i32 @unicode_reverse_byte_order_utf16(i32 %0, i32 %7)
br label %22
9: ; preds = %3
%10 = add nuw i32 %0, 983040
%11 = lshr i32 %10, 10
%12 = and i32 %11, 1023
%13 = or i32 %12, 55296
%14 = icmp eq i32 %2, 0
%15 = zext i1 %14 to i32
%16 = tail call i32 @unicode_reverse_byte_order_utf16(i32 %13, i32 %15)
%17 = bitcast i8* %1 to i32*
store i32 %16, i32* %17
%18 = and i32 %0, 1023
%19 = or i32 %18, 56320
%20 = tail call i32 @unicode_reverse_byte_order_utf16(i32 %19, i32 %15)
%21 = getelementptr inbounds i8, i8* %1, i64 2
br label %22
22: ; preds = %9, %5
%23 = phi i8* [ %21, %9 ], [ %1, %5 ]
%24 = phi i32 [ %20, %9 ], [ %8, %5 ]
%25 = phi i32 [ 4, %9 ], [ 2, %5 ]
%26 = bitcast i8* %23 to i32*
store i32 %24, i32* %26
ret i32 %25
}