#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 guac_utf8_charsize(unsigned char);
size_t guac_utf8_strlen(const char *str) {
  int length = 0;
  int skip = 0;
  while (*str != 0) {
    if (skip > 0)
      skip--;
    else {
      unsigned char c = (unsigned char)*str;
      skip = guac_utf8_charsize(c) - 1;
      length++;
    }
    str++;
  }
  return length;
}


x86 assembly (clang, -O0):
<guac_utf8_strlen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1184 <guac_utf8_strlen+0x74>
cmpl   $0x0,-0x10(%rbp)
jle    1152 <guac_utf8_strlen+0x42>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1173 <guac_utf8_strlen+0x63>
mov    -0x8(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x11(%rbp)
movzbl -0x11(%rbp),%edi
call   1030 <guac_utf8_charsize@plt>
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    112a <guac_utf8_strlen+0x1a>
movslq -0xc(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<guac_utf8_strlen>:
push   %rbp
push   %rbx
push   %rax
mov    (%rdi),%cl
test   %cl,%cl
je     114f <guac_utf8_strlen+0x3f>
mov    %rdi,%rbx
add    $0x1,%rbx
xor    %eax,%eax
xor    %ebp,%ebp
jmp    113e <guac_utf8_strlen+0x2e>
cs nopw 0x0(%rax,%rax,1)
add    $0xffffffff,%eax
movzbl (%rbx),%ecx
add    $0x1,%rbx
test   %cl,%cl
je     1151 <guac_utf8_strlen+0x41>
test   %eax,%eax
jg     1130 <guac_utf8_strlen+0x20>
movzbl %cl,%edi
call   1030 <guac_utf8_charsize@plt>
add    $0x1,%ebp
jmp    1130 <guac_utf8_strlen+0x20>
xor    %ebp,%ebp
movslq %ebp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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