#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 */
typedef int uint8_t;

/* Variables and functions */
size_t utf8_charlen(int const);
size_t utf8_valid(const uint8_t *c) {
  size_t clen = utf8_charlen(*c);
  switch (clen) {
  case 4:
    if ((c[3] & 0xc0) != 0x80)
      return 0;
  case 3:
    if ((c[2] & 0xc0) != 0x80)
      return 0;
  case 2:
    if ((c[1] & 0xc0) != 0x80)
      return 0;
  case 1:
    return clen;
  case 0:
    return 0;
  }
  return clen;
}


x86 assembly (clang, -O0):
<utf8_valid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <utf8_charlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
sub    $0x4,%rax
ja     11e6 <utf8_valid+0xd6>
mov    -0x20(%rbp),%rax
lea    0xeb8(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%eax
and    $0xc0,%eax
cmp    $0x80,%eax
je     1175 <utf8_valid+0x65>
movq   $0x0,-0x8(%rbp)
jmp    11ee <utf8_valid+0xde>
jmp    117a <utf8_valid+0x6a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%eax
and    $0xc0,%eax
cmp    $0x80,%eax
je     119e <utf8_valid+0x8e>
movq   $0x0,-0x8(%rbp)
jmp    11ee <utf8_valid+0xde>
jmp    11a3 <utf8_valid+0x93>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
and    $0xc0,%eax
cmp    $0x80,%eax
je     11c7 <utf8_valid+0xb7>
movq   $0x0,-0x8(%rbp)
jmp    11ee <utf8_valid+0xde>
jmp    11cc <utf8_valid+0xbc>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ee <utf8_valid+0xde>
movq   $0x0,-0x8(%rbp)
jmp    11ee <utf8_valid+0xde>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_valid>:
push   %rbx
mov    %rdi,%rbx
mov    (%rdi),%edi
call   1030 <utf8_charlen@plt>
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
cmp    $0x3,%rcx
ja     116a <utf8_valid+0x5a>
lea    0xed1(%rip),%rdx
movslq (%rdx,%rcx,4),%rcx
add    %rdx,%rcx
jmp    *%rcx
mov    $0xc0,%ecx
and    0xc(%rbx),%ecx
cmp    $0x80,%ecx
jne    1168 <utf8_valid+0x58>
mov    $0xc0,%ecx
and    0x8(%rbx),%ecx
cmp    $0x80,%ecx
jne    1168 <utf8_valid+0x58>
mov    $0xc0,%ecx
and    0x4(%rbx),%ecx
cmp    $0x80,%ecx
je     116a <utf8_valid+0x5a>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i64 @utf8_valid(i32* %0) {
%2 = alloca i64
%3 = alloca i32*
%4 = alloca i64
store i32* %0, i32** %3
%5 = load i32*, i32** %3
%6 = load i32, i32* %5
%7 = call i64 @utf8_charlen(i32 %6)
store i64 %7, i64* %4
%8 = load i64, i64* %4
switch i64 %8, label %36 [
i64 4, label %9
i64 3, label %17
i64 2, label %25
i64 1, label %33
i64 0, label %35
]
9: ; preds = %1
%10 = load i32*, i32** %3
%11 = getelementptr inbounds i32, i32* %10, i64 3
%12 = load i32, i32* %11
%13 = and i32 %12, 192
%14 = icmp ne i32 %13, 128
br i1 %14, label %15, label %16
15: ; preds = %9
store i64 0, i64* %2
br label %38
16: ; preds = %9
br label %17
17: ; preds = %1, %16
%18 = load i32*, i32** %3
%19 = getelementptr inbounds i32, i32* %18, i64 2
%20 = load i32, i32* %19
%21 = and i32 %20, 192
%22 = icmp ne i32 %21, 128
br i1 %22, label %23, label %24
23: ; preds = %17
store i64 0, i64* %2
br label %38
24: ; preds = %17
br label %25
25: ; preds = %1, %24
%26 = load i32*, i32** %3
%27 = getelementptr inbounds i32, i32* %26, i64 1
%28 = load i32, i32* %27
%29 = and i32 %28, 192
%30 = icmp ne i32 %29, 128
br i1 %30, label %31, label %32
31: ; preds = %25
store i64 0, i64* %2
br label %38
32: ; preds = %25
br label %33
33: ; preds = %1, %32
%34 = load i64, i64* %4
store i64 %34, i64* %2
br label %38
35: ; preds = %1
store i64 0, i64* %2
br label %38
36: ; preds = %1
%37 = load i64, i64* %4
store i64 %37, i64* %2
br label %38
38: ; preds = %36, %35, %33, %31, %23, %15
%39 = load i64, i64* %2
ret i64 %39
}

LLVM IR (-O2):
define i64 @utf8_valid(i32* nocapture %0) {
%2 = load i32, i32* %0
%3 = tail call i64 @utf8_charlen(i32 %2)
switch i64 %3, label %20 [
i64 4, label %4
i64 3, label %9
i64 2, label %14
i64 1, label %19
]
4: ; preds = %1
%5 = getelementptr inbounds i32, i32* %0, i64 3
%6 = load i32, i32* %5
%7 = and i32 %6, 192
%8 = icmp eq i32 %7, 128
br i1 %8, label %9, label %20
9: ; preds = %4, %1
%10 = getelementptr inbounds i32, i32* %0, i64 2
%11 = load i32, i32* %10
%12 = and i32 %11, 192
%13 = icmp eq i32 %12, 128
br i1 %13, label %14, label %20
14: ; preds = %9, %1
%15 = getelementptr inbounds i32, i32* %0, i64 1
%16 = load i32, i32* %15
%17 = and i32 %16, 192
%18 = icmp eq i32 %17, 128
br i1 %18, label %19, label %20
19: ; preds = %14, %1
br label %20
20: ; preds = %1, %14, %9, %4, %19
%21 = phi i64 [ %3, %19 ], [ 0, %4 ], [ 0, %9 ], [ 0, %14 ], [ %3, %1 ]
ret i64 %21
}