#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*/ exit(int);
int /*<<< orphan*/ log_stderr(char *, char *, char const *, int);
int strlen(char const *);
int /*<<< orphan*/ strncat(char *, char const *, int);
void concat(char *buf, const char *thing) {
  if ((strlen(buf) + strlen(thing) + 1) > (4096)) {
    log_stderr("3000 Buffer overflow: string length too long to concatenate %s "
               "and %s.  maxlen = %d\n",
               buf, thing, (4096));
    exit(1);
  }
  strncat(buf, thing, ((4096) - 1));
  buf[((4096) - 1)] = '\0';
}


x86 assembly (clang, -O0):
<concat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cmp    $0x1000,%eax
jle    119d <concat+0x5d>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xe77(%rip),%rdi
mov    $0x1000,%ecx
call   1030 <log_stderr@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0xfff,%edx
call   1050 <strncat@plt>
mov    -0x8(%rbp),%rax
movb   $0x0,0xfff(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<concat>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %ebp,%eax
cmp    $0x1000,%eax
jge    117e <concat+0x3e>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0xfff,%edx
call   1050 <strncat@plt>
movb   $0x0,0xfff(%rbx)
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe7b(%rip),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    $0x1000,%ecx
call   1030 <log_stderr@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [85 x i8] c"3000 Buffer overflow: string length too long to concatenate %s and %s. maxlen = %d\0A\00"
define void @concat(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%5 = load i8*, i8** %3
%6 = call i32 @strlen(i8* %5)
%7 = load i8*, i8** %4
%8 = call i32 @strlen(i8* %7)
%9 = add nsw i32 %6, %8
%10 = add nsw i32 %9, 1
%11 = icmp sgt i32 %10, 4096
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8*, i8** %3
%14 = load i8*, i8** %4
%15 = call i32 @log_stderr(i8* getelementptr inbounds ([85 x i8], [85 x i8]* @.str, i64 0, i64 0), i8* %13, i8* %14, i32 4096)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i8*, i8** %3
%19 = load i8*, i8** %4
%20 = call i32 @strncat(i8* %18, i8* %19, i32 4095)
%21 = load i8*, i8** %3
%22 = getelementptr inbounds i8, i8* %21, i64 4095
store i8 0, i8* %22
ret void
}

LLVM IR (-O2):
@.str = [85 x i8] c"3000 Buffer overflow: string length too long to concatenate %s and %s. maxlen = %d\0A\00"
define void @concat(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = tail call i32 @strlen(i8* %1)
%5 = add nsw i32 %4, %3
%6 = icmp sgt i32 %5, 4095
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @log_stderr(i8* getelementptr inbounds ([85 x i8], [85 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1, i32 4096)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = tail call i32 @strncat(i8* %0, i8* %1, i32 4095)
%12 = getelementptr inbounds i8, i8* %0, i64 4095
store i8 0, i8* %12
ret void
}