#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 */
typedef int /*<<< orphan*/ mbstate_t;

/* Variables and functions */
int /*<<< orphan*/ MB_LEN_MAX;
size_t mbrtowc(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/,
               int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
size_t dvm_mbstowcs_len(const char *src) {
  size_t src_idx = 0, dest_count = 0, wc_len = 0;
  mbstate_t ps;
  memset(&ps, 0, sizeof(mbstate_t));
  while (src[src_idx] != '\0') {
    wc_len = mbrtowc(NULL, &src[src_idx], MB_LEN_MAX, &ps);
    if (wc_len == (size_t)-1 || wc_len == (size_t)-2) {
      return wc_len;
    }
    dest_count++;
    src_idx += wc_len;
  }
  return dest_count;
}


x86 assembly (clang, -O0):
<dvm_mbstowcs_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x2c(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     11cb <dvm_mbstowcs_len+0xab>
mov    -0x10(%rbp),%rsi
add    -0x18(%rbp),%rsi
mov    0x2e68(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%edi
lea    -0x2c(%rbp),%rcx
call   1030 <mbrtowc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
je     11a1 <dvm_mbstowcs_len+0x81>
cmpq   $0xfffffffffffffffe,-0x28(%rbp)
jne    11ae <dvm_mbstowcs_len+0x8e>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d3 <dvm_mbstowcs_len+0xb3>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1154 <dvm_mbstowcs_len+0x34>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dvm_mbstowcs_len>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0xc(%rsp),%rdi
xor    %r13d,%r13d
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
cmpb   $0x0,(%r14)
je     118b <dvm_mbstowcs_len+0x6b>
xor    %r13d,%r13d
mov    0x2e8c(%rip),%r12
lea    0xc(%rsp),%r15
mov    %r14,%rsi
xor    %ebx,%ebx
xchg   %ax,%ax
mov    (%r12),%edx
xor    %edi,%edi
mov    %r15,%rcx
call   1030 <mbrtowc@plt>
cmp    $0xfffffffffffffffd,%rax
ja     1188 <dvm_mbstowcs_len+0x68>
add    $0x1,%r13
add    %rax,%rbx
lea    (%r14,%rbx,1),%rsi
cmpb   $0x0,(%r14,%rbx,1)
jne    1160 <dvm_mbstowcs_len+0x40>
jmp    118b <dvm_mbstowcs_len+0x6b>
mov    %rax,%r13
mov    %r13,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@MB_LEN_MAX = global i32 0
define i64 @dvm_mbstowcs_len(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %3
store i64 0, i64* %4
store i64 0, i64* %5
store i64 0, i64* %6
%8 = call i32 @memset(i32* %7, i32 0, i32 4)
br label %9
9: ; preds = %29, %1
%10 = load i8*, i8** %3
%11 = load i64, i64* %4
%12 = getelementptr inbounds i8, i8* %10, i64 %11
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %35
16: ; preds = %9
%17 = load i8*, i8** %3
%18 = load i64, i64* %4
%19 = getelementptr inbounds i8, i8* %17, i64 %18
%20 = load i32, i32* @MB_LEN_MAX
%21 = call i64 @mbrtowc(i32* null, i8* %19, i32 %20, i32* %7)
store i64 %21, i64* %6
%22 = load i64, i64* %6
%23 = icmp eq i64 %22, -1
br i1 %23, label %27, label %24
24: ; preds = %16
%25 = load i64, i64* %6
%26 = icmp eq i64 %25, -2
br i1 %26, label %27, label %29
27: ; preds = %24, %16
%28 = load i64, i64* %6
store i64 %28, i64* %2
br label %37
29: ; preds = %24
%30 = load i64, i64* %5
%31 = add i64 %30, 1
store i64 %31, i64* %5
%32 = load i64, i64* %6
%33 = load i64, i64* %4
%34 = add i64 %33, %32
store i64 %34, i64* %4
br label %9
35: ; preds = %9
%36 = load i64, i64* %5
store i64 %36, i64* %2
br label %37
37: ; preds = %35, %27
%38 = load i64, i64* %2
ret i64 %38
}

LLVM IR (-O2):
@MB_LEN_MAX = global i32 0
define i64 @dvm_mbstowcs_len(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i32 @memset(i32* %2, i32 0, i32 4)
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %20, label %7
7: ; preds = %1, %14
%8 = phi i8* [ %17, %14 ], [ %0, %1 ]
%9 = phi i64 [ %15, %14 ], [ 0, %1 ]
%10 = phi i64 [ %16, %14 ], [ 0, %1 ]
%11 = load i32, i32* @MB_LEN_MAX
%12 = call i64 @mbrtowc(i32* null, i8* %8, i32 %11, i32* %2)
%13 = icmp ugt i64 %12, -3
br i1 %13, label %20, label %14
14: ; preds = %7
%15 = add i64 %9, 1
%16 = add i64 %12, %10
%17 = getelementptr inbounds i8, i8* %0, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
br i1 %19, label %20, label %7
20: ; preds = %7, %14, %1
%21 = phi i64 [ 0, %1 ], [ %15, %14 ], [ %12, %7 ]
ret i64 %21
}