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


x86 assembly (clang, -O0):
<dvm_mbstowcs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-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    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     11b7 <dvm_mbstowcs+0x97>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
add    -0x18(%rbp),%rsi
mov    0x2e55(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rcx
call   1030 <mbrtowc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1158 <dvm_mbstowcs+0x38>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dvm_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)
mov    %rbx,0x10(%rsp)
je     1194 <dvm_mbstowcs+0x74>
xor    %r13d,%r13d
mov    0x2e82(%rip),%rbp
lea    0xc(%rsp),%r15
mov    %r12,%rsi
xor    %r14d,%r14d
nopl   0x0(%rax)
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
mov    %r15,%rcx
call   1030 <mbrtowc@plt>
add    $0x1,%r13
add    %rax,%r14
lea    (%r12,%r14,1),%rsi
add    $0x4,%rbx
cmpb   $0x0,(%r12,%r14,1)
jne    1170 <dvm_mbstowcs+0x50>
mov    0x10(%rsp),%rax
movl   $0x0,(%rax,%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 @dvm_mbstowcs(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
store i64 0, i64* %5
store i64 0, i64* %6
store i64 0, i64* %7
%9 = call i32 @memset(i32* %8, i32 0, i32 4)
br label %10
10: ; preds = %17, %2
%11 = load i8*, i8** %3
%12 = load i64, i64* %5
%13 = getelementptr inbounds i8, i8* %11, 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 %31
17: ; preds = %10
%18 = load i32*, i32** %4
%19 = load i64, i64* %6
%20 = getelementptr inbounds i32, i32* %18, i64 %19
%21 = load i8*, i8** %3
%22 = load i64, i64* %5
%23 = getelementptr inbounds i8, i8* %21, i64 %22
%24 = load i32, i32* @MB_LEN_MAX
%25 = call i64 @mbrtowc(i32* %20, i8* %23, i32 %24, i32* %8)
store i64 %25, i64* %7
%26 = load i64, i64* %6
%27 = add i64 %26, 1
store i64 %27, i64* %6
%28 = load i64, i64* %7
%29 = load i64, i64* %5
%30 = add i64 %29, %28
store i64 %30, i64* %5
br label %10
31: ; preds = %10
%32 = load i32*, i32** %4
%33 = load i64, i64* %6
%34 = getelementptr inbounds i32, i32* %32, i64 %33
store i32 0, i32* %34
ret void
}

LLVM IR (-O2):
@MB_LEN_MAX = global i32 0
define void @dvm_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 %20, label %8
8: ; preds = %2, %8
%9 = phi i8* [ %17, %8 ], [ %0, %2 ]
%10 = phi i64 [ %15, %8 ], [ 0, %2 ]
%11 = phi i64 [ %16, %8 ], [ 0, %2 ]
%12 = getelementptr inbounds i32, i32* %1, i64 %10
%13 = load i32, i32* @MB_LEN_MAX
%14 = call i64 @mbrtowc(i32* %12, i8* %9, i32 %13, i32* %3)
%15 = add i64 %10, 1
%16 = add i64 %14, %11
%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 %8
20: ; preds = %8, %2
%21 = phi i64 [ 0, %2 ], [ %15, %8 ]
%22 = getelementptr inbounds i32, i32* %1, i64 %21
store i32 0, i32* %22
ret void
}