#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 */
size_t utf8str_char_length(const char *str) {
  if (str == NULL || *str == '\0') {
    return 0;
  }
  unsigned int c = *str;
  if (c < 128) {
    return 1;
  }
  if ((c - 0xc2) > (0xf4 - 0xc2)) {
    return (size_t)-1;
  }
  if (c < 0xe0) {
    return 2;
  } else if (c < 0xf0) {
    return 3;
  } else {
    return 4;
  }
}


x86 assembly (clang, -O0):
<utf8str_char_length>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1123 <utf8str_char_length+0x23>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1130 <utf8str_char_length+0x30>
movq   $0x0,-0x8(%rbp)
jmp    11ae <utf8str_char_length+0xae>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x80,-0x14(%rbp)
jae    1154 <utf8str_char_length+0x54>
movq   $0x1,-0x8(%rbp)
jmp    11ae <utf8str_char_length+0xae>
mov    -0x14(%rbp),%eax
sub    $0xc2,%eax
cmp    $0x32,%eax
jbe    1172 <utf8str_char_length+0x72>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11ae <utf8str_char_length+0xae>
cmpl   $0xe0,-0x14(%rbp)
jae    118c <utf8str_char_length+0x8c>
movq   $0x2,-0x8(%rbp)
jmp    11ae <utf8str_char_length+0xae>
cmpl   $0xf0,-0x14(%rbp)
jae    11a6 <utf8str_char_length+0xa6>
movq   $0x3,-0x8(%rbp)
jmp    11ae <utf8str_char_length+0xae>
movq   $0x4,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8str_char_length>:
test   %rdi,%rdi
je     1118 <utf8str_char_length+0x18>
cmpb   $0x0,(%rdi)
je     1118 <utf8str_char_length+0x18>
setns  %al
movzbl %al,%eax
add    %rax,%rax
add    $0xffffffffffffffff,%rax
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
define i64 @utf8str_char_length(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = icmp eq i8* %5, null
br i1 %6, label %12, label %7
7: ; preds = %1
%8 = load i8*, i8** %3
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %7, %1
store i64 0, i64* %2
br label %34
13: ; preds = %7
%14 = load i8*, i8** %3
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp ult i32 %17, 128
br i1 %18, label %19, label %20
19: ; preds = %13
store i64 1, i64* %2
br label %34
20: ; preds = %13
%21 = load i32, i32* %4
%22 = sub i32 %21, 194
%23 = icmp ugt i32 %22, 50
br i1 %23, label %24, label %25
24: ; preds = %20
store i64 -1, i64* %2
br label %34
25: ; preds = %20
%26 = load i32, i32* %4
%27 = icmp ult i32 %26, 224
br i1 %27, label %28, label %29
28: ; preds = %25
store i64 2, i64* %2
br label %34
29: ; preds = %25
%30 = load i32, i32* %4
%31 = icmp ult i32 %30, 240
br i1 %31, label %32, label %33
32: ; preds = %29
store i64 3, i64* %2
br label %34
33: ; preds = %29
store i64 4, i64* %2
br label %34
34: ; preds = %33, %32, %28, %24, %19, %12
%35 = load i64, i64* %2
ret i64 %35
}

LLVM IR (-O2):
define i64 @utf8str_char_length(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %9, label %3
3: ; preds = %1
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %3
%7 = icmp sgt i8 %4, -1
%8 = select i1 %7, i64 1, i64 -1
br label %9
9: ; preds = %1, %3, %6
%10 = phi i64 [ %8, %6 ], [ 0, %3 ], [ 0, %1 ]
ret i64 %10
}