#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 *realloc(char *, size_t);
int /*<<< orphan*/ strcpy(char *, char const *);
size_t string_length(char const *);
size_t string_cat(char **prefix, const char *suffix) {
  size_t prefix_length = string_length(*prefix);
  size_t suffix_length = string_length(suffix);
  if (suffix_length) {
    *prefix = realloc(*prefix, prefix_length + suffix_length + 1);
    strcpy(*prefix + prefix_length, suffix);
  }
  return prefix_length + suffix_length;
}


x86 assembly (clang, -O0):
<string_cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1040 <string_length@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <string_length@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     119e <string_cat+0x6e>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
add    -0x20(%rbp),%rsi
add    $0x1,%rsi
call   1050 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
add    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rax
add    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_cat>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
mov    (%rdi),%rdi
call   1040 <string_length@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1040 <string_length@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1181 <string_cat+0x51>
mov    (%r12),%rdi
lea    (%rbx,%r15,1),%rsi
add    $0x1,%rsi
add    %r15,%rbx
call   1050 <realloc@plt>
mov    %rax,(%r12)
add    %rax,%r15
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
jmp    1184 <string_cat+0x54>
add    %r15,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @string_cat(i8** %0, i8* %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
store i8** %0, i8*** %3
store i8* %1, i8** %4
%7 = load i8**, i8*** %3
%8 = load i8*, i8** %7
%9 = call i64 @string_length(i8* %8)
store i64 %9, i64* %5
%10 = load i8*, i8** %4
%11 = call i64 @string_length(i8* %10)
store i64 %11, i64* %6
%12 = load i64, i64* %6
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %29
14: ; preds = %2
%15 = load i8**, i8*** %3
%16 = load i8*, i8** %15
%17 = load i64, i64* %5
%18 = load i64, i64* %6
%19 = add i64 %17, %18
%20 = add i64 %19, 1
%21 = call i8* @realloc(i8* %16, i64 %20)
%22 = load i8**, i8*** %3
store i8* %21, i8** %22
%23 = load i8**, i8*** %3
%24 = load i8*, i8** %23
%25 = load i64, i64* %5
%26 = getelementptr inbounds i8, i8* %24, i64 %25
%27 = load i8*, i8** %4
%28 = call i32 @strcpy(i8* %26, i8* %27)
br label %29
29: ; preds = %14, %2
%30 = load i64, i64* %5
%31 = load i64, i64* %6
%32 = add i64 %30, %31
ret i64 %32
}

LLVM IR (-O2):
define i64 @string_cat(i8** nocapture %0, i8* %1) {
%3 = load i8*, i8** %0
%4 = tail call i64 @string_length(i8* %3)
%5 = tail call i64 @string_length(i8* %1)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = add nuw nsw i64 %5, %4
br label %16
9: ; preds = %2
%10 = load i8*, i8** %0
%11 = add i64 %5, %4
%12 = add i64 %11, 1
%13 = tail call i8* @realloc(i8* %10, i64 %12)
store i8* %13, i8** %0
%14 = getelementptr inbounds i8, i8* %13, i64 %4
%15 = tail call i32 @strcpy(i8* %14, i8* %1)
br label %16
16: ; preds = %7, %9
%17 = phi i64 [ %8, %7 ], [ %11, %9 ]
ret i64 %17
}