#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;
int mbrtowc(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/,
            int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int ISandBox_mbstowcs_len(const char *src) {
  int src_idx, dest_idx;
  int status;
  mbstate_t ps;
  memset(&ps, 0, sizeof(mbstate_t));
  for (src_idx = dest_idx = 0; src[src_idx] != '\0';) {
    status = mbrtowc(NULL, &src[src_idx], MB_LEN_MAX, &ps);
    if (status < 0) {
      return status;
    }
    dest_idx++;
    src_idx += status;
  }
  return dest_idx;
}


x86 assembly (clang, -O0):
<ISandBox_mbstowcs_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     11af <ISandBox_mbstowcs_len+0x8f>
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    0x2e6f(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%edi
lea    -0x20(%rbp),%rcx
call   1030 <mbrtowc@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1198 <ISandBox_mbstowcs_len+0x78>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b5 <ISandBox_mbstowcs_len+0x95>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    114a <ISandBox_mbstowcs_len+0x2a>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ISandBox_mbstowcs_len>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0xc(%rsp),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
cmpb   $0x0,(%r14)
je     1189 <ISandBox_mbstowcs_len+0x69>
xor    %ebx,%ebx
mov    0x2e8f(%rip),%r12
lea    0xc(%rsp),%r15
mov    %r14,%rsi
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    (%r12),%edx
xor    %edi,%edi
mov    %r15,%rcx
call   1030 <mbrtowc@plt>
test   %eax,%eax
js     1187 <ISandBox_mbstowcs_len+0x67>
add    $0x1,%ebx
add    %eax,%ebp
movslq %ebp,%rax
lea    (%r14,%rax,1),%rsi
cmpb   $0x0,(%r14,%rax,1)
jne    1160 <ISandBox_mbstowcs_len+0x40>
jmp    1189 <ISandBox_mbstowcs_len+0x69>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@MB_LEN_MAX = global i32 0
define i32 @ISandBox_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 %21, label %7
7: ; preds = %1, %14
%8 = phi i8* [ %18, %14 ], [ %0, %1 ]
%9 = phi i32 [ %15, %14 ], [ 0, %1 ]
%10 = phi i32 [ %16, %14 ], [ 0, %1 ]
%11 = load i32, i32* @MB_LEN_MAX
%12 = call i32 @mbrtowc(i32* null, i8* %8, i32 %11, i32* %2)
%13 = icmp slt i32 %12, 0
br i1 %13, label %21, label %14
14: ; preds = %7
%15 = add nuw nsw i32 %9, 1
%16 = add nsw i32 %12, %10
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 0
br i1 %20, label %21, label %7
21: ; preds = %7, %14, %1
%22 = phi i32 [ 0, %1 ], [ %15, %14 ], [ %12, %7 ]
ret i32 %22
}