#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 */
size_t SIZE_MAX;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *gettext(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char const *);
size_t strlen(char const *);
char *utils_realloc(char *, int);
char *utils_strdup(char const *);
void utils_copy_string(const char *src, char **dst) {
  assert(src != NULL);
  assert(dst != NULL);
  if (*dst) {
    size_t old_len = strlen(*dst);
    size_t new_len = strlen(src);
    if (new_len == SIZE_MAX) {
      fprintf(stderr, gettext("Couldn't allocate memory.\n"));
      abort();
    }
    if (new_len > old_len) {
      *dst = utils_realloc(*dst, sizeof(char *) * (new_len + 1));
    }
    strcpy(*dst, src);
  } else {
    *dst = utils_strdup(src);
  }
}


x86 assembly (clang, -O0):
<utils_copy_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     1264 <utils_copy_string+0xe4>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2de6(%rip),%rcx
cmp    (%rcx),%rax
jne    121d <utils_copy_string+0x9d>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
lea    0xdfa(%rip),%rdi
call   1070 <gettext@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rsi
call   1090 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    124f <utils_copy_string+0xcf>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rax
add    $0x1,%rax
shl    $0x3,%rax
mov    %eax,%esi
call   10a0 <utils_realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    1277 <utils_copy_string+0xf7>
mov    -0x8(%rbp),%rdi
call   1050 <utils_strdup@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utils_copy_string>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1080 <assert@plt>
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1080 <assert@plt>
mov    (%r15),%rbx
test   %rbx,%rbx
je     1202 <utils_copy_string+0x82>
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %rax,%r12
mov    0x2e0c(%rip),%rax
cmp    (%rax),%r12
je     1219 <utils_copy_string+0x99>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cmp    %rax,%r12
jbe    11ec <utils_copy_string+0x6c>
lea    0x8(,%r12,8),%esi
mov    %rbx,%rdi
call   10a0 <utils_realloc@plt>
mov    %rax,%rbx
mov    %rax,(%r15)
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <strcpy@plt>
mov    %r14,%rdi
call   1050 <utils_strdup@plt>
mov    %rax,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2dc0(%rip),%rax
mov    (%rax),%ebx
lea    0xdd7(%rip),%rdi
call   1070 <gettext@plt>
mov    %ebx,%edi
mov    %rax,%rsi
call   1090 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@SIZE_MAX = global i64 0
@stderr = global i32 0
@.str = [27 x i8] c"Couldn't allocate memory.\0A\00"
define void @utils_copy_string(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i64
%6 = alloca i64
store i8* %0, i8** %3
store i8** %1, i8*** %4
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = load i8**, i8*** %4
%12 = icmp ne i8** %11, null
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i8**, i8*** %4
%16 = load i8*, i8** %15
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %50
18: ; preds = %2
%19 = load i8**, i8*** %4
%20 = load i8*, i8** %19
%21 = call i64 @strlen(i8* %20)
store i64 %21, i64* %5
%22 = load i8*, i8** %3
%23 = call i64 @strlen(i8* %22)
store i64 %23, i64* %6
%24 = load i64, i64* %6
%25 = load i64, i64* @SIZE_MAX
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %32
27: ; preds = %18
%28 = load i32, i32* @stderr
%29 = call i8* @gettext(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%30 = call i32 @fprintf(i32 %28, i8* %29)
%31 = call i32 (...) @abort()
unreachable
32: ; preds = %18
%33 = load i64, i64* %6
%34 = load i64, i64* %5
%35 = icmp ugt i64 %33, %34
br i1 %35, label %36, label %45
36: ; preds = %32
%37 = load i8**, i8*** %4
%38 = load i8*, i8** %37
%39 = load i64, i64* %6
%40 = add i64 %39, 1
%41 = mul i64 8, %40
%42 = trunc i64 %41 to i32
%43 = call i8* @utils_realloc(i8* %38, i32 %42)
%44 = load i8**, i8*** %4
store i8* %43, i8** %44
br label %45
45: ; preds = %36, %32
%46 = load i8**, i8*** %4
%47 = load i8*, i8** %46
%48 = load i8*, i8** %3
%49 = call i32 @strcpy(i8* %47, i8* %48)
br label %54
50: ; preds = %2
%51 = load i8*, i8** %3
%52 = call i8* @utils_strdup(i8* %51)
%53 = load i8**, i8*** %4
store i8* %52, i8** %53
br label %54
54: ; preds = %50, %45
ret void
}

LLVM IR (-O2):
@SIZE_MAX = global i64 0
@stderr = global i32 0
@.str = [27 x i8] c"Couldn't allocate memory.\0A\00"
define void @utils_copy_string(i8* %0, i8** %1) {
%3 = icmp ne i8* %0, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = icmp ne i8** %1, null
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = load i8*, i8** %1
%10 = icmp eq i8* %9, null
br i1 %10, label %31, label %11
11: ; preds = %2
%12 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%13 = load i64, i64* @SIZE_MAX
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = tail call i8* @gettext(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%18 = tail call i32 @fprintf(i32 %16, i8* %17)
%19 = tail call i32 (...) @abort()
unreachable
20: ; preds = %11
%21 = tail call i64 @strlen(i8* %9)
%22 = icmp ugt i64 %12, %21
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = trunc i64 %12 to i32
%25 = shl i32 %24, 3
%26 = add i32 %25, 8
%27 = tail call i8* @utils_realloc(i8* %9, i32 %26)
store i8* %27, i8** %1
br label %28
28: ; preds = %23, %20
%29 = phi i8* [ %27, %23 ], [ %9, %20 ]
%30 = tail call i32 @strcpy(i8* %29, i8* %0)
br label %33
31: ; preds = %2
%32 = tail call i8* @utils_strdup(i8* %0)
store i8* %32, i8** %1
br label %33
33: ; preds = %31, %28
ret void
}