#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_left = strlen(str);
  size_t length = 0;
  while (length_left != '\0') {
    size_t char_width = guess_char_width(*str);
    if (char_width > length_left) {
      break;
    }
    ++length;
    str += char_width;
    length_left -= 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)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     119b <get_normal_utf8_string_length+0x7b>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1040 <guess_char_width@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jbe    116f <get_normal_utf8_string_length+0x4f>
jmp    119b <get_normal_utf8_string_length+0x7b>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1141 <get_normal_utf8_string_length+0x21>
mov    -0x18(%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,%r15
call   1030 <strlen@plt>
test   %rax,%rax
je     115c <get_normal_utf8_string_length+0x3c>
mov    %rax,%rbx
xor    %r14d,%r14d
nopl   0x0(%rax,%rax,1)
movsbl (%r15),%edi
call   1040 <guess_char_width@plt>
cmp    %rax,%rbx
jb     115f <get_normal_utf8_string_length+0x3f>
add    $0x1,%r14
add    %rax,%r15
sub    %rax,%rbx
jne    1140 <get_normal_utf8_string_length+0x20>
jmp    115f <get_normal_utf8_string_length+0x3f>
xor    %r14d,%r14d
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
%5 = alloca i64
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i64 @strlen(i8* %6)
store i64 %7, i64* %3
store i64 0, i64* %4
br label %8
8: ; preds = %19, %1
%9 = load i64, i64* %3
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %28
11: ; preds = %8
%12 = load i8*, i8** %2
%13 = load i8, i8* %12
%14 = call i64 @guess_char_width(i8 signext %13)
store i64 %14, i64* %5
%15 = load i64, i64* %5
%16 = load i64, i64* %3
%17 = icmp ugt i64 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %11
br label %28
19: ; preds = %11
%20 = load i64, i64* %4
%21 = add i64 %20, 1
store i64 %21, i64* %4
%22 = load i64, i64* %5
%23 = load i8*, i8** %2
%24 = getelementptr inbounds i8, i8* %23, i64 %22
store i8* %24, i8** %2
%25 = load i64, i64* %5
%26 = load i64, i64* %3
%27 = sub i64 %26, %25
store i64 %27, i64* %3
br label %8
28: ; preds = %18, %8
%29 = load i64, i64* %4
ret i64 %29
}

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