#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 utf8_encoded_valid_unichar(char const *);
size_t utf8_n_codepoints(const char *str) {
  size_t n = 0;
  while (*str != 0) {
    int k;
    k = utf8_encoded_valid_unichar(str);
    if (k < 0)
      return (size_t)-1;
    str += k;
    n++;
  }
  return n;
}


x86 assembly (clang, -O0):
<utf8_n_codepoints>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1179 <utf8_n_codepoints+0x69>
mov    -0x10(%rbp),%rdi
call   1030 <utf8_encoded_valid_unichar@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1157 <utf8_n_codepoints+0x47>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1181 <utf8_n_codepoints+0x71>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1124 <utf8_n_codepoints+0x14>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_n_codepoints>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
cmpb   $0x0,(%rbx)
je     1141 <utf8_n_codepoints+0x31>
mov    %rbx,%rdi
call   1030 <utf8_encoded_valid_unichar@plt>
cltq
add    %rax,%rbx
add    $0x1,%r14
test   %eax,%eax
jns    1120 <utf8_n_codepoints+0x10>
mov    $0xffffffffffffffff,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i64 @utf8_n_codepoints(i8* %0) {
br label %2
2: ; preds = %7, %1
%3 = phi i8* [ %0, %1 ], [ %11, %7 ]
%4 = phi i64 [ 0, %1 ], [ %12, %7 ]
%5 = load i8, i8* %3
%6 = icmp eq i8 %5, 0
br i1 %6, label %13, label %7
7: ; preds = %2
%8 = tail call i32 @utf8_encoded_valid_unichar(i8* %3)
%9 = icmp slt i32 %8, 0
%10 = sext i32 %8 to i64
%11 = getelementptr inbounds i8, i8* %3, i64 %10
%12 = add i64 %4, 1
br i1 %9, label %13, label %2
13: ; preds = %2, %7
%14 = phi i64 [ -1, %7 ], [ %4, %2 ]
ret i64 %14
}