#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*/ Niall_Warning(char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
void safeStrcat(char *Buffer, int BufSize, char *Word) {
  int i;
  if ((strlen(Buffer) + strlen(Word) + 1) > BufSize) {
    Niall_Warning("Buffer overflow - %d bytes exceeded.", BufSize);
    for (i = strlen(Buffer); i < BufSize; i++)
      Buffer[i] = '<';
    Buffer[BufSize - 1] = 0;
  } else {
    strcat(Buffer, Word);
  }
}


x86 assembly (clang, -O0):
<safeStrcat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cmp    -0xc(%rbp),%eax
jle    11c2 <safeStrcat+0x92>
mov    -0xc(%rbp),%esi
lea    0xe8b(%rip),%rdi
call   1030 <Niall_Warning@plt>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ac <safeStrcat+0x7c>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x3c,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1186 <safeStrcat+0x56>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11cf <safeStrcat+0x9f>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strcat@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safeStrcat>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1040 <strlen@plt>
add    %ebx,%eax
cmp    %ebp,%eax
jge    1179 <safeStrcat+0x39>
mov    %r14,%rdi
mov    %r15,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <strcat@plt>
lea    0xe80(%rip),%rdi
mov    %ebp,%esi
call   1030 <Niall_Warning@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
cmp    %ebp,%eax
jge    11b0 <safeStrcat+0x70>
movslq %eax,%rdi
add    %r14,%rdi
add    $0xffffffff,%ebp
mov    %ebp,%edx
sub    %eax,%edx
add    $0x1,%rdx
mov    $0x3c,%esi
call   1050 <memset@plt>
jmp    11b3 <safeStrcat+0x73>
add    $0xffffffff,%ebp
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [37 x i8] c"Buffer overflow - %d bytes exceeded.\00"
define void @safeStrcat(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%8 = load i8*, i8** %4
%9 = call i32 @strlen(i8* %8)
%10 = load i8*, i8** %6
%11 = call i32 @strlen(i8* %10)
%12 = add nsw i32 %9, %11
%13 = add nsw i32 %12, 1
%14 = load i32, i32* %5
%15 = icmp sgt i32 %13, %14
br i1 %15, label %16, label %39
16: ; preds = %3
%17 = load i32, i32* %5
%18 = call i32 @Niall_Warning(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = load i8*, i8** %4
%20 = call i32 @strlen(i8* %19)
store i32 %20, i32* %7
br label %21
21: ; preds = %30, %16
%22 = load i32, i32* %7
%23 = load i32, i32* %5
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %33
25: ; preds = %21
%26 = load i8*, i8** %4
%27 = load i32, i32* %7
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 60, i8* %29
br label %30
30: ; preds = %25
%31 = load i32, i32* %7
%32 = add nsw i32 %31, 1
store i32 %32, i32* %7
br label %21
33: ; preds = %21
%34 = load i8*, i8** %4
%35 = load i32, i32* %5
%36 = sub nsw i32 %35, 1
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %34, i64 %37
store i8 0, i8* %38
br label %43
39: ; preds = %3
%40 = load i8*, i8** %4
%41 = load i8*, i8** %6
%42 = call i32 @strcat(i8* %40, i8* %41)
br label %43
43: ; preds = %39, %33
ret void
}

LLVM IR (-O2):
@.str = [37 x i8] c"Buffer overflow - %d bytes exceeded.\00"
define void @safeStrcat(i8* %0, i32 %1, i8* %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = tail call i32 @strlen(i8* %2)
%6 = add nsw i32 %5, %4
%7 = icmp slt i32 %6, %1
br i1 %7, label %25, label %8
8: ; preds = %3
%9 = tail call i32 @Niall_Warning(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %1)
%10 = tail call i32 @strlen(i8* %0)
%11 = icmp slt i32 %10, %1
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = add nsw i32 %1, -1
br label %21
14: ; preds = %8
%15 = sext i32 %10 to i64
%16 = getelementptr i8, i8* %0, i64 %15
%17 = add nsw i32 %1, -1
%18 = sub i32 %17, %10
%19 = zext i32 %18 to i64
%20 = add nuw nsw i64 %19, 1
br label %21
21: ; preds = %12, %14
%22 = phi i32 [ %13, %12 ], [ %17, %14 ]
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %0, i64 %23
store i8 0, i8* %24
br label %27
25: ; preds = %3
%26 = tail call i32 @strcat(i8* %0, i8* %2)
br label %27
27: ; preds = %25, %21
ret void
}