#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 my_strcpy(char *, char const *, size_t);
size_t strlen(char const *);
size_t my_strcat(char *buf, const char *src, size_t bufsize) {
  size_t dlen = strlen(buf);
  if (dlen < bufsize - 1) {
    return (dlen + my_strcpy(buf + dlen, src, bufsize - dlen));
  } else {
    return (dlen + strlen(src));
  }
}


x86 assembly (clang, -O0):
<my_strcat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jae    118a <my_strcat+0x6a>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1030 <my_strcpy@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11a9 <my_strcat+0x89>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@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):
<my_strcat>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %rax,%r15
lea    -0x1(%r12),%rax
cmp    %rax,%r15
jae    1159 <my_strcat+0x39>
add    %r15,%rbx
sub    %r15,%r12
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1030 <my_strcpy@plt>
jmp    1161 <my_strcat+0x41>
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @my_strcat(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 = call i64 @strlen(i8* %9)
store i64 %10, i64* %8
%11 = load i64, i64* %8
%12 = load i64, i64* %7
%13 = sub i64 %12, 1
%14 = icmp ult i64 %11, %13
br i1 %14, label %15, label %26
15: ; preds = %3
%16 = load i64, i64* %8
%17 = load i8*, i8** %5
%18 = load i64, i64* %8
%19 = getelementptr inbounds i8, i8* %17, i64 %18
%20 = load i8*, i8** %6
%21 = load i64, i64* %7
%22 = load i64, i64* %8
%23 = sub i64 %21, %22
%24 = call i64 @my_strcpy(i8* %19, i8* %20, i64 %23)
%25 = add i64 %16, %24
store i64 %25, i64* %4
br label %31
26: ; preds = %3
%27 = load i64, i64* %8
%28 = load i8*, i8** %6
%29 = call i64 @strlen(i8* %28)
%30 = add i64 %27, %29
store i64 %30, i64* %4
br label %31
31: ; preds = %26, %15
%32 = load i64, i64* %4
ret i64 %32
}

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