#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*/ utf8_decode(unsigned int *, unsigned int *, unsigned char);
int utf8_len(unsigned char *s, unsigned int *count) {
  unsigned int codepoint;
  unsigned int state = 0;
  for (*count = 0; *s; ++s)
    if (!utf8_decode(&state, &codepoint, *s))
      *count += 1;
  return state != 0;
}


x86 assembly (clang, -O0):
<utf8_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     117c <utf8_len+0x6c>
mov    -0x8(%rbp),%rax
lea    -0x18(%rbp),%rdi
lea    -0x14(%rbp),%rsi
movzbl (%rax),%edx
call   1030 <utf8_decode@plt>
cmp    $0x0,%eax
jne    1166 <utf8_len+0x56>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
jmp    116b <utf8_len+0x5b>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1131 <utf8_len+0x21>
cmpl   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_len>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
movl   $0x0,(%rsp)
movl   $0x0,(%rsi)
mov    (%rdi),%al
test   %al,%al
je     116e <utf8_len+0x5e>
mov    %rsi,%r14
mov    %rdi,%rbx
add    $0x1,%rbx
mov    %rsp,%r15
lea    0x4(%rsp),%r12
jmp    114b <utf8_len+0x3b>
nop
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     1163 <utf8_len+0x53>
movzbl %al,%edx
mov    %r15,%rdi
mov    %r12,%rsi
call   1030 <utf8_decode@plt>
test   %eax,%eax
jne    1140 <utf8_len+0x30>
addl   $0x1,(%r14)
jmp    1140 <utf8_len+0x30>
xor    %eax,%eax
cmpl   $0x0,(%rsp)
setne  %al
jmp    1170 <utf8_len+0x60>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i32 @utf8_len(i8* nocapture %0, i32* nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
store i32 0, i32* %1
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %25, label %9
9: ; preds = %2, %17
%10 = phi i8 [ %19, %17 ], [ %7, %2 ]
%11 = phi i8* [ %18, %17 ], [ %0, %2 ]
%12 = call i32 @utf8_decode(i32* %4, i32* %3, i8 zeroext %10)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = load i32, i32* %1
%16 = add i32 %15, 1
store i32 %16, i32* %1
br label %17
17: ; preds = %9, %14
%18 = getelementptr inbounds i8, i8* %11, i64 1
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 0
br i1 %20, label %21, label %9
21: ; preds = %17
%22 = load i32, i32* %4
%23 = icmp ne i32 %22, 0
%24 = zext i1 %23 to i32
br label %25
25: ; preds = %21, %2
%26 = phi i32 [ %24, %21 ], [ 0, %2 ]
ret i32 %26
}