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

/* Variables and functions */
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int wcrtomb(char *, char const, int /*<<< orphan*/ *);
void ISandBox_wcstombs_i(const wchar_t *src, char *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 = wcrtomb(&dest[dest_idx], src[src_idx], &ps);
    src_idx++;
    dest_idx += status;
  }
  dest[dest_idx] = '\0';
}


x86 assembly (clang, -O0):
<ISandBox_wcstombs_i>:
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   1030 <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     119d <ISandBox_wcstombs_i+0x7d>
mov    -0x10(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
lea    -0x20(%rbp),%rdx
movsbl (%rax,%rcx,1),%esi
call   1040 <wcrtomb@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    114e <ISandBox_wcstombs_i+0x2e>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ISandBox_wcstombs_i>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
lea    0x4(%rsp),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    (%r12),%al
test   %al,%al
je     1184 <ISandBox_wcstombs_i+0x64>
add    $0x1,%r12
lea    0x4(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movslq %ebx,%rbx
lea    (%r14,%rbx,1),%rdi
movsbl %al,%esi
mov    %r15,%rdx
call   1040 <wcrtomb@plt>
add    %eax,%ebx
movzbl (%r12),%eax
add    $0x1,%r12
test   %al,%al
jne    1160 <ISandBox_wcstombs_i+0x40>
movslq %ebx,%rbx
movb   $0x0,(%r14,%rbx,1)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @ISandBox_wcstombs_i(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %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 i8*, i8** %4
%20 = load i32, i32* %6
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %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 i8, i8* %26
%28 = call i32 @wcrtomb(i8* %22, i8 signext %27, i32* %8)
store i32 %28, i32* %7
%29 = load i32, i32* %5
%30 = add nsw i32 %29, 1
store i32 %30, i32* %5
%31 = load i32, i32* %7
%32 = load i32, i32* %6
%33 = add nsw i32 %32, %31
store i32 %33, i32* %6
br label %10
34: ; preds = %10
%35 = load i8*, i8** %4
%36 = load i32, i32* %6
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 0, i8* %38
ret void
}

LLVM IR (-O2):
define void @ISandBox_wcstombs_i(i8* 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 i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %22, label %8
8: ; preds = %2, %8
%9 = phi i64 [ %15, %8 ], [ 0, %2 ]
%10 = phi i8 [ %18, %8 ], [ %6, %2 ]
%11 = phi i32 [ %16, %8 ], [ 0, %2 ]
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %1, i64 %12
%14 = call i32 @wcrtomb(i8* %13, i8 signext %10, i32* %3)
%15 = add nuw i64 %9, 1
%16 = add nsw i32 %14, %11
%17 = getelementptr inbounds i8, i8* %0, i64 %15
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
br i1 %19, label %20, label %8
20: ; preds = %8
%21 = sext i32 %16 to i64
br label %22
22: ; preds = %20, %2
%23 = phi i64 [ 0, %2 ], [ %21, %20 ]
%24 = getelementptr inbounds i8, i8* %1, i64 %23
store i8 0, i8* %24
ret void
}