#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 */
scalar_t__ malloc(int);
int /*<<< orphan*/ mbstowcs(int /*<<< orphan*/ *, char *, size_t);
scalar_t__ realloc(int /*<<< orphan*/ *, int);
int strlen(char *);
wchar_t *mbs2wcs(char *mbs) {
  static wchar_t *wcs = 0;
  static size_t bufSize = 0;
  size_t len = strlen(mbs) + 1;
  if (bufSize < len) {
    wcs = wcs ? (wchar_t *)realloc(wcs, sizeof(wchar_t) * len)
              : (wchar_t *)malloc(sizeof(wchar_t) * len);
    bufSize = len;
  }
  mbstowcs(wcs, mbs, bufSize);
  return wcs;
}


x86 assembly (clang, -O0):
<mbs2wcs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x10(%rbp)
mov    0x2eeb(%rip),%rax
cmp    -0x10(%rbp),%rax
jae    11c5 <mbs2wcs+0x85>
cmpq   $0x0,0x2ed1(%rip)
je     119c <mbs2wcs+0x5c>
mov    0x2ec4(%rip),%rdi
mov    -0x10(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
jmp    11af <mbs2wcs+0x6f>
mov    -0x10(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,0x2e8e(%rip)
mov    -0x10(%rbp),%rax
mov    %rax,0x2e8b(%rip)
mov    0x2e7c(%rip),%rdi
mov    -0x8(%rbp),%rsi
mov    0x2e79(%rip),%rdx
call   1030 <mbstowcs@plt>
mov    0x2e65(%rip),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mbs2wcs>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rbx
mov    0x2ef7(%rip),%rcx
mov    0x2ee8(%rip),%rdi
cmp    %rbx,%rcx
jae    1176 <mbs2wcs+0x36>
shl    $0x2,%eax
test   %rdi,%rdi
je     117b <mbs2wcs+0x3b>
mov    %eax,%esi
call   1060 <realloc@plt>
jmp    1182 <mbs2wcs+0x42>
mov    %rcx,%rbx
jmp    1193 <mbs2wcs+0x53>
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
mov    %rax,0x2ebc(%rip)
mov    %rbx,0x2ebd(%rip)
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <mbstowcs@plt>
mov    0x2ea3(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@mbs2wcs.wcs = internal global i32* null
@mbs2wcs.bufSize = internal global i64 0
define i32* @mbs2wcs(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i32 @strlen(i8* %4)
%6 = add nsw i32 %5, 1
%7 = sext i32 %6 to i64
store i64 %7, i64* %3
%8 = load i64, i64* @mbs2wcs.bufSize
%9 = load i64, i64* %3
%10 = icmp ult i64 %8, %9
br i1 %10, label %11, label %30
11: ; preds = %1
%12 = load i32*, i32** @mbs2wcs.wcs
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %21
14: ; preds = %11
%15 = load i32*, i32** @mbs2wcs.wcs
%16 = load i64, i64* %3
%17 = mul i64 4, %16
%18 = trunc i64 %17 to i32
%19 = call i64 @realloc(i32* %15, i32 %18)
%20 = inttoptr i64 %19 to i32*
br label %27
21: ; preds = %11
%22 = load i64, i64* %3
%23 = mul i64 4, %22
%24 = trunc i64 %23 to i32
%25 = call i64 @malloc(i32 %24)
%26 = inttoptr i64 %25 to i32*
br label %27
27: ; preds = %21, %14
%28 = phi i32* [ %20, %14 ], [ %26, %21 ]
store i32* %28, i32** @mbs2wcs.wcs
%29 = load i64, i64* %3
store i64 %29, i64* @mbs2wcs.bufSize
br label %30
30: ; preds = %27, %1
%31 = load i32*, i32** @mbs2wcs.wcs
%32 = load i8*, i8** %2
%33 = load i64, i64* @mbs2wcs.bufSize
%34 = call i32 @mbstowcs(i32* %31, i8* %32, i64 %33)
%35 = load i32*, i32** @mbs2wcs.wcs
ret i32* %35
}

LLVM IR (-O2):
@mbs2wcs.wcs = internal unnamed_addr global i32* null
@mbs2wcs.bufSize = internal unnamed_addr global i64 0
define i32* @mbs2wcs(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, 1
%4 = sext i32 %3 to i64
%5 = load i64, i64* @mbs2wcs.bufSize
%6 = icmp ult i64 %5, %4
%7 = load i32*, i32** @mbs2wcs.wcs
br i1 %6, label %8, label %18
8: ; preds = %1
%9 = icmp eq i32* %7, null
%10 = shl i32 %3, 2
br i1 %9, label %13, label %11
11: ; preds = %8
%12 = tail call i64 @realloc(i32* %7, i32 %10)
br label %15
13: ; preds = %8
%14 = tail call i64 @malloc(i32 %10)
br label %15
15: ; preds = %13, %11
%16 = phi i64 [ %12, %11 ], [ %14, %13 ]
%17 = inttoptr i64 %16 to i32*
store i32* %17, i32** @mbs2wcs.wcs
store i64 %4, i64* @mbs2wcs.bufSize
br label %18
18: ; preds = %1, %15
%19 = phi i64 [ %4, %15 ], [ %5, %1 ]
%20 = phi i32* [ %17, %15 ], [ %7, %1 ]
%21 = tail call i32 @mbstowcs(i32* %20, i8* %0, i64 %19)
%22 = load i32*, i32** @mbs2wcs.wcs
ret i32* %22
}