#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*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ *malloc(size_t);
int wcslen(int /*<<< orphan*/ const *);
int /*<<< orphan*/ *wmemcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                            size_t);
wchar_t *wcsdup(const wchar_t *str) {
  wchar_t *copy;
  size_t len;
  assert(str != NULL);
  len = wcslen(str) + 1;
  copy = malloc(len * sizeof(wchar_t));
  if (!copy)
    return NULL;
  return wmemcpy(copy, str, len);
}


x86 assembly (clang, -O0):
<wcsdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1040 <wcslen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
shl    $0x2,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1199 <wcsdup+0x59>
movq   $0x0,-0x8(%rbp)
jmp    11ae <wcsdup+0x6e>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <wmemcpy@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wcsdup>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1050 <assert@plt>
mov    %r14,%rdi
call   1040 <wcslen@plt>
movslq %eax,%rbx
lea    0x4(,%rbx,4),%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     118b <wcsdup+0x4b>
add    $0x1,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <wmemcpy@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32* @wcsdup(i32* %0) {
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i64
store i32* %0, i32** %3
%6 = load i32*, i32** %3
%7 = icmp ne i32* %6, null
%8 = zext i1 %7 to i32
%9 = call i32 @assert(i32 %8)
%10 = load i32*, i32** %3
%11 = call i32 @wcslen(i32* %10)
%12 = add nsw i32 %11, 1
%13 = sext i32 %12 to i64
store i64 %13, i64* %5
%14 = load i64, i64* %5
%15 = mul i64 %14, 4
%16 = call i32* @malloc(i64 %15)
store i32* %16, i32** %4
%17 = load i32*, i32** %4
%18 = icmp ne i32* %17, null
br i1 %18, label %20, label %19
19: ; preds = %1
store i32* null, i32** %2
br label %25
20: ; preds = %1
%21 = load i32*, i32** %4
%22 = load i32*, i32** %3
%23 = load i64, i64* %5
%24 = call i32* @wmemcpy(i32* %21, i32* %22, i64 %23)
store i32* %24, i32** %2
br label %25
25: ; preds = %20, %19
%26 = load i32*, i32** %2
ret i32* %26
}

LLVM IR (-O2):
define i32* @wcsdup(i32* %0) {
%2 = icmp ne i32* %0, null
%3 = zext i1 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = tail call i32 @wcslen(i32* %0)
%6 = add nsw i32 %5, 1
%7 = sext i32 %6 to i64
%8 = shl nsw i64 %7, 2
%9 = tail call i32* @malloc(i64 %8)
%10 = icmp eq i32* %9, null
br i1 %10, label %13, label %11
11: ; preds = %1
%12 = tail call i32* @wmemcpy(i32* %9, i32* %0, i64 %7)
br label %13
13: ; preds = %1, %11
%14 = phi i32* [ %12, %11 ], [ null, %1 ]
ret i32* %14
}