#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 *realloc(char *, size_t);
int /*<<< orphan*/ strcpy(char *, char const *);
int /*<<< orphan*/ string_destroy(char **);
size_t string_length(char const *);
size_t string_copy(char **dst, const char *src) {
  size_t src_length = string_length(src);
  if (src_length) {
    *dst = realloc(*dst, src_length + 1);
    strcpy(*dst, src);
  } else
    string_destroy(dst);
  return src_length;
}


x86 assembly (clang, -O0):
<string_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <string_length@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     119b <string_copy+0x5b>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
call   1060 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    11a4 <string_copy+0x64>
mov    -0x8(%rbp),%rdi
call   1040 <string_destroy@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_copy>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
call   1050 <string_length@plt>
mov    %rax,%rbx
test   %rax,%rax
je     117a <string_copy+0x3a>
mov    (%r15),%rdi
mov    %rbx,%rsi
add    $0x1,%rsi
call   1060 <realloc@plt>
mov    %rax,(%r15)
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
jmp    1182 <string_copy+0x42>
mov    %r15,%rdi
call   1040 <string_destroy@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @string_copy(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** %4
%7 = call i64 @string_length(i8* %6)
store i64 %7, i64* %5
%8 = load i64, i64* %5
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %21
10: ; preds = %2
%11 = load i8**, i8*** %3
%12 = load i8*, i8** %11
%13 = load i64, i64* %5
%14 = add i64 %13, 1
%15 = call i8* @realloc(i8* %12, i64 %14)
%16 = load i8**, i8*** %3
store i8* %15, i8** %16
%17 = load i8**, i8*** %3
%18 = load i8*, i8** %17
%19 = load i8*, i8** %4
%20 = call i32 @strcpy(i8* %18, i8* %19)
br label %24
21: ; preds = %2
%22 = load i8**, i8*** %3
%23 = call i32 @string_destroy(i8** %22)
br label %24
24: ; preds = %21, %10
%25 = load i64, i64* %5
ret i64 %25
}

LLVM IR (-O2):
define i64 @string_copy(i8** %0, i8* %1) {
%3 = tail call i64 @string_length(i8* %1)
%4 = icmp eq i64 %3, 0
br i1 %4, label %10, label %5
5: ; preds = %2
%6 = load i8*, i8** %0
%7 = add i64 %3, 1
%8 = tail call i8* @realloc(i8* %6, i64 %7)
store i8* %8, i8** %0
%9 = tail call i32 @strcpy(i8* %8, i8* %1)
br label %12
10: ; preds = %2
%11 = tail call i32 @string_destroy(i8** %0)
br label %12
12: ; preds = %10, %5
ret i64 %3
}