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

/* Variables and functions */
int MB_LEN_MAX;
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int wcrtomb(char *, int const, int /*<<< orphan*/ *);
int ISandBox_wcstombs_len(const wchar_t *src) {
  int src_idx, dest_idx;
  int status;
  char dummy[MB_LEN_MAX];
  mbstate_t ps;
  memset(&ps, 0, sizeof(mbstate_t));
  for (src_idx = dest_idx = 0; src[src_idx] != L'\0';) {
    status = wcrtomb(dummy, src[src_idx], &ps);
    src_idx++;
    dest_idx += status;
  }
  return dest_idx;
}


x86 assembly (clang, -O0):
<ISandBox_wcstombs_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2ead(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
lea    -0x2c(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
je     11ba <ISandBox_wcstombs_len+0x9a>
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    -0x2c(%rbp),%rdx
call   1040 <wcrtomb@plt>
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x14(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1176 <ISandBox_wcstombs_len+0x56>
mov    -0x10(%rbp),%eax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ISandBox_wcstombs_len>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r15
mov    0x2ea7(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
lea    -0x24(%rbp),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    (%r15),%esi
test   %esi,%esi
je     1188 <ISandBox_wcstombs_len+0x68>
add    $0x4,%r15
xor    %ebx,%ebx
lea    -0x24(%rbp),%r12
nop
mov    %r14,%rdi
mov    %r12,%rdx
call   1040 <wcrtomb@plt>
add    %eax,%ebx
mov    (%r15),%esi
add    $0x4,%r15
test   %esi,%esi
jne    1170 <ISandBox_wcstombs_len+0x50>
mov    %ebx,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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