int unicode_count(char *ux) {
  if ((*ux & 0x80) == 0)
    return 1;
  if ((*ux & 0xC0) == 0x80)
    return -1;
  if ((*ux & 0xE0) == 0xC0)
    return 2;
  if ((*ux & 0xF0) == 0xE0)
    return 3;
  if ((*ux & 0xF8) == 0xF0)
    return 4;
  return 0;
}


x86 assembly (clang, -O0):
<unicode_count>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    1129 <unicode_count+0x29>
movl   $0x1,-0x4(%rbp)
jmp    11bc <unicode_count+0xbc>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
and    $0xc0,%eax
cmp    $0x80,%eax
jne    114c <unicode_count+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <unicode_count+0xbc>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
and    $0xe0,%eax
cmp    $0xc0,%eax
jne    116f <unicode_count+0x6f>
movl   $0x2,-0x4(%rbp)
jmp    11bc <unicode_count+0xbc>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
and    $0xf0,%eax
cmp    $0xe0,%eax
jne    1192 <unicode_count+0x92>
movl   $0x3,-0x4(%rbp)
jmp    11bc <unicode_count+0xbc>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
and    $0xf8,%eax
cmp    $0xf0,%eax
jne    11b5 <unicode_count+0xb5>
movl   $0x4,-0x4(%rbp)
jmp    11bc <unicode_count+0xbc>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<unicode_count>:
movzbl (%rdi),%ecx
mov    $0x1,%eax
test   $0x80,%cl
je     1154 <unicode_count+0x54>
mov    %ecx,%edx
and    $0xffffffc0,%edx
mov    $0xffffffff,%eax
cmp    $0x80,%edx
je     1154 <unicode_count+0x54>
mov    %ecx,%edx
and    $0xffffffe0,%edx
mov    $0x2,%eax
cmp    $0xc0,%edx
je     1154 <unicode_count+0x54>
mov    %ecx,%edx
and    $0xfffffff0,%edx
mov    $0x3,%eax
cmp    $0xe0,%edx
je     1154 <unicode_count+0x54>
and    $0xfffffff8,%ecx
xor    %eax,%eax
cmp    $0xf0,%ecx
sete   %al
shl    $0x2,%eax
ret

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

LLVM IR (-O2):
define i32 @unicode_count(i8* nocapture %0) {
%2 = load i8, i8* %0
%3 = zext i8 %2 to i32
%4 = and i32 %3, 128
%5 = icmp eq i32 %4, 0
br i1 %5, label %19, label %6
6: ; preds = %1
%7 = and i32 %3, 192
%8 = icmp eq i32 %7, 128
br i1 %8, label %19, label %9
9: ; preds = %6
%10 = and i32 %3, 224
%11 = icmp eq i32 %10, 192
br i1 %11, label %19, label %12
12: ; preds = %9
%13 = and i32 %3, 240
%14 = icmp eq i32 %13, 224
br i1 %14, label %19, label %15
15: ; preds = %12
%16 = and i32 %3, 248
%17 = icmp eq i32 %16, 240
%18 = select i1 %17, i32 4, i32 0
br label %19
19: ; preds = %15, %12, %9, %6, %1
%20 = phi i32 [ 1, %1 ], [ -1, %6 ], [ 2, %9 ], [ 3, %12 ], [ %18, %15 ]
ret i32 %20
}