#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*/ memcpy(char *, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
void strcatmax(char *s1, char *s2, int max) {
  if (strlen(s1) + strlen(s2) > max) {
    memcpy(s1 + strlen(s1), s2, max - strlen(s1));
    *(s1 + max) = 0;
  } else
    strcat(s1, s2);
}


x86 assembly (clang, -O0):
<strcatmax>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
add    %ecx,%eax
cmp    -0x14(%rbp),%eax
jle    11c3 <strcatmax+0x93>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x2c(%rbp),%edx
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
sub    %eax,%edx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11d0 <strcatmax+0xa0>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcat@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strcatmax>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1030 <strlen@plt>
add    %ebp,%eax
cmp    %r14d,%eax
jle    118e <strcatmax+0x5e>
mov    %rbx,%rdi
call   1030 <strlen@plt>
movslq %eax,%rbp
add    %rbx,%rbp
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %r14d,%edx
sub    %eax,%edx
mov    %rbp,%rdi
mov    %r15,%rsi
call   1040 <memcpy@plt>
movslq %r14d,%rax
movb   $0x0,(%rbx,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
mov    %r15,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <strcat@plt>

LLVM IR (-O0):
define void @strcatmax(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** %4
%8 = call i32 @strlen(i8* %7)
%9 = load i8*, i8** %5
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 %8, %10
%12 = load i32, i32* %6
%13 = icmp sgt i32 %11, %12
br i1 %13, label %14, label %30
14: ; preds = %3
%15 = load i8*, i8** %4
%16 = load i8*, i8** %4
%17 = call i32 @strlen(i8* %16)
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %15, i64 %18
%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 = call i32 @memcpy(i8* %19, i8* %20, i32 %24)
%26 = load i8*, i8** %4
%27 = load i32, i32* %6
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 0, i8* %29
br label %34
30: ; preds = %3
%31 = load i8*, i8** %4
%32 = load i8*, i8** %5
%33 = call i32 @strcat(i8* %31, i8* %32)
br label %34
34: ; preds = %30, %14
ret void
}

LLVM IR (-O2):
define void @strcatmax(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = tail call i32 @strlen(i8* %1)
%6 = add nsw i32 %5, %4
%7 = icmp sgt i32 %6, %2
br i1 %7, label %8, label %17
8: ; preds = %3
%9 = tail call i32 @strlen(i8* %0)
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = tail call i32 @strlen(i8* %0)
%13 = sub nsw i32 %2, %12
%14 = tail call i32 @memcpy(i8* %11, i8* %1, i32 %13)
%15 = sext i32 %2 to i64
%16 = getelementptr inbounds i8, i8* %0, i64 %15
store i8 0, i8* %16
br label %19
17: ; preds = %3
%18 = tail call i32 @strcat(i8* %0, i8* %1)
br label %19
19: ; preds = %17, %8
ret void
}