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

/* Variables and functions */
int EINVAL;
int ERANGE;
int /*<<< orphan*/ memcpy(scalar_t__ *, scalar_t__ const *, size_t);
int wcslen(scalar_t__ const *);
int wcscpy_s(wchar_t *dest, size_t numElem, const wchar_t *src) {
  size_t size = 0;
  if (!dest || !numElem)
    return EINVAL;
  dest[0] = 0;
  if (!src)
    return EINVAL;
  size = wcslen(src) + 1;
  if (size > numElem)
    return ERANGE;
  memcpy(dest, src, size * sizeof(wchar_t));
  return 0;
}


x86 assembly (clang, -O0):
<wcscpy_s>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1152 <wcscpy_s+0x32>
cmpq   $0x0,-0x18(%rbp)
jne    1163 <wcscpy_s+0x43>
mov    0x2e7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11d7 <wcscpy_s+0xb7>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
cmpq   $0x0,-0x20(%rbp)
jne    118a <wcscpy_s+0x6a>
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11d7 <wcscpy_s+0xb7>
mov    -0x20(%rbp),%rdi
call   1030 <wcslen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    11bb <wcscpy_s+0x9b>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11d7 <wcscpy_s+0xb7>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
shl    $0x3,%rdx
call   1040 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wcscpy_s>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1160 <wcscpy_s+0x40>
mov    %rsi,%r15
test   %rsi,%rsi
je     1160 <wcscpy_s+0x40>
mov    %rdx,%r14
mov    %rdi,%rbx
movq   $0x0,(%rdi)
test   %rdx,%rdx
je     1160 <wcscpy_s+0x40>
mov    %r14,%rdi
call   1030 <wcslen@plt>
add    $0x1,%eax
movslq %eax,%rdx
cmp    %r15,%rdx
jbe    116f <wcscpy_s+0x4f>
mov    0x2e8a(%rip),%rax
jmp    1167 <wcscpy_s+0x47>
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r14
pop    %r15
ret
shl    $0x3,%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <memcpy@plt>
xor    %eax,%eax
jmp    1169 <wcscpy_s+0x49>

LLVM IR (-O0):
@EINVAL = global i32 0
@ERANGE = global i32 0
define i32 @wcscpy_s(i64* %0, i64 %1, i64* %2) {
%4 = alloca i32
%5 = alloca i64*
%6 = alloca i64
%7 = alloca i64*
%8 = alloca i64
store i64* %0, i64** %5
store i64 %1, i64* %6
store i64* %2, i64** %7
store i64 0, i64* %8
%9 = load i64*, i64** %5
%10 = icmp ne i64* %9, null
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load i64, i64* %6
%13 = icmp ne i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %11, %3
%15 = load i32, i32* @EINVAL
store i32 %15, i32* %4
br label %39
16: ; preds = %11
%17 = load i64*, i64** %5
%18 = getelementptr inbounds i64, i64* %17, i64 0
store i64 0, i64* %18
%19 = load i64*, i64** %7
%20 = icmp ne i64* %19, null
br i1 %20, label %23, label %21
21: ; preds = %16
%22 = load i32, i32* @EINVAL
store i32 %22, i32* %4
br label %39
23: ; preds = %16
%24 = load i64*, i64** %7
%25 = call i32 @wcslen(i64* %24)
%26 = add nsw i32 %25, 1
%27 = sext i32 %26 to i64
store i64 %27, i64* %8
%28 = load i64, i64* %8
%29 = load i64, i64* %6
%30 = icmp ugt i64 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %23
%32 = load i32, i32* @ERANGE
store i32 %32, i32* %4
br label %39
33: ; preds = %23
%34 = load i64*, i64** %5
%35 = load i64*, i64** %7
%36 = load i64, i64* %8
%37 = mul i64 %36, 8
%38 = call i32 @memcpy(i64* %34, i64* %35, i64 %37)
store i32 0, i32* %4
br label %39
39: ; preds = %33, %31, %21, %14
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
@EINVAL = global i32 0
@ERANGE = global i32 0
define i32 @wcscpy_s(i64* %0, i64 %1, i64* %2) {
%4 = icmp ne i64* %0, null
%5 = icmp ne i64 %1, 0
%6 = and i1 %4, %5
br i1 %6, label %9, label %7
7: ; preds = %3
%8 = load i32, i32* @EINVAL
br label %23
9: ; preds = %3
store i64 0, i64* %0
%10 = icmp eq i64* %2, null
br i1 %10, label %11, label %13
11: ; preds = %9
%12 = load i32, i32* @EINVAL
br label %23
13: ; preds = %9
%14 = tail call i32 @wcslen(i64* %2)
%15 = add nsw i32 %14, 1
%16 = sext i32 %15 to i64
%17 = icmp ugt i64 %16, %1
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = load i32, i32* @ERANGE
br label %23
20: ; preds = %13
%21 = shl nsw i64 %16, 3
%22 = tail call i32 @memcpy(i64* %0, i64* %2, i64 %21)
br label %23
23: ; preds = %20, %18, %11, %7
%24 = phi i32 [ %19, %18 ], [ 0, %20 ], [ %12, %11 ], [ %8, %7 ]
ret i32 %24
}