#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 /*<<< orphan*/ assert(int /*<<< orphan*/);
wchar_t *wcsncpy(wchar_t *s1, const wchar_t *s2, size_t n) {
  wchar_t *p = s1;
  assert(s1 != NULL);
  assert(s2 != NULL);
  while (n > 0 && *s2) {
    *p++ = *s2++;
    n--;
  }
  while (n > 0) {
    *p++ = L'\0';
    n--;
  }
  return s1;
}


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)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x21(%rbp)
jbe    116e <wcsncpy+0x5e>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    117e <wcsncpy+0x6e>
jmp    11b3 <wcsncpy+0xa3>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%rcx
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x8,%rdx
mov    %rdx,-0x20(%rbp)
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1150 <wcsncpy+0x40>
jmp    11b8 <wcsncpy+0xa8>
cmpq   $0x0,-0x18(%rbp)
jbe    11ea <wcsncpy+0xda>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x20(%rbp)
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    11b8 <wcsncpy+0xa8>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wcsncpy>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1040 <assert@plt>
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1040 <assert@plt>
test   %r14,%r14
je     118e <wcsncpy+0x6e>
xor    %eax,%eax
mov    %r14,%rcx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%rbx,%rax,1),%rdx
test   %rdx,%rdx
je     1179 <wcsncpy+0x59>
mov    %rdx,(%r15,%rax,1)
add    $0x8,%rax
add    $0xffffffffffffffff,%rcx
jne    1160 <wcsncpy+0x40>
jmp    118e <wcsncpy+0x6e>
lea    (%r15,%rax,1),%rdi
shl    $0x3,%r14
sub    %rax,%r14
xor    %esi,%esi
mov    %r14,%rdx
call   1030 <memset@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64* @wcsncpy(i64* %0, i64* %1, i64 %2) {
%4 = alloca i64*
%5 = alloca i64*
%6 = alloca i64
%7 = alloca i64*
store i64* %0, i64** %4
store i64* %1, i64** %5
store i64 %2, i64* %6
%8 = load i64*, i64** %4
store i64* %8, i64** %7
%9 = load i64*, i64** %4
%10 = icmp ne i64* %9, null
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i64*, i64** %5
%14 = icmp ne i64* %13, null
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
br label %17
17: ; preds = %26, %3
%18 = load i64, i64* %6
%19 = icmp ugt i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i64*, i64** %5
%22 = load i64, i64* %21
%23 = icmp ne i64 %22, 0
br label %24
24: ; preds = %20, %17
%25 = phi i1 [ false, %17 ], [ %23, %20 ]
br i1 %25, label %26, label %34
26: ; preds = %24
%27 = load i64*, i64** %5
%28 = getelementptr inbounds i64, i64* %27, i32 1
store i64* %28, i64** %5
%29 = load i64, i64* %27
%30 = load i64*, i64** %7
%31 = getelementptr inbounds i64, i64* %30, i32 1
store i64* %31, i64** %7
store i64 %29, i64* %30
%32 = load i64, i64* %6
%33 = add i64 %32, -1
store i64 %33, i64* %6
br label %17
34: ; preds = %24
br label %35
35: ; preds = %38, %34
%36 = load i64, i64* %6
%37 = icmp ugt i64 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %35
%39 = load i64*, i64** %7
%40 = getelementptr inbounds i64, i64* %39, i32 1
store i64* %40, i64** %7
store i64 0, i64* %39
%41 = load i64, i64* %6
%42 = add i64 %41, -1
store i64 %42, i64* %6
br label %35
43: ; preds = %35
%44 = load i64*, i64** %4
ret i64* %44
}

LLVM IR (-O2):
define i64* @wcsncpy(i64* returned writeonly %0, i64* %1, i64 %2) {
%4 = icmp ne i64* %0, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = icmp ne i64* %1, null
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = icmp eq i64 %2, 0
br i1 %10, label %25, label %11
11: ; preds = %3, %17
%12 = phi i64* [ %19, %17 ], [ %0, %3 ]
%13 = phi i64 [ %20, %17 ], [ %2, %3 ]
%14 = phi i64* [ %18, %17 ], [ %1, %3 ]
%15 = load i64, i64* %14
%16 = icmp eq i64 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %11
%18 = getelementptr inbounds i64, i64* %14, i64 1
%19 = getelementptr inbounds i64, i64* %12, i64 1
store i64 %15, i64* %12
%20 = add i64 %13, -1
%21 = icmp eq i64 %20, 0
br i1 %21, label %25, label %11
22: ; preds = %11
%23 = bitcast i64* %12 to i8*
%24 = shl nuw i64 %13, 3
br label %25
25: ; preds = %17, %3, %22
ret i64* %0
}