#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 UINT32_C(int);
int Utf16Encoder_EncodeCharacter(uint32_t ch, uint16_t *out) {
  if (ch <= UINT32_C(0xFFFF)) {
    if (ch >= UINT32_C(0xD800) && ch <= UINT32_C(0xDFFF)) {
      return 0;
    }
    out[0] = ch;
    return 1;
  }
  if (ch <= UINT32_C(0x10FFFF)) {
    uint32_t x = ch - UINT32_C(0x10000);
    out[0] = UINT32_C(0xD800) + (x >> 10);
    out[1] = UINT32_C(0xDC00) + (x & UINT32_C(0x3FF));
    return 2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<Utf16Encoder_EncodeCharacter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0xffff,%edi
call   1030 <UINT32_C@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
cmp    %ecx,%eax
jg     1197 <Utf16Encoder_EncodeCharacter+0x87>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    $0xd800,%edi
call   1030 <UINT32_C@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
cmp    %ecx,%eax
jl     1182 <Utf16Encoder_EncodeCharacter+0x72>
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    $0xdfff,%edi
call   1030 <UINT32_C@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cmp    %ecx,%eax
jg     1182 <Utf16Encoder_EncodeCharacter+0x72>
movl   $0x0,-0x4(%rbp)
jmp    122b <Utf16Encoder_EncodeCharacter+0x11b>
mov    -0x8(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    122b <Utf16Encoder_EncodeCharacter+0x11b>
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    $0x10ffff,%edi
call   1030 <UINT32_C@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
jg     1224 <Utf16Encoder_EncodeCharacter+0x114>
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    $0x10000,%edi
call   1030 <UINT32_C@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    $0xd800,%edi
call   1030 <UINT32_C@plt>
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
sar    $0xa,%eax
add    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    $0xdc00,%edi
call   1030 <UINT32_C@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    $0x3ff,%edi
call   1030 <UINT32_C@plt>
mov    -0x2c(%rbp),%ecx
mov    %eax,%edx
mov    -0x28(%rbp),%eax
and    %edx,%eax
add    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
movl   $0x2,-0x4(%rbp)
jmp    122b <Utf16Encoder_EncodeCharacter+0x11b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Utf16Encoder_EncodeCharacter>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
mov    $0xffff,%edi
call   1030 <UINT32_C@plt>
cmp    %ebp,%eax
jge    117e <Utf16Encoder_EncodeCharacter+0x6e>
mov    $0x10ffff,%edi
call   1030 <UINT32_C@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    %ebp,%ecx
jl     11a5 <Utf16Encoder_EncodeCharacter+0x95>
mov    $0x10000,%edi
call   1030 <UINT32_C@plt>
sub    %eax,%ebp
mov    $0xd800,%edi
call   1030 <UINT32_C@plt>
mov    %ebp,%ecx
sar    $0xa,%ecx
add    %eax,%ecx
mov    %ecx,(%rbx)
mov    $0xdc00,%edi
call   1030 <UINT32_C@plt>
mov    %eax,%r14d
mov    $0x3ff,%edi
call   1030 <UINT32_C@plt>
and    %ebp,%eax
add    %r14d,%eax
mov    %eax,0x4(%rbx)
mov    $0x2,%eax
jmp    11a5 <Utf16Encoder_EncodeCharacter+0x95>
mov    $0xd800,%edi
call   1030 <UINT32_C@plt>
cmp    %ebp,%eax
jg     119e <Utf16Encoder_EncodeCharacter+0x8e>
mov    $0xdfff,%edi
call   1030 <UINT32_C@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    %ebp,%ecx
jge    11a5 <Utf16Encoder_EncodeCharacter+0x95>
mov    %ebp,(%rbx)
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @Utf16Encoder_EncodeCharacter(i32 %0, i32* nocapture writeonly %1) {
%3 = tail call i32 @UINT32_C(i32 65535)
%4 = icmp slt i32 %3, %0
br i1 %4, label %12, label %5
5: ; preds = %2
%6 = tail call i32 @UINT32_C(i32 55296)
%7 = icmp sgt i32 %6, %0
br i1 %7, label %11, label %8
8: ; preds = %5
%9 = tail call i32 @UINT32_C(i32 57343)
%10 = icmp slt i32 %9, %0
br i1 %10, label %11, label %26
11: ; preds = %8, %5
store i32 %0, i32* %1
br label %26
12: ; preds = %2
%13 = tail call i32 @UINT32_C(i32 1114111)
%14 = icmp slt i32 %13, %0
br i1 %14, label %26, label %15
15: ; preds = %12
%16 = tail call i32 @UINT32_C(i32 65536)
%17 = sub nsw i32 %0, %16
%18 = tail call i32 @UINT32_C(i32 55296)
%19 = ashr i32 %17, 10
%20 = add nsw i32 %19, %18
store i32 %20, i32* %1
%21 = tail call i32 @UINT32_C(i32 56320)
%22 = tail call i32 @UINT32_C(i32 1023)
%23 = and i32 %22, %17
%24 = add nsw i32 %23, %21
%25 = getelementptr inbounds i32, i32* %1, i64 1
store i32 %24, i32* %25
br label %26
26: ; preds = %12, %8, %15, %11
%27 = phi i32 [ 1, %11 ], [ 2, %15 ], [ 0, %8 ], [ 0, %12 ]
ret i32 %27
}