#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 */
char *utf8_next(char const *, char const *);
size_t utf8_length(const char *s, const char *e) {
  size_t i = 0;
  while (s < e) {
    if ((*s & 0xFF) < 0xC0)
      ++s;
    else
      s = utf8_next(s, e);
    ++i;
  }
  return i;
}


x86 assembly (clang, -O0):
<utf8_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1180 <utf8_length+0x70>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
and    $0xff,%eax
cmp    $0xc0,%eax
jge    115e <utf8_length+0x4e>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    116f <utf8_length+0x5f>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <utf8_next@plt>
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1128 <utf8_length+0x18>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_length>:
push   %r14
push   %rbx
push   %rax
cmp    %rsi,%rdi
jae    113f <utf8_length+0x2f>
mov    %rsi,%r14
xor    %ebx,%ebx
jmp    1134 <utf8_length+0x24>
mov    %r14,%rsi
call   1030 <utf8_next@plt>
mov    %rax,%rdi
add    $0x1,%rbx
cmp    %r14,%rdi
jae    1141 <utf8_length+0x31>
cmpb   $0xbf,(%rdi)
ja     1120 <utf8_length+0x10>
add    $0x1,%rdi
jmp    112b <utf8_length+0x1b>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i64 @utf8_length(i8* %0, i8* %1) {
%3 = icmp ult i8* %0, %1
br i1 %3, label %4, label %17
4: ; preds = %2, %13
%5 = phi i64 [ %15, %13 ], [ 0, %2 ]
%6 = phi i8* [ %14, %13 ], [ %0, %2 ]
%7 = load i8, i8* %6
%8 = icmp ult i8 %7, -64
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = getelementptr inbounds i8, i8* %6, i64 1
br label %13
11: ; preds = %4
%12 = tail call i8* @utf8_next(i8* %6, i8* %1)
br label %13
13: ; preds = %11, %9
%14 = phi i8* [ %10, %9 ], [ %12, %11 ]
%15 = add i64 %5, 1
%16 = icmp ult i8* %14, %1
br i1 %16, label %4, label %17
17: ; preds = %13, %2
%18 = phi i64 [ 0, %2 ], [ %15, %13 ]
ret i64 %18
}