#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 wchar_t;

/* Variables and functions */
int *malloc(size_t);
size_t mbstowcs(int *, char const *, size_t);
int /*<<< orphan*/ memset(int *, int /*<<< orphan*/, size_t);
size_t strlen(char const *);
wchar_t *wcsdupchar(const char *string) {
  if (string == NULL)
    return NULL;
  size_t length = strlen(string);
  size_t buffer_size = sizeof(wchar_t) * (length + 1);
  wchar_t *buffer = malloc(buffer_size);
  memset(buffer, 0, buffer_size);
  if (mbstowcs(buffer, string, length) != length) {
    return NULL;
  }
  buffer[length] = L'\0';
  return buffer;
}


x86 assembly (clang, -O0):
<wcsdupchar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1164 <wcsdupchar+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11dc <wcsdupchar+0x9c>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
shl    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <mbstowcs@plt>
cmp    -0x18(%rbp),%rax
je     11c5 <wcsdupchar+0x85>
movq   $0x0,-0x8(%rbp)
jmp    11dc <wcsdupchar+0x9c>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wcsdupchar>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %rdi,%rdi
je     119c <wcsdupchar+0x5c>
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %rax,%r15
lea    0x4(,%rax,4),%r13
mov    %r13,%rdi
call   1060 <malloc@plt>
mov    %rax,%r12
xor    %r14d,%r14d
mov    %rax,%rdi
xor    %esi,%esi
mov    %r13,%rdx
call   1050 <memset@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <mbstowcs@plt>
cmp    %r15,%rax
jne    119f <wcsdupchar+0x5f>
movl   $0x0,(%r12,%r15,4)
mov    %r12,%r14
jmp    119f <wcsdupchar+0x5f>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i32* @wcsdupchar(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %13, label %3
3: ; preds = %1
%4 = tail call i64 @strlen(i8* %0)
%5 = shl i64 %4, 2
%6 = add i64 %5, 4
%7 = tail call i32* @malloc(i64 %6)
%8 = tail call i32 @memset(i32* %7, i32 0, i64 %6)
%9 = tail call i64 @mbstowcs(i32* %7, i8* %0, i64 %4)
%10 = icmp eq i64 %9, %4
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = getelementptr inbounds i32, i32* %7, i64 %4
store i32 0, i32* %12
br label %13
13: ; preds = %11, %3, %1
%14 = phi i32* [ null, %1 ], [ %7, %11 ], [ null, %3 ]
ret i32* %14
}