#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 */
int /*<<< orphan*/ *malloc(size_t);
scalar_t__ mbstowcs(int /*<<< orphan*/ *, char const *, size_t);
wchar_t *mbs_to_wcs(const char *s) {
  size_t n;
  wchar_t *wcs;
  n = mbstowcs((wchar_t *)0, s, 0);
  if (n < 0)
    return NULL;
  wcs = malloc((n + 1) * sizeof(wchar_t));
  if (!wcs)
    return NULL;
  if (mbstowcs(wcs, s, n + 1) < 0)
    return NULL;
  return wcs;
}


x86 assembly (clang, -O0):
<mbs_to_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   $0x0,-0x18(%rbp)
jae    1158 <mbs_to_wcs+0x38>
movq   $0x0,-0x8(%rbp)
jmp    11b9 <mbs_to_wcs+0x99>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
shl    $0x2,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1185 <mbs_to_wcs+0x65>
movq   $0x0,-0x8(%rbp)
jmp    11b9 <mbs_to_wcs+0x99>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
add    $0x1,%rdx
call   1030 <mbstowcs@plt>
cmp    $0x0,%rax
jge    11b1 <mbs_to_wcs+0x91>
movq   $0x0,-0x8(%rbp)
jmp    11b9 <mbs_to_wcs+0x99>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mbs_to_wcs>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %r12d,%r12d
xor    %edi,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1030 <mbstowcs@plt>
mov    %rax,%rbx
lea    0x4(,%rax,4),%rdi
call   1040 <malloc@plt>
test   %rax,%rax
je     116e <mbs_to_wcs+0x4e>
mov    %rax,%r15
add    $0x1,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <mbstowcs@plt>
xor    %r12d,%r12d
test   %rax,%rax
cmovns %r15,%r12
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32* @mbs_to_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 ult i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store i32* null, i32** %2
br label %29
11: ; preds = %1
%12 = load i64, i64* %4
%13 = add i64 %12, 1
%14 = mul i64 %13, 4
%15 = call i32* @malloc(i64 %14)
store i32* %15, i32** %5
%16 = load i32*, i32** %5
%17 = icmp ne i32* %16, null
br i1 %17, label %19, label %18
18: ; preds = %11
store i32* null, i32** %2
br label %29
19: ; preds = %11
%20 = load i32*, i32** %5
%21 = load i8*, i8** %3
%22 = load i64, i64* %4
%23 = add i64 %22, 1
%24 = call i64 @mbstowcs(i32* %20, i8* %21, i64 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %19
store i32* null, i32** %2
br label %29
27: ; preds = %19
%28 = load i32*, i32** %5
store i32* %28, i32** %2
br label %29
29: ; preds = %27, %26, %18, %10
%30 = load i32*, i32** %2
ret i32* %30
}

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