#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 */
void *_memcpy_forward(void *, void const *, size_t);
void *_memcpy_reverse(void *, void const *, size_t);
void *memmove(void *dest, const void *src, size_t count) {
  char *d = (char *)dest;
  const char *s = (const char *)src;
  if (s < d && d < s + count)
    return _memcpy_reverse(dest, src, count);
  else
    return _memcpy_forward(dest, src, count);
}


x86 assembly (clang, -O0):
<memmove>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    1181 <memmove+0x61>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
add    -0x20(%rbp),%rcx
cmp    %rcx,%rax
jae    1181 <memmove+0x61>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <_memcpy_reverse@plt>
mov    %rax,-0x8(%rbp)
jmp    1196 <memmove+0x76>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <_memcpy_forward@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memmove>:
cmp    %rdi,%rsi
jae    1133 <memmove+0x13>
lea    (%rsi,%rdx,1),%rax
cmp    %rdi,%rax
jbe    1133 <memmove+0x13>
jmp    1040 <_memcpy_reverse@plt>
jmp    1030 <_memcpy_forward@plt>

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

LLVM IR (-O2):
define i8* @memmove(i8* %0, i8* %1, i64 %2) {
%4 = icmp ult i8* %1, %0
%5 = getelementptr inbounds i8, i8* %1, i64 %2
%6 = icmp ugt i8* %5, %0
%7 = select i1 %4, i1 %6, i1 false
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i8* @_memcpy_reverse(i8* %0, i8* %1, i64 %2)
br label %12
10: ; preds = %3
%11 = tail call i8* @_memcpy_forward(i8* %0, i8* %1, i64 %2)
br label %12
12: ; preds = %10, %8
%13 = phi i8* [ %9, %8 ], [ %11, %10 ]
ret i8* %13
}