#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 */
char *dtx_utf8_next_char(char *);
int dtx_utf8_char_count_range(const char *str, int nbytes) {
  int n = 0;
  while (*str && nbytes > 0) {
    char *next = dtx_utf8_next_char((char *)str);
    ++n;
    nbytes -= next - str;
    str = next;
  }
  return (nbytes < 0 && n > 0) ? n - 1 : n;
}


x86 assembly (clang, -O0):
<dtx_utf8_char_count_range>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x19(%rbp)
je     1145 <dtx_utf8_char_count_range+0x35>
cmpl   $0x0,-0xc(%rbp)
setg   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1155 <dtx_utf8_char_count_range+0x45>
jmp    118d <dtx_utf8_char_count_range+0x7d>
mov    -0x8(%rbp),%rdi
call   1030 <dtx_utf8_next_char@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
sub    %rax,%rcx
movslq -0xc(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1126 <dtx_utf8_char_count_range+0x16>
cmpl   $0x0,-0xc(%rbp)
jge    11af <dtx_utf8_char_count_range+0x9f>
cmpl   $0x0,-0x10(%rbp)
jle    11af <dtx_utf8_char_count_range+0x9f>
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11b5 <dtx_utf8_char_count_range+0xa5>
mov    -0x10(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dtx_utf8_char_count_range>:
push   %rbp
push   %r14
push   %rbx
xor    %ebp,%ebp
cmpb   $0x0,(%rdi)
je     1151 <dtx_utf8_char_count_range+0x41>
test   %esi,%esi
jle    1151 <dtx_utf8_char_count_range+0x41>
mov    %rdi,%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %esi,%r14d
mov    %rbx,%rdi
call   1030 <dtx_utf8_next_char@plt>
add    $0x1,%ebp
sub    %eax,%ebx
mov    %ebx,%esi
add    %r14d,%esi
cmpb   $0x0,(%rax)
je     1151 <dtx_utf8_char_count_range+0x41>
mov    %rax,%rbx
test   %esi,%esi
jg     1130 <dtx_utf8_char_count_range+0x20>
test   %esi,%esi
sets   %al
test   %ebp,%ebp
setne  %cl
and    %al,%cl
movzbl %cl,%eax
sub    %eax,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @dtx_utf8_char_count_range(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
br label %7
7: ; preds = %17, %2
%8 = load i8*, i8** %3
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = load i32, i32* %4
%14 = icmp sgt i32 %13, 0
br label %15
15: ; preds = %12, %7
%16 = phi i1 [ false, %7 ], [ %14, %12 ]
br i1 %16, label %17, label %32
17: ; preds = %15
%18 = load i8*, i8** %3
%19 = call i8* @dtx_utf8_next_char(i8* %18)
store i8* %19, i8** %6
%20 = load i32, i32* %5
%21 = add nsw i32 %20, 1
store i32 %21, i32* %5
%22 = load i8*, i8** %6
%23 = load i8*, i8** %3
%24 = ptrtoint i8* %22 to i64
%25 = ptrtoint i8* %23 to i64
%26 = sub i64 %24, %25
%27 = load i32, i32* %4
%28 = sext i32 %27 to i64
%29 = sub nsw i64 %28, %26
%30 = trunc i64 %29 to i32
store i32 %30, i32* %4
%31 = load i8*, i8** %6
store i8* %31, i8** %3
br label %7
32: ; preds = %15
%33 = load i32, i32* %4
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %32
%36 = load i32, i32* %5
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i32, i32* %5
%40 = sub nsw i32 %39, 1
br label %43
41: ; preds = %35, %32
%42 = load i32, i32* %5
br label %43
43: ; preds = %41, %38
%44 = phi i32 [ %40, %38 ], [ %42, %41 ]
ret i32 %44
}

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