#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*/ free(char *);
scalar_t__ realloc(char *, scalar_t__);
char *strcat(char *, char const *);
scalar_t__ strlen(char const *);
char *gs_strcat_r(char *dest, const char *src) {
  static char *save;
  if (!dest || !src) {
    free(dest);
    return NULL;
  }
  dest = (char *)realloc(save = dest, strlen(dest) + strlen(src) + 1);
  if (!dest) {
    free(save);
    return dest;
  }
  return strcat(dest, src);
}


x86 assembly (clang, -O0):
<gs_strcat_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1166 <gs_strcat_r+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    117c <gs_strcat_r+0x3c>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ee <gs_strcat_r+0xae>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    %rax,0x2ebd(%rip)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdi
add    %rax,%rsi
add    $0x1,%rsi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11dd <gs_strcat_r+0x9d>
mov    0x2e7d(%rip),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ee <gs_strcat_r+0xae>
mov    -0x10(%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):
<gs_strcat_r>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
test   %rdi,%rdi
je     1194 <gs_strcat_r+0x54>
mov    %rsi,%r14
test   %rsi,%rsi
je     1194 <gs_strcat_r+0x54>
mov    %rbx,0x2eec(%rip)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%r15,%rax,1),%rsi
add    $0x1,%rsi
mov    %rbx,%rdi
call   1050 <realloc@plt>
test   %rax,%rax
je     1199 <gs_strcat_r+0x59>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
jmp    11a7 <gs_strcat_r+0x67>
mov    %rbx,%rdi
jmp    11a0 <gs_strcat_r+0x60>
mov    0x2ea8(%rip),%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@gs_strcat_r.save = internal global i8* null
define i8* @gs_strcat_r(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 %14, label %11
11: ; preds = %8, %2
%12 = load i8*, i8** %4
%13 = call i32 @free(i8* %12)
store i8* null, i8** %3
br label %34
14: ; preds = %8
%15 = load i8*, i8** %4
store i8* %15, i8** @gs_strcat_r.save
%16 = load i8*, i8** %4
%17 = call i64 @strlen(i8* %16)
%18 = load i8*, i8** %5
%19 = call i64 @strlen(i8* %18)
%20 = add nsw i64 %17, %19
%21 = add nsw i64 %20, 1
%22 = call i64 @realloc(i8* %15, i64 %21)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %4
%24 = load i8*, i8** %4
%25 = icmp ne i8* %24, null
br i1 %25, label %30, label %26
26: ; preds = %14
%27 = load i8*, i8** @gs_strcat_r.save
%28 = call i32 @free(i8* %27)
%29 = load i8*, i8** %4
store i8* %29, i8** %3
br label %34
30: ; preds = %14
%31 = load i8*, i8** %4
%32 = load i8*, i8** %5
%33 = call i8* @strcat(i8* %31, i8* %32)
store i8* %33, i8** %3
br label %34
34: ; preds = %30, %26, %11
%35 = load i8*, i8** %3
ret i8* %35
}

LLVM IR (-O2):
@gs_strcat_r.save = internal unnamed_addr global i8* null
define i8* @gs_strcat_r(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 %8, label %6
6: ; preds = %2
%7 = tail call i32 @free(i8* %0)
br label %21
8: ; preds = %2
store i8* %0, i8** @gs_strcat_r.save
%9 = tail call i64 @strlen(i8* %0)
%10 = tail call i64 @strlen(i8* %1)
%11 = add i64 %9, 1
%12 = add i64 %11, %10
%13 = tail call i64 @realloc(i8* %0, i64 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %8
%16 = load i8*, i8** @gs_strcat_r.save
%17 = tail call i32 @free(i8* %16)
br label %21
18: ; preds = %8
%19 = inttoptr i64 %13 to i8*
%20 = tail call i8* @strcat(i8* %19, i8* %1)
br label %21
21: ; preds = %18, %15, %6
%22 = phi i8* [ %19, %18 ], [ null, %15 ], [ null, %6 ]
ret i8* %22
}