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


x86 assembly (clang, -O0):
<wcsncat>:
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)
mov    -0x8(%rbp),%rdi
call   1030 <wcslen@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x21(%rbp)
je     1184 <wcsncat+0x74>
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    1194 <wcsncat+0x84>
jmp    11a5 <wcsncat+0x95>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1149 <wcsncat+0x39>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x8(%rbp)
movl   $0x0,(%rax)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wcsncat>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
call   1030 <wcslen@plt>
cltq
lea    (%r14,%rax,4),%rax
test   %rbx,%rbx
je     115a <wcsncat+0x4a>
add    $0xffffffffffffffff,%rbx
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r15,%rcx,4),%edx
mov    %edx,(%rax)
add    $0x4,%rax
test   %edx,%edx
je     115a <wcsncat+0x4a>
lea    0x1(%rcx),%rdx
cmp    %rcx,%rbx
mov    %rdx,%rcx
jne    1140 <wcsncat+0x30>
movl   $0x0,(%rax)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32* @wcsncat(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
%9 = load i32*, i32** %4
%10 = call i32 @wcslen(i32* %9)
%11 = load i32*, i32** %4
%12 = sext i32 %10 to i64
%13 = getelementptr inbounds i32, i32* %11, i64 %12
store i32* %13, i32** %4
br label %14
14: ; preds = %26, %3
%15 = load i64, i64* %6
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = load i32*, i32** %5
%19 = getelementptr inbounds i32, i32* %18, i32 1
store i32* %19, i32** %5
%20 = load i32, i32* %18
%21 = load i32*, i32** %4
%22 = getelementptr inbounds i32, i32* %21, i32 1
store i32* %22, i32** %4
store i32 %20, i32* %21
%23 = icmp ne i32 %20, 0
br label %24
24: ; preds = %17, %14
%25 = phi i1 [ false, %14 ], [ %23, %17 ]
br i1 %25, label %26, label %29
26: ; preds = %24
%27 = load i64, i64* %6
%28 = add i64 %27, -1
store i64 %28, i64* %6
br label %14
29: ; preds = %24
%30 = load i32*, i32** %4
%31 = getelementptr inbounds i32, i32* %30, i32 1
store i32* %31, i32** %4
store i32 0, i32* %30
%32 = load i32*, i32** %7
ret i32* %32
}

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