#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*/ strcpy(char *, char const *);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *strncat(char *s1, const char *s2, size_t n) {
  if (n < strlen(s2))
    strncpy(s1 + strlen(s1), s2, n);
  else
    strcpy(s1 + strlen(s1), s2);
  return s1;
}


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    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
jae    118f <strncat+0x5f>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x28(%rbp),%rdi
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <strncpy@plt>
jmp    11b0 <strncat+0x80>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncat>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1050 <strlen@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1050 <strlen@plt>
add    %rbx,%rax
mov    %rax,%rdi
mov    %r14,%rsi
cmp    %r15,%r12
jbe    116c <strncat+0x3c>
mov    %r15,%rdx
call   1030 <strncpy@plt>
jmp    1171 <strncat+0x41>
call   1040 <strcpy@plt>
mov    %rbx,%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
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%7 = load i64, i64* %6
%8 = load i8*, i8** %5
%9 = call i64 @strlen(i8* %8)
%10 = icmp ult i64 %7, %9
br i1 %10, label %11, label %19
11: ; preds = %3
%12 = load i8*, i8** %4
%13 = load i8*, i8** %4
%14 = call i64 @strlen(i8* %13)
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = load i8*, i8** %5
%17 = load i64, i64* %6
%18 = call i32 @strncpy(i8* %15, i8* %16, i64 %17)
br label %26
19: ; preds = %3
%20 = load i8*, i8** %4
%21 = load i8*, i8** %4
%22 = call i64 @strlen(i8* %21)
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = load i8*, i8** %5
%25 = call i32 @strcpy(i8* %23, i8* %24)
br label %26
26: ; preds = %19, %11
%27 = load i8*, i8** %4
ret i8* %27
}

LLVM IR (-O2):
define i8* @strncat(i8* returned %0, i8* %1, i64 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = icmp ugt i64 %4, %2
%6 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%7 = getelementptr inbounds i8, i8* %0, i64 %6
br i1 %5, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @strncpy(i8* %7, i8* %1, i64 %2)
br label %12
10: ; preds = %3
%11 = tail call i32 @strcpy(i8* %7, i8* %1)
br label %12
12: ; preds = %10, %8
ret i8* %0
}