int put_len_utf8(int _ch) {
  unsigned int ch = (unsigned int)_ch;
  int len = 0;
  if (ch <= 0x7F)
    len = 1;
  else if (ch <= 0x07FF)
    len = 2;
  else if (ch <= 0xFFFF)
    len = 3;
  else if (ch <= 0x01FFFFF)
    len = 4;
  else if (ch <= 0x03FFFFFF)
    len = 5;
  else if (ch <= 0x7FFFFFFF)
    len = 6;
  return len;
}


x86 assembly (clang, -O0):
<put_len_utf8>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x7f,-0x8(%rbp)
ja     112a <put_len_utf8+0x2a>
movl   $0x1,-0xc(%rbp)
jmp    11bb <put_len_utf8+0xbb>
cmpl   $0x7ff,-0x8(%rbp)
ja     1143 <put_len_utf8+0x43>
movl   $0x2,-0xc(%rbp)
jmp    11b6 <put_len_utf8+0xb6>
cmpl   $0xffff,-0x8(%rbp)
ja     115c <put_len_utf8+0x5c>
movl   $0x3,-0xc(%rbp)
jmp    11b1 <put_len_utf8+0xb1>
cmpl   $0x1fffff,-0x8(%rbp)
ja     1175 <put_len_utf8+0x75>
movl   $0x4,-0xc(%rbp)
jmp    11ac <put_len_utf8+0xac>
cmpl   $0x3ffffff,-0x8(%rbp)
ja     118e <put_len_utf8+0x8e>
movl   $0x5,-0xc(%rbp)
jmp    11a7 <put_len_utf8+0xa7>
cmpl   $0x7fffffff,-0x8(%rbp)
ja     11a2 <put_len_utf8+0xa2>
movl   $0x6,-0xc(%rbp)
jmp    11a7 <put_len_utf8+0xa7>
jmp    11ac <put_len_utf8+0xac>
jmp    11b1 <put_len_utf8+0xb1>
jmp    11b6 <put_len_utf8+0xb6>
jmp    11bb <put_len_utf8+0xbb>
mov    -0xc(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<put_len_utf8>:
mov    $0x1,%eax
cmp    $0x80,%edi
jb     114d <put_len_utf8+0x4d>
mov    $0x2,%eax
cmp    $0x800,%edi
jb     114d <put_len_utf8+0x4d>
mov    $0x3,%eax
cmp    $0x10000,%edi
jb     114d <put_len_utf8+0x4d>
mov    $0x4,%eax
cmp    $0x200000,%edi
jb     114d <put_len_utf8+0x4d>
mov    $0x5,%eax
cmp    $0x4000000,%edi
jb     114d <put_len_utf8+0x4d>
xor    %ecx,%ecx
test   %edi,%edi
mov    $0x6,%eax
cmovs  %ecx,%eax
ret

LLVM IR (-O0):
define i32 @put_len_utf8(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
store i32 %5, i32* %3
store i32 0, i32* %4
%6 = load i32, i32* %3
%7 = icmp ule i32 %6, 127
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 1, i32* %4
br label %34
9: ; preds = %1
%10 = load i32, i32* %3
%11 = icmp ule i32 %10, 2047
br i1 %11, label %12, label %13
12: ; preds = %9
store i32 2, i32* %4
br label %33
13: ; preds = %9
%14 = load i32, i32* %3
%15 = icmp ule i32 %14, 65535
br i1 %15, label %16, label %17
16: ; preds = %13
store i32 3, i32* %4
br label %32
17: ; preds = %13
%18 = load i32, i32* %3
%19 = icmp ule i32 %18, 2097151
br i1 %19, label %20, label %21
20: ; preds = %17
store i32 4, i32* %4
br label %31
21: ; preds = %17
%22 = load i32, i32* %3
%23 = icmp ule i32 %22, 67108863
br i1 %23, label %24, label %25
24: ; preds = %21
store i32 5, i32* %4
br label %30
25: ; preds = %21
%26 = load i32, i32* %3
%27 = icmp ule i32 %26, 2147483647
br i1 %27, label %28, label %29
28: ; preds = %25
store i32 6, i32* %4
br label %29
29: ; preds = %28, %25
br label %30
30: ; preds = %29, %24
br label %31
31: ; preds = %30, %20
br label %32
32: ; preds = %31, %16
br label %33
33: ; preds = %32, %12
br label %34
34: ; preds = %33, %8
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
define i32 @put_len_utf8(i32 %0) {
%2 = icmp ult i32 %0, 128
br i1 %2, label %14, label %3
3: ; preds = %1
%4 = icmp ult i32 %0, 2048
br i1 %4, label %14, label %5
5: ; preds = %3
%6 = icmp ult i32 %0, 65536
br i1 %6, label %14, label %7
7: ; preds = %5
%8 = icmp ult i32 %0, 2097152
br i1 %8, label %14, label %9
9: ; preds = %7
%10 = icmp ult i32 %0, 67108864
br i1 %10, label %14, label %11
11: ; preds = %9
%12 = icmp slt i32 %0, 0
%13 = select i1 %12, i32 0, i32 6
br label %14
14: ; preds = %11, %9, %7, %5, %3, %1
%15 = phi i32 [ 1, %1 ], [ 2, %3 ], [ 3, %5 ], [ 4, %7 ], [ 5, %9 ], [ %13, %11 ]
ret i32 %15
}