#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 */
typedef int /*<<< orphan*/ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, void const *, int);
void *xbuffer_ensure_size(void *, int);
void *xbuffer_copyin(void *buf, int index, const void *data, int len) {
  if (!buf || !data) {
    return NULL;
  }
  buf = xbuffer_ensure_size(buf, index + len);
  memcpy(((uint8_t *)buf) + index, data, len);
  return buf;
}


x86 assembly (clang, -O0):
<xbuffer_copyin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     114c <xbuffer_copyin+0x2c>
cmpq   $0x0,-0x20(%rbp)
jne    1159 <xbuffer_copyin+0x39>
movq   $0x0,-0x8(%rbp)
jmp    118f <xbuffer_copyin+0x6f>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    -0x24(%rbp),%esi
call   1040 <xbuffer_ensure_size@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xbuffer_copyin>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %ebx,%ebx
test   %rdi,%rdi
je     115b <xbuffer_copyin+0x3b>
mov    %rdx,%r15
test   %rdx,%rdx
je     115b <xbuffer_copyin+0x3b>
mov    %ecx,%r14d
mov    %esi,%r12d
lea    (%r14,%r12,1),%esi
call   1040 <xbuffer_ensure_size@plt>
mov    %rax,%rbx
movslq %r12d,%rax
lea    (%rbx,%rax,4),%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <memcpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @xbuffer_copyin(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = icmp ne i8* %0, null
%6 = icmp ne i8* %2, null
%7 = and i1 %5, %6
br i1 %7, label %8, label %15
8: ; preds = %4
%9 = add nsw i32 %3, %1
%10 = tail call i8* @xbuffer_ensure_size(i8* %0, i32 %9)
%11 = bitcast i8* %10 to i32*
%12 = sext i32 %1 to i64
%13 = getelementptr inbounds i32, i32* %11, i64 %12
%14 = tail call i32 @memcpy(i32* %13, i8* %2, i32 %3)
br label %15
15: ; preds = %4, %8
%16 = phi i8* [ %10, %8 ], [ null, %4 ]
ret i8* %16
}