#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 */
size_t guess_char_width(char const);
size_t strlen(char const *);
size_t get_normal_utf8_string_length(const char str[]) {
  size_t length = 0;
  while (*str != '\0') {
    size_t char_width = guess_char_width(*str);
    if (char_width <= strlen(str))
      length++;
    else
      break;
    str += char_width;
  }
  return length;
}


x86 assembly (clang, -O0):
<get_normal_utf8_string_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     119c <get_normal_utf8_string_length+0x7c>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1040 <guess_char_width@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
ja     1186 <get_normal_utf8_string_length+0x66>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    118b <get_normal_utf8_string_length+0x6b>
jmp    119c <get_normal_utf8_string_length+0x7c>
mov    -0x18(%rbp),%rax
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1134 <get_normal_utf8_string_length+0x14>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_normal_utf8_string_length>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
xor    %ecx,%ecx
nopw   0x0(%rax,%rax,1)
mov    %rcx,%r14
movzbl (%rbx),%eax
test   %al,%al
je     1159 <get_normal_utf8_string_length+0x39>
movsbl %al,%edi
call   1040 <guess_char_width@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0x1(%r14),%rcx
add    %r15,%rbx
cmp    %rax,%r15
jbe    1130 <get_normal_utf8_string_length+0x10>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i64 @get_normal_utf8_string_length(i8* nocapture %0) {
br label %2
2: ; preds = %7, %1
%3 = phi i64 [ 0, %1 ], [ %11, %7 ]
%4 = phi i8* [ %0, %1 ], [ %12, %7 ]
%5 = load i8, i8* %4
%6 = icmp eq i8 %5, 0
br i1 %6, label %13, label %7
7: ; preds = %2
%8 = tail call i64 @guess_char_width(i8 signext %5)
%9 = tail call i64 @strlen(i8* %4)
%10 = icmp ugt i64 %8, %9
%11 = add i64 %3, 1
%12 = getelementptr inbounds i8, i8* %4, i64 %8
br i1 %10, label %13, label %2
13: ; preds = %7, %2
ret i64 %3
}