#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 */
scalar_t__ utf32_is_surrogate(unsigned long);
int /*<<< orphan*/ utf32_is_valid(unsigned long);
unsigned utf8_encoded_len(unsigned long cp) {
  if (cp < 0x80U) {
    return 1;
  } else if (cp < 0x800U) {
    return 2;
  } else if (!utf32_is_valid(cp) || utf32_is_surrogate(cp)) {
    return 0;
  } else if (cp < 0x10000U) {
    return 3;
  } else {
    return 4;
  }
}


x86 assembly (clang, -O0):
<utf8_encoded_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x80,-0x10(%rbp)
jae    1146 <utf8_encoded_len+0x26>
movl   $0x1,-0x4(%rbp)
jmp    11b2 <utf8_encoded_len+0x92>
cmpq   $0x800,-0x10(%rbp)
jae    1160 <utf8_encoded_len+0x40>
movl   $0x2,-0x4(%rbp)
jmp    11b2 <utf8_encoded_len+0x92>
mov    -0x10(%rbp),%rdi
call   1030 <utf32_is_valid@plt>
cmp    $0x0,%eax
je     1185 <utf8_encoded_len+0x65>
mov    -0x10(%rbp),%rdi
call   1040 <utf32_is_surrogate@plt>
cmp    $0x0,%rax
je     1191 <utf8_encoded_len+0x71>
movl   $0x0,-0x4(%rbp)
jmp    11b2 <utf8_encoded_len+0x92>
cmpq   $0x10000,-0x10(%rbp)
jae    11ab <utf8_encoded_len+0x8b>
movl   $0x3,-0x4(%rbp)
jmp    11b2 <utf8_encoded_len+0x92>
movl   $0x4,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_encoded_len>:
push   %rbp
push   %rbx
push   %rax
mov    $0x1,%ebp
cmp    $0x80,%rdi
jb     116c <utf8_encoded_len+0x4c>
mov    %rdi,%rbx
mov    $0x2,%ebp
cmp    $0x800,%rdi
jb     116c <utf8_encoded_len+0x4c>
mov    %rbx,%rdi
call   1030 <utf32_is_valid@plt>
xor    %ebp,%ebp
test   %eax,%eax
je     116c <utf8_encoded_len+0x4c>
mov    %rbx,%rdi
call   1040 <utf32_is_surrogate@plt>
test   %rax,%rax
jne    116c <utf8_encoded_len+0x4c>
cmp    $0x10000,%rbx
mov    $0x4,%ebp
sbb    $0x0,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @utf8_encoded_len(i64 %0) {
%2 = alloca i32
%3 = alloca i64
store i64 %0, i64* %3
%4 = load i64, i64* %3
%5 = icmp ult i64 %4, 128
br i1 %5, label %6, label %7
6: ; preds = %1
store i32 1, i32* %2
br label %25
7: ; preds = %1
%8 = load i64, i64* %3
%9 = icmp ult i64 %8, 2048
br i1 %9, label %10, label %11
10: ; preds = %7
store i32 2, i32* %2
br label %25
11: ; preds = %7
%12 = load i64, i64* %3
%13 = call i32 @utf32_is_valid(i64 %12)
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = load i64, i64* %3
%17 = call i64 @utf32_is_surrogate(i64 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %15, %11
store i32 0, i32* %2
br label %25
20: ; preds = %15
%21 = load i64, i64* %3
%22 = icmp ult i64 %21, 65536
br i1 %22, label %23, label %24
23: ; preds = %20
store i32 3, i32* %2
br label %25
24: ; preds = %20
store i32 4, i32* %2
br label %25
25: ; preds = %24, %23, %19, %10, %6
%26 = load i32, i32* %2
ret i32 %26
}

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