#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 *realloc(char *, size_t);
char *strcat(char *, char *);
size_t strlen(char *);
char *safe_cat(char **dest, size_t *length, char *src) {
  size_t dest_l = strlen(*dest), src_l = strlen(src);
  if (dest_l + src_l + 1 > *length) {
    *length += 512;
    *dest = realloc(*dest, *length * sizeof(char));
  }
  return strcat(*dest, src);
}


x86 assembly (clang, -O0):
<safe_cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
add    $0x1,%rax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%rax
jbe    11ac <safe_cat+0x7c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
add    $0x200,%rcx
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
shl    $0x0,%rsi
call   1040 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strcat@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_cat>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
mov    (%rdi),%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,%r13
mov    %r14,%rdi
call   1030 <strlen@plt>
add    %r13,%rax
add    $0x1,%rax
mov    (%r12),%rsi
cmp    %rsi,%rax
jbe    1181 <safe_cat+0x51>
add    $0x200,%rsi
mov    %rsi,(%r12)
mov    %rbx,%rdi
call   1040 <realloc@plt>
mov    %rax,%rbx
mov    %rax,(%r15)
mov    %rbx,%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1050 <strcat@plt>

LLVM IR (-O0):
define i8* @safe_cat(i8** %0, i64* %1, i8* %2) {
%4 = alloca i8**
%5 = alloca i64*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i8** %0, i8*** %4
store i64* %1, i64** %5
store i8* %2, i8** %6
%9 = load i8**, i8*** %4
%10 = load i8*, i8** %9
%11 = call i64 @strlen(i8* %10)
store i64 %11, i64* %7
%12 = load i8*, i8** %6
%13 = call i64 @strlen(i8* %12)
store i64 %13, i64* %8
%14 = load i64, i64* %7
%15 = load i64, i64* %8
%16 = add i64 %14, %15
%17 = add i64 %16, 1
%18 = load i64*, i64** %5
%19 = load i64, i64* %18
%20 = icmp ugt i64 %17, %19
br i1 %20, label %21, label %32
21: ; preds = %3
%22 = load i64*, i64** %5
%23 = load i64, i64* %22
%24 = add i64 %23, 512
store i64 %24, i64* %22
%25 = load i8**, i8*** %4
%26 = load i8*, i8** %25
%27 = load i64*, i64** %5
%28 = load i64, i64* %27
%29 = mul i64 %28, 1
%30 = call i8* @realloc(i8* %26, i64 %29)
%31 = load i8**, i8*** %4
store i8* %30, i8** %31
br label %32
32: ; preds = %21, %3
%33 = load i8**, i8*** %4
%34 = load i8*, i8** %33
%35 = load i8*, i8** %6
%36 = call i8* @strcat(i8* %34, i8* %35)
ret i8* %36
}

LLVM IR (-O2):
define i8* @safe_cat(i8** nocapture %0, i64* nocapture %1, i8* nocapture %2) {
%4 = load i8*, i8** %0
%5 = tail call i64 @strlen(i8* dereferenceable(1) %4)
%6 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%7 = add i64 %5, 1
%8 = add i64 %7, %6
%9 = load i64, i64* %1
%10 = icmp ugt i64 %8, %9
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = add i64 %9, 512
store i64 %12, i64* %1
%13 = tail call i8* @realloc(i8* %4, i64 %12)
store i8* %13, i8** %0
br label %14
14: ; preds = %11, %3
%15 = phi i8* [ %13, %11 ], [ %4, %3 ]
%16 = tail call i8* @strcat(i8* dereferenceable(1) %15, i8* dereferenceable(1) %2)
ret i8* %15
}