#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 */
int /*<<< orphan*/ assert(char const *);
int cbstrcountutf(const char *str) {
  const unsigned char *rp;
  int cnt;
  assert(str);
  rp = (unsigned char *)str;
  cnt = 0;
  while (*rp != '\0') {
    if ((*rp & 0x80) == 0x00 || (*rp & 0xe0) == 0xc0 || (*rp & 0xf0) == 0xe0 ||
        (*rp & 0xf8) == 0xf0)
      cnt++;
    rp++;
  }
  return cnt;
}


x86 assembly (clang, -O0):
<cbstrcountutf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <assert@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x0,%eax
je     11b8 <cbstrcountutf+0xa8>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
and    $0x80,%eax
cmp    $0x0,%eax
je     119e <cbstrcountutf+0x8e>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
and    $0xe0,%eax
cmp    $0xc0,%eax
je     119e <cbstrcountutf+0x8e>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
and    $0xf0,%eax
cmp    $0xe0,%eax
je     119e <cbstrcountutf+0x8e>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
and    $0xf8,%eax
cmp    $0xf0,%eax
jne    11a7 <cbstrcountutf+0x97>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1134 <cbstrcountutf+0x24>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cbstrcountutf>:
push   %rbx
mov    %rdi,%rbx
call   1030 <assert@plt>
mov    (%rbx),%cl
test   %cl,%cl
je     116a <cbstrcountutf+0x5a>
add    $0x1,%rbx
xor    %eax,%eax
movdqa 0xed3(%rip),%xmm0
movdqa 0xedb(%rip),%xmm1
cs nopw 0x0(%rax,%rax,1)
nop
movzbl %cl,%ecx
movd   %ecx,%xmm2
pshufd $0x0,%xmm2,%xmm2
pand   %xmm0,%xmm2
pcmpeqd %xmm1,%xmm2
movmskps %xmm2,%ecx
cmp    $0x1,%ecx
sbb    $0xffffffff,%eax
movzbl (%rbx),%ecx
add    $0x1,%rbx
test   %cl,%cl
jne    1140 <cbstrcountutf+0x30>
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
define i32 @cbstrcountutf(i8* %0) {
%2 = tail call i32 @assert(i8* %0)
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 0
br i1 %4, label %22, label %5
5: ; preds = %1, %5
%6 = phi i8 [ %20, %5 ], [ %3, %1 ]
%7 = phi i32 [ %18, %5 ], [ 0, %1 ]
%8 = phi i8* [ %19, %5 ], [ %0, %1 ]
%9 = zext i8 %6 to i32
%10 = insertelement <4 x i32> poison, i32 %9, i64 0
%11 = shufflevector <4 x i32> %10, <4 x i32> poison, <4 x i32> zeroinitializer
%12 = freeze <4 x i32> %11
%13 = and <4 x i32> %12, <i32 128, i32 224, i32 240, i32 248>
%14 = icmp eq <4 x i32> %13, <i32 0, i32 192, i32 224, i32 240>
%15 = bitcast <4 x i1> %14 to i4
%16 = icmp ne i4 %15, 0
%17 = zext i1 %16 to i32
%18 = add nuw nsw i32 %7, %17
%19 = getelementptr inbounds i8, i8* %8, i64 1
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 0
br i1 %21, label %22, label %5
22: ; preds = %5, %1
%23 = phi i32 [ 0, %1 ], [ %18, %5 ]
ret i32 %23
}