#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*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(int);
size_t mbstowcs(int /*<<< orphan*/ *, char const *, size_t);
wchar_t *_mbstowcs(wchar_t *dest, const char *src) {
  size_t len;
  len = mbstowcs(dest, src, 255);
  if (len == (size_t)-1)
    return NULL;
  else if (len >= 255) {
    len = mbstowcs(NULL, src, 0);
    dest = malloc(sizeof(wchar_t) * (len + 1));
    len = mbstowcs(dest, src, len);
    if (len == (size_t)-1) {
      free(dest);
      return NULL;
    }
  }
  return dest;
}


x86 assembly (clang, -O0):
<_mbstowcs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0xff,%edx
call   1040 <mbstowcs@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    116e <_mbstowcs+0x3e>
movq   $0x0,-0x8(%rbp)
jmp    11ef <_mbstowcs+0xbf>
cmpq   $0xff,-0x20(%rbp)
jb     11e2 <_mbstowcs+0xb2>
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rdi
call   1040 <mbstowcs@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <mbstowcs@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11dd <_mbstowcs+0xad>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ef <_mbstowcs+0xbf>
jmp    11e2 <_mbstowcs+0xb2>
jmp    11e7 <_mbstowcs+0xb7>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_mbstowcs>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    $0xff,%edx
call   1040 <mbstowcs@plt>
cmp    $0xffffffffffffffff,%rax
je     118d <_mbstowcs+0x5d>
cmp    $0xff,%rax
jb     118f <_mbstowcs+0x5f>
xor    %edi,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1040 <mbstowcs@plt>
mov    %rax,%r15
lea    0x4(,%rax,4),%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <mbstowcs@plt>
cmp    $0xffffffffffffffff,%rax
jne    118f <_mbstowcs+0x5f>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32* @_mbstowcs(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
store i32* %0, i32** %4
store i8* %1, i8** %5
%7 = load i32*, i32** %4
%8 = load i8*, i8** %5
%9 = call i64 @mbstowcs(i32* %7, i8* %8, i64 255)
store i64 %9, i64* %6
%10 = load i64, i64* %6
%11 = icmp eq i64 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %2
store i32* null, i32** %3
br label %37
13: ; preds = %2
%14 = load i64, i64* %6
%15 = icmp uge i64 %14, 255
br i1 %15, label %16, label %34
16: ; preds = %13
%17 = load i8*, i8** %5
%18 = call i64 @mbstowcs(i32* null, i8* %17, i64 0)
store i64 %18, i64* %6
%19 = load i64, i64* %6
%20 = add i64 %19, 1
%21 = mul i64 4, %20
%22 = trunc i64 %21 to i32
%23 = call i32* @malloc(i32 %22)
store i32* %23, i32** %4
%24 = load i32*, i32** %4
%25 = load i8*, i8** %5
%26 = load i64, i64* %6
%27 = call i64 @mbstowcs(i32* %24, i8* %25, i64 %26)
store i64 %27, i64* %6
%28 = load i64, i64* %6
%29 = icmp eq i64 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %16
%31 = load i32*, i32** %4
%32 = call i32 @free(i32* %31)
store i32* null, i32** %3
br label %37
33: ; preds = %16
br label %34
34: ; preds = %33, %13
br label %35
35: ; preds = %34
%36 = load i32*, i32** %4
store i32* %36, i32** %3
br label %37
37: ; preds = %35, %30, %12
%38 = load i32*, i32** %3
ret i32* %38
}

LLVM IR (-O2):
define i32* @_mbstowcs(i32* %0, i8* %1) {
%3 = tail call i64 @mbstowcs(i32* %0, i8* %1, i64 255)
%4 = icmp eq i64 %3, -1
br i1 %4, label %17, label %5
5: ; preds = %2
%6 = icmp ugt i64 %3, 254
br i1 %6, label %7, label %17
7: ; preds = %5
%8 = tail call i64 @mbstowcs(i32* null, i8* %1, i64 0)
%9 = trunc i64 %8 to i32
%10 = shl i32 %9, 2
%11 = add i32 %10, 4
%12 = tail call i32* @malloc(i32 %11)
%13 = tail call i64 @mbstowcs(i32* %12, i8* %1, i64 %8)
%14 = icmp eq i64 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %7
%16 = tail call i32 @free(i32* %12)
br label %17
17: ; preds = %7, %5, %2, %15
%18 = phi i32* [ null, %15 ], [ null, %2 ], [ %12, %7 ], [ %0, %5 ]
ret i32* %18
}