#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*/ strcat(void *, char *);
scalar_t__ strlen(char *);
void *xbuffer_ensure_size(void *, scalar_t__);
void *xbuffer_strcat(void *buf, char *data) {
  if (!buf || !data) {
    return NULL;
  }
  buf = xbuffer_ensure_size(buf, strlen(buf) + strlen(data) + 1);
  strcat(buf, data);
  return buf;
}


x86 assembly (clang, -O0):
<xbuffer_strcat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1156 <xbuffer_strcat+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1163 <xbuffer_strcat+0x33>
movq   $0x0,-0x8(%rbp)
jmp    11ae <xbuffer_strcat+0x7e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdi
add    %rax,%rsi
add    $0x1,%rsi
call   1040 <xbuffer_ensure_size@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strcat@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xbuffer_strcat>:
push   %r15
push   %r14
push   %rbx
xor    %ebx,%ebx
test   %rdi,%rdi
je     1175 <xbuffer_strcat+0x45>
mov    %rsi,%r14
test   %rsi,%rsi
je     1175 <xbuffer_strcat+0x45>
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    (%rbx,%rax,1),%rsi
add    $0x1,%rsi
mov    %r15,%rdi
call   1040 <xbuffer_ensure_size@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1050 <strcat@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @xbuffer_strcat(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%6 = load i8*, i8** %4
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %8, %2
store i8* null, i8** %3
br label %25
12: ; preds = %8
%13 = load i8*, i8** %4
%14 = load i8*, i8** %4
%15 = call i64 @strlen(i8* %14)
%16 = load i8*, i8** %5
%17 = call i64 @strlen(i8* %16)
%18 = add nsw i64 %15, %17
%19 = add nsw i64 %18, 1
%20 = call i8* @xbuffer_ensure_size(i8* %13, i64 %19)
store i8* %20, i8** %4
%21 = load i8*, i8** %4
%22 = load i8*, i8** %5
%23 = call i32 @strcat(i8* %21, i8* %22)
%24 = load i8*, i8** %4
store i8* %24, i8** %3
br label %25
25: ; preds = %12, %11
%26 = load i8*, i8** %3
ret i8* %26
}

LLVM IR (-O2):
define i8* @xbuffer_strcat(i8* %0, i8* %1) {
%3 = icmp ne i8* %0, null
%4 = icmp ne i8* %1, null
%5 = and i1 %3, %4
br i1 %5, label %6, label %13
6: ; preds = %2
%7 = tail call i64 @strlen(i8* %0)
%8 = tail call i64 @strlen(i8* %1)
%9 = add i64 %7, 1
%10 = add i64 %9, %8
%11 = tail call i8* @xbuffer_ensure_size(i8* %0, i64 %10)
%12 = tail call i32 @strcat(i8* %11, i8* %1)
br label %13
13: ; preds = %2, %6
%14 = phi i8* [ %11, %6 ], [ null, %2 ]
ret i8* %14
}