#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*/ *reallocarray(int /*<<< orphan*/ *, size_t, int);
wchar_t *to_wide(const char s[]) {
  wchar_t *result = NULL;
  size_t len;
  len = mbstowcs(NULL, s, 0);
  if (len != (size_t)-1) {
    result = reallocarray(NULL, len + 1, sizeof(wchar_t));
    if (result != NULL) {
      (void)mbstowcs(result, s, len + 1);
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<to_wide>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rdi
call   1040 <mbstowcs@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
je     1192 <to_wide+0x72>
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%edx
call   1030 <reallocarray@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     118d <to_wide+0x6d>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
add    $0x1,%rdx
call   1040 <mbstowcs@plt>
jmp    1192 <to_wide+0x72>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<to_wide>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %r15d,%r15d
xor    %edi,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1040 <mbstowcs@plt>
cmp    $0xffffffffffffffff,%rax
je     1172 <to_wide+0x52>
mov    %rax,%rbx
add    $0x1,%rbx
xor    %r15d,%r15d
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <reallocarray@plt>
test   %rax,%rax
je     1172 <to_wide+0x52>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <mbstowcs@plt>
mov    %r12,%r15
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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