#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 *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
extern char *str_dupcat(const char *a, const char *b) {
  char *copy;
  size_t alen, blen;
  alen = strlen(a);
  blen = strlen(b);
  if (!(copy = malloc(alen + blen + 1))) {
    return 0;
  }
  memcpy(copy, a, alen);
  memcpy(copy + alen, b, blen + 1);
  return copy;
}


x86 assembly (clang, -O0):
<str_dupcat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
add    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1186 <str_dupcat+0x56>
movq   $0x0,-0x8(%rbp)
jmp    11b8 <str_dupcat+0x88>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
add    $0x1,%rdx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_dupcat>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rax,%r13
lea    (%rax,%r12,1),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     118a <str_dupcat+0x5a>
add    $0x1,%r13
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <memcpy@plt>
mov    %rbx,%rdi
add    %r12,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   1040 <memcpy@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @str_dupcat(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
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* %7
%11 = load i8*, i8** %5
%12 = call i64 @strlen(i8* %11)
store i64 %12, i64* %8
%13 = load i64, i64* %7
%14 = load i64, i64* %8
%15 = add i64 %13, %14
%16 = add i64 %15, 1
%17 = call i8* @malloc(i64 %16)
store i8* %17, i8** %6
%18 = icmp ne i8* %17, null
br i1 %18, label %20, label %19
19: ; preds = %2
store i8* null, i8** %3
br label %33
20: ; preds = %2
%21 = load i8*, i8** %6
%22 = load i8*, i8** %4
%23 = load i64, i64* %7
%24 = call i32 @memcpy(i8* %21, i8* %22, i64 %23)
%25 = load i8*, i8** %6
%26 = load i64, i64* %7
%27 = getelementptr inbounds i8, i8* %25, i64 %26
%28 = load i8*, i8** %5
%29 = load i64, i64* %8
%30 = add i64 %29, 1
%31 = call i32 @memcpy(i8* %27, i8* %28, i64 %30)
%32 = load i8*, i8** %6
store i8* %32, i8** %3
br label %33
33: ; preds = %20, %19
%34 = load i8*, i8** %3
ret i8* %34
}

LLVM IR (-O2):
define i8* @str_dupcat(i8* %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* @malloc(i64 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %13, label %9
9: ; preds = %2
%10 = tail call i32 @memcpy(i8* %7, i8* %0, i64 %3)
%11 = getelementptr inbounds i8, i8* %7, i64 %3
%12 = tail call i32 @memcpy(i8* %11, i8* %1, i64 %5)
br label %13
13: ; preds = %2, %9
ret i8* %7
}