#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 */
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *strncat(char *d, const char *s, size_t n) {
  if (strlen(s) < n) {
    n = strlen(s);
  }
  strncpy(d + strlen(d), s, n);
  *(d + strlen(d) + n + 1) = '\0';
  return d;
}


x86 assembly (clang, -O0):
<strncat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    -0x18(%rbp),%rax
jae    1154 <strncat+0x34>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rdi
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
add    %rcx,%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,0x1(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

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

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

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