#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 */
char *smartstrcat(char *dest, char *src, size_t max_chars) {
  size_t copied = 0;
  while ((*(dest++) = *(src++)) && copied++ < max_chars) {
  }
  *(--dest) = '\0';
  return dest;
}


x86 assembly (clang, -O0):
<smartstrcat>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%al
mov    -0x8(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %al,(%rcx)
movsbl %al,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x21(%rbp)
je     1164 <smartstrcat+0x64>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
cmp    -0x18(%rbp),%rax
setb   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1174 <smartstrcat+0x74>
jmp    1179 <smartstrcat+0x79>
jmp    1118 <smartstrcat+0x18>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x0,-0x1(%rax)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<smartstrcat>:
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl (%rsi,%rax,1),%r8d
mov    %r8b,(%rdi,%rax,1)
lea    0x1(%rax),%rcx
test   %r8b,%r8b
je     112a <smartstrcat+0x2a>
cmp    %rdx,%rax
mov    %rcx,%rax
jb     1110 <smartstrcat+0x10>
lea    (%rdi,%rcx,1),%rax
add    $0xffffffffffffffff,%rax
movb   $0x0,-0x1(%rdi,%rcx,1)
ret

LLVM IR (-O0):
define i8* @smartstrcat(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i64 0, i64* %7
br label %8
8: ; preds = %23, %3
%9 = load i8*, i8** %5
%10 = getelementptr inbounds i8, i8* %9, i32 1
store i8* %10, i8** %5
%11 = load i8, i8* %9
%12 = load i8*, i8** %4
%13 = getelementptr inbounds i8, i8* %12, i32 1
store i8* %13, i8** %4
store i8 %11, i8* %12
%14 = sext i8 %11 to i32
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %8
%17 = load i64, i64* %7
%18 = add i64 %17, 1
store i64 %18, i64* %7
%19 = load i64, i64* %6
%20 = icmp ult i64 %17, %19
br label %21
21: ; preds = %16, %8
%22 = phi i1 [ false, %8 ], [ %20, %16 ]
br i1 %22, label %23, label %24
23: ; preds = %21
br label %8
24: ; preds = %21
%25 = load i8*, i8** %4
%26 = getelementptr inbounds i8, i8* %25, i32 -1
store i8* %26, i8** %4
store i8 0, i8* %26
%27 = load i8*, i8** %4
ret i8* %27
}

LLVM IR (-O2):
define i8* @smartstrcat(i8* writeonly %0, i8* nocapture %1, i64 %2) {
br label %4
4: ; preds = %4, %3
%5 = phi i8* [ %1, %3 ], [ %11, %4 ]
%6 = phi i8* [ %0, %3 ], [ %10, %4 ]
%7 = phi i64 [ 0, %3 ], [ %12, %4 ]
%8 = load i8, i8* %5
store i8 %8, i8* %6
%9 = icmp ne i8 %8, 0
%10 = getelementptr inbounds i8, i8* %6, i64 1
%11 = getelementptr inbounds i8, i8* %5, i64 1
%12 = add i64 %7, 1
%13 = icmp ult i64 %7, %2
%14 = select i1 %9, i1 %13, i1 false
br i1 %14, label %4, label %15
15: ; preds = %4
store i8 0, i8* %6
ret i8* %6
}