#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 uft8towcs_octetcount(char const);
size_t utf8towcs_length(const char *pSrc, size_t SrcSize, size_t DestLen) {
  size_t retval = 0;
  size_t cnt, len = 0;
  while (len < SrcSize && retval < DestLen) {
    cnt = uft8towcs_octetcount(*pSrc);
    if (cnt <= 0 || (len + cnt) > SrcSize)
      break;
    retval++;
    pSrc += cnt;
  }
  return retval;
}


x86 assembly (clang, -O0):
<utf8towcs_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rcx
xor    %eax,%eax
cmp    -0x10(%rbp),%rcx
mov    %al,-0x31(%rbp)
jae    1155 <utf8towcs_length+0x45>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
setb   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1165 <utf8towcs_length+0x55>
jmp    11b4 <utf8towcs_length+0xa4>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1030 <uft8towcs_octetcount@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    1192 <utf8towcs_length+0x82>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jbe    1197 <utf8towcs_length+0x87>
jmp    11b4 <utf8towcs_length+0xa4>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1134 <utf8towcs_length+0x24>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8towcs_length>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %r12d,%r12d
test   %rsi,%rsi
je     115d <utf8towcs_length+0x4d>
mov    %rdx,%r14
test   %rdx,%rdx
je     115d <utf8towcs_length+0x4d>
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl (%rbx),%edi
call   1030 <uft8towcs_octetcount@plt>
lea    -0x1(%rax),%rcx
cmp    %r15,%rcx
jae    115d <utf8towcs_length+0x4d>
add    $0x1,%r12
add    %rax,%rbx
cmp    %r14,%r12
jb     1140 <utf8towcs_length+0x30>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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