#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 strlen(char const *);
char *strncpy(char *, char const *, size_t);
char *strncat(char *destination, const char *source, size_t num) {
  size_t s_size = strlen(source);
  return strncpy((char *)destination + (strlen(destination) + 1), source,
                 (num > s_size) ? s_size : num);
}


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    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cltq
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
add    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    118a <strncat+0x6a>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    1192 <strncat+0x72>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdi
mov    -0x40(%rbp),%rdx
call   1030 <strncpy@plt>
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
mov    %rsi,%rdi
call   1040 <strlen@plt>
movslq %eax,%r12
mov    %r15,%rdi
call   1040 <strlen@plt>
cltq
lea    (%rax,%r15,1),%rdi
add    $0x1,%rdi
cmp    %rbx,%r12
cmovb  %r12,%rbx
mov    %r14,%rsi
mov    %rbx,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <strncpy@plt>

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

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