#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(size_t);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                          size_t);
int wcslen(int /*<<< orphan*/ const *);
wchar_t *dupwcs(const wchar_t *s) {
  size_t n;
  wchar_t *p = NULL;
  if (s != NULL) {
    n = (1 + wcslen(s)) * sizeof(wchar_t);
    if ((p = (wchar_t *)malloc(n)) != NULL)
      memcpy(p, s, n);
  }
  return p;
}


x86 assembly (clang, -O0):
<dupwcs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1192 <dupwcs+0x62>
mov    -0x8(%rbp),%rdi
call   1030 <wcslen@plt>
add    $0x1,%eax
cltq
shl    $0x2,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     118d <dupwcs+0x5d>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <memcpy@plt>
jmp    1192 <dupwcs+0x62>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dupwcs>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     116c <dupwcs+0x3c>
mov    %rdi,%r14
call   1030 <wcslen@plt>
cltq
lea    0x4(,%rax,4),%r15
mov    %r15,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     116c <dupwcs+0x3c>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
jmp    116e <dupwcs+0x3e>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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