#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__ nmalloc(int);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *mallocstrcpy(char *dest, const char *src) {
  if (src == dest)
    return dest;
  if (dest != NULL)
    free(dest);
  if (src == NULL)
    return NULL;
  dest = (char *)nmalloc((strlen(src) + 1) * sizeof(char));
  strcpy(dest, src);
  return dest;
}


x86 assembly (clang, -O0):
<mallocstrcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    116b <mallocstrcpy+0x2b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c9 <mallocstrcpy+0x89>
cmpq   $0x0,-0x10(%rbp)
je     117f <mallocstrcpy+0x3f>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1197 <mallocstrcpy+0x57>
movq   $0x0,-0x8(%rbp)
jmp    11c9 <mallocstrcpy+0x89>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <nmalloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mallocstrcpy>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    %rdi,%rsi
je     1180 <mallocstrcpy+0x40>
test   %rdi,%rdi
je     1156 <mallocstrcpy+0x16>
call   1030 <free@plt>
test   %rbx,%rbx
je     117e <mallocstrcpy+0x3e>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   1060 <nmalloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %r14,%rbx
jmp    1180 <mallocstrcpy+0x40>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @mallocstrcpy(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%6 = load i8*, i8** %5
%7 = load i8*, i8** %4
%8 = icmp eq i8* %6, %7
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = load i8*, i8** %4
store i8* %10, i8** %3
br label %34
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i8*, i8** %4
%16 = call i32 @free(i8* %15)
br label %17
17: ; preds = %14, %11
%18 = load i8*, i8** %5
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %17
store i8* null, i8** %3
br label %34
21: ; preds = %17
%22 = load i8*, i8** %5
%23 = call i32 @strlen(i8* %22)
%24 = add nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = mul i64 %25, 1
%27 = trunc i64 %26 to i32
%28 = call i64 @nmalloc(i32 %27)
%29 = inttoptr i64 %28 to i8*
store i8* %29, i8** %4
%30 = load i8*, i8** %4
%31 = load i8*, i8** %5
%32 = call i32 @strcpy(i8* %30, i8* %31)
%33 = load i8*, i8** %4
store i8* %33, i8** %3
br label %34
34: ; preds = %21, %20, %9
%35 = load i8*, i8** %3
ret i8* %35
}

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