#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
scalar_t__ realloc(char *, int);
int /*<<< orphan*/ stderr;
int strlen(char const *);
int /*<<< orphan*/ strncat(char *, char const *, int);
char **alloc_strcat(char **dest, char const *src) {
  int destlen = 0, srclen = 0, wasdestnull = 0;
  destlen = (*dest) ? strlen(*dest) : 0;
  srclen = strlen(src);
  wasdestnull = !(*dest);
  *dest = (char *)realloc(*dest, (destlen + srclen + 1) * sizeof(char));
  if (dest) {
    if (wasdestnull) {
      memset(*dest, 0, strlen(src) + 1);
    }
    strncat(*dest, src, strlen(src));
  } else {
    fprintf(stderr, "realloc failed\n");
  }
  return dest;
}


x86 assembly (clang, -O0):
<alloc_strcat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
je     1197 <alloc_strcat+0x47>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x20(%rbp)
jmp    11a1 <alloc_strcat+0x51>
xor    %eax,%eax
mov    %eax,-0x20(%rbp)
jmp    11a1 <alloc_strcat+0x51>
mov    -0x20(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x8(%rbp)
je     1258 <alloc_strcat+0x108>
cmpl   $0x0,-0x1c(%rbp)
je     1228 <alloc_strcat+0xd8>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%edx
add    $0x1,%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1050 <strncat@plt>
jmp    126d <alloc_strcat+0x11d>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
lea    0xd98(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_strcat>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    (%rdi),%rdi
test   %rdi,%rdi
je     1162 <alloc_strcat+0x22>
call   1030 <strlen@plt>
mov    %eax,%ebx
add    $0x1,%ebx
jmp    1167 <alloc_strcat+0x27>
mov    $0x1,%ebx
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    (%r15),%r12
add    %eax,%ebx
mov    %r12,%rdi
mov    %ebx,%esi
call   1060 <realloc@plt>
mov    %rax,%rbx
mov    %rax,(%r15)
test   %r12,%r12
jne    11a1 <alloc_strcat+0x61>
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
mov    (%r15),%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1050 <strncat@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@stderr = global i32 0
define i8** @alloc_strcat(i8** returned %0, i8* %1) {
%3 = load i8*, i8** %0
%4 = icmp eq i8* %3, null
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = tail call i32 @strlen(i8* %3)
%7 = add i32 %6, 1
br label %8
8: ; preds = %2, %5
%9 = phi i32 [ %7, %5 ], [ 1, %2 ]
%10 = tail call i32 @strlen(i8* %1)
%11 = load i8*, i8** %0
%12 = add i32 %9, %10
%13 = tail call i64 @realloc(i8* %11, i32 %12)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %0
%15 = icmp eq i8* %11, null
br i1 %15, label %16, label %21
16: ; preds = %8
%17 = tail call i32 @strlen(i8* %1)
%18 = add nsw i32 %17, 1
%19 = tail call i32 @memset(i8* %14, i32 0, i32 %18)
%20 = load i8*, i8** %0
br label %21
21: ; preds = %16, %8
%22 = phi i8* [ %20, %16 ], [ %14, %8 ]
%23 = tail call i32 @strlen(i8* %1)
%24 = tail call i32 @strncat(i8* %22, i8* %1, i32 %23)
ret i8** %0
}