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

/* Variables and functions */
uint32_t OKJSONParserUniCharToUTF8(const uint32_t uniChar, uint8_t *cursor) {
  int count = 0;
  wchar_t u = uniChar;
  if (u < (uint8_t)0x80) {
    *cursor++ = (uint8_t)u;
    count++;
  } else {
    if (u < 0x0800) {
      *cursor++ = (uint8_t)0xc0 | ((uint8_t)(u >> 6));
      count++;
    } else {
      if (u > 0xffff) {
        if ((u > 0x10fe00) && (u < 0x10ff00)) {
          *cursor++ = (u - 0x10fe00);
          count++;
        } else {
          *cursor++ = (uint8_t)0xf0 | ((uint8_t)(u >> 18));
          *cursor++ = (uint8_t)0x80 | (((uint8_t)(u >> 12)) & (uint8_t)0x3f);
          count += 2;
        }
      } else {
        *cursor++ = (uint8_t)0xe0 | ((uint8_t)(u >> 12));
        count++;
      }
      *cursor++ = (uint8_t)0x80 | (((uint8_t)(u >> 6)) & (uint8_t)0x3f);
      count++;
    }
    *cursor++ = (uint8_t)0x80 | ((uint8_t)(u & (uint8_t)0x3f));
    count++;
  }
  return count;
}


x86 assembly (clang, -O0):
<OKJSONParserUniCharToUTF8>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x80,-0x18(%rbp)
jge    1147 <OKJSONParserUniCharToUTF8+0x47>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x10(%rbp)
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    128e <OKJSONParserUniCharToUTF8+0x18e>
cmpl   $0x800,-0x18(%rbp)
jge    117f <OKJSONParserUniCharToUTF8+0x7f>
mov    -0x18(%rbp),%ecx
sar    $0x6,%ecx
or     $0xc0,%ecx
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x10(%rbp)
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1268 <OKJSONParserUniCharToUTF8+0x168>
cmpl   $0xffff,-0x18(%rbp)
jle    1219 <OKJSONParserUniCharToUTF8+0x119>
cmpl   $0x10fe00,-0x18(%rbp)
jle    11ce <OKJSONParserUniCharToUTF8+0xce>
cmpl   $0x10ff00,-0x18(%rbp)
jge    11ce <OKJSONParserUniCharToUTF8+0xce>
mov    -0x18(%rbp),%ecx
sub    $0x10fe00,%ecx
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x10(%rbp)
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1214 <OKJSONParserUniCharToUTF8+0x114>
mov    -0x18(%rbp),%ecx
sar    $0x12,%ecx
or     $0xf0,%ecx
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x10(%rbp)
mov    %ecx,(%rax)
mov    -0x18(%rbp),%ecx
sar    $0xc,%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x10(%rbp)
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x14(%rbp)
jmp    123f <OKJSONParserUniCharToUTF8+0x13f>
mov    -0x18(%rbp),%ecx
sar    $0xc,%ecx
or     $0xe0,%ecx
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x10(%rbp)
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%ecx
sar    $0x6,%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x10(%rbp)
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x10(%rbp)
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<OKJSONParserUniCharToUTF8>:
cmp    $0x7f,%edi
jg     110d <OKJSONParserUniCharToUTF8+0xd>
mov    %edi,(%rsi)
mov    $0x1,%eax
ret
cmp    $0x7ff,%edi
ja     1128 <OKJSONParserUniCharToUTF8+0x28>
mov    %edi,%eax
shr    $0x6,%eax
or     $0xc0,%eax
mov    %eax,(%rsi)
mov    $0x1,%eax
jmp    1191 <OKJSONParserUniCharToUTF8+0x91>
cmp    $0x10000,%edi
jb     1145 <OKJSONParserUniCharToUTF8+0x45>
lea    -0x10fe01(%rdi),%eax
cmp    $0xfe,%eax
ja     115c <OKJSONParserUniCharToUTF8+0x5c>
lea    -0x10fe00(%rdi),%eax
jmp    114f <OKJSONParserUniCharToUTF8+0x4f>
mov    %edi,%eax
shr    $0xc,%eax
or     $0xe0,%eax
mov    %eax,(%rsi)
add    $0x4,%rsi
mov    $0x2,%eax
jmp    1181 <OKJSONParserUniCharToUTF8+0x81>
mov    %edi,%eax
shr    $0x12,%eax
or     $0xf0,%eax
mov    %eax,(%rsi)
mov    %edi,%eax
shr    $0xc,%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %eax,0x4(%rsi)
add    $0x8,%rsi
mov    $0x3,%eax
mov    %edi,%ecx
shr    $0x6,%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    %ecx,(%rsi)
and    $0x3f,%edi
or     $0x80,%edi
mov    %edi,0x4(%rsi)
add    $0x1,%eax
ret

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

LLVM IR (-O2):
define i32 @OKJSONParserUniCharToUTF8(i32 %0, i32* nocapture writeonly %1) {
%3 = icmp slt i32 %0, 128
br i1 %3, label %4, label %5
4: ; preds = %2
store i32 %0, i32* %1
br label %43
5: ; preds = %2
%6 = icmp ult i32 %0, 2048
br i1 %6, label %7, label %10
7: ; preds = %5
%8 = lshr i32 %0, 6
%9 = or i32 %8, 192
store i32 %9, i32* %1
br label %36
10: ; preds = %5
%11 = icmp ugt i32 %0, 65535
br i1 %11, label %12, label %26
12: ; preds = %10
%13 = add nsw i32 %0, -1113601
%14 = icmp ult i32 %13, 255
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = add nsw i32 %0, -1113600
%17 = getelementptr inbounds i32, i32* %1, i64 1
store i32 %16, i32* %1
br label %30
18: ; preds = %12
%19 = lshr i32 %0, 18
%20 = or i32 %19, 240
%21 = getelementptr inbounds i32, i32* %1, i64 1
store i32 %20, i32* %1
%22 = lshr i32 %0, 12
%23 = and i32 %22, 63
%24 = or i32 %23, 128
%25 = getelementptr inbounds i32, i32* %1, i64 2
store i32 %24, i32* %21
br label %30
26: ; preds = %10
%27 = lshr i32 %0, 12
%28 = or i32 %27, 224
%29 = getelementptr inbounds i32, i32* %1, i64 1
store i32 %28, i32* %1
br label %30
30: ; preds = %15, %18, %26
%31 = phi i32 [ 2, %15 ], [ 3, %18 ], [ 2, %26 ]
%32 = phi i32* [ %17, %15 ], [ %25, %18 ], [ %29, %26 ]
%33 = lshr i32 %0, 6
%34 = and i32 %33, 63
%35 = or i32 %34, 128
store i32 %35, i32* %32
br label %36
36: ; preds = %30, %7
%37 = phi i32 [ 1, %7 ], [ %31, %30 ]
%38 = phi i32* [ %1, %7 ], [ %32, %30 ]
%39 = getelementptr inbounds i32, i32* %38, i64 1
%40 = and i32 %0, 63
%41 = or i32 %40, 128
store i32 %41, i32* %39
%42 = add nuw nsw i32 %37, 1
br label %43
43: ; preds = %36, %4
%44 = phi i32 [ 1, %4 ], [ %42, %36 ]
ret i32 %44
}