#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(scalar_t__);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char const *);
scalar_t__ strlen(char const *);
char *strdup2(const char *s1, const char *s2) {
  char *result = NULL;
  if (s1 != NULL && s2 != NULL && s1 != s2) {
    result = (char *)malloc(strlen(s1) + strlen(s2) + 1);
    strcpy(result, "");
    strcat(result, s1);
    strcat(result, s2);
  } else if (s1 != NULL) {
    result = strdup(s1);
  } else if (s2 != NULL) {
    result = strdup(s2);
  }
  return result;
}


x86 assembly (clang, -O0):
<strdup2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11e5 <strdup2+0x95>
cmpq   $0x0,-0x10(%rbp)
je     11e5 <strdup2+0x95>
mov    -0x8(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     11e5 <strdup2+0x95>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x20(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe3f(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcat@plt>
jmp    1224 <strdup2+0xd4>
cmpq   $0x0,-0x8(%rbp)
je     1202 <strdup2+0xb2>
mov    -0x8(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x18(%rbp)
jmp    121f <strdup2+0xcf>
cmpq   $0x0,-0x10(%rbp)
je     121a <strdup2+0xca>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x18(%rbp)
jmp    121f <strdup2+0xcf>
jmp    1224 <strdup2+0xd4>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strdup2>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
cmp    %rsi,%rdi
je     11bb <strdup2+0x6b>
test   %rbx,%rbx
je     11bb <strdup2+0x6b>
test   %r14,%r14
je     11bb <strdup2+0x6b>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%r15,%rax,1),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,%r15
lea    0xe6c(%rip),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1060 <strcat@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
test   %rbx,%rbx
je     11c5 <strdup2+0x75>
mov    %rbx,%rdi
jmp    11cd <strdup2+0x7d>
test   %r14,%r14
je     11d7 <strdup2+0x87>
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1070 <strdup@plt>
xor    %r15d,%r15d
jmp    11b2 <strdup2+0x62>

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

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i8* @strdup2(i8* %0, i8* %1) {
%3 = icmp ne i8* %0, null
%4 = icmp ne i8* %1, null
%5 = and i1 %3, %4
%6 = icmp ne i8* %0, %1
%7 = and i1 %6, %5
br i1 %7, label %8, label %18
8: ; preds = %2
%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 @malloc(i64 %12)
%14 = inttoptr i64 %13 to i8*
%15 = tail call i32 @strcpy(i8* %14, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @strcat(i8* %14, i8* %0)
%17 = tail call i32 @strcat(i8* %14, i8* %1)
br label %24
18: ; preds = %2
br i1 %3, label %19, label %21
19: ; preds = %18
%20 = tail call i8* @strdup(i8* %0)
br label %24
21: ; preds = %18
br i1 %4, label %22, label %24
22: ; preds = %21
%23 = tail call i8* @strdup(i8* %1)
br label %24
24: ; preds = %19, %22, %21, %8
%25 = phi i8* [ %14, %8 ], [ %20, %19 ], [ %23, %22 ], [ null, %21 ]
ret i8* %25
}