#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*/ exit(int);
int /*<<< orphan*/ *malloc(int);
int mbstowcs(int /*<<< orphan*/ *, char const *, int);
int /*<<< orphan*/ perror(char *);
wchar_t *mb2wc(const char *s) {
  int wl = mbstowcs(NULL, s, 0);
  if (wl > 0) {
    wchar_t *ws = malloc((wl + 1) * sizeof(wchar_t));
    mbstowcs(ws, s, wl + 1);
    return ws;
  }
  perror("mbstowcs");
  exit(1);
}


x86 assembly (clang, -O0):
<mb2wc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
xor    %edx,%edx
call   1030 <mbstowcs@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    119c <mb2wc+0x5c>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
add    $0x1,%edx
call   1030 <mbstowcs@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret
lea    0xe5d(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<mb2wc>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %edi,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1030 <mbstowcs@plt>
test   %eax,%eax
jle    1180 <mb2wc+0x40>
mov    %eax,%ebp
add    $0x1,%ebp
lea    0x4(,%rax,4),%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1030 <mbstowcs@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe79(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"mbstowcs\00"
define i32* @mb2wc(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32 @mbstowcs(i32* null, i8* %5, i32 0)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %22
9: ; preds = %1
%10 = load i32, i32* %3
%11 = add nsw i32 %10, 1
%12 = sext i32 %11 to i64
%13 = mul i64 %12, 4
%14 = trunc i64 %13 to i32
%15 = call i32* @malloc(i32 %14)
store i32* %15, i32** %4
%16 = load i32*, i32** %4
%17 = load i8*, i8** %2
%18 = load i32, i32* %3
%19 = add nsw i32 %18, 1
%20 = call i32 @mbstowcs(i32* %16, i8* %17, i32 %19)
%21 = load i32*, i32** %4
ret i32* %21
22: ; preds = %1
%23 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@.str = [9 x i8] c"mbstowcs\00"
define i32* @mb2wc(i8* %0) {
%2 = tail call i32 @mbstowcs(i32* null, i8* %0, i32 0)
%3 = icmp sgt i32 %2, 0
br i1 %3, label %4, label %9
4: ; preds = %1
%5 = add nuw nsw i32 %2, 1
%6 = shl i32 %5, 2
%7 = tail call i32* @malloc(i32 %6)
%8 = tail call i32 @mbstowcs(i32* %7, i8* %0, i32 %5)
ret i32* %7
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
}