#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 */

/* Variables and functions */
int /*<<< orphan*/ utf8_build(unsigned int, unsigned char *, int, int);
int utf8_encode(unsigned int value, unsigned char out[6]) {
  int i;
  for (i = 1; i < 6; ++i)
    out[i] = 0x80;
  if (value < 0x80) {
    out[0] = 0x0;
    utf8_build(value, out, 25, 1);
    return 1;
  } else if (value < 0x800) {
    out[0] = 0xC0;
    utf8_build(value, out, 21, 3);
    return 2;
  } else if (value < 0x10000) {
    out[0] = 0xE0;
    utf8_build(value, out, 16, 4);
    return 3;
  } else if (value < 0x200000) {
    out[0] = 0xF0;
    utf8_build(value, out, 11, 5);
    return 4;
  } else if (value < 0x4000000) {
    out[0] = 0xF8;
    utf8_build(value, out, 6, 6);
    return 5;
  } else if (value < (unsigned int)0x80000000) {
    out[0] = 0xFC;
    utf8_build(value, out, 1, 7);
    return 6;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<utf8_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
cmpl   $0x6,-0x14(%rbp)
jge    114a <utf8_encode+0x3a>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x80,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1126 <utf8_encode+0x16>
cmpl   $0x80,-0x8(%rbp)
jae    1180 <utf8_encode+0x70>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x19,%edx
mov    $0x1,%ecx
call   1030 <utf8_build@plt>
movl   $0x1,-0x4(%rbp)
jmp    1292 <utf8_encode+0x182>
cmpl   $0x800,-0x8(%rbp)
jae    11b6 <utf8_encode+0xa6>
mov    -0x10(%rbp),%rax
movb   $0xc0,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x15,%edx
mov    $0x3,%ecx
call   1030 <utf8_build@plt>
movl   $0x2,-0x4(%rbp)
jmp    1292 <utf8_encode+0x182>
cmpl   $0x10000,-0x8(%rbp)
jae    11ec <utf8_encode+0xdc>
mov    -0x10(%rbp),%rax
movb   $0xe0,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x10,%edx
mov    $0x4,%ecx
call   1030 <utf8_build@plt>
movl   $0x3,-0x4(%rbp)
jmp    1292 <utf8_encode+0x182>
cmpl   $0x200000,-0x8(%rbp)
jae    1222 <utf8_encode+0x112>
mov    -0x10(%rbp),%rax
movb   $0xf0,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0xb,%edx
mov    $0x5,%ecx
call   1030 <utf8_build@plt>
movl   $0x4,-0x4(%rbp)
jmp    1292 <utf8_encode+0x182>
cmpl   $0x4000000,-0x8(%rbp)
jae    1255 <utf8_encode+0x145>
mov    -0x10(%rbp),%rax
movb   $0xf8,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x6,%ecx
mov    %ecx,%edx
call   1030 <utf8_build@plt>
movl   $0x5,-0x4(%rbp)
jmp    1292 <utf8_encode+0x182>
cmpl   $0x80000000,-0x8(%rbp)
jae    128b <utf8_encode+0x17b>
mov    -0x10(%rbp),%rax
movb   $0xfc,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
mov    $0x7,%ecx
call   1030 <utf8_build@plt>
movl   $0x6,-0x4(%rbp)
jmp    1292 <utf8_encode+0x182>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_encode>:
push   %rbx
movb   $0x80,0x5(%rsi)
movl   $0x80808080,0x1(%rsi)
cmp    $0x7f,%edi
ja     113d <utf8_encode+0x2d>
movb   $0x0,(%rsi)
mov    $0x1,%ebx
mov    $0x19,%edx
mov    $0x1,%ecx
call   1030 <utf8_build@plt>
jmp    11e3 <utf8_encode+0xd3>
cmp    $0x7ff,%edi
ja     1161 <utf8_encode+0x51>
movb   $0xc0,(%rsi)
mov    $0x15,%edx
mov    $0x3,%ecx
call   1030 <utf8_build@plt>
mov    $0x2,%ebx
jmp    11e3 <utf8_encode+0xd3>
cmp    $0xffff,%edi
ja     1182 <utf8_encode+0x72>
movb   $0xe0,(%rsi)
mov    $0x10,%edx
mov    $0x4,%ecx
call   1030 <utf8_build@plt>
mov    $0x3,%ebx
jmp    11e3 <utf8_encode+0xd3>
cmp    $0x1fffff,%edi
ja     11a3 <utf8_encode+0x93>
movb   $0xf0,(%rsi)
mov    $0xb,%edx
mov    $0x5,%ecx
call   1030 <utf8_build@plt>
mov    $0x4,%ebx
jmp    11e3 <utf8_encode+0xd3>
cmp    $0x3ffffff,%edi
ja     11c4 <utf8_encode+0xb4>
movb   $0xf8,(%rsi)
mov    $0x6,%edx
mov    $0x6,%ecx
call   1030 <utf8_build@plt>
mov    $0x5,%ebx
jmp    11e3 <utf8_encode+0xd3>
test   %edi,%edi
js     11e1 <utf8_encode+0xd1>
movb   $0xfc,(%rsi)
mov    $0x1,%edx
mov    $0x7,%ecx
call   1030 <utf8_build@plt>
mov    $0x6,%ebx
jmp    11e3 <utf8_encode+0xd3>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @utf8_encode(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
store i32 1, i32* %6
br label %7
7: ; preds = %15, %2
%8 = load i32, i32* %6
%9 = icmp slt i32 %8, 6
br i1 %9, label %10, label %18
10: ; preds = %7
%11 = load i8*, i8** %5
%12 = load i32, i32* %6
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
store i8 -128, i8* %14
br label %15
15: ; preds = %10
%16 = load i32, i32* %6
%17 = add nsw i32 %16, 1
store i32 %17, i32* %6
br label %7
18: ; preds = %7
%19 = load i32, i32* %4
%20 = icmp ult i32 %19, 128
br i1 %20, label %21, label %27
21: ; preds = %18
%22 = load i8*, i8** %5
%23 = getelementptr inbounds i8, i8* %22, i64 0
store i8 0, i8* %23
%24 = load i32, i32* %4
%25 = load i8*, i8** %5
%26 = call i32 @utf8_build(i32 %24, i8* %25, i32 25, i32 1)
store i32 1, i32* %3
br label %73
27: ; preds = %18
%28 = load i32, i32* %4
%29 = icmp ult i32 %28, 2048
br i1 %29, label %30, label %36
30: ; preds = %27
%31 = load i8*, i8** %5
%32 = getelementptr inbounds i8, i8* %31, i64 0
store i8 -64, i8* %32
%33 = load i32, i32* %4
%34 = load i8*, i8** %5
%35 = call i32 @utf8_build(i32 %33, i8* %34, i32 21, i32 3)
store i32 2, i32* %3
br label %73
36: ; preds = %27
%37 = load i32, i32* %4
%38 = icmp ult i32 %37, 65536
br i1 %38, label %39, label %45
39: ; preds = %36
%40 = load i8*, i8** %5
%41 = getelementptr inbounds i8, i8* %40, i64 0
store i8 -32, i8* %41
%42 = load i32, i32* %4
%43 = load i8*, i8** %5
%44 = call i32 @utf8_build(i32 %42, i8* %43, i32 16, i32 4)
store i32 3, i32* %3
br label %73
45: ; preds = %36
%46 = load i32, i32* %4
%47 = icmp ult i32 %46, 2097152
br i1 %47, label %48, label %54
48: ; preds = %45
%49 = load i8*, i8** %5
%50 = getelementptr inbounds i8, i8* %49, i64 0
store i8 -16, i8* %50
%51 = load i32, i32* %4
%52 = load i8*, i8** %5
%53 = call i32 @utf8_build(i32 %51, i8* %52, i32 11, i32 5)
store i32 4, i32* %3
br label %73
54: ; preds = %45
%55 = load i32, i32* %4
%56 = icmp ult i32 %55, 67108864
br i1 %56, label %57, label %63
57: ; preds = %54
%58 = load i8*, i8** %5
%59 = getelementptr inbounds i8, i8* %58, i64 0
store i8 -8, i8* %59
%60 = load i32, i32* %4
%61 = load i8*, i8** %5
%62 = call i32 @utf8_build(i32 %60, i8* %61, i32 6, i32 6)
store i32 5, i32* %3
br label %73
63: ; preds = %54
%64 = load i32, i32* %4
%65 = icmp ult i32 %64, -2147483648
br i1 %65, label %66, label %72
66: ; preds = %63
%67 = load i8*, i8** %5
%68 = getelementptr inbounds i8, i8* %67, i64 0
store i8 -4, i8* %68
%69 = load i32, i32* %4
%70 = load i8*, i8** %5
%71 = call i32 @utf8_build(i32 %69, i8* %70, i32 1, i32 7)
store i32 6, i32* %3
br label %73
72: ; preds = %63
store i32 0, i32* %3
br label %73
73: ; preds = %72, %66, %57, %48, %39, %30, %21
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
define i32 @utf8_encode(i32 %0, i8* %1) {
%3 = getelementptr i8, i8* %1, i64 1
%4 = icmp ult i32 %0, 128
br i1 %4, label %5, label %7
5: ; preds = %2
store i8 0, i8* %1
%6 = tail call i32 @utf8_build(i32 %0, i8* %1, i32 25, i32 1)
br label %27
7: ; preds = %2
%8 = icmp ult i32 %0, 2048
br i1 %8, label %9, label %11
9: ; preds = %7
store i8 -64, i8* %1
%10 = tail call i32 @utf8_build(i32 %0, i8* %1, i32 21, i32 3)
br label %27
11: ; preds = %7
%12 = icmp ult i32 %0, 65536
br i1 %12, label %13, label %15
13: ; preds = %11
store i8 -32, i8* %1
%14 = tail call i32 @utf8_build(i32 %0, i8* %1, i32 16, i32 4)
br label %27
15: ; preds = %11
%16 = icmp ult i32 %0, 2097152
br i1 %16, label %17, label %19
17: ; preds = %15
store i8 -16, i8* %1
%18 = tail call i32 @utf8_build(i32 %0, i8* %1, i32 11, i32 5)
br label %27
19: ; preds = %15
%20 = icmp ult i32 %0, 67108864
br i1 %20, label %21, label %23
21: ; preds = %19
store i8 -8, i8* %1
%22 = tail call i32 @utf8_build(i32 %0, i8* %1, i32 6, i32 6)
br label %27
23: ; preds = %19
%24 = icmp sgt i32 %0, -1
br i1 %24, label %25, label %27
25: ; preds = %23
store i8 -4, i8* %1
%26 = tail call i32 @utf8_build(i32 %0, i8* %1, i32 1, i32 7)
br label %27
27: ; preds = %23, %25, %21, %17, %13, %9, %5
%28 = phi i32 [ 1, %5 ], [ 2, %9 ], [ 3, %13 ], [ 4, %17 ], [ 5, %21 ], [ 6, %25 ], [ 0, %23 ]
ret i32 %28
}