#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 */
scalar_t__ hpgs_next_utf8(char const **);
int hpgs_utf8_strlen(const char *p, int n) {
  const char **pp = &p;
  int ret = 0;
  while ((n < 0 || ((*pp) - p) < n) && hpgs_next_utf8(pp) != 0)
    ++ret;
  return ret;
}


x86 assembly (clang, -O0):
<hpgs_utf8_strlen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     1158 <hpgs_utf8_strlen+0x48>
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
mov    -0x8(%rbp),%rax
sub    %rax,%rcx
movslq -0xc(%rbp),%rdx
xor    %eax,%eax
cmp    %rdx,%rcx
mov    %al,-0x1d(%rbp)
jge    116b <hpgs_utf8_strlen+0x5b>
mov    -0x18(%rbp),%rdi
call   1030 <hpgs_next_utf8@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    117b <hpgs_utf8_strlen+0x6b>
jmp    1189 <hpgs_utf8_strlen+0x79>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    112e <hpgs_utf8_strlen+0x1e>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hpgs_utf8_strlen>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,(%rsp)
test   %esi,%esi
je     1142 <hpgs_utf8_strlen+0x32>
mov    $0xffffffff,%ebx
mov    %rsp,%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
call   1030 <hpgs_next_utf8@plt>
add    $0x1,%ebx
test   %rax,%rax
jne    1130 <hpgs_utf8_strlen+0x20>
jmp    1144 <hpgs_utf8_strlen+0x34>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i32 @hpgs_utf8_strlen(i8* %0, i32 %1) {
%3 = alloca i8*
store i8* %0, i8** %3
%4 = icmp eq i32 %1, 0
br i1 %4, label %10, label %5
5: ; preds = %2, %5
%6 = phi i32 [ %9, %5 ], [ 0, %2 ]
%7 = call i64 @hpgs_next_utf8(i8** %3)
%8 = icmp eq i64 %7, 0
%9 = add nuw nsw i32 %6, 1
br i1 %8, label %10, label %5
10: ; preds = %5, %2
%11 = phi i32 [ 0, %2 ], [ %6, %5 ]
ret i32 %11
}