#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 mbstowcs(int /*<<< orphan*/ *, char const *, size_t);
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 = malloc((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   1030 <mbstowcs@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
je     118d <to_wide+0x6d>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
shl    $0x2,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1188 <to_wide+0x68>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
add    $0x1,%rdx
call   1030 <mbstowcs@plt>
jmp    118d <to_wide+0x6d>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<to_wide>:
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     116c <to_wide+0x4c>
mov    %rax,%rbx
lea    0x4(,%rax,4),%rdi
call   1040 <malloc@plt>
test   %rax,%rax
je     1169 <to_wide+0x49>
mov    %rax,%r15
add    $0x1,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <mbstowcs@plt>
jmp    116c <to_wide+0x4c>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
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 %23
9: ; preds = %1
%10 = load i64, i64* %4
%11 = add i64 %10, 1
%12 = mul i64 %11, 4
%13 = call i32* @malloc(i64 %12)
store i32* %13, i32** %3
%14 = load i32*, i32** %3
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %22
16: ; preds = %9
%17 = load i32*, i32** %3
%18 = load i8*, i8** %2
%19 = load i64, i64* %4
%20 = add i64 %19, 1
%21 = call i64 @mbstowcs(i32* %17, i8* %18, i64 %20)
br label %22
22: ; preds = %16, %9
br label %23
23: ; preds = %22, %1
%24 = load i32*, i32** %3
ret i32* %24
}

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 %11, label %4
4: ; preds = %1
%5 = add nuw i64 %2, 1
%6 = shl i64 %5, 2
%7 = tail call i32* @malloc(i64 %6)
%8 = icmp eq i32* %7, null
br i1 %8, label %11, label %9
9: ; preds = %4
%10 = tail call i64 @mbstowcs(i32* %7, i8* %0, i64 %5)
br label %11
11: ; preds = %4, %9, %1
%12 = phi i32* [ %7, %9 ], [ null, %4 ], [ null, %1 ]
ret i32* %12
}