#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_error(char *);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
void safe_concatenate(char *buffer, int size, char *string1, char *string2) {
  if (strlen(string1) + strlen(string2) > size) {
    log_error("Catched bufferoverflow!");
    exit(5);
  } else {
    strcat(buffer, string1);
    strcat(buffer, string2);
  }
}


x86 assembly (clang, -O0):
<safe_concatenate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
add    %ecx,%eax
cmp    -0xc(%rbp),%eax
jle    1192 <safe_concatenate+0x52>
lea    0xe7d(%rip),%rdi
call   1030 <log_error@plt>
mov    $0x5,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strcat@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <strcat@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_concatenate>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %esi,%r12d
mov    %rdi,%r15
mov    %rdx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %ebp,%eax
cmp    %r12d,%eax
jg     118b <safe_concatenate+0x4b>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <strcat@plt>
mov    %r15,%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <strcat@plt>
lea    0xe6e(%rip),%rdi
call   1030 <log_error@plt>
mov    $0x5,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"Catched bufferoverflow!\00"
define void @safe_concatenate(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%9 = load i8*, i8** %7
%10 = call i32 @strlen(i8* %9)
%11 = load i8*, i8** %8
%12 = call i32 @strlen(i8* %11)
%13 = add nsw i32 %10, %12
%14 = load i32, i32* %6
%15 = icmp sgt i32 %13, %14
br i1 %15, label %16, label %19
16: ; preds = %4
%17 = call i32 @log_error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 5)
unreachable
19: ; preds = %4
%20 = load i8*, i8** %5
%21 = load i8*, i8** %7
%22 = call i32 @strcat(i8* %20, i8* %21)
%23 = load i8*, i8** %5
%24 = load i8*, i8** %8
%25 = call i32 @strcat(i8* %23, i8* %24)
br label %26
26: ; preds = %19
ret void
}

LLVM IR (-O2):
@.str = [24 x i8] c"Catched bufferoverflow!\00"
define void @safe_concatenate(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = tail call i32 @strlen(i8* %2)
%6 = tail call i32 @strlen(i8* %3)
%7 = add nsw i32 %6, %5
%8 = icmp sgt i32 %7, %1
br i1 %8, label %9, label %12
9: ; preds = %4
%10 = tail call i32 @log_error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 5)
unreachable
12: ; preds = %4
%13 = tail call i32 @strcat(i8* %0, i8* %2)
%14 = tail call i32 @strcat(i8* %0, i8* %3)
ret void
}