#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
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++) {
      fprintf(stderr, "blork\n");
      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    11e7 <safeStrcat+0xa7>
mov    -0xc(%rbp),%esi
lea    0xe7b(%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    11d1 <safeStrcat+0x91>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
lea    0xe73(%rip),%rsi
call   1050 <fprintf@plt>
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    1196 <safeStrcat+0x56>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11f4 <safeStrcat+0xb4>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcat@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safeStrcat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %ebp,%eax
cmp    %r15d,%eax
jge    1183 <safeStrcat+0x43>
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <strcat@plt>
lea    0xe76(%rip),%rdi
mov    %r15d,%esi
call   1030 <Niall_Warning@plt>
mov    %rbx,(%rsp)
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    %r15d,%eax
jge    11d9 <safeStrcat+0x99>
movslq %eax,%r13
add    (%rsp),%r13
mov    %r15d,%ebx
sub    %eax,%ebx
xor    %ebp,%ebp
mov    0x2e28(%rip),%r14
lea    0xe66(%rip),%r12
nop
mov    (%r14),%edi
mov    %r12,%rsi
call   1050 <fprintf@plt>
movb   $0x3c,0x0(%r13,%rbp,1)
add    $0x1,%rbp
cmp    %ebp,%ebx
jne    11c0 <safeStrcat+0x80>
movslq %r15d,%rax
mov    (%rsp),%rcx
movb   $0x0,-0x1(%rax,%rcx,1)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [37 x i8] c"Buffer overflow - %d bytes exceeded.\00"
@stderr = global i32 0
@.str.1 = [7 x i8] c"blork\0A\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 %41
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 = %32, %16
%22 = load i32, i32* %7
%23 = load i32, i32* %5
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %35
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%28 = load i8*, i8** %4
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 60, i8* %31
br label %32
32: ; preds = %25
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
store i32 %34, i32* %7
br label %21
35: ; preds = %21
%36 = load i8*, i8** %4
%37 = load i32, i32* %5
%38 = sub nsw i32 %37, 1
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
store i8 0, i8* %40
br label %45
41: ; preds = %3
%42 = load i8*, i8** %4
%43 = load i8*, i8** %6
%44 = call i32 @strcat(i8* %42, i8* %43)
br label %45
45: ; preds = %41, %35
ret void
}

LLVM IR (-O2):
@.str = [37 x i8] c"Buffer overflow - %d bytes exceeded.\00"
@stderr = global i32 0
@.str.1 = [7 x i8] c"blork\0A\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 %26, 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 %12, label %22
12: ; preds = %8
%13 = sext i32 %10 to i64
br label %14
14: ; preds = %12, %14
%15 = phi i64 [ %13, %12 ], [ %19, %14 ]
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%18 = getelementptr inbounds i8, i8* %0, i64 %15
store i8 60, i8* %18
%19 = add nsw i64 %15, 1
%20 = trunc i64 %19 to i32
%21 = icmp eq i32 %20, %1
br i1 %21, label %22, label %14
22: ; preds = %14, %8
%23 = add nsw i32 %1, -1
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %0, i64 %24
store i8 0, i8* %25
br label %28
26: ; preds = %3
%27 = tail call i32 @strcat(i8* %0, i8* %2)
br label %28
28: ; preds = %26, %22
ret void
}