#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*/ wmemset(int /*<<< orphan*/ *, int /*<<< orphan*/, size_t);
wchar_t *wcsncpy(wchar_t *d, const wchar_t *s, size_t n) {
  wchar_t *a = d;
  while (n && (*d++ = *s++))
    n--;
  wmemset(d, 0, n);
  return a;
}


x86 assembly (clang, -O0):
<wcsncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x21(%rbp)
je     1167 <wcsncpy+0x57>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%eax
mov    -0x8(%rbp),%rcx
mov    %rcx,%rdx
add    $0x4,%rdx
mov    %rdx,-0x8(%rbp)
mov    %eax,(%rcx)
cmp    $0x0,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1177 <wcsncpy+0x67>
jmp    1188 <wcsncpy+0x78>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    112c <wcsncpy+0x1c>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1030 <wmemset@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wcsncpy>:
push   %rbx
mov    %rdi,%rbx
test   %rdx,%rdx
je     113e <wcsncpy+0x2e>
lea    0x4(%rbx),%rax
nopl   (%rax)
mov    %rax,%rdi
mov    (%rsi),%eax
mov    %eax,-0x4(%rdi)
test   %eax,%eax
je     1143 <wcsncpy+0x33>
add    $0x4,%rsi
lea    0x4(%rdi),%rax
add    $0xffffffffffffffff,%rdx
jne    1120 <wcsncpy+0x10>
xor    %edx,%edx
jmp    1143 <wcsncpy+0x33>
xor    %edx,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <wmemset@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
define i32* @wcsncpy(i32* %0, i32* %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i32*
store i32* %0, i32** %4
store i32* %1, i32** %5
store i64 %2, i64* %6
%8 = load i32*, i32** %4
store i32* %8, i32** %7
br label %9
9: ; preds = %21, %3
%10 = load i64, i64* %6
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %19
12: ; preds = %9
%13 = load i32*, i32** %5
%14 = getelementptr inbounds i32, i32* %13, i32 1
store i32* %14, i32** %5
%15 = load i32, i32* %13
%16 = load i32*, i32** %4
%17 = getelementptr inbounds i32, i32* %16, i32 1
store i32* %17, i32** %4
store i32 %15, i32* %16
%18 = icmp ne i32 %15, 0
br label %19
19: ; preds = %12, %9
%20 = phi i1 [ false, %9 ], [ %18, %12 ]
br i1 %20, label %21, label %24
21: ; preds = %19
%22 = load i64, i64* %6
%23 = add i64 %22, -1
store i64 %23, i64* %6
br label %9
24: ; preds = %19
%25 = load i32*, i32** %4
%26 = load i64, i64* %6
%27 = call i32 @wmemset(i32* %25, i32 0, i64 %26)
%28 = load i32*, i32** %7
ret i32* %28
}

LLVM IR (-O2):
define i32* @wcsncpy(i32* returned %0, i32* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %16, label %5
5: ; preds = %3, %12
%6 = phi i32* [ %10, %12 ], [ %0, %3 ]
%7 = phi i64 [ %14, %12 ], [ %2, %3 ]
%8 = phi i32* [ %13, %12 ], [ %1, %3 ]
%9 = load i32, i32* %8
%10 = getelementptr inbounds i32, i32* %6, i64 1
store i32 %9, i32* %6
%11 = icmp eq i32 %9, 0
br i1 %11, label %16, label %12
12: ; preds = %5
%13 = getelementptr inbounds i32, i32* %8, i64 1
%14 = add i64 %7, -1
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %5
16: ; preds = %5, %12, %3
%17 = phi i64 [ 0, %3 ], [ 0, %12 ], [ %7, %5 ]
%18 = phi i32* [ %0, %3 ], [ %10, %12 ], [ %10, %5 ]
%19 = tail call i32 @wmemset(i32* %18, i32 0, i64 %17)
ret i32* %0
}