#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*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *dstrcat(char *a, char *b) {
  char *r = (char *)malloc((a ? strlen(a) : 0) + (b ? strlen(b) : 0) + 1);
  strcpy(r, a ? a : "");
  strcat(r, b ? b : "");
  if (a)
    free(a);
  if (b)
    free(b);
  return r;
}


x86 assembly (clang, -O0):
<dstrcat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     117c <dstrcat+0x2c>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x1c(%rbp)
jmp    1186 <dstrcat+0x36>
xor    %eax,%eax
mov    %eax,-0x1c(%rbp)
jmp    1186 <dstrcat+0x36>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a8 <dstrcat+0x58>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
jmp    11b2 <dstrcat+0x62>
xor    %eax,%eax
mov    %eax,-0x24(%rbp)
jmp    11b2 <dstrcat+0x62>
mov    -0x20(%rbp),%edi
mov    -0x24(%rbp),%eax
add    %eax,%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11e6 <dstrcat+0x96>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    11f6 <dstrcat+0xa6>
lea    0xe13(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    11f6 <dstrcat+0xa6>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1223 <dstrcat+0xd3>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    1233 <dstrcat+0xe3>
lea    0xdd6(%rip),%rax
mov    %rax,-0x48(%rbp)
jmp    1233 <dstrcat+0xe3>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1070 <strcat@plt>
cmpq   $0x0,-0x8(%rbp)
je     1254 <dstrcat+0x104>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x10(%rbp)
je     1268 <dstrcat+0x118>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dstrcat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
test   %rdi,%rdi
je     1175 <dstrcat+0x25>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
add    $0x1,%ebp
jmp    117a <dstrcat+0x2a>
mov    $0x1,%ebp
lea    0xe7f(%rip),%rbx
test   %r14,%r14
je     1193 <dstrcat+0x43>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %r14,%r13
jmp    1198 <dstrcat+0x48>
xor    %eax,%eax
mov    %rbx,%r13
add    %eax,%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
test   %r12,%r12
cmovne %r12,%rbx
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %r15,%rdi
mov    %r13,%rsi
call   1070 <strcat@plt>
test   %r12,%r12
je     11ce <dstrcat+0x7e>
mov    %r12,%rdi
call   1030 <free@plt>
test   %r14,%r14
je     11db <dstrcat+0x8b>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define i8* @dstrcat(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* %9)
br label %12
11: ; preds = %2
br label %12
12: ; preds = %11, %8
%13 = phi i32 [ %10, %8 ], [ 0, %11 ]
%14 = load i8*, i8** %4
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i8*, i8** %4
%18 = call i32 @strlen(i8* %17)
br label %20
19: ; preds = %12
br label %20
20: ; preds = %19, %16
%21 = phi i32 [ %18, %16 ], [ 0, %19 ]
%22 = add nsw i32 %13, %21
%23 = add nsw i32 %22, 1
%24 = call i64 @malloc(i32 %23)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %5
%26 = load i8*, i8** %5
%27 = load i8*, i8** %3
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %31
29: ; preds = %20
%30 = load i8*, i8** %3
br label %32
31: ; preds = %20
br label %32
32: ; preds = %31, %29
%33 = phi i8* [ %30, %29 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %31 ]
%34 = call i32 @strcpy(i8* %26, i8* %33)
%35 = load i8*, i8** %5
%36 = load i8*, i8** %4
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = load i8*, i8** %4
br label %41
40: ; preds = %32
br label %41
41: ; preds = %40, %38
%42 = phi i8* [ %39, %38 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %40 ]
%43 = call i32 @strcat(i8* %35, i8* %42)
%44 = load i8*, i8** %3
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = load i8*, i8** %3
%48 = call i32 @free(i8* %47)
br label %49
49: ; preds = %46, %41
%50 = load i8*, i8** %4
%51 = icmp ne i8* %50, null
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = load i8*, i8** %4
%54 = call i32 @free(i8* %53)
br label %55
55: ; preds = %52, %49
%56 = load i8*, i8** %5
ret i8* %56
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i8* @dstrcat(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = tail call i32 @strlen(i8* %0)
%6 = add i32 %5, 1
br label %7
7: ; preds = %2, %4
%8 = phi i32 [ %6, %4 ], [ 1, %2 ]
%9 = icmp eq i8* %1, null
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = tail call i32 @strlen(i8* %1)
br label %12
12: ; preds = %7, %10
%13 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %7 ], [ %1, %10 ]
%14 = phi i32 [ 0, %7 ], [ %11, %10 ]
%15 = add i32 %8, %14
%16 = tail call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i8*
%18 = select i1 %3, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* %0
%19 = tail call i32 @strcpy(i8* %17, i8* %18)
%20 = tail call i32 @strcat(i8* %17, i8* %13)
br i1 %3, label %23, label %21
21: ; preds = %12
%22 = tail call i32 @free(i8* %0)
br label %23
23: ; preds = %21, %12
br i1 %9, label %26, label %24
24: ; preds = %23
%25 = tail call i32 @free(i8* %1)
br label %26
26: ; preds = %24, %23
ret i8* %17
}