void *chunk_put(void *p, char *data, int len) {
  char *p1;
  if (len == 0)
    error1("attempt to store 0 bytes in the memory chunk set\n");
  if (len < 0)
    len = strlen(data) + 1;
  p1 = chunk_alloc(p, len);
  memcpy(p1, data, len);
  return p1;
}


x86 assembly (clang, -O0):
<chunk_put>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    116b <chunk_put+0x2b>
lea    0xe9c(%rip),%rdi
mov    $0x0,%al
call   1060 <error1@plt>
cmpl   $0x0,-0x14(%rbp)
jge    1185 <chunk_put+0x45>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%rax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    $0x0,%al
call   1050 <chunk_alloc@plt>
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chunk_put>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
test   %edx,%edx
je     1163 <chunk_put+0x23>
mov    %edx,%ebx
jns    1173 <chunk_put+0x33>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
add    $0x1,%ebx
jmp    1173 <chunk_put+0x33>
lea    0xe96(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <error1@plt>
mov    %r15,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <chunk_alloc@plt>
movslq %eax,%rdi
movslq %ebx,%rdx
mov    %r14,%rsi
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <memcpy@plt>

LLVM IR (-O0):
@.str = [50 x i8] c"attempt to store 0 bytes in the memory chunk set\0A\00"
define i8* @chunk_put(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i32, i32* %6
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = call i32 (i8*, ...) bitcast (i32 (...)* @error1 to i32 (i8*, ...)*)(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %3
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i8*, i8** %5
%17 = call i64 @strlen(i8* %16)
%18 = add i64 %17, 1
%19 = trunc i64 %18 to i32
store i32 %19, i32* %6
br label %20
20: ; preds = %15, %12
%21 = load i8*, i8** %4
%22 = load i32, i32* %6
%23 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @chunk_alloc to i32 (i8*, i32, ...)*)(i8* %21, i32 %22)
%24 = sext i32 %23 to i64
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %7
%26 = load i8*, i8** %7
%27 = load i8*, i8** %5
%28 = load i32, i32* %6
%29 = sext i32 %28 to i64
%30 = load i8*, i8** %7
ret i8* %30
}

LLVM IR (-O2):
@.str = [50 x i8] c"attempt to store 0 bytes in the memory chunk set\0A\00"
define i8* @chunk_put(i8* %0, i8* nocapture %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 (i8*, ...) bitcast (i32 (...)* @error1 to i32 (i8*, ...)*)(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
br label %13
7: ; preds = %3
%8 = icmp slt i32 %2, 0
br i1 %8, label %9, label %13
9: ; preds = %7
%10 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%11 = trunc i64 %10 to i32
%12 = add i32 %11, 1
br label %13
13: ; preds = %5, %9, %7
%14 = phi i32 [ %12, %9 ], [ %2, %7 ], [ 0, %5 ]
%15 = tail call i32 (i8*, i32, ...) bitcast (i32 (...)* @chunk_alloc to i32 (i8*, i32, ...)*)(i8* %0, i32 %14)
%16 = sext i32 %15 to i64
%17 = inttoptr i64 %16 to i8*
%18 = sext i32 %14 to i64
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %17, i8* align 1 %1, i64 %18, i1 false)
ret i8* %17
}