#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 */
size_t wcsnlen(int /*<<< orphan*/ const *, size_t);
int /*<<< orphan*/ *wcsnstr(int /*<<< orphan*/ const *,
                            int /*<<< orphan*/ const *, size_t);
int /*<<< orphan*/ wmemcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                           size_t);
int /*<<< orphan*/ wmemset(int /*<<< orphan*/ *, int /*<<< orphan*/, size_t);
wchar_t *wcsstrncpy(wchar_t *restrict whither, const wchar_t *restrict whence,
                    const wchar_t *restrict str, size_t maxlen) {
  const wchar_t *stop = wcsnstr(whence, str, maxlen);
  size_t n = stop == NULL ? wcsnlen(whence, maxlen) : (size_t)(stop - whence);
  wchar_t *r = stop == NULL ? NULL : (whither + n);
  wmemcpy(whither, whence, n);
  wmemset(whither + n, 0, maxlen - n);
  return r;
}


x86 assembly (clang, -O0):
<wcsstrncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <wcsnstr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118e <wcsstrncpy+0x4e>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <wcsnlen@plt>
mov    %rax,-0x40(%rbp)
jmp    11a1 <wcsstrncpy+0x61>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
sar    $0x2,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11bf <wcsstrncpy+0x7f>
xor    %eax,%eax
mov    %rax,-0x48(%rbp)
jmp    11d2 <wcsstrncpy+0x92>
mov    -0x8(%rbp),%rax
mov    -0x30(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <wmemcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rdx
sub    -0x30(%rbp),%rdx
xor    %esi,%esi
call   1050 <wmemset@plt>
mov    -0x38(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wcsstrncpy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rsi,%rdi
mov    %rdx,%rsi
mov    %rcx,%rdx
call   1030 <wcsnstr@plt>
mov    %rax,%r13
test   %rax,%rax
je     1176 <wcsstrncpy+0x36>
mov    %r13,%rbx
sub    %r15,%rbx
sar    $0x2,%rbx
jmp    1184 <wcsstrncpy+0x44>
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <wcsnlen@plt>
mov    %rax,%rbx
test   %r13,%r13
lea    (%r12,%rbx,4),%rbp
cmovne %rbp,%r13
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <wmemcpy@plt>
sub    %rbx,%r14
mov    %rbp,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1050 <wmemset@plt>
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32* @wcsstrncpy(i32* noalias %0, i32* noalias %1, i32* noalias %2, i64 %3) {
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i32*
store i32* %0, i32** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i64 %3, i64* %8
%12 = load i32*, i32** %6
%13 = load i32*, i32** %7
%14 = load i64, i64* %8
%15 = call i32* @wcsnstr(i32* %12, i32* %13, i64 %14)
store i32* %15, i32** %9
%16 = load i32*, i32** %9
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %22
18: ; preds = %4
%19 = load i32*, i32** %6
%20 = load i64, i64* %8
%21 = call i64 @wcsnlen(i32* %19, i64 %20)
br label %29
22: ; preds = %4
%23 = load i32*, i32** %9
%24 = load i32*, i32** %6
%25 = ptrtoint i32* %23 to i64
%26 = ptrtoint i32* %24 to i64
%27 = sub i64 %25, %26
%28 = sdiv exact i64 %27, 4
br label %29
29: ; preds = %22, %18
%30 = phi i64 [ %21, %18 ], [ %28, %22 ]
store i64 %30, i64* %10
%31 = load i32*, i32** %9
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %34
33: ; preds = %29
br label %38
34: ; preds = %29
%35 = load i32*, i32** %5
%36 = load i64, i64* %10
%37 = getelementptr inbounds i32, i32* %35, i64 %36
br label %38
38: ; preds = %34, %33
%39 = phi i32* [ null, %33 ], [ %37, %34 ]
store i32* %39, i32** %11
%40 = load i32*, i32** %5
%41 = load i32*, i32** %6
%42 = load i64, i64* %10
%43 = call i32 @wmemcpy(i32* %40, i32* %41, i64 %42)
%44 = load i32*, i32** %5
%45 = load i64, i64* %10
%46 = getelementptr inbounds i32, i32* %44, i64 %45
%47 = load i64, i64* %8
%48 = load i64, i64* %10
%49 = sub i64 %47, %48
%50 = call i32 @wmemset(i32* %46, i32 0, i64 %49)
%51 = load i32*, i32** %11
ret i32* %51
}

LLVM IR (-O2):
define i32* @wcsstrncpy(i32* noalias %0, i32* noalias %1, i32* noalias %2, i64 %3) {
%5 = tail call i32* @wcsnstr(i32* %1, i32* %2, i64 %3)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i64 @wcsnlen(i32* %1, i64 %3)
br label %14
9: ; preds = %4
%10 = ptrtoint i32* %5 to i64
%11 = ptrtoint i32* %1 to i64
%12 = sub i64 %10, %11
%13 = ashr exact i64 %12, 2
br label %14
14: ; preds = %9, %7
%15 = phi i64 [ %8, %7 ], [ %13, %9 ]
%16 = getelementptr inbounds i32, i32* %0, i64 %15
%17 = select i1 %6, i32* null, i32* %16
%18 = tail call i32 @wmemcpy(i32* %0, i32* %1, i64 %15)
%19 = sub i64 %3, %15
%20 = tail call i32 @wmemset(i32* %16, i32 0, i64 %19)
ret i32* %17
}