#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*/ memcpy(char *, char const *, size_t);
int strlen(char const *);
void bh_strfill(char **dst, const char *src) {
  size_t length;
  {
    if (*dst) {
      free(*dst);
      (*dst) = NULL;
    }
  };
  length = strlen(src) + 1;
  *dst = (char *)calloc(length, sizeof(char));
  memcpy(*dst, src, length);
}


x86 assembly (clang, -O0):
<bh_strfill>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
je     1175 <bh_strfill+0x35>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <memcpy@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bh_strfill>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    (%rdi),%rdi
test   %rdi,%rdi
je     115f <bh_strfill+0x1f>
call   1030 <free@plt>
movq   $0x0,(%r15)
mov    %r14,%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,(%r15)
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <memcpy@plt>

LLVM IR (-O0):
define void @bh_strfill(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 = load i8*, i8** %6
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = load i8**, i8*** %3
%11 = load i8*, i8** %10
%12 = call i32 @free(i8* %11)
%13 = load i8**, i8*** %3
store i8* null, i8** %13
br label %14
14: ; preds = %9, %2
%15 = load i8*, i8** %4
%16 = call i32 @strlen(i8* %15)
%17 = add nsw i32 %16, 1
%18 = sext i32 %17 to i64
store i64 %18, i64* %5
%19 = load i64, i64* %5
%20 = call i64 @calloc(i64 %19, i32 1)
%21 = inttoptr i64 %20 to i8*
%22 = load i8**, i8*** %3
store i8* %21, i8** %22
%23 = load i8**, i8*** %3
%24 = load i8*, i8** %23
%25 = load i8*, i8** %4
%26 = load i64, i64* %5
%27 = call i32 @memcpy(i8* %24, i8* %25, i64 %26)
ret void
}

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