#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 *dstrdup(char const *);
char *realloc(char *, size_t);
char *strcat(char *, char const *);
size_t strlen(char const *);
char *dstrcati(char *a, const char *b) {
  size_t newlen;
  char *newstr;
  if (a == NULL)
    return dstrdup(b);
  newlen = strlen(a) + strlen(b);
  newstr = realloc(a, newlen + 1);
  return strcat(newstr, b);
}


x86 assembly (clang, -O0):
<dstrcati>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116d <dstrcati+0x2d>
mov    -0x18(%rbp),%rdi
call   1040 <dstrdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11b7 <dstrcati+0x77>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
call   1050 <realloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcat@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dstrcati>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
test   %rdi,%rdi
je     1180 <dstrcati+0x40>
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    (%r15,%rax,1),%rsi
add    $0x1,%rsi
mov    %rbx,%rdi
call   1050 <realloc@plt>
mov    %rax,%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <strcat@plt>
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <dstrdup@plt>

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

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