#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*/ memcpy(void *, void *, size_t);
void *realloc(void *, size_t);
int strlen(void *);
void copy(void **a, void *b, size_t size) {
  if (size == 0) {
    size_t len = strlen(b) + 1;
    *a = realloc(*a, len);
    memcpy(*a, b, len);
  } else {
    memcpy(*a, b, size);
  }
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1194 <copy+0x64>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
jmp    11a8 <copy+0x78>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
test   %rdx,%rdx
je     1148 <copy+0x18>
mov    (%rbx),%rdi
mov    %r14,%rsi
jmp    116d <copy+0x3d>
mov    %r14,%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
movslq %eax,%r15
mov    (%rbx),%rdi
mov    %r15,%rsi
call   1050 <realloc@plt>
mov    %rax,(%rbx)
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <memcpy@plt>

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

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