#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 *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *strcdup(const char *s1, const char *s2) {
  if (!s1 || !s2) {
    return NULL;
  }
  size_t slen = strlen(s1);
  size_t s2len = strlen(s2);
  size_t nlen = slen + s2len + 1;
  char *dst = malloc(nlen);
  if (dst == NULL) {
    return NULL;
  }
  memcpy(dst, s1, slen);
  strncpy(dst + slen, s2, s2len + 1);
  return dst;
}


x86 assembly (clang, -O0):
<strcdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1166 <strcdup+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1173 <strcdup+0x33>
movq   $0x0,-0x8(%rbp)
jmp    11f4 <strcdup+0xb4>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11c2 <strcdup+0x82>
movq   $0x0,-0x8(%rbp)
jmp    11f4 <strcdup+0xb4>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x38(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strcdup>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
xor    %ebx,%ebx
test   %rdi,%rdi
je     11aa <strcdup+0x6a>
mov    %rsi,%r14
test   %rsi,%rsi
je     11aa <strcdup+0x6a>
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%r13
lea    (%rax,%r12,1),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11a8 <strcdup+0x68>
mov    %rax,%rbx
add    $0x1,%r13
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1050 <memcpy@plt>
mov    %rbx,%rdi
add    %r12,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   1030 <strncpy@plt>
jmp    11aa <strcdup+0x6a>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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