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

/* Variables and functions */
int UINT32_C(int);
int Utf8Encoder_EncodeCharacter(uint32_t ch, uint8_t *out) {
  if (ch <= UINT32_C(0x007F)) {
    out[0] = ch;
    return 1;
  }
  if (ch <= UINT32_C(0x07FF)) {
    out[0] = (0xC0 | (ch >> 6));
    out[1] = (0x80 | ((ch >> 0) & 0x3F));
    return 2;
  }
  if (ch <= UINT32_C(0xFFFF)) {
    if (ch >= UINT32_C(0xD800) && ch <= UINT32_C(0xDFFF)) {
      return 0;
    }
    out[0] = (0xE0 | (ch >> 12));
    out[1] = (0x80 | ((ch >> 6) & 0x3F));
    out[2] = (0x80 | ((ch >> 0) & 0x3F));
    return 3;
  }
  if (ch < UINT32_C(0x10FFFF)) {
    out[0] = (0xF0 | (ch >> 18));
    out[1] = (0x80 | ((ch >> 12) & 0x3F));
    out[2] = (0x80 | ((ch >> 6) & 0x3F));
    out[3] = (0x80 | ((ch >> 0) & 0x3F));
    return 4;
  }
  return 0;
}


x86 assembly (clang, -O0):
<Utf8Encoder_EncodeCharacter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    $0x7f,%edi
call   1030 <UINT32_C@plt>
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
cmp    %ecx,%eax
jg     1151 <Utf8Encoder_EncodeCharacter+0x41>
mov    -0x8(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    12d3 <Utf8Encoder_EncodeCharacter+0x1c3>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x7ff,%edi
call   1030 <UINT32_C@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
cmp    %ecx,%eax
jg     11a2 <Utf8Encoder_EncodeCharacter+0x92>
mov    -0x8(%rbp),%ecx
sar    $0x6,%ecx
or     $0xc0,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%ecx
sar    $0x0,%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
movl   $0x2,-0x4(%rbp)
jmp    12d3 <Utf8Encoder_EncodeCharacter+0x1c3>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    $0xffff,%edi
call   1030 <UINT32_C@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
cmp    %ecx,%eax
jg     124f <Utf8Encoder_EncodeCharacter+0x13f>
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    $0xd800,%edi
call   1030 <UINT32_C@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cmp    %ecx,%eax
jl     1205 <Utf8Encoder_EncodeCharacter+0xf5>
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    $0xdfff,%edi
call   1030 <UINT32_C@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
jg     1205 <Utf8Encoder_EncodeCharacter+0xf5>
movl   $0x0,-0x4(%rbp)
jmp    12d3 <Utf8Encoder_EncodeCharacter+0x1c3>
mov    -0x8(%rbp),%ecx
sar    $0xc,%ecx
or     $0xe0,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%ecx
sar    $0x6,%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x8(%rbp),%ecx
sar    $0x0,%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
movl   $0x3,-0x4(%rbp)
jmp    12d3 <Utf8Encoder_EncodeCharacter+0x1c3>
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    $0x10ffff,%edi
call   1030 <UINT32_C@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
cmp    %ecx,%eax
jge    12cc <Utf8Encoder_EncodeCharacter+0x1bc>
mov    -0x8(%rbp),%ecx
sar    $0x12,%ecx
or     $0xf0,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%ecx
sar    $0xc,%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x8(%rbp),%ecx
sar    $0x6,%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x8(%rbp),%ecx
sar    $0x0,%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0xc(%rax)
movl   $0x4,-0x4(%rbp)
jmp    12d3 <Utf8Encoder_EncodeCharacter+0x1c3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Utf8Encoder_EncodeCharacter>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    $0x7f,%edi
call   1030 <UINT32_C@plt>
cmp    %ebp,%eax
jge    1197 <Utf8Encoder_EncodeCharacter+0x87>
mov    $0x7ff,%edi
call   1030 <UINT32_C@plt>
cmp    %ebp,%eax
jge    11a0 <Utf8Encoder_EncodeCharacter+0x90>
mov    $0xffff,%edi
call   1030 <UINT32_C@plt>
cmp    %ebp,%eax
jge    11bf <Utf8Encoder_EncodeCharacter+0xaf>
mov    $0x10ffff,%edi
call   1030 <UINT32_C@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    %ebp,%ecx
jle    120c <Utf8Encoder_EncodeCharacter+0xfc>
mov    %ebp,%eax
sar    $0x12,%eax
or     $0xf0,%eax
mov    %eax,(%rbx)
mov    %ebp,%eax
shr    $0xc,%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %eax,0x4(%rbx)
mov    %ebp,%eax
shr    $0x6,%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %eax,0x8(%rbx)
and    $0x3f,%ebp
or     $0x80,%ebp
mov    %ebp,0xc(%rbx)
mov    $0x4,%eax
jmp    120c <Utf8Encoder_EncodeCharacter+0xfc>
mov    %ebp,(%rbx)
mov    $0x1,%eax
jmp    120c <Utf8Encoder_EncodeCharacter+0xfc>
mov    %ebp,%eax
sar    $0x6,%eax
or     $0xc0,%eax
mov    %eax,(%rbx)
and    $0x3f,%ebp
or     $0x80,%ebp
mov    %ebp,0x4(%rbx)
mov    $0x2,%eax
jmp    120c <Utf8Encoder_EncodeCharacter+0xfc>
mov    $0xd800,%edi
call   1030 <UINT32_C@plt>
cmp    %ebp,%eax
jg     11df <Utf8Encoder_EncodeCharacter+0xcf>
mov    $0xdfff,%edi
call   1030 <UINT32_C@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    %ebp,%ecx
jge    120c <Utf8Encoder_EncodeCharacter+0xfc>
mov    %ebp,%eax
sar    $0xc,%eax
or     $0xe0,%eax
mov    %eax,(%rbx)
mov    %ebp,%eax
shr    $0x6,%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %eax,0x4(%rbx)
and    $0x3f,%ebp
or     $0x80,%ebp
mov    %ebp,0x8(%rbx)
mov    $0x3,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @Utf8Encoder_EncodeCharacter(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
store i32 %0, i32* %4
store i32* %1, i32** %5
%6 = load i32, i32* %4
%7 = call i32 @UINT32_C(i32 127)
%8 = icmp sle i32 %6, %7
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i32, i32* %4
%11 = load i32*, i32** %5
%12 = getelementptr inbounds i32, i32* %11, i64 0
store i32 %10, i32* %12
store i32 1, i32* %3
br label %89
13: ; preds = %2
%14 = load i32, i32* %4
%15 = call i32 @UINT32_C(i32 2047)
%16 = icmp sle i32 %14, %15
br i1 %16, label %17, label %29
17: ; preds = %13
%18 = load i32, i32* %4
%19 = ashr i32 %18, 6
%20 = or i32 192, %19
%21 = load i32*, i32** %5
%22 = getelementptr inbounds i32, i32* %21, i64 0
store i32 %20, i32* %22
%23 = load i32, i32* %4
%24 = ashr i32 %23, 0
%25 = and i32 %24, 63
%26 = or i32 128, %25
%27 = load i32*, i32** %5
%28 = getelementptr inbounds i32, i32* %27, i64 1
store i32 %26, i32* %28
store i32 2, i32* %3
br label %89
29: ; preds = %13
%30 = load i32, i32* %4
%31 = call i32 @UINT32_C(i32 65535)
%32 = icmp sle i32 %30, %31
br i1 %32, label %33, label %60
33: ; preds = %29
%34 = load i32, i32* %4
%35 = call i32 @UINT32_C(i32 55296)
%36 = icmp sge i32 %34, %35
br i1 %36, label %37, label %42
37: ; preds = %33
%38 = load i32, i32* %4
%39 = call i32 @UINT32_C(i32 57343)
%40 = icmp sle i32 %38, %39
br i1 %40, label %41, label %42
41: ; preds = %37
store i32 0, i32* %3
br label %89
42: ; preds = %37, %33
%43 = load i32, i32* %4
%44 = ashr i32 %43, 12
%45 = or i32 224, %44
%46 = load i32*, i32** %5
%47 = getelementptr inbounds i32, i32* %46, i64 0
store i32 %45, i32* %47
%48 = load i32, i32* %4
%49 = ashr i32 %48, 6
%50 = and i32 %49, 63
%51 = or i32 128, %50
%52 = load i32*, i32** %5
%53 = getelementptr inbounds i32, i32* %52, i64 1
store i32 %51, i32* %53
%54 = load i32, i32* %4
%55 = ashr i32 %54, 0
%56 = and i32 %55, 63
%57 = or i32 128, %56
%58 = load i32*, i32** %5
%59 = getelementptr inbounds i32, i32* %58, i64 2
store i32 %57, i32* %59
store i32 3, i32* %3
br label %89
60: ; preds = %29
%61 = load i32, i32* %4
%62 = call i32 @UINT32_C(i32 1114111)
%63 = icmp slt i32 %61, %62
br i1 %63, label %64, label %88
64: ; preds = %60
%65 = load i32, i32* %4
%66 = ashr i32 %65, 18
%67 = or i32 240, %66
%68 = load i32*, i32** %5
%69 = getelementptr inbounds i32, i32* %68, i64 0
store i32 %67, i32* %69
%70 = load i32, i32* %4
%71 = ashr i32 %70, 12
%72 = and i32 %71, 63
%73 = or i32 128, %72
%74 = load i32*, i32** %5
%75 = getelementptr inbounds i32, i32* %74, i64 1
store i32 %73, i32* %75
%76 = load i32, i32* %4
%77 = ashr i32 %76, 6
%78 = and i32 %77, 63
%79 = or i32 128, %78
%80 = load i32*, i32** %5
%81 = getelementptr inbounds i32, i32* %80, i64 2
store i32 %79, i32* %81
%82 = load i32, i32* %4
%83 = ashr i32 %82, 0
%84 = and i32 %83, 63
%85 = or i32 128, %84
%86 = load i32*, i32** %5
%87 = getelementptr inbounds i32, i32* %86, i64 3
store i32 %85, i32* %87
store i32 4, i32* %3
br label %89
88: ; preds = %60
store i32 0, i32* %3
br label %89
89: ; preds = %88, %64, %42, %41, %17, %9
%90 = load i32, i32* %3
ret i32 %90
}

LLVM IR (-O2):
define i32 @Utf8Encoder_EncodeCharacter(i32 %0, i32* nocapture writeonly %1) {
%3 = tail call i32 @UINT32_C(i32 127)
%4 = icmp slt i32 %3, %0
br i1 %4, label %6, label %5
5: ; preds = %2
store i32 %0, i32* %1
br label %51
6: ; preds = %2
%7 = tail call i32 @UINT32_C(i32 2047)
%8 = icmp slt i32 %7, %0
br i1 %8, label %15, label %9
9: ; preds = %6
%10 = ashr i32 %0, 6
%11 = or i32 %10, 192
store i32 %11, i32* %1
%12 = and i32 %0, 63
%13 = or i32 %12, 128
%14 = getelementptr inbounds i32, i32* %1, i64 1
store i32 %13, i32* %14
br label %51
15: ; preds = %6
%16 = tail call i32 @UINT32_C(i32 65535)
%17 = icmp slt i32 %16, %0
br i1 %17, label %34, label %18
18: ; preds = %15
%19 = tail call i32 @UINT32_C(i32 55296)
%20 = icmp sgt i32 %19, %0
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = tail call i32 @UINT32_C(i32 57343)
%23 = icmp slt i32 %22, %0
br i1 %23, label %24, label %51
24: ; preds = %21, %18
%25 = ashr i32 %0, 12
%26 = or i32 %25, 224
store i32 %26, i32* %1
%27 = lshr i32 %0, 6
%28 = and i32 %27, 63
%29 = or i32 %28, 128
%30 = getelementptr inbounds i32, i32* %1, i64 1
store i32 %29, i32* %30
%31 = and i32 %0, 63
%32 = or i32 %31, 128
%33 = getelementptr inbounds i32, i32* %1, i64 2
store i32 %32, i32* %33
br label %51
34: ; preds = %15
%35 = tail call i32 @UINT32_C(i32 1114111)
%36 = icmp sgt i32 %35, %0
br i1 %36, label %37, label %51
37: ; preds = %34
%38 = ashr i32 %0, 18
%39 = or i32 %38, 240
store i32 %39, i32* %1
%40 = lshr i32 %0, 12
%41 = and i32 %40, 63
%42 = or i32 %41, 128
%43 = getelementptr inbounds i32, i32* %1, i64 1
store i32 %42, i32* %43
%44 = lshr i32 %0, 6
%45 = and i32 %44, 63
%46 = or i32 %45, 128
%47 = getelementptr inbounds i32, i32* %1, i64 2
store i32 %46, i32* %47
%48 = and i32 %0, 63
%49 = or i32 %48, 128
%50 = getelementptr inbounds i32, i32* %1, i64 3
store i32 %49, i32* %50
br label %51
51: ; preds = %34, %21, %37, %24, %9, %5
%52 = phi i32 [ 1, %5 ], [ 2, %9 ], [ 3, %24 ], [ 4, %37 ], [ 0, %21 ], [ 0, %34 ]
ret i32 %52
}