#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 XUtf8CharByteLen(unsigned char const *, int);
int XCountUtf8Char(const unsigned char *buf, int len) {
  int i = 0;
  int nbc = 0;
  while (i < len) {
    int cl = XUtf8CharByteLen(buf + i, len - i);
    if (cl < 1)
      cl = 1;
    nbc++;
    i += cl;
  }
  return nbc;
}


x86 assembly (clang, -O0):
<XCountUtf8Char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    117a <XCountUtf8Char+0x6a>
mov    -0x8(%rbp),%rdi
movslq -0x10(%rbp),%rax
add    %rax,%rdi
mov    -0xc(%rbp),%esi
sub    -0x10(%rbp),%esi
call   1030 <XUtf8CharByteLen@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
jge    1163 <XCountUtf8Char+0x53>
movl   $0x1,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    112d <XCountUtf8Char+0x1d>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<XCountUtf8Char>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %esi,%esi
jle    1151 <XCountUtf8Char+0x41>
mov    %esi,%r15d
mov    %rdi,%r14
xor    %ebx,%ebx
mov    $0x1,%r12d
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %ebx,%edi
add    %r14,%rdi
mov    %r15d,%esi
sub    %ebx,%esi
call   1030 <XUtf8CharByteLen@plt>
test   %eax,%eax
cmovle %r12d,%eax
add    $0x1,%ebp
add    %eax,%ebx
cmp    %r15d,%ebx
jl     1130 <XCountUtf8Char+0x20>
jmp    1153 <XCountUtf8Char+0x43>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @XCountUtf8Char(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
br label %8
8: ; preds = %24, %2
%9 = load i32, i32* %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %30
12: ; preds = %8
%13 = load i8*, i8** %3
%14 = load i32, i32* %5
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = load i32, i32* %4
%18 = load i32, i32* %5
%19 = sub nsw i32 %17, %18
%20 = call i32 @XUtf8CharByteLen(i8* %16, i32 %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp slt i32 %21, 1
br i1 %22, label %23, label %24
23: ; preds = %12
store i32 1, i32* %7
br label %24
24: ; preds = %23, %12
%25 = load i32, i32* %6
%26 = add nsw i32 %25, 1
store i32 %26, i32* %6
%27 = load i32, i32* %7
%28 = load i32, i32* %5
%29 = add nsw i32 %28, %27
store i32 %29, i32* %5
br label %8
30: ; preds = %8
%31 = load i32, i32* %6
ret i32 %31
}

LLVM IR (-O2):
define i32 @XCountUtf8Char(i8* %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %16
4: ; preds = %2, %4
%5 = phi i32 [ %14, %4 ], [ 0, %2 ]
%6 = phi i32 [ %13, %4 ], [ 0, %2 ]
%7 = zext i32 %5 to i64
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = sub nsw i32 %1, %5
%10 = tail call i32 @XUtf8CharByteLen(i8* %8, i32 %9)
%11 = icmp sgt i32 %10, 1
%12 = select i1 %11, i32 %10, i32 1
%13 = add nuw nsw i32 %6, 1
%14 = add nuw nsw i32 %12, %5
%15 = icmp slt i32 %14, %1
br i1 %15, label %4, label %16
16: ; preds = %4, %2
%17 = phi i32 [ 0, %2 ], [ %13, %4 ]
ret i32 %17
}