#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 */
scalar_t__ malloc(size_t);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char const *);
char *catstr(const char *a, const char *b) {
  char *value;
  size_t lena, lenb;
  if (!(a && b))
    return ((char *)NULL);
  lena = strlen(a);
  lenb = strlen(b);
  if (!(value = (char *)malloc(lena + lenb + 1)))
    return ((char *)NULL);
  (void)strcpy((char *)value, (char *)a);
  (void)strcpy((char *)(value + lena), (char *)b);
  return (value);
}


x86 assembly (clang, -O0):
<catstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1156 <catstr+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1163 <catstr+0x33>
movq   $0x0,-0x8(%rbp)
jmp    11cf <catstr+0x9f>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <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    11a9 <catstr+0x79>
movq   $0x0,-0x8(%rbp)
jmp    11cf <catstr+0x9f>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<catstr>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %ebx,%ebx
test   %rdi,%rdi
je     118c <catstr+0x5c>
mov    %rsi,%r14
test   %rsi,%rsi
je     118c <catstr+0x5c>
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%r12,%rax,1),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     118a <catstr+0x5a>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
add    %rbx,%r12
mov    %r12,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
jmp    118c <catstr+0x5c>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @catstr(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 %6, label %18
6: ; preds = %2
%7 = tail call i64 @strlen(i8* %0)
%8 = tail call i64 @strlen(i8* %1)
%9 = add i64 %7, 1
%10 = add i64 %9, %8
%11 = tail call i64 @malloc(i64 %10)
%12 = inttoptr i64 %11 to i8*
%13 = icmp eq i64 %11, 0
br i1 %13, label %18, label %14
14: ; preds = %6
%15 = tail call i32 @strcpy(i8* %12, i8* %0)
%16 = getelementptr inbounds i8, i8* %12, i64 %7
%17 = tail call i32 @strcpy(i8* %16, i8* %1)
br label %18
18: ; preds = %6, %2, %14
%19 = phi i8* [ %12, %14 ], [ null, %2 ], [ null, %6 ]
ret i8* %19
}