#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);
size_t wcrtomb(char *, int const, int /*<<< orphan*/ *);
size_t dvm_wcstombs_len(const wchar_t *src) {
  size_t src_idx = 0, dest_count = 0, wc_len = 0;
  char dummy[MB_LEN_MAX];
  mbstate_t ps;
  memset(&ps, 0, sizeof(mbstate_t));
  while (src[src_idx] != L'\0') {
    wc_len = wcrtomb(dummy, src[src_idx], &ps);
    if (wc_len == (size_t)-1) {
      return -1;
    }
    src_idx++;
    dest_count += wc_len;
  }
  return dest_count;
}


x86 assembly (clang, -O0):
<dvm_wcstombs_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
lea    -0x3c(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
je     11ea <dvm_wcstombs_len+0xca>
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    -0x3c(%rbp),%rdx
call   1040 <wcrtomb@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11cd <dvm_wcstombs_len+0xad>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
movl   $0x1,-0x40(%rbp)
jmp    11f9 <dvm_wcstombs_len+0xd9>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1180 <dvm_wcstombs_len+0x60>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dvm_wcstombs_len>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
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    %r15d,%r15d
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    (%rbx),%esi
test   %esi,%esi
je     1197 <dvm_wcstombs_len+0x77>
add    $0x4,%rbx
xor    %r15d,%r15d
lea    -0x24(%rbp),%r12
mov    %r14,%rdi
mov    %r12,%rdx
call   1040 <wcrtomb@plt>
cmp    $0xffffffffffffffff,%rax
je     1190 <dvm_wcstombs_len+0x70>
add    %rax,%r15
mov    (%rbx),%esi
add    $0x4,%rbx
test   %esi,%esi
jne    1170 <dvm_wcstombs_len+0x50>
jmp    1197 <dvm_wcstombs_len+0x77>
mov    $0xffffffffffffffff,%r15
mov    %r15,%rax
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 i64 @dvm_wcstombs_len(i32* %0) {
%2 = alloca i64
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %3
store i64 0, i64* %4
store i64 0, i64* %5
store i64 0, i64* %6
%11 = load i32, i32* @MB_LEN_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %7
%14 = alloca i8, i64 %12
store i64 %12, i64* %8
%15 = call i32 @memset(i32* %9, i32 0, i32 4)
br label %16
16: ; preds = %31, %1
%17 = load i32*, i32** %3
%18 = load i64, i64* %4
%19 = getelementptr inbounds i32, i32* %17, i64 %18
%20 = load i32, i32* %19
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %37
22: ; preds = %16
%23 = load i32*, i32** %3
%24 = load i64, i64* %4
%25 = getelementptr inbounds i32, i32* %23, i64 %24
%26 = load i32, i32* %25
%27 = call i64 @wcrtomb(i8* %14, i32 %26, i32* %9)
store i64 %27, i64* %6
%28 = load i64, i64* %6
%29 = icmp eq i64 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %22
store i64 -1, i64* %2
store i32 1, i32* %10
br label %39
31: ; preds = %22
%32 = load i64, i64* %4
%33 = add i64 %32, 1
store i64 %33, i64* %4
%34 = load i64, i64* %6
%35 = load i64, i64* %5
%36 = add i64 %35, %34
store i64 %36, i64* %5
br label %16
37: ; preds = %16
%38 = load i64, i64* %5
store i64 %38, i64* %2
store i32 1, i32* %10
br label %39
39: ; preds = %37, %30
%40 = load i8*, i8** %7
%41 = load i64, i64* %2
ret i64 %41
}

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