#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*/ __bound_check(void const *, size_t);
int /*<<< orphan*/ bound_error(char *);
void *memcpy(void *, void const *, size_t);
void *__bound_memcpy(void *dst, const void *src, size_t size) {
  void *p;

  ;
  __bound_check(dst, size);
  __bound_check(src, size);
  if (src >= dst && src < dst + size)
    bound_error("overlapping regions in memcpy()");
  p = memcpy(dst, src, size);
  ;
  return p;
}


x86 assembly (clang, -O0):
<__bound_memcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <__bound_check@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <__bound_check@plt>
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
jb     118d <__bound_memcpy+0x5d>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
add    -0x18(%rbp),%rcx
cmp    %rcx,%rax
jae    118d <__bound_memcpy+0x5d>
lea    0xe78(%rip),%rdi
call   1050 <bound_error@plt>
mov    -0x8(%rbp),%rdi
mov    %rdi,-0x28(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__bound_memcpy>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rdx,%rsi
call   1040 <__bound_check@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <__bound_check@plt>
cmp    %rbx,%r15
jb     116b <__bound_memcpy+0x3b>
lea    (%rbx,%r14,1),%rax
cmp    %r15,%rax
jbe    116b <__bound_memcpy+0x3b>
lea    0xe9a(%rip),%rdi
call   1050 <bound_error@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <memcpy@plt>

LLVM IR (-O0):
@.str = [32 x i8] c"overlapping regions in memcpy()\00"
define i8* @__bound_memcpy(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%8 = load i8*, i8** %4
%9 = load i64, i64* %6
%10 = call i32 @__bound_check(i8* %8, i64 %9)
%11 = load i8*, i8** %5
%12 = load i64, i64* %6
%13 = call i32 @__bound_check(i8* %11, i64 %12)
%14 = load i8*, i8** %5
%15 = load i8*, i8** %4
%16 = icmp uge i8* %14, %15
br i1 %16, label %17, label %25
17: ; preds = %3
%18 = load i8*, i8** %5
%19 = load i8*, i8** %4
%20 = load i64, i64* %6
%21 = getelementptr i8, i8* %19, i64 %20
%22 = icmp ult i8* %18, %21
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 @bound_error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %17, %3
%26 = load i8*, i8** %4
%27 = load i8*, i8** %5
%28 = load i64, i64* %6
store i8* %26, i8** %7
%29 = load i8*, i8** %7
ret i8* %29
}

LLVM IR (-O2):
@.str = [32 x i8] c"overlapping regions in memcpy()\00"
define i8* @__bound_memcpy(i8* returned %0, i8* %1, i64 %2) {
%4 = tail call i32 @__bound_check(i8* %0, i64 %2)
%5 = tail call i32 @__bound_check(i8* %1, i64 %2)
%6 = icmp uge i8* %1, %0
%7 = getelementptr i8, i8* %0, i64 %2
%8 = icmp ugt i8* %7, %1
%9 = and i1 %6, %8
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = tail call i32 @bound_error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %3
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %0, i8* align 1 %1, i64 %2, i1 false)
ret i8* %0
}