#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 /*<<< orphan*/ wchar_t;

/* Variables and functions */
size_t mbstowcs(int /*<<< orphan*/ *, char const *, size_t);
int /*<<< orphan*/ *xcalloc(size_t, int);
int /*<<< orphan*/ xfree(int /*<<< orphan*/ *);
wchar_t *mbs_duplicate_as_wcs(const char *str) {
  size_t bytelen;
  wchar_t *output;
  bytelen = mbstowcs(NULL, str, 0);
  if (bytelen == (size_t)-1) {
    return (NULL);
  }
  output = xcalloc(bytelen + 1, sizeof(wchar_t));
  bytelen = mbstowcs(output, str, bytelen + 1);
  if (bytelen == (size_t)-1) {
    xfree(output);
    return (NULL);
  }
  return (output);
}


x86 assembly (clang, -O0):
<mbs_duplicate_as_wcs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rdi
call   1030 <mbstowcs@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    1168 <mbs_duplicate_as_wcs+0x38>
movq   $0x0,-0x8(%rbp)
jmp    11c0 <mbs_duplicate_as_wcs+0x90>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
mov    $0x4,%esi
call   1050 <xcalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
add    $0x1,%rdx
call   1030 <mbstowcs@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    11b8 <mbs_duplicate_as_wcs+0x88>
mov    -0x20(%rbp),%rdi
call   1040 <xfree@plt>
movq   $0x0,-0x8(%rbp)
jmp    11c0 <mbs_duplicate_as_wcs+0x90>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mbs_duplicate_as_wcs>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %r15d,%r15d
xor    %edi,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1030 <mbstowcs@plt>
cmp    $0xffffffffffffffff,%rax
je     1183 <mbs_duplicate_as_wcs+0x53>
mov    %rax,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x4,%esi
call   1050 <xcalloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <mbstowcs@plt>
cmp    $0xffffffffffffffff,%rax
jne    1183 <mbs_duplicate_as_wcs+0x53>
mov    %r15,%rdi
call   1040 <xfree@plt>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32* @mbs_duplicate_as_wcs(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i64 @mbstowcs(i32* null, i8* %6, i64 0)
store i64 %7, i64* %4
%8 = load i64, i64* %4
%9 = icmp eq i64 %8, -1
br i1 %9, label %10, label %11
10: ; preds = %1
store i32* null, i32** %2
br label %27
11: ; preds = %1
%12 = load i64, i64* %4
%13 = add i64 %12, 1
%14 = call i32* @xcalloc(i64 %13, i32 4)
store i32* %14, i32** %5
%15 = load i32*, i32** %5
%16 = load i8*, i8** %3
%17 = load i64, i64* %4
%18 = add i64 %17, 1
%19 = call i64 @mbstowcs(i32* %15, i8* %16, i64 %18)
store i64 %19, i64* %4
%20 = load i64, i64* %4
%21 = icmp eq i64 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %11
%23 = load i32*, i32** %5
%24 = call i32 @xfree(i32* %23)
store i32* null, i32** %2
br label %27
25: ; preds = %11
%26 = load i32*, i32** %5
store i32* %26, i32** %2
br label %27
27: ; preds = %25, %22, %10
%28 = load i32*, i32** %2
ret i32* %28
}

LLVM IR (-O2):
define i32* @mbs_duplicate_as_wcs(i8* %0) {
%2 = tail call i64 @mbstowcs(i32* null, i8* %0, i64 0)
%3 = icmp eq i64 %2, -1
br i1 %3, label %11, label %4
4: ; preds = %1
%5 = add nuw i64 %2, 1
%6 = tail call i32* @xcalloc(i64 %5, i32 4)
%7 = tail call i64 @mbstowcs(i32* %6, i8* %0, i64 %5)
%8 = icmp eq i64 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = tail call i32 @xfree(i32* %6)
br label %11
11: ; preds = %4, %1, %9
%12 = phi i32* [ null, %9 ], [ null, %1 ], [ %6, %4 ]
ret i32* %12
}