#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*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
size_t wcrtomb(char *, int const, int /*<<< orphan*/ *);
void dvm_wcstombs_i(const wchar_t *src, char *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] != L'\0') {
    wc_len = wcrtomb(&dest[dest_idx], src[src_idx], &ps);
    if (wc_len == (size_t)-1) {
      return;
    }
    src_idx++;
    dest_idx += wc_len;
  }
  dest[dest_idx] = '\0';
}


x86 assembly (clang, -O0):
<dvm_wcstombs_i>:
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   1030 <memset@plt>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
je     11b7 <dvm_wcstombs_i+0x97>
mov    -0x10(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    -0x2c(%rbp),%rdx
call   1040 <wcrtomb@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    119a <dvm_wcstombs_i+0x7a>
jmp    11c3 <dvm_wcstombs_i+0xa3>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1158 <dvm_wcstombs_i+0x38>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dvm_wcstombs_i>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x4(%rsp),%rdi
xor    %r12d,%r12d
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    (%rbx),%esi
test   %esi,%esi
je     117f <dvm_wcstombs_i+0x5f>
add    $0x4,%rbx
xor    %r12d,%r12d
lea    0x4(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    (%r14,%r12,1),%rdi
mov    %r15,%rdx
call   1040 <wcrtomb@plt>
cmp    $0xffffffffffffffff,%rax
je     1184 <dvm_wcstombs_i+0x64>
add    %rax,%r12
mov    (%rbx),%esi
add    $0x4,%rbx
test   %esi,%esi
jne    1160 <dvm_wcstombs_i+0x40>
movb   $0x0,(%r14,%r12,1)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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