#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 utf8_char_next(char const *, size_t, size_t);
scalar_t__ utf8_char_width(char const *, size_t, size_t);
size_t utf8_grapheme_next(char const *, size_t, size_t);
size_t utf8_grapheme_width(const char *s, size_t len, size_t point,
                           size_t *pnext) {
  size_t next;
  size_t width;
  size_t i;
  next = utf8_grapheme_next(s, len, point);
  if (pnext)
    *pnext = next;
  width = 0;
  for (i = point; i < next; i = utf8_char_next(s, len, i))
    width += utf8_char_width(s, len, i);
  return width;
}


x86 assembly (clang, -O0):
<utf8_grapheme_width>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <utf8_grapheme_next@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1173 <utf8_grapheme_width+0x43>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    11c4 <utf8_grapheme_width+0x94>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1030 <utf8_char_width@plt>
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1040 <utf8_char_next@plt>
mov    %rax,-0x38(%rbp)
jmp    1183 <utf8_grapheme_width+0x53>
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_grapheme_width>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%rbx
mov    %rdx,%r13
mov    %rsi,%r14
mov    %rdi,%r15
call   1050 <utf8_grapheme_next@plt>
mov    %rax,%r12
test   %rbx,%rbx
je     1155 <utf8_grapheme_width+0x25>
mov    %r12,(%rbx)
xor    %ebx,%ebx
cmp    %r13,%r12
jbe    1187 <utf8_grapheme_width+0x57>
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   1030 <utf8_char_width@plt>
add    %rax,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   1040 <utf8_char_next@plt>
mov    %rax,%r13
cmp    %r12,%rax
jb     1160 <utf8_grapheme_width+0x30>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @utf8_grapheme_width(i8* %0, i64 %1, i64 %2, i64* %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i64* %3, i64** %8
%12 = load i8*, i8** %5
%13 = load i64, i64* %6
%14 = load i64, i64* %7
%15 = call i64 @utf8_grapheme_next(i8* %12, i64 %13, i64 %14)
store i64 %15, i64* %9
%16 = load i64*, i64** %8
%17 = icmp ne i64* %16, null
br i1 %17, label %18, label %21
18: ; preds = %4
%19 = load i64, i64* %9
%20 = load i64*, i64** %8
store i64 %19, i64* %20
br label %21
21: ; preds = %18, %4
store i64 0, i64* %10
%22 = load i64, i64* %7
store i64 %22, i64* %11
br label %23
23: ; preds = %34, %21
%24 = load i64, i64* %11
%25 = load i64, i64* %9
%26 = icmp ult i64 %24, %25
br i1 %26, label %27, label %39
27: ; preds = %23
%28 = load i8*, i8** %5
%29 = load i64, i64* %6
%30 = load i64, i64* %11
%31 = call i64 @utf8_char_width(i8* %28, i64 %29, i64 %30)
%32 = load i64, i64* %10
%33 = add i64 %32, %31
store i64 %33, i64* %10
br label %34
34: ; preds = %27
%35 = load i8*, i8** %5
%36 = load i64, i64* %6
%37 = load i64, i64* %11
%38 = call i64 @utf8_char_next(i8* %35, i64 %36, i64 %37)
store i64 %38, i64* %11
br label %23
39: ; preds = %23
%40 = load i64, i64* %10
ret i64 %40
}

LLVM IR (-O2):
define i64 @utf8_grapheme_width(i8* %0, i64 %1, i64 %2, i64* writeonly %3) {
%5 = tail call i64 @utf8_grapheme_next(i8* %0, i64 %1, i64 %2)
%6 = icmp eq i64* %3, null
br i1 %6, label %8, label %7
7: ; preds = %4
store i64 %5, i64* %3
br label %8
8: ; preds = %7, %4
%9 = icmp ugt i64 %5, %2
br i1 %9, label %10, label %17
10: ; preds = %8, %10
%11 = phi i64 [ %15, %10 ], [ %2, %8 ]
%12 = phi i64 [ %14, %10 ], [ 0, %8 ]
%13 = tail call i64 @utf8_char_width(i8* %0, i64 %1, i64 %11)
%14 = add i64 %13, %12
%15 = tail call i64 @utf8_char_next(i8* %0, i64 %1, i64 %11)
%16 = icmp ult i64 %15, %5
br i1 %16, label %10, label %17
17: ; preds = %10, %8
%18 = phi i64 [ 0, %8 ], [ %14, %10 ]
ret i64 %18
}