#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 */

/* Variables and functions */
int /*<<< orphan*/ memcpy(char *, char const *, size_t const);
size_t strlen(char *);
size_t strnlen(char const *, size_t);
char *strncat(char *__restrict dst, const char *__restrict src, size_t maxlen) {
  const size_t dstlen = strlen(dst);
  const size_t srclen = strnlen(src, maxlen);
  const size_t cpylen = srclen < maxlen ? srclen : maxlen;
  memcpy(dst + dstlen, src, cpylen);
  dst[dstlen + cpylen] = '\0';
  return dst;
}


x86 assembly (clang, -O0):
<strncat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strnlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    117d <strncat+0x4d>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1185 <strncat+0x55>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
add    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncat>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %rax,%r12
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <strnlen@plt>
cmp    %rbx,%rax
cmovb  %rax,%rbx
lea    (%r15,%r12,1),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
add    %r12,%rbx
movb   $0x0,(%r15,%rbx,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @strncat(i8* noalias %0, i8* noalias %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = load i8*, i8** %4
%11 = call i64 @strlen(i8* %10)
store i64 %11, i64* %7
%12 = load i8*, i8** %5
%13 = load i64, i64* %6
%14 = call i64 @strnlen(i8* %12, i64 %13)
store i64 %14, i64* %8
%15 = load i64, i64* %8
%16 = load i64, i64* %6
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = load i64, i64* %8
br label %22
20: ; preds = %3
%21 = load i64, i64* %6
br label %22
22: ; preds = %20, %18
%23 = phi i64 [ %19, %18 ], [ %21, %20 ]
store i64 %23, i64* %9
%24 = load i8*, i8** %4
%25 = load i64, i64* %7
%26 = getelementptr inbounds i8, i8* %24, i64 %25
%27 = load i8*, i8** %5
%28 = load i64, i64* %9
%29 = call i32 @memcpy(i8* %26, i8* %27, i64 %28)
%30 = load i8*, i8** %4
%31 = load i64, i64* %7
%32 = load i64, i64* %9
%33 = add i64 %31, %32
%34 = getelementptr inbounds i8, i8* %30, i64 %33
store i8 0, i8* %34
%35 = load i8*, i8** %4
ret i8* %35
}

LLVM IR (-O2):
define i8* @strncat(i8* noalias returned %0, i8* noalias %1, i64 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = tail call i64 @strnlen(i8* %1, i64 %2)
%6 = icmp ult i64 %5, %2
%7 = select i1 %6, i64 %5, i64 %2
%8 = getelementptr inbounds i8, i8* %0, i64 %4
%9 = tail call i32 @memcpy(i8* %8, i8* %1, i64 %7)
%10 = add i64 %7, %4
%11 = getelementptr inbounds i8, i8* %0, i64 %10
store i8 0, i8* %11
ret i8* %0
}