#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 unicode_code_point_byte_requirement_utf8(int);
int unicode_write_utf8(uint32_t code_point, char *string) {
  int byte_requirement = unicode_code_point_byte_requirement_utf8(code_point);
  if (byte_requirement == 1) {
    string[0] = (char)code_point;
  } else if (byte_requirement == 2) {
    string[0] = (char)(((code_point >> 6) & 0x1F) | 0xC0);
    string[1] = (char)((code_point & 0x3F) | 0x80);
  } else if (byte_requirement == 3) {
    string[0] = (char)(((code_point >> 12) & 0x0F) | 0xE0);
    string[1] = (char)(((code_point >> 6) & 0x3F) | 0x80);
    string[2] = (char)((code_point & 0x3F) | 0x80);
  } else if (byte_requirement == 4) {
    string[0] = (char)(((code_point >> 18) & 0x07) | 0xF0);
    string[1] = (char)(((code_point >> 12) & 0x3F) | 0x80);
    string[2] = (char)(((code_point >> 6) & 0x3F) | 0x80);
    string[3] = (char)((code_point & 0x3F) | 0x80);
  } else
    return 0;
  return byte_requirement;
}


x86 assembly (clang, -O0):
<unicode_write_utf8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <unicode_code_point_byte_requirement_utf8@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jne    1144 <unicode_write_utf8+0x34>
mov    -0x8(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
jmp    124f <unicode_write_utf8+0x13f>
cmpl   $0x2,-0x14(%rbp)
jne    117d <unicode_write_utf8+0x6d>
mov    -0x8(%rbp),%eax
sar    $0x6,%eax
and    $0x1f,%eax
or     $0xc0,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x1(%rax)
jmp    124a <unicode_write_utf8+0x13a>
cmpl   $0x3,-0x14(%rbp)
jne    11cd <unicode_write_utf8+0xbd>
mov    -0x8(%rbp),%eax
sar    $0xc,%eax
and    $0xf,%eax
or     $0xe0,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%eax
sar    $0x6,%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0x8(%rbp),%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x2(%rax)
jmp    1245 <unicode_write_utf8+0x135>
cmpl   $0x4,-0x14(%rbp)
jne    1234 <unicode_write_utf8+0x124>
mov    -0x8(%rbp),%eax
sar    $0x12,%eax
and    $0x7,%eax
or     $0xf0,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%eax
sar    $0xc,%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0x8(%rbp),%eax
sar    $0x6,%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x2(%rax)
mov    -0x8(%rbp),%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x3(%rax)
jmp    1240 <unicode_write_utf8+0x130>
movl   $0x0,-0x4(%rbp)
jmp    1255 <unicode_write_utf8+0x145>
jmp    1245 <unicode_write_utf8+0x135>
jmp    124a <unicode_write_utf8+0x13a>
jmp    124f <unicode_write_utf8+0x13f>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unicode_write_utf8>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
call   1030 <unicode_code_point_byte_requirement_utf8@plt>
add    $0xffffffff,%eax
cmp    $0x3,%eax
ja     1142 <unicode_write_utf8+0x32>
lea    0xed4(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    %bpl,(%rbx)
mov    $0x1,%eax
jmp    11c2 <unicode_write_utf8+0xb2>
xor    %eax,%eax
jmp    11c2 <unicode_write_utf8+0xb2>
mov    %ebp,%eax
shr    $0x6,%eax
and    $0x1f,%al
or     $0xc0,%al
mov    %al,(%rbx)
and    $0x3f,%bpl
or     $0x80,%bpl
mov    %bpl,0x1(%rbx)
mov    $0x2,%eax
jmp    11c2 <unicode_write_utf8+0xb2>
mov    %ebp,%eax
shr    $0xc,%eax
and    $0xf,%al
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    11c2 <unicode_write_utf8+0xb2>
mov    %ebp,%eax
shr    $0x12,%eax
and    $0x7,%al
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 @unicode_write_utf8(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%7 = load i32, i32* %4
%8 = call i32 @unicode_code_point_byte_requirement_utf8(i32 %7)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i32, i32* %4
%13 = trunc i32 %12 to i8
%14 = load i8*, i8** %5
%15 = getelementptr inbounds i8, i8* %14, i64 0
store i8 %13, i8* %15
br label %92
16: ; preds = %2
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, 2
br i1 %18, label %19, label %33
19: ; preds = %16
%20 = load i32, i32* %4
%21 = ashr i32 %20, 6
%22 = and i32 %21, 31
%23 = or i32 %22, 192
%24 = trunc i32 %23 to i8
%25 = load i8*, i8** %5
%26 = getelementptr inbounds i8, i8* %25, i64 0
store i8 %24, i8* %26
%27 = load i32, i32* %4
%28 = and i32 %27, 63
%29 = or i32 %28, 128
%30 = trunc i32 %29 to i8
%31 = load i8*, i8** %5
%32 = getelementptr inbounds i8, i8* %31, i64 1
store i8 %30, i8* %32
br label %91
33: ; preds = %16
%34 = load i32, i32* %6
%35 = icmp eq i32 %34, 3
br i1 %35, label %36, label %57
36: ; preds = %33
%37 = load i32, i32* %4
%38 = ashr i32 %37, 12
%39 = and i32 %38, 15
%40 = or i32 %39, 224
%41 = trunc i32 %40 to i8
%42 = load i8*, i8** %5
%43 = getelementptr inbounds i8, i8* %42, i64 0
store i8 %41, i8* %43
%44 = load i32, i32* %4
%45 = ashr i32 %44, 6
%46 = and i32 %45, 63
%47 = or i32 %46, 128
%48 = trunc i32 %47 to i8
%49 = load i8*, i8** %5
%50 = getelementptr inbounds i8, i8* %49, i64 1
store i8 %48, i8* %50
%51 = load i32, i32* %4
%52 = and i32 %51, 63
%53 = or i32 %52, 128
%54 = trunc i32 %53 to i8
%55 = load i8*, i8** %5
%56 = getelementptr inbounds i8, i8* %55, i64 2
store i8 %54, i8* %56
br label %90
57: ; preds = %33
%58 = load i32, i32* %6
%59 = icmp eq i32 %58, 4
br i1 %59, label %60, label %88
60: ; preds = %57
%61 = load i32, i32* %4
%62 = ashr i32 %61, 18
%63 = and i32 %62, 7
%64 = or i32 %63, 240
%65 = trunc i32 %64 to i8
%66 = load i8*, i8** %5
%67 = getelementptr inbounds i8, i8* %66, i64 0
store i8 %65, i8* %67
%68 = load i32, i32* %4
%69 = ashr i32 %68, 12
%70 = and i32 %69, 63
%71 = or i32 %70, 128
%72 = trunc i32 %71 to i8
%73 = load i8*, i8** %5
%74 = getelementptr inbounds i8, i8* %73, i64 1
store i8 %72, i8* %74
%75 = load i32, i32* %4
%76 = ashr i32 %75, 6
%77 = and i32 %76, 63
%78 = or i32 %77, 128
%79 = trunc i32 %78 to i8
%80 = load i8*, i8** %5
%81 = getelementptr inbounds i8, i8* %80, i64 2
store i8 %79, i8* %81
%82 = load i32, i32* %4
%83 = and i32 %82, 63
%84 = or i32 %83, 128
%85 = trunc i32 %84 to i8
%86 = load i8*, i8** %5
%87 = getelementptr inbounds i8, i8* %86, i64 3
store i8 %85, i8* %87
br label %89
88: ; preds = %57
store i32 0, i32* %3
br label %94
89: ; preds = %60
br label %90
90: ; preds = %89, %36
br label %91
91: ; preds = %90, %19
br label %92
92: ; preds = %91, %11
%93 = load i32, i32* %6
store i32 %93, i32* %3
br label %94
94: ; preds = %92, %88
%95 = load i32, i32* %3
ret i32 %95
}

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