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


x86 assembly (clang, -O0):
<__logc_strcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1166 <__logc_strcpy+0x26>
cmpq   $0x0,-0x10(%rbp)
jne    116b <__logc_strcpy+0x2b>
jmp    11be <__logc_strcpy+0x7e>
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
je     1185 <__logc_strcpy+0x45>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__logc_strcpy>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     118c <__logc_strcpy+0x4c>
mov    %rsi,%r14
test   %rsi,%rsi
je     118c <__logc_strcpy+0x4c>
mov    %rdi,%rbx
mov    (%rdi),%rdi
test   %rdi,%rdi
je     1161 <__logc_strcpy+0x21>
call   1030 <free@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,(%rbx)
mov    %rax,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <strcpy@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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