#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 */
uint8_t utf8_encode(uint8_t *bp, uint32_t cp) {
  if (cp <= 0x7F) {
    bp[0] = (uint8_t)cp;
    return 1;
  }
  if (cp <= 0x07FF) {
    bp[0] = (uint8_t)((6 << 5) | (cp >> 6));
    bp[1] = (uint8_t)((2 << 6) | (cp & 0x3F));
    return 2;
  }
  if (cp <= 0xFFFF) {
    bp[0] = (uint8_t)((14 << 4) | (cp >> 12));
    bp[1] = (uint8_t)((2 << 6) | ((cp >> 6) & 0x3F));
    bp[2] = (uint8_t)((2 << 6) | (cp & 0x3F));
    return 3;
  }
  if (cp <= 0x10FFFF) {
    bp[0] = (uint8_t)((30 << 3) | (cp >> 18));
    bp[1] = (uint8_t)((2 << 6) | ((cp >> 12) & 0x3F));
    bp[2] = (uint8_t)((2 << 6) | ((cp >> 6) & 0x3F));
    bp[3] = (uint8_t)((2 << 6) | (cp & 0x3F));
    return 4;
  }
  return 0;
}


x86 assembly (clang, -O0):
<utf8_encode>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x7f,-0x14(%rbp)
jg     112a <utf8_encode+0x2a>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    122d <utf8_encode+0x12d>
cmpl   $0x7ff,-0x14(%rbp)
jg     1168 <utf8_encode+0x68>
mov    -0x14(%rbp),%ecx
sar    $0x6,%ecx
or     $0xc0,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
movl   $0x2,-0x4(%rbp)
jmp    122d <utf8_encode+0x12d>
cmpl   $0xffff,-0x14(%rbp)
jg     11bc <utf8_encode+0xbc>
mov    -0x14(%rbp),%ecx
sar    $0xc,%ecx
or     $0xe0,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%ecx
sar    $0x6,%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x14(%rbp),%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
movl   $0x3,-0x4(%rbp)
jmp    122d <utf8_encode+0x12d>
cmpl   $0x10ffff,-0x14(%rbp)
jg     1226 <utf8_encode+0x126>
mov    -0x14(%rbp),%ecx
sar    $0x12,%ecx
or     $0xf0,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%ecx
sar    $0xc,%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x14(%rbp),%ecx
sar    $0x6,%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x14(%rbp),%ecx
and    $0x3f,%ecx
or     $0x80,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0xc(%rax)
movl   $0x4,-0x4(%rbp)
jmp    122d <utf8_encode+0x12d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_encode>:
cmp    $0x7f,%esi
jg     110d <utf8_encode+0xd>
mov    %esi,(%rdi)
mov    $0x1,%eax
ret
cmp    $0x7ff,%esi
ja     1133 <utf8_encode+0x33>
mov    %esi,%eax
shr    $0x6,%eax
or     $0xc0,%eax
mov    %eax,(%rdi)
and    $0x3f,%esi
or     $0x80,%esi
mov    %esi,0x4(%rdi)
mov    $0x2,%eax
ret
cmp    $0xffff,%esi
ja     1169 <utf8_encode+0x69>
mov    %esi,%eax
shr    $0xc,%eax
or     $0xe0,%eax
mov    %eax,(%rdi)
mov    %esi,%eax
shr    $0x6,%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %eax,0x4(%rdi)
and    $0x3f,%esi
or     $0x80,%esi
mov    %esi,0x8(%rdi)
mov    $0x3,%eax
ret
xor    %eax,%eax
cmp    $0x10ffff,%esi
ja     11b0 <utf8_encode+0xb0>
mov    %esi,%eax
shr    $0x12,%eax
or     $0xf0,%eax
mov    %eax,(%rdi)
mov    %esi,%eax
shr    $0xc,%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %eax,0x4(%rdi)
mov    %esi,%eax
shr    $0x6,%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %eax,0x8(%rdi)
and    $0x3f,%esi
or     $0x80,%esi
mov    %esi,0xc(%rdi)
mov    $0x4,%eax
ret

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

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