#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 */
scalar_t__ isascii(unsigned char);
int utf8len(const char *p) {
  int len = 1;
  if (*p == '\0')
    return 0;
  if (isascii((unsigned char)*p) || (((unsigned char)*p) & 0xc0) == 0x80)
    return 0;
  p++;
  while ((((unsigned char)*p++) & 0xc0) == 0x80)
    len++;
  return len;
}


x86 assembly (clang, -O0):
<utf8len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    113f <utf8len+0x2f>
movl   $0x0,-0x4(%rbp)
jmp    11ba <utf8len+0xaa>
mov    -0x10(%rbp),%rax
movzbl (%rax),%edi
call   1030 <isascii@plt>
cmp    $0x0,%rax
jne    116c <utf8len+0x5c>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
and    $0xc0,%eax
cmp    $0x80,%eax
jne    1178 <utf8len+0x68>
movl   $0x0,-0x4(%rbp)
jmp    11ba <utf8len+0xaa>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movzbl (%rax),%eax
and    $0xc0,%eax
cmp    $0x80,%eax
jne    11b4 <utf8len+0xa4>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1184 <utf8len+0x74>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8len>:
push   %r14
push   %rbx
push   %rax
mov    (%rdi),%al
xor    %ebx,%ebx
test   %al,%al
je     1150 <utf8len+0x40>
mov    %rdi,%r14
movzbl %al,%edi
call   1030 <isascii@plt>
test   %rax,%rax
jne    1150 <utf8len+0x40>
mov    (%r14),%al
and    $0xc0,%al
cmp    $0x80,%al
je     1150 <utf8len+0x40>
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movzbl 0x1(%r14,%rbx,1),%eax
and    $0xc0,%al
add    $0x1,%rbx
cmp    $0x80,%al
je     1140 <utf8len+0x30>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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