#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 *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
void strlcat(char *dst, char *src, int n) {
  if (strlen(src) + 1 > n - strlen(dst))
    strcpy(dst, "errorBuf has been be truncated");
  (void)strncat(dst, src, n - strlen(dst) - 1);
}


x86 assembly (clang, -O0):
<strlcat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x1c(%rbp),%ecx
mov    %eax,%edx
mov    -0x18(%rbp),%eax
sub    %edx,%ecx
cmp    %ecx,%eax
jle    1183 <strlcat+0x53>
mov    -0x8(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edx
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
sub    %eax,%edx
sub    $0x1,%edx
call   1050 <strncat@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strlcat>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %ebx,%ecx
sub    %eax,%ecx
cmp    %ecx,%ebp
jl     1168 <strlcat+0x38>
lea    0xea0(%rip),%rsi
mov    %r15,%rdi
call   1030 <strcpy@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
not    %eax
add    %eax,%ebx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <strncat@plt>

LLVM IR (-O0):
@.str = [31 x i8] c"errorBuf has been be truncated\00"
define void @strlcat(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%7 = load i8*, i8** %5
%8 = call i32 @strlen(i8* %7)
%9 = add nsw i32 %8, 1
%10 = load i32, i32* %6
%11 = load i8*, i8** %4
%12 = call i32 @strlen(i8* %11)
%13 = sub nsw i32 %10, %12
%14 = icmp sgt i32 %9, %13
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load i8*, i8** %4
%17 = call i32 @strcpy(i8* %16, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %15, %3
%19 = load i8*, i8** %4
%20 = load i8*, i8** %5
%21 = load i32, i32* %6
%22 = load i8*, i8** %4
%23 = call i32 @strlen(i8* %22)
%24 = sub nsw i32 %21, %23
%25 = sub nsw i32 %24, 1
%26 = call i32 @strncat(i8* %19, i8* %20, i32 %25)
ret void
}

LLVM IR (-O2):
@.str = [31 x i8] c"errorBuf has been be truncated\00"
define void @strlcat(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %1)
%5 = tail call i32 @strlen(i8* %0)
%6 = sub nsw i32 %2, %5
%7 = icmp slt i32 %4, %6
br i1 %7, label %10, label %8
8: ; preds = %3
%9 = tail call i32 @strcpy(i8* %0, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %3
%11 = tail call i32 @strlen(i8* %0)
%12 = xor i32 %11, -1
%13 = add i32 %12, %2
%14 = tail call i32 @strncat(i8* %0, i8* %1, i32 %13)
ret void
}