#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 guess_char_width(char const);
size_t utf8_chrw(const char str[]) {
  const size_t expected = guess_char_width(str[0]);
  if (expected == 2 && (str[1] & 0xc0) == 0x80)
    return 2;
  else if (expected == 3 && (str[1] & 0xc0) == 0x80 && (str[2] & 0xc0) == 0x80)
    return 3;
  else if (expected == 4 && (str[1] & 0xc0) == 0x80 &&
           (str[2] & 0xc0) == 0x80 && (str[3] & 0xc0) == 0x80)
    return 4;
  else if (str[0] == '\0')
    return 0;
  return 1;
}


x86 assembly (clang, -O0):
<utf8_chrw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <guess_char_width@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x2,-0x18(%rbp)
jne    115c <utf8_chrw+0x4c>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
and    $0xc0,%eax
cmp    $0x80,%eax
jne    115c <utf8_chrw+0x4c>
movq   $0x2,-0x8(%rbp)
jmp    1238 <utf8_chrw+0x128>
cmpq   $0x3,-0x18(%rbp)
jne    11a4 <utf8_chrw+0x94>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
and    $0xc0,%eax
cmp    $0x80,%eax
jne    11a4 <utf8_chrw+0x94>
mov    -0x10(%rbp),%rax
movsbl 0x2(%rax),%eax
and    $0xc0,%eax
cmp    $0x80,%eax
jne    11a4 <utf8_chrw+0x94>
movq   $0x3,-0x8(%rbp)
jmp    1238 <utf8_chrw+0x128>
cmpq   $0x4,-0x18(%rbp)
jne    1204 <utf8_chrw+0xf4>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
and    $0xc0,%eax
cmp    $0x80,%eax
jne    1204 <utf8_chrw+0xf4>
mov    -0x10(%rbp),%rax
movsbl 0x2(%rax),%eax
and    $0xc0,%eax
cmp    $0x80,%eax
jne    1204 <utf8_chrw+0xf4>
mov    -0x10(%rbp),%rax
movsbl 0x3(%rax),%eax
and    $0xc0,%eax
cmp    $0x80,%eax
jne    1204 <utf8_chrw+0xf4>
movq   $0x4,-0x8(%rbp)
jmp    1238 <utf8_chrw+0x128>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1221 <utf8_chrw+0x111>
movq   $0x0,-0x8(%rbp)
jmp    1238 <utf8_chrw+0x128>
jmp    1226 <utf8_chrw+0x116>
jmp    122b <utf8_chrw+0x11b>
jmp    1230 <utf8_chrw+0x120>
movq   $0x1,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_chrw>:
push   %rbx
mov    %rdi,%rbx
movsbl (%rdi),%edi
call   1030 <guess_char_width@plt>
cmp    $0x4,%rax
je     115b <utf8_chrw+0x4b>
cmp    $0x3,%rax
je     1140 <utf8_chrw+0x30>
cmp    $0x2,%rax
jne    117d <utf8_chrw+0x6d>
mov    0x1(%rbx),%cl
and    $0xc0,%cl
mov    $0x2,%eax
cmp    $0x80,%cl
je     1159 <utf8_chrw+0x49>
jmp    117d <utf8_chrw+0x6d>
mov    0x1(%rbx),%al
and    $0xc0,%al
cmp    $0x80,%al
jne    117d <utf8_chrw+0x6d>
mov    0x2(%rbx),%cl
and    $0xc0,%cl
mov    $0x3,%eax
cmp    $0x80,%cl
jne    117d <utf8_chrw+0x6d>
pop    %rbx
ret
mov    0x1(%rbx),%al
and    $0xc0,%al
cmp    $0x80,%al
jne    117d <utf8_chrw+0x6d>
mov    0x2(%rbx),%al
and    $0xc0,%al
cmp    $0x80,%al
jne    117d <utf8_chrw+0x6d>
mov    0x3(%rbx),%cl
and    $0xc0,%cl
mov    $0x4,%eax
cmp    $0x80,%cl
je     1159 <utf8_chrw+0x49>
xor    %eax,%eax
cmpb   $0x0,(%rbx)
setne  %al
pop    %rbx
ret

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

LLVM IR (-O2):
define i64 @utf8_chrw(i8* nocapture %0) {
%2 = load i8, i8* %0
%3 = tail call i64 @guess_char_width(i8 signext %2)
switch i64 %3, label %34 [
i64 2, label %4
i64 3, label %9
i64 4, label %19
]
4: ; preds = %1
%5 = getelementptr inbounds i8, i8* %0, i64 1
%6 = load i8, i8* %5
%7 = and i8 %6, -64
%8 = icmp eq i8 %7, -128
br i1 %8, label %38, label %34
9: ; preds = %1
%10 = getelementptr inbounds i8, i8* %0, i64 1
%11 = load i8, i8* %10
%12 = and i8 %11, -64
%13 = icmp eq i8 %12, -128
br i1 %13, label %14, label %34
14: ; preds = %9
%15 = getelementptr inbounds i8, i8* %0, i64 2
%16 = load i8, i8* %15
%17 = and i8 %16, -64
%18 = icmp eq i8 %17, -128
br i1 %18, label %38, label %34
19: ; preds = %1
%20 = getelementptr inbounds i8, i8* %0, i64 1
%21 = load i8, i8* %20
%22 = and i8 %21, -64
%23 = icmp eq i8 %22, -128
br i1 %23, label %24, label %34
24: ; preds = %19
%25 = getelementptr inbounds i8, i8* %0, i64 2
%26 = load i8, i8* %25
%27 = and i8 %26, -64
%28 = icmp eq i8 %27, -128
br i1 %28, label %29, label %34
29: ; preds = %24
%30 = getelementptr inbounds i8, i8* %0, i64 3
%31 = load i8, i8* %30
%32 = and i8 %31, -64
%33 = icmp eq i8 %32, -128
br i1 %33, label %38, label %34
34: ; preds = %9, %14, %4, %1, %29, %24, %19
%35 = load i8, i8* %0
%36 = icmp ne i8 %35, 0
%37 = zext i1 %36 to i64
br label %38
38: ; preds = %34, %29, %14, %4
%39 = phi i64 [ 2, %4 ], [ 3, %14 ], [ 4, %29 ], [ %37, %34 ]
ret i64 %39
}