#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*/ snprintf(char *, size_t, char *, char *, char const *);
int /*<<< orphan*/ xfree(char *);
char *xmalloc(size_t);
char *xstrdup(char *);
int xstrlen(char const *);
char *xstrcat(char *dest, const char *src) {
  char *save = xstrdup(dest);
  size_t len = xstrlen(save) + xstrlen(src) + 1;
  xfree(dest);
  dest = xmalloc(len);
  if (dest == NULL) {
    xfree(save);
    return NULL;
  }
  snprintf(dest, len, "%s%s", save, src);
  xfree(save);
  return dest;
}


x86 assembly (clang, -O0):
<xstrcat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <xstrdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <xstrlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <xstrlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <xfree@plt>
mov    -0x28(%rbp),%rdi
call   1050 <xmalloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c9 <xstrcat+0x79>
mov    -0x20(%rbp),%rdi
call   1030 <xfree@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f6 <xstrcat+0xa6>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xe20(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <xfree@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xstrcat>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1060 <xstrdup@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1070 <xstrlen@plt>
mov    %eax,%r12d
mov    %r14,%rdi
call   1070 <xstrlen@plt>
add    %r12d,%eax
add    $0x1,%eax
movslq %eax,%r12
mov    %rbx,%rdi
call   1030 <xfree@plt>
mov    %r12,%rdi
call   1050 <xmalloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11b2 <xstrcat+0x62>
lea    0xe5f(%rip),%rdx
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rcx
mov    %r14,%r8
call   1040 <snprintf@plt>
mov    %r15,%rdi
call   1030 <xfree@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%s%s\00"
define i8* @xstrcat(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = call i8* @xstrdup(i8* %8)
store i8* %9, i8** %6
%10 = load i8*, i8** %6
%11 = call i32 @xstrlen(i8* %10)
%12 = load i8*, i8** %5
%13 = call i32 @xstrlen(i8* %12)
%14 = add nsw i32 %11, %13
%15 = add nsw i32 %14, 1
%16 = sext i32 %15 to i64
store i64 %16, i64* %7
%17 = load i8*, i8** %4
%18 = call i32 @xfree(i8* %17)
%19 = load i64, i64* %7
%20 = call i8* @xmalloc(i64 %19)
store i8* %20, i8** %4
%21 = load i8*, i8** %4
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %2
%24 = load i8*, i8** %6
%25 = call i32 @xfree(i8* %24)
store i8* null, i8** %3
br label %35
26: ; preds = %2
%27 = load i8*, i8** %4
%28 = load i64, i64* %7
%29 = load i8*, i8** %6
%30 = load i8*, i8** %5
%31 = call i32 @snprintf(i8* %27, i64 %28, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %29, i8* %30)
%32 = load i8*, i8** %6
%33 = call i32 @xfree(i8* %32)
%34 = load i8*, i8** %4
store i8* %34, i8** %3
br label %35
35: ; preds = %26, %23
%36 = load i8*, i8** %3
ret i8* %36
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s%s\00"
define i8* @xstrcat(i8* %0, i8* %1) {
%3 = tail call i8* @xstrdup(i8* %0)
%4 = tail call i32 @xstrlen(i8* %3)
%5 = tail call i32 @xstrlen(i8* %1)
%6 = add i32 %4, 1
%7 = add i32 %6, %5
%8 = sext i32 %7 to i64
%9 = tail call i32 @xfree(i8* %0)
%10 = tail call i8* @xmalloc(i64 %8)
%11 = icmp eq i8* %10, null
br i1 %11, label %14, label %12
12: ; preds = %2
%13 = tail call i32 @snprintf(i8* %10, i64 %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %3, i8* %1)
br label %14
14: ; preds = %2, %12
%15 = tail call i32 @xfree(i8* %3)
ret i8* %10
}