#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 /*<<< orphan*/ MB_LEN_MAX;
int mbrtowc(int *, char const *, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
void ISandBox_mbstowcs(const char *src, wchar_t *dest) {
  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(&dest[dest_idx], &src[src_idx], MB_LEN_MAX, &ps);
    dest_idx++;
    src_idx += status;
  }
  dest[dest_idx] = L'\0';
}


x86 assembly (clang, -O0):
<ISandBox_mbstowcs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-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    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     11a9 <ISandBox_mbstowcs+0x89>
mov    -0x10(%rbp),%rdi
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    0x2e5c(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
call   1030 <mbrtowc@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    114e <ISandBox_mbstowcs+0x2e>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ISandBox_mbstowcs>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%r12
lea    0xc(%rsp),%rdi
xor    %r13d,%r13d
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
cmpb   $0x0,(%r12)
je     119f <ISandBox_mbstowcs+0x7f>
mov    0x2e8a(%rip),%rbp
lea    0xc(%rsp),%r15
mov    %rbx,0x10(%rsp)
xor    %r14d,%r14d
mov    %r12,%rsi
cs nopw 0x0(%rax,%rax,1)
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
mov    %r15,%rcx
call   1030 <mbrtowc@plt>
add    $0x1,%r14
add    %eax,%r13d
movslq %r13d,%rax
lea    (%r12,%rax,1),%rsi
add    $0x4,%rbx
cmpb   $0x0,(%r12,%rax,1)
jne    1170 <ISandBox_mbstowcs+0x50>
mov    %r14d,%r13d
mov    0x10(%rsp),%rbx
movl   $0x0,(%rbx,%r13,4)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@MB_LEN_MAX = global i32 0
define void @ISandBox_mbstowcs(i8* %0, i32* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i32 @memset(i32* %3, i32 0, i32 4)
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %23, label %8
8: ; preds = %2, %8
%9 = phi i64 [ %15, %8 ], [ 0, %2 ]
%10 = phi i8* [ %18, %8 ], [ %0, %2 ]
%11 = phi i32 [ %16, %8 ], [ 0, %2 ]
%12 = getelementptr inbounds i32, i32* %1, i64 %9
%13 = load i32, i32* @MB_LEN_MAX
%14 = call i32 @mbrtowc(i32* %12, i8* %10, i32 %13, i32* %3)
%15 = add nuw i64 %9, 1
%16 = add nsw i32 %14, %11
%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 %8
21: ; preds = %8
%22 = and i64 %15, 4294967295
br label %23
23: ; preds = %21, %2
%24 = phi i64 [ 0, %2 ], [ %22, %21 ]
%25 = getelementptr inbounds i32, i32* %1, i64 %24
store i32 0, i32* %25
ret void
}