#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*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ xfree(char *);
scalar_t__ xmalloc(scalar_t__);
char *xstrdup(char const *);
char *xstrmerge3(char const *str1, char *str2) {
  char *res;
  if (!str1 && !str2)
    return 0;
  if (!str1)
    return str2;
  if (!str2)
    return xstrdup(str1);
  res = (char *)xmalloc(strlen(str1) + strlen(str2) + 1);
  strcpy(res, str1);
  strcat(res, str2);
  xfree(str2);
  return res;
}


x86 assembly (clang, -O0):
<xstrmerge3>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1193 <xstrmerge3+0x33>
cmpq   $0x0,-0x18(%rbp)
jne    1193 <xstrmerge3+0x33>
movq   $0x0,-0x8(%rbp)
jmp    121d <xstrmerge3+0xbd>
cmpq   $0x0,-0x10(%rbp)
jne    11ab <xstrmerge3+0x4b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    121d <xstrmerge3+0xbd>
cmpq   $0x0,-0x18(%rbp)
jne    11c8 <xstrmerge3+0x68>
mov    -0x10(%rbp),%rdi
call   1070 <xstrdup@plt>
mov    %rax,-0x8(%rbp)
jmp    121d <xstrmerge3+0xbd>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1060 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x18(%rbp),%rdi
call   1050 <xfree@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xstrmerge3>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
test   %rdi,%rdi
je     11b9 <xstrmerge3+0x59>
mov    %rdi,%r14
test   %rbx,%rbx
je     11c2 <xstrmerge3+0x62>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    (%r15,%rax,1),%rdi
add    $0x1,%rdi
call   1060 <xmalloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1080 <strcat@plt>
mov    %rbx,%rdi
call   1050 <xfree@plt>
mov    %r15,%rbx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1070 <xstrdup@plt>

LLVM IR (-O0):
define i8* @xstrmerge3(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %13, label %9
9: ; preds = %2
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %9
store i8* null, i8** %3
br label %42
13: ; preds = %9, %2
%14 = load i8*, i8** %4
%15 = icmp ne i8* %14, null
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = load i8*, i8** %5
store i8* %17, i8** %3
br label %42
18: ; preds = %13
%19 = load i8*, i8** %5
%20 = icmp ne i8* %19, null
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = load i8*, i8** %4
%23 = call i8* @xstrdup(i8* %22)
store i8* %23, i8** %3
br label %42
24: ; preds = %18
%25 = load i8*, i8** %4
%26 = call i64 @strlen(i8* %25)
%27 = load i8*, i8** %5
%28 = call i64 @strlen(i8* %27)
%29 = add nsw i64 %26, %28
%30 = add nsw i64 %29, 1
%31 = call i64 @xmalloc(i64 %30)
%32 = inttoptr i64 %31 to i8*
store i8* %32, i8** %6
%33 = load i8*, i8** %6
%34 = load i8*, i8** %4
%35 = call i32 @strcpy(i8* %33, i8* %34)
%36 = load i8*, i8** %6
%37 = load i8*, i8** %5
%38 = call i32 @strcat(i8* %36, i8* %37)
%39 = load i8*, i8** %5
%40 = call i32 @xfree(i8* %39)
%41 = load i8*, i8** %6
store i8* %41, i8** %3
br label %42
42: ; preds = %24, %21, %16, %12
%43 = load i8*, i8** %3
ret i8* %43
}

LLVM IR (-O2):
define i8* @xstrmerge3(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %18, label %4
4: ; preds = %2
%5 = icmp eq i8* %1, null
br i1 %5, label %6, label %8
6: ; preds = %4
%7 = tail call i8* @xstrdup(i8* %0)
br label %18
8: ; preds = %4
%9 = tail call i64 @strlen(i8* %0)
%10 = tail call i64 @strlen(i8* %1)
%11 = add i64 %9, 1
%12 = add i64 %11, %10
%13 = tail call i64 @xmalloc(i64 %12)
%14 = inttoptr i64 %13 to i8*
%15 = tail call i32 @strcpy(i8* %14, i8* %0)
%16 = tail call i32 @strcat(i8* %14, i8* %1)
%17 = tail call i32 @xfree(i8* %1)
br label %18
18: ; preds = %2, %8, %6
%19 = phi i8* [ %14, %8 ], [ %7, %6 ], [ %1, %2 ]
ret i8* %19
}