#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 */
unsigned int strlen(char *);
char *strncat(char *destination, const char *appendix, size_t lenght) {
  unsigned int i = strlen(destination);
  while (((destination[i] = *appendix++) != '\0') && lenght--)
    i++;
  return destination;
}


x86 assembly (clang, -O0):
<strncat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%al
mov    -0x8(%rbp),%rcx
mov    -0x1c(%rbp),%edx
mov    %al,(%rcx,%rdx,1)
movsbl %al,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x1d(%rbp)
je     1175 <strncat+0x65>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
cmp    $0x0,%rax
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1185 <strncat+0x75>
jmp    1193 <strncat+0x83>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1130 <strncat+0x20>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncat>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
call   1030 <strlen@plt>
mov    (%r15),%cl
mov    %eax,%eax
mov    %cl,(%r12,%rax,1)
test   %cl,%cl
je     115c <strncat+0x4c>
test   %r14,%r14
je     115c <strncat+0x4c>
mov    $0x1,%ecx
nopl   (%rax)
lea    (%rax,%rcx,1),%edx
movzbl (%r15,%rcx,1),%ebx
mov    %bl,(%r12,%rdx,1)
test   %bl,%bl
je     115c <strncat+0x4c>
lea    0x1(%rcx),%rdx
cmp    %rcx,%r14
mov    %rdx,%rcx
jne    1140 <strncat+0x30>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
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
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%8 = load i8*, i8** %4
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %7
br label %10
10: ; preds = %26, %3
%11 = load i8*, i8** %5
%12 = getelementptr inbounds i8, i8* %11, i32 1
store i8* %12, i8** %5
%13 = load i8, i8* %11
%14 = load i8*, i8** %4
%15 = load i32, i32* %7
%16 = zext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
store i8 %13, i8* %17
%18 = sext i8 %13 to i32
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %10
%21 = load i64, i64* %6
%22 = add i64 %21, -1
store i64 %22, i64* %6
%23 = icmp ne i64 %21, 0
br label %24
24: ; preds = %20, %10
%25 = phi i1 [ false, %10 ], [ %23, %20 ]
br i1 %25, label %26, label %29
26: ; preds = %24
%27 = load i32, i32* %7
%28 = add i32 %27, 1
store i32 %28, i32* %7
br label %10
29: ; preds = %24
%30 = load i8*, i8** %4
ret i8* %30
}

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