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


x86 assembly (clang, -O0):
<towcs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x10(%rbp),%rsi
mov    %rcx,%rdi
mov    %rcx,%rdx
call   1030 <mbsrtowcs@plt>
mov    %rax,-0x20(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    115a <towcs+0x3a>
movq   $0x0,-0x8(%rbp)
jmp    11a7 <towcs+0x87>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
shl    $0x2,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1186 <towcs+0x66>
movq   $0x0,-0x8(%rbp)
jmp    11a7 <towcs+0x87>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
add    $0x1,%rdx
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
call   1030 <mbsrtowcs@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<towcs>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,(%rsp)
xor    %r14d,%r14d
mov    %rsp,%rsi
xor    %edi,%edi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <mbsrtowcs@plt>
cmp    $0xffffffffffffffff,%rax
je     1170 <towcs+0x50>
mov    %rax,%rbx
lea    0x4(,%rax,4),%rdi
call   1040 <malloc@plt>
test   %rax,%rax
je     116d <towcs+0x4d>
mov    %rax,%r14
add    $0x1,%rbx
mov    %rsp,%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1030 <mbsrtowcs@plt>
jmp    1170 <towcs+0x50>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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