#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);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *mastrcat(char *dest, const char *src) {
  size_t dest_len = strlen(dest);
  size_t src_len = strlen(src);
  char *result = realloc(dest, dest_len + src_len + 1);
  if (NULL == result)
    return NULL;
  strncpy(result + dest_len, src, src_len + 1);
  return result;
}


x86 assembly (clang, -O0):
<mastrcat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    -0x28(%rbp),%rsi
add    $0x1,%rsi
call   1050 <realloc@plt>
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
cmp    -0x30(%rbp),%rax
jne    118c <mastrcat+0x5c>
movq   $0x0,-0x8(%rbp)
jmp    11ad <mastrcat+0x7d>
mov    -0x30(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mastrcat>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
call   1040 <strlen@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
lea    (%rax,%r15,1),%rsi
add    $0x1,%rsi
mov    %r12,%rdi
call   1050 <realloc@plt>
mov    %rax,%r12
test   %rax,%rax
je     117e <mastrcat+0x4e>
add    $0x1,%rbx
mov    %r12,%rdi
add    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <strncpy@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @mastrcat(i8* nocapture %0, i8* %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = add i64 %4, 1
%6 = add i64 %5, %3
%7 = tail call i8* @realloc(i8* %0, i64 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = getelementptr inbounds i8, i8* %7, i64 %3
%11 = tail call i32 @strncpy(i8* %10, i8* %1, i64 %5)
br label %12
12: ; preds = %2, %9
ret i8* %7
}