#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 strlcpy(char *, char const *, size_t);
size_t strlen(char const *);
size_t strnlen(char *, size_t);
size_t strlcat(char *d, const char *s, size_t n) {
  size_t l = strnlen(d, n);
  if (l == n) {
    return l + strlen(s);
  }
  return l + strlcpy(d + l, s, n - l);
}


x86 assembly (clang, -O0):
<strlcat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strnlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    1187 <strlcat+0x57>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11b6 <strlcat+0x86>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1050 <strlcpy@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strlcat>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r12
mov    %rdx,%rsi
call   1040 <strnlen@plt>
mov    %rax,%r15
cmp    %rbx,%rax
jne    115b <strlcat+0x2b>
mov    %r14,%rdi
call   1030 <strlen@plt>
jmp    116f <strlcat+0x3f>
add    %r15,%r12
sub    %r15,%rbx
mov    %r12,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <strlcpy@plt>
add    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i64 @strlcat(i8* %0, i8* %1, i64 %2) {
%4 = tail call i64 @strnlen(i8* %0, i64 %2)
%5 = icmp eq i64 %4, %2
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i64 @strlen(i8* dereferenceable(1) %1)
br label %12
8: ; preds = %3
%9 = getelementptr inbounds i8, i8* %0, i64 %4
%10 = sub i64 %2, %4
%11 = tail call i64 @strlcpy(i8* %9, i8* %1, i64 %10)
br label %12
12: ; preds = %8, %6
%13 = phi i64 [ %7, %6 ], [ %11, %8 ]
%14 = add i64 %13, %4
ret i64 %14
}