#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 */
wchar_t *wcsncpy(wchar_t *dst, const wchar_t *src, size_t num) {
  wchar_t *d = dst;
  while (num--) {
    if (*src)
      *d++ = *src++;
    else {
      while (num--)
        *d++ = 0;
      break;
    }
  }
  return dst;
}


x86 assembly (clang, -O0):
<wcsncpy>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
cmp    $0x0,%rax
je     11ab <wcsncpy+0xab>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     1168 <wcsncpy+0x68>
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)
jmp    11a6 <wcsncpy+0xa6>
jmp    116d <wcsncpy+0x6d>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
cmp    $0x0,%rax
je     11a1 <wcsncpy+0xa1>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x20(%rbp)
movq   $0x0,(%rax)
jmp    116d <wcsncpy+0x6d>
jmp    11ab <wcsncpy+0xab>
jmp    1118 <wcsncpy+0x18>
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<wcsncpy>:
push   %rbx
mov    %rdi,%rbx
test   %rdx,%rdx
je     1158 <wcsncpy+0x48>
xor    %eax,%eax
mov    %rdx,%rcx
xchg   %ax,%ax
mov    (%rsi,%rax,1),%rdi
test   %rdi,%rdi
je     113c <wcsncpy+0x2c>
add    $0xffffffffffffffff,%rcx
mov    %rdi,(%rbx,%rax,1)
add    $0x8,%rax
test   %rcx,%rcx
jne    1120 <wcsncpy+0x10>
jmp    1158 <wcsncpy+0x48>
cmp    $0x1,%rcx
je     1158 <wcsncpy+0x48>
lea    (%rbx,%rax,1),%rdi
shl    $0x3,%rdx
sub    %rax,%rdx
add    $0xfffffffffffffff8,%rdx
xor    %esi,%esi
call   1030 <memset@plt>
mov    %rbx,%rax
pop    %rbx
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
br label %9
9: ; preds = %32, %3
%10 = load i64, i64* %6
%11 = add i64 %10, -1
store i64 %11, i64* %6
%12 = icmp ne i64 %10, 0
br i1 %12, label %13, label %33
13: ; preds = %9
%14 = load i64*, i64** %5
%15 = load i64, i64* %14
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %13
%18 = load i64*, i64** %5
%19 = getelementptr inbounds i64, i64* %18, i32 1
store i64* %19, i64** %5
%20 = load i64, i64* %18
%21 = load i64*, i64** %7
%22 = getelementptr inbounds i64, i64* %21, i32 1
store i64* %22, i64** %7
store i64 %20, i64* %21
br label %32
23: ; preds = %13
br label %24
24: ; preds = %28, %23
%25 = load i64, i64* %6
%26 = add i64 %25, -1
store i64 %26, i64* %6
%27 = icmp ne i64 %25, 0
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i64*, i64** %7
%30 = getelementptr inbounds i64, i64* %29, i32 1
store i64* %30, i64** %7
store i64 0, i64* %29
br label %24
31: ; preds = %24
br label %33
32: ; preds = %17
br label %9
33: ; preds = %31, %9
%34 = load i64*, i64** %4
ret i64* %34
}

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