#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 */
scalar_t__ malloc(int);
scalar_t__ realloc(void *, int);
int /*<<< orphan*/ sprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ strchr(char *, int /*<<< orphan*/);
int strlen(char *);
void strcat_realloc(char **buffer, char *str, char *delimiter) {
  int delimiter_length = 0;
  if (delimiter)
    delimiter_length = strlen(delimiter);
  if (*buffer == 0) {
    if ((*buffer = (char *)malloc(strlen(str) + delimiter_length + 1)))
      **buffer = 0;
  } else
    *buffer = (char *)realloc((void *)*buffer, strlen(*buffer) + strlen(str) +
                                                   delimiter_length + 1);
  if (*buffer)
    sprintf(strchr(*buffer, 0), "%s%s", str, (delimiter) ? delimiter : "");
}


x86 assembly (clang, -O0):
<strcat_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1182 <strcat_realloc+0x32>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11c6 <strcat_realloc+0x76>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%edi
add    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    -0x8(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
je     11c1 <strcat_realloc+0x71>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movb   $0x0,(%rax)
jmp    1207 <strcat_realloc+0xb7>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%rdi
add    %eax,%esi
add    -0x1c(%rbp),%esi
add    $0x1,%esi
call   1060 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
je     126d <strcat_realloc+0x11d>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
xor    %esi,%esi
call   1040 <strchr@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1246 <strcat_realloc+0xf6>
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    1256 <strcat_realloc+0x106>
lea    0xdb7(%rip),%rax
mov    %rax,-0x48(%rbp)
jmp    1256 <strcat_realloc+0x106>
mov    -0x38(%rbp),%rdx
mov    -0x3c(%rbp),%edi
mov    -0x48(%rbp),%rcx
lea    0xd98(%rip),%rsi
call   1070 <sprintf@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strcat_realloc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
test   %rdx,%rdx
je     1178 <strcat_realloc+0x28>
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
add    $0x1,%ebp
jmp    117d <strcat_realloc+0x2d>
mov    $0x1,%ebp
mov    (%r12),%rbx
test   %rbx,%rbx
je     11ae <strcat_realloc+0x5e>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%r13d
mov    %r14,%rdi
call   1030 <strlen@plt>
add    %r13d,%ebp
add    %eax,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
call   1060 <realloc@plt>
mov    %rax,(%r12)
jmp    11cf <strcat_realloc+0x7f>
mov    %r14,%rdi
call   1030 <strlen@plt>
add    %eax,%ebp
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,(%r12)
test   %rax,%rax
je     11cf <strcat_realloc+0x7f>
movb   $0x0,(%rax)
mov    (%r12),%rax
test   %rax,%rax
je     120b <strcat_realloc+0xbb>
mov    %rax,%rdi
xor    %esi,%esi
call   1040 <strchr@plt>
test   %r15,%r15
lea    0xe1c(%rip),%rcx
cmovne %r15,%rcx
lea    0xe0d(%rip),%rsi
mov    %eax,%edi
mov    %r14,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <sprintf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [1 x i8] zeroinitializer
define void @strcat_realloc(i8** %0, i8* %1, i8* %2) {
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i8** %0, i8*** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i32 0, i32* %7
%8 = load i8*, i8** %6
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = load i8*, i8** %6
%12 = call i32 @strlen(i8* %11)
store i32 %12, i32* %7
br label %13
13: ; preds = %10, %3
%14 = load i8**, i8*** %4
%15 = load i8*, i8** %14
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %31
17: ; preds = %13
%18 = load i8*, i8** %5
%19 = call i32 @strlen(i8* %18)
%20 = load i32, i32* %7
%21 = add nsw i32 %19, %20
%22 = add nsw i32 %21, 1
%23 = call i64 @malloc(i32 %22)
%24 = inttoptr i64 %23 to i8*
%25 = load i8**, i8*** %4
store i8* %24, i8** %25
%26 = icmp ne i8* %24, null
br i1 %26, label %27, label %30
27: ; preds = %17
%28 = load i8**, i8*** %4
%29 = load i8*, i8** %28
store i8 0, i8* %29
br label %30
30: ; preds = %27, %17
br label %46
31: ; preds = %13
%32 = load i8**, i8*** %4
%33 = load i8*, i8** %32
%34 = load i8**, i8*** %4
%35 = load i8*, i8** %34
%36 = call i32 @strlen(i8* %35)
%37 = load i8*, i8** %5
%38 = call i32 @strlen(i8* %37)
%39 = add nsw i32 %36, %38
%40 = load i32, i32* %7
%41 = add nsw i32 %39, %40
%42 = add nsw i32 %41, 1
%43 = call i64 @realloc(i8* %33, i32 %42)
%44 = inttoptr i64 %43 to i8*
%45 = load i8**, i8*** %4
store i8* %44, i8** %45
br label %46
46: ; preds = %31, %30
%47 = load i8**, i8*** %4
%48 = load i8*, i8** %47
%49 = icmp ne i8* %48, null
br i1 %49, label %50, label %63
50: ; preds = %46
%51 = load i8**, i8*** %4
%52 = load i8*, i8** %51
%53 = call i32 @strchr(i8* %52, i32 0)
%54 = load i8*, i8** %5
%55 = load i8*, i8** %6
%56 = icmp ne i8* %55, null
br i1 %56, label %57, label %59
57: ; preds = %50
%58 = load i8*, i8** %6
br label %60
59: ; preds = %50
br label %60
60: ; preds = %59, %57
%61 = phi i8* [ %58, %57 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %59 ]
%62 = call i32 @sprintf(i32 %53, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %54, i8* %61)
br label %63
63: ; preds = %60, %46
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [1 x i8] zeroinitializer
define void @strcat_realloc(i8** nocapture %0, i8* %1, i8* %2) {
%4 = icmp eq i8* %2, null
br i1 %4, label %8, label %5
5: ; preds = %3
%6 = tail call i32 @strlen(i8* %2)
%7 = add i32 %6, 1
br label %8
8: ; preds = %5, %3
%9 = phi i32 [ %7, %5 ], [ 1, %3 ]
%10 = load i8*, i8** %0
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %20
12: ; preds = %8
%13 = tail call i32 @strlen(i8* %1)
%14 = add i32 %9, %13
%15 = tail call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %0
%17 = icmp eq i64 %15, 0
br i1 %17, label %27, label %18
18: ; preds = %12
store i8 0, i8* %16
%19 = load i8*, i8** %0
br label %27
20: ; preds = %8
%21 = tail call i32 @strlen(i8* %10)
%22 = tail call i32 @strlen(i8* %1)
%23 = add i32 %9, %21
%24 = add i32 %23, %22
%25 = tail call i64 @realloc(i8* %10, i32 %24)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %0
br label %27
27: ; preds = %12, %18, %20
%28 = phi i8* [ %16, %12 ], [ %19, %18 ], [ %26, %20 ]
%29 = icmp eq i8* %28, null
br i1 %29, label %34, label %30
30: ; preds = %27
%31 = tail call i32 @strchr(i8* %28, i32 0)
%32 = select i1 %4, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8* %2
%33 = tail call i32 @sprintf(i32 %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %32)
br label %34
34: ; preds = %30, %27
ret void
}