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

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int utf8_write_char(uint32_t unicode, char *out) {
  unsigned char *o = (unsigned char *)out;
  assert(unicode <= 0x10FFFF && !(unicode >= 0xD800 && unicode <= 0xDFFF));
  if (unicode <= 0x7F) {
    *o++ = unicode;
    return 1;
  } else if (unicode <= 0x7FF) {
    *o++ = 0xC0 | unicode >> 6;
    *o++ = 0x80 | (unicode & 0x3F);
    return 2;
  } else if (unicode <= 0xFFFF) {
    *o++ = 0xE0 | unicode >> 12;
    *o++ = 0x80 | (unicode >> 6 & 0x3F);
    *o++ = 0x80 | (unicode & 0x3F);
    return 3;
  } else {
    *o++ = 0xF0 | unicode >> 18;
    *o++ = 0x80 | (unicode >> 12 & 0x3F);
    *o++ = 0x80 | (unicode >> 6 & 0x3F);
    *o++ = 0x80 | (unicode & 0x3F);
    return 4;
  }
}


x86 assembly (clang, -O0):
<utf8_write_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x10ffff,-0x8(%rbp)
mov    %al,-0x19(%rbp)
jg     1160 <utf8_write_char+0x50>
xor    %eax,%eax
cmpl   $0xd800,-0x8(%rbp)
mov    %al,-0x1a(%rbp)
jl     1158 <utf8_write_char+0x48>
cmpl   $0xdfff,-0x8(%rbp)
setle  %al
mov    %al,-0x1a(%rbp)
mov    -0x1a(%rbp),%al
xor    $0xff,%al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpl   $0x7f,-0x8(%rbp)
jg     1199 <utf8_write_char+0x89>
mov    -0x8(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    12e9 <utf8_write_char+0x1d9>
cmpl   $0x7ff,-0x8(%rbp)
jg     11ee <utf8_write_char+0xde>
mov    -0x8(%rbp),%eax
sar    $0x6,%eax
or     $0xc0,%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
and    $0x3f,%eax
or     $0x80,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
movl   $0x2,-0x4(%rbp)
jmp    12e9 <utf8_write_char+0x1d9>
cmpl   $0xffff,-0x8(%rbp)
jg     1264 <utf8_write_char+0x154>
mov    -0x8(%rbp),%eax
sar    $0xc,%eax
or     $0xe0,%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
sar    $0x6,%eax
and    $0x3f,%eax
or     $0x80,%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
and    $0x3f,%eax
or     $0x80,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
movl   $0x3,-0x4(%rbp)
jmp    12e9 <utf8_write_char+0x1d9>
mov    -0x8(%rbp),%eax
sar    $0x12,%eax
or     $0xf0,%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
sar    $0xc,%eax
and    $0x3f,%eax
or     $0x80,%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
sar    $0x6,%eax
and    $0x3f,%eax
or     $0x80,%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
and    $0x3f,%eax
or     $0x80,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
movl   $0x4,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_write_char>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
cmp    $0x110000,%edi
setl   %al
mov    %edi,%ecx
and    $0xfffff800,%ecx
cmp    $0xd800,%ecx
setne  %cl
and    %al,%cl
movzbl %cl,%edi
call   1030 <assert@plt>
cmp    $0x7f,%ebp
jg     114e <utf8_write_char+0x3e>
mov    %bpl,(%rbx)
mov    $0x1,%eax
jmp    11d0 <utf8_write_char+0xc0>
mov    %ebp,%eax
cmp    $0x7ff,%ebp
ja     1172 <utf8_write_char+0x62>
shr    $0x6,%eax
or     $0xc0,%al
mov    %al,(%rbx)
and    $0x3f,%bpl
or     $0x80,%bpl
mov    %bpl,0x1(%rbx)
mov    $0x2,%eax
jmp    11d0 <utf8_write_char+0xc0>
cmp    $0xffff,%ebp
ja     11a0 <utf8_write_char+0x90>
shr    $0xc,%eax
or     $0xe0,%al
mov    %al,(%rbx)
mov    %ebp,%eax
shr    $0x6,%eax
and    $0x3f,%al
or     $0x80,%al
mov    %al,0x1(%rbx)
and    $0x3f,%bpl
or     $0x80,%bpl
mov    %bpl,0x2(%rbx)
mov    $0x3,%eax
jmp    11d0 <utf8_write_char+0xc0>
shr    $0x12,%eax
or     $0xf0,%al
mov    %al,(%rbx)
mov    %ebp,%eax
shr    $0xc,%eax
and    $0x3f,%al
or     $0x80,%al
mov    %al,0x1(%rbx)
mov    %ebp,%eax
shr    $0x6,%eax
and    $0x3f,%al
or     $0x80,%al
mov    %al,0x2(%rbx)
and    $0x3f,%bpl
or     $0x80,%bpl
mov    %bpl,0x3(%rbx)
mov    $0x4,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @utf8_write_char(i32 %0, i8* nocapture writeonly %1) {
%3 = icmp slt i32 %0, 1114112
%4 = and i32 %0, -2048
%5 = icmp ne i32 %4, 55296
%6 = and i1 %3, %5
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = icmp slt i32 %0, 128
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = trunc i32 %0 to i8
store i8 %11, i8* %1
br label %55
12: ; preds = %2
%13 = icmp ult i32 %0, 2048
br i1 %13, label %14, label %22
14: ; preds = %12
%15 = lshr i32 %0, 6
%16 = trunc i32 %15 to i8
%17 = or i8 %16, -64
%18 = getelementptr inbounds i8, i8* %1, i64 1
store i8 %17, i8* %1
%19 = trunc i32 %0 to i8
%20 = and i8 %19, 63
%21 = or i8 %20, -128
store i8 %21, i8* %18
br label %55
22: ; preds = %12
%23 = icmp ult i32 %0, 65536
br i1 %23, label %24, label %37
24: ; preds = %22
%25 = lshr i32 %0, 12
%26 = trunc i32 %25 to i8
%27 = or i8 %26, -32
%28 = getelementptr inbounds i8, i8* %1, i64 1
store i8 %27, i8* %1
%29 = lshr i32 %0, 6
%30 = trunc i32 %29 to i8
%31 = and i8 %30, 63
%32 = or i8 %31, -128
%33 = getelementptr inbounds i8, i8* %1, i64 2
store i8 %32, i8* %28
%34 = trunc i32 %0 to i8
%35 = and i8 %34, 63
%36 = or i8 %35, -128
store i8 %36, i8* %33
br label %55
37: ; preds = %22
%38 = lshr i32 %0, 18
%39 = trunc i32 %38 to i8
%40 = or i8 %39, -16
%41 = getelementptr inbounds i8, i8* %1, i64 1
store i8 %40, i8* %1
%42 = lshr i32 %0, 12
%43 = trunc i32 %42 to i8
%44 = and i8 %43, 63
%45 = or i8 %44, -128
%46 = getelementptr inbounds i8, i8* %1, i64 2
store i8 %45, i8* %41
%47 = lshr i32 %0, 6
%48 = trunc i32 %47 to i8
%49 = and i8 %48, 63
%50 = or i8 %49, -128
%51 = getelementptr inbounds i8, i8* %1, i64 3
store i8 %50, i8* %46
%52 = trunc i32 %0 to i8
%53 = and i8 %52, 63
%54 = or i8 %53, -128
store i8 %54, i8* %51
br label %55
55: ; preds = %37, %24, %14, %10
%56 = phi i32 [ 1, %10 ], [ 2, %14 ], [ 3, %24 ], [ 4, %37 ]
ret i32 %56
}