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

/* Variables and functions */
int utf8_fn_encode(wchar_t wc, unsigned char *cp, int maxclen) {
  if (wc >= 0 && wc <= 0x7F) {
    if (!(maxclen >= 1)) {
      return 0;
    }
    *cp = 0x00FF & wc;
    return 1;
  }
  if (wc >= 0x80 && wc <= 0x7FF) {
    if (!(maxclen >= 2)) {
      return 0;
    }
    *(cp++) = 0xFF & (0xC0 | ((wc & 0x07C0) >> 6));
    *(cp++) = 0xFF & (0x80 | ((wc & 0x003F)));
    return 2;
  }
  if (wc >= 0x800 && wc <= 0xFFFF) {
    if (!(maxclen >= 3)) {
      return 0;
    }
    *(cp++) = 0xFF & (0xE0 | ((wc & 0xF000) >> 12));
    *(cp++) = 0xFF & (0x80 | ((wc & 0x0FC0) >> 6));
    *(cp++) = 0xFF & (0x80 | ((wc & 0x003F)));
    return 3;
  }
  if (wc >= 0x10000 && wc <= 0x1FFFF) {
    if (sizeof(wchar_t) >= 3) {
      *(cp++) = 0xFF & (0xF0 | ((wc & 0x001C0000) >> 18));
      *(cp++) = 0xFF & (0x80 | ((wc & 0x0003F000) >> 12));
      *(cp++) = 0xFF & (0x80 | ((wc & 0x00000FC0) >> 6));
      *(cp++) = 0xFF & (0x80 | ((wc & 0x0000003F)));
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<utf8_fn_encode>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1154 <utf8_fn_encode+0x54>
cmpl   $0x7f,-0x8(%rbp)
jg     1154 <utf8_fn_encode+0x54>
cmpl   $0x1,-0x14(%rbp)
jge    1138 <utf8_fn_encode+0x38>
movl   $0x0,-0x4(%rbp)
jmp    1346 <utf8_fn_encode+0x246>
mov    -0x8(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1346 <utf8_fn_encode+0x246>
cmpl   $0x80,-0x8(%rbp)
jl     11db <utf8_fn_encode+0xdb>
cmpl   $0x7ff,-0x8(%rbp)
jg     11db <utf8_fn_encode+0xdb>
cmpl   $0x2,-0x14(%rbp)
jge    1184 <utf8_fn_encode+0x84>
movl   $0x0,-0x4(%rbp)
jmp    1346 <utf8_fn_encode+0x246>
mov    -0x8(%rbp),%eax
and    $0x7c0,%eax
sar    $0x6,%eax
or     $0xc0,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%eax
and    $0x3f,%eax
or     $0x80,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
movl   $0x2,-0x4(%rbp)
jmp    1346 <utf8_fn_encode+0x246>
cmpl   $0x800,-0x8(%rbp)
jl     128a <utf8_fn_encode+0x18a>
cmpl   $0xffff,-0x8(%rbp)
jg     128a <utf8_fn_encode+0x18a>
cmpl   $0x3,-0x14(%rbp)
jge    120b <utf8_fn_encode+0x10b>
movl   $0x0,-0x4(%rbp)
jmp    1346 <utf8_fn_encode+0x246>
mov    -0x8(%rbp),%eax
and    $0xf000,%eax
sar    $0xc,%eax
or     $0xe0,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%eax
and    $0xfc0,%eax
sar    $0x6,%eax
or     $0x80,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%eax
and    $0x3f,%eax
or     $0x80,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
movl   $0x3,-0x4(%rbp)
jmp    1346 <utf8_fn_encode+0x246>
cmpl   $0x10000,-0x8(%rbp)
jl     133f <utf8_fn_encode+0x23f>
cmpl   $0x1ffff,-0x8(%rbp)
jg     133f <utf8_fn_encode+0x23f>
mov    -0x8(%rbp),%eax
and    $0x1c0000,%eax
sar    $0x12,%eax
or     $0xf0,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%eax
and    $0x3f000,%eax
sar    $0xc,%eax
or     $0x80,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%eax
and    $0xfc0,%eax
sar    $0x6,%eax
or     $0x80,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%eax
and    $0x3f,%eax
or     $0x80,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_fn_encode>:
cmp    $0x7f,%edi
ja     1112 <utf8_fn_encode+0x12>
test   %edx,%edx
jle    117d <utf8_fn_encode+0x7d>
mov    %dil,(%rsi)
mov    $0x1,%eax
ret
lea    -0x80(%rdi),%eax
cmp    $0x77f,%eax
ja     1142 <utf8_fn_encode+0x42>
xor    %eax,%eax
cmp    $0x2,%edx
jl     11ba <utf8_fn_encode+0xba>
mov    %edi,%eax
shr    $0x6,%eax
or     $0xc0,%al
mov    %al,(%rsi)
and    $0x3f,%dil
or     $0x80,%dil
mov    %dil,0x1(%rsi)
mov    $0x2,%eax
ret
lea    -0x800(%rdi),%eax
cmp    $0xf7ff,%eax
ja     1180 <utf8_fn_encode+0x80>
xor    %eax,%eax
cmp    $0x3,%edx
jl     11ba <utf8_fn_encode+0xba>
mov    %edi,%eax
shr    $0xc,%eax
or     $0xe0,%al
mov    %al,(%rsi)
mov    %edi,%eax
shr    $0x6,%eax
and    $0x3f,%al
or     $0x80,%al
mov    %al,0x1(%rsi)
and    $0x3f,%dil
or     $0x80,%dil
mov    %dil,0x2(%rsi)
mov    $0x3,%eax
ret
xor    %eax,%eax
ret
mov    %edi,%ecx
and    $0xffff0000,%ecx
xor    %eax,%eax
cmp    $0x10000,%ecx
jne    11ba <utf8_fn_encode+0xba>
movb   $0xf0,(%rsi)
mov    %edi,%ecx
shr    $0xc,%ecx
or     $0x80,%cl
mov    %cl,0x1(%rsi)
mov    %edi,%ecx
shr    $0x6,%ecx
and    $0x3f,%cl
or     $0x80,%cl
mov    %cl,0x2(%rsi)
and    $0x3f,%dil
or     $0x80,%dil
mov    %dil,0x3(%rsi)
ret

LLVM IR (-O0):
define i32 @utf8_fn_encode(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%8 = load i32, i32* %5
%9 = icmp sge i32 %8, 0
br i1 %9, label %10, label %22
10: ; preds = %3
%11 = load i32, i32* %5
%12 = icmp sle i32 %11, 127
br i1 %12, label %13, label %22
13: ; preds = %10
%14 = load i32, i32* %7
%15 = icmp sge i32 %14, 1
br i1 %15, label %17, label %16
16: ; preds = %13
store i32 0, i32* %4
br label %121
17: ; preds = %13
%18 = load i32, i32* %5
%19 = and i32 255, %18
%20 = trunc i32 %19 to i8
%21 = load i8*, i8** %6
store i8 %20, i8* %21
store i32 1, i32* %4
br label %121
22: ; preds = %10, %3
%23 = load i32, i32* %5
%24 = icmp sge i32 %23, 128
br i1 %24, label %25, label %48
25: ; preds = %22
%26 = load i32, i32* %5
%27 = icmp sle i32 %26, 2047
br i1 %27, label %28, label %48
28: ; preds = %25
%29 = load i32, i32* %7
%30 = icmp sge i32 %29, 2
br i1 %30, label %32, label %31
31: ; preds = %28
store i32 0, i32* %4
br label %121
32: ; preds = %28
%33 = load i32, i32* %5
%34 = and i32 %33, 1984
%35 = ashr i32 %34, 6
%36 = or i32 192, %35
%37 = and i32 255, %36
%38 = trunc i32 %37 to i8
%39 = load i8*, i8** %6
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %6
store i8 %38, i8* %39
%41 = load i32, i32* %5
%42 = and i32 %41, 63
%43 = or i32 128, %42
%44 = and i32 255, %43
%45 = trunc i32 %44 to i8
%46 = load i8*, i8** %6
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %6
store i8 %45, i8* %46
store i32 2, i32* %4
br label %121
48: ; preds = %25, %22
%49 = load i32, i32* %5
%50 = icmp sge i32 %49, 2048
br i1 %50, label %51, label %82
51: ; preds = %48
%52 = load i32, i32* %5
%53 = icmp sle i32 %52, 65535
br i1 %53, label %54, label %82
54: ; preds = %51
%55 = load i32, i32* %7
%56 = icmp sge i32 %55, 3
br i1 %56, label %58, label %57
57: ; preds = %54
store i32 0, i32* %4
br label %121
58: ; preds = %54
%59 = load i32, i32* %5
%60 = and i32 %59, 61440
%61 = ashr i32 %60, 12
%62 = or i32 224, %61
%63 = and i32 255, %62
%64 = trunc i32 %63 to i8
%65 = load i8*, i8** %6
%66 = getelementptr inbounds i8, i8* %65, i32 1
store i8* %66, i8** %6
store i8 %64, i8* %65
%67 = load i32, i32* %5
%68 = and i32 %67, 4032
%69 = ashr i32 %68, 6
%70 = or i32 128, %69
%71 = and i32 255, %70
%72 = trunc i32 %71 to i8
%73 = load i8*, i8** %6
%74 = getelementptr inbounds i8, i8* %73, i32 1
store i8* %74, i8** %6
store i8 %72, i8* %73
%75 = load i32, i32* %5
%76 = and i32 %75, 63
%77 = or i32 128, %76
%78 = and i32 255, %77
%79 = trunc i32 %78 to i8
%80 = load i8*, i8** %6
%81 = getelementptr inbounds i8, i8* %80, i32 1
store i8* %81, i8** %6
store i8 %79, i8* %80
store i32 3, i32* %4
br label %121
82: ; preds = %51, %48
%83 = load i32, i32* %5
%84 = icmp sge i32 %83, 65536
br i1 %84, label %85, label %120
85: ; preds = %82
%86 = load i32, i32* %5
%87 = icmp sle i32 %86, 131071
br i1 %87, label %88, label %120
88: ; preds = %85
%89 = load i32, i32* %5
%90 = and i32 %89, 1835008
%91 = ashr i32 %90, 18
%92 = or i32 240, %91
%93 = and i32 255, %92
%94 = trunc i32 %93 to i8
%95 = load i8*, i8** %6
%96 = getelementptr inbounds i8, i8* %95, i32 1
store i8* %96, i8** %6
store i8 %94, i8* %95
%97 = load i32, i32* %5
%98 = and i32 %97, 258048
%99 = ashr i32 %98, 12
%100 = or i32 128, %99
%101 = and i32 255, %100
%102 = trunc i32 %101 to i8
%103 = load i8*, i8** %6
%104 = getelementptr inbounds i8, i8* %103, i32 1
store i8* %104, i8** %6
store i8 %102, i8* %103
%105 = load i32, i32* %5
%106 = and i32 %105, 4032
%107 = ashr i32 %106, 6
%108 = or i32 128, %107
%109 = and i32 255, %108
%110 = trunc i32 %109 to i8
%111 = load i8*, i8** %6
%112 = getelementptr inbounds i8, i8* %111, i32 1
store i8* %112, i8** %6
store i8 %110, i8* %111
%113 = load i32, i32* %5
%114 = and i32 %113, 63
%115 = or i32 128, %114
%116 = and i32 255, %115
%117 = trunc i32 %116 to i8
%118 = load i8*, i8** %6
%119 = getelementptr inbounds i8, i8* %118, i32 1
store i8* %119, i8** %6
store i8 %117, i8* %118
br label %120
120: ; preds = %88, %85, %82
store i32 0, i32* %4
br label %121
121: ; preds = %120, %58, %57, %32, %31, %17, %16
%122 = load i32, i32* %4
ret i32 %122
}

LLVM IR (-O2):
define i32 @utf8_fn_encode(i32 %0, i8* nocapture writeonly %1, i32 %2) {
%4 = icmp ult i32 %0, 128
br i1 %4, label %5, label %9
5: ; preds = %3
%6 = icmp sgt i32 %2, 0
br i1 %6, label %7, label %57
7: ; preds = %5
%8 = trunc i32 %0 to i8
store i8 %8, i8* %1
br label %57
9: ; preds = %3
%10 = add i32 %0, -128
%11 = icmp ult i32 %10, 1920
br i1 %11, label %12, label %22
12: ; preds = %9
%13 = icmp sgt i32 %2, 1
br i1 %13, label %14, label %57
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 %57
22: ; preds = %9
%23 = add i32 %0, -2048
%24 = icmp ult i32 %23, 63488
br i1 %24, label %25, label %40
25: ; preds = %22
%26 = icmp sgt i32 %2, 2
br i1 %26, label %27, label %57
27: ; preds = %25
%28 = lshr i32 %0, 12
%29 = trunc i32 %28 to i8
%30 = or i8 %29, -32
%31 = getelementptr inbounds i8, i8* %1, i64 1
store i8 %30, i8* %1
%32 = lshr i32 %0, 6
%33 = trunc i32 %32 to i8
%34 = and i8 %33, 63
%35 = or i8 %34, -128
%36 = getelementptr inbounds i8, i8* %1, i64 2
store i8 %35, i8* %31
%37 = trunc i32 %0 to i8
%38 = and i8 %37, 63
%39 = or i8 %38, -128
store i8 %39, i8* %36
br label %57
40: ; preds = %22
%41 = and i32 %0, -65536
%42 = icmp eq i32 %41, 65536
br i1 %42, label %43, label %57
43: ; preds = %40
%44 = getelementptr inbounds i8, i8* %1, i64 1
store i8 -16, i8* %1
%45 = lshr i32 %0, 12
%46 = trunc i32 %45 to i8
%47 = or i8 %46, -128
%48 = getelementptr inbounds i8, i8* %1, i64 2
store i8 %47, i8* %44
%49 = lshr i32 %0, 6
%50 = trunc i32 %49 to i8
%51 = and i8 %50, 63
%52 = or i8 %51, -128
%53 = getelementptr inbounds i8, i8* %1, i64 3
store i8 %52, i8* %48
%54 = trunc i32 %0 to i8
%55 = and i8 %54, 63
%56 = or i8 %55, -128
store i8 %56, i8* %53
br label %57
57: ; preds = %40, %43, %25, %12, %5, %27, %14, %7
%58 = phi i32 [ 1, %7 ], [ 2, %14 ], [ 3, %27 ], [ 0, %5 ], [ 0, %12 ], [ 0, %25 ], [ 0, %43 ], [ 0, %40 ]
ret i32 %58
}