#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 *char_buffer_malloc(size_t);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memcpy(char *, char const *const, size_t const);
void charcpy(char **pp_dest, size_t *p_dest_len, const char *const p_source,
             const size_t source_len) {
  char *dest = *pp_dest;
  if (source_len > *p_dest_len) {
    free(dest);
    *p_dest_len = source_len + 16;
    dest = char_buffer_malloc(*p_dest_len);
    *pp_dest = dest;
  }
  if (dest) {
    memcpy(dest, p_source, source_len);
    dest[source_len] = '\0';
  }
}


x86 assembly (clang, -O0):
<charcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%rax
jbe    1197 <charcpy+0x67>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rcx
add    $0x10,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1050 <char_buffer_malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x28(%rbp)
je     11bf <charcpy+0x8f>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<charcpy>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r15
mov    %rdx,%r14
mov    (%rdi),%rbx
cmp    %rcx,(%rsi)
jae    1169 <charcpy+0x39>
mov    %rsi,%r13
mov    %rdi,%r12
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0x10(%r15),%rdi
mov    %rdi,0x0(%r13)
call   1050 <char_buffer_malloc@plt>
mov    %rax,%rbx
mov    %rax,(%r12)
test   %rbx,%rbx
je     1181 <charcpy+0x51>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%rbx,%r15,1)
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @charcpy(i8** %0, i64* %1, i8* %2, i64 %3) {
%5 = alloca i8**
%6 = alloca i64*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
store i8** %0, i8*** %5
store i64* %1, i64** %6
store i8* %2, i8** %7
store i64 %3, i64* %8
%10 = load i8**, i8*** %5
%11 = load i8*, i8** %10
store i8* %11, i8** %9
%12 = load i64, i64* %8
%13 = load i64*, i64** %6
%14 = load i64, i64* %13
%15 = icmp ugt i64 %12, %14
br i1 %15, label %16, label %27
16: ; preds = %4
%17 = load i8*, i8** %9
%18 = call i32 @free(i8* %17)
%19 = load i64, i64* %8
%20 = add i64 %19, 16
%21 = load i64*, i64** %6
store i64 %20, i64* %21
%22 = load i64*, i64** %6
%23 = load i64, i64* %22
%24 = call i8* @char_buffer_malloc(i64 %23)
store i8* %24, i8** %9
%25 = load i8*, i8** %9
%26 = load i8**, i8*** %5
store i8* %25, i8** %26
br label %27
27: ; preds = %16, %4
%28 = load i8*, i8** %9
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %38
30: ; preds = %27
%31 = load i8*, i8** %9
%32 = load i8*, i8** %7
%33 = load i64, i64* %8
%34 = call i32 @memcpy(i8* %31, i8* %32, i64 %33)
%35 = load i8*, i8** %9
%36 = load i64, i64* %8
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8 0, i8* %37
br label %38
38: ; preds = %30, %27
ret void
}

LLVM IR (-O2):
define void @charcpy(i8** nocapture %0, i64* nocapture %1, i8* %2, i64 %3) {
%5 = load i8*, i8** %0
%6 = load i64, i64* %1
%7 = icmp ult i64 %6, %3
br i1 %7, label %8, label %12
8: ; preds = %4
%9 = tail call i32 @free(i8* %5)
%10 = add i64 %3, 16
store i64 %10, i64* %1
%11 = tail call i8* @char_buffer_malloc(i64 %10)
store i8* %11, i8** %0
br label %12
12: ; preds = %8, %4
%13 = phi i8* [ %11, %8 ], [ %5, %4 ]
%14 = icmp eq i8* %13, null
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = tail call i32 @memcpy(i8* %13, i8* %2, i64 %3)
%17 = getelementptr inbounds i8, i8* %13, i64 %3
store i8 0, i8* %17
br label %18
18: ; preds = %15, %12
ret void
}