#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*/ byte_copy(void *, void const *, size_t);
int /*<<< orphan*/ word_copy(void *, void const *, size_t);
void *memcpy(void *dest, const void *src, size_t num) {
  void *dest_saved = dest;
  size_t dest_head = sizeof(size_t) - ((size_t)dest % sizeof(size_t));
  size_t src_head = sizeof(size_t) - ((size_t)src % sizeof(size_t));
  if (src_head == dest_head) {
    byte_copy(dest, src, dest_head);
    *(char **)dest += dest_head;
    *(char **)src += dest_head;
    num -= dest_head;
    size_t words = num / sizeof(size_t);
    word_copy(dest, src, words);
    byte_copy(dest + words * sizeof(size_t), src + words * sizeof(size_t),
              num & 0x7);
  } else {
    byte_copy(dest, src, num);
  }
  return dest_saved;
}


x86 assembly (clang, -O0):
<memcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rcx
and    $0x7,%rcx
mov    $0x8,%eax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rcx
and    $0x7,%rcx
mov    $0x8,%eax
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    11fb <memcpy+0xdb>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <byte_copy@plt>
mov    -0x28(%rbp),%rcx
mov    -0x8(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
shr    $0x3,%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1030 <word_copy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
and    $0x7,%rdx
call   1040 <byte_copy@plt>
jmp    120c <memcpy+0xec>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <byte_copy@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memcpy>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
mov    %r15d,%eax
and    $0x7,%eax
mov    %r12d,%ecx
and    $0x7,%ecx
cmp    %rcx,%rax
jne    1188 <memcpy+0x68>
mov    $0x8,%ebx
sub    %rax,%rbx
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <byte_copy@plt>
add    %rbx,(%r15)
add    %rbx,(%r12)
sub    %rbx,%r14
mov    %r14,%rdx
shr    $0x3,%rdx
mov    %r15,%rdi
mov    %r12,%rsi
call   1030 <word_copy@plt>
mov    %r14,%rsi
and    $0xfffffffffffffff8,%rsi
lea    (%r15,%rsi,1),%rdi
add    %r12,%rsi
and    $0x7,%r14d
jmp    118e <memcpy+0x6e>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   1040 <byte_copy@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @memcpy(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%11 = load i8*, i8** %4
store i8* %11, i8** %7
%12 = load i8*, i8** %4
%13 = ptrtoint i8* %12 to i64
%14 = urem i64 %13, 8
%15 = sub i64 8, %14
store i64 %15, i64* %8
%16 = load i8*, i8** %5
%17 = ptrtoint i8* %16 to i64
%18 = urem i64 %17, 8
%19 = sub i64 8, %18
store i64 %19, i64* %9
%20 = load i64, i64* %9
%21 = load i64, i64* %8
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %58
23: ; preds = %3
%24 = load i8*, i8** %4
%25 = load i8*, i8** %5
%26 = load i64, i64* %8
%27 = call i32 @byte_copy(i8* %24, i8* %25, i64 %26)
%28 = load i64, i64* %8
%29 = load i8*, i8** %4
%30 = bitcast i8* %29 to i8**
%31 = load i8*, i8** %30
%32 = getelementptr inbounds i8, i8* %31, i64 %28
store i8* %32, i8** %30
%33 = load i64, i64* %8
%34 = load i8*, i8** %5
%35 = bitcast i8* %34 to i8**
%36 = load i8*, i8** %35
%37 = getelementptr inbounds i8, i8* %36, i64 %33
store i8* %37, i8** %35
%38 = load i64, i64* %8
%39 = load i64, i64* %6
%40 = sub i64 %39, %38
store i64 %40, i64* %6
%41 = load i64, i64* %6
%42 = udiv i64 %41, 8
store i64 %42, i64* %10
%43 = load i8*, i8** %4
%44 = load i8*, i8** %5
%45 = load i64, i64* %10
%46 = call i32 @word_copy(i8* %43, i8* %44, i64 %45)
%47 = load i8*, i8** %4
%48 = load i64, i64* %10
%49 = mul i64 %48, 8
%50 = getelementptr i8, i8* %47, i64 %49
%51 = load i8*, i8** %5
%52 = load i64, i64* %10
%53 = mul i64 %52, 8
%54 = getelementptr i8, i8* %51, i64 %53
%55 = load i64, i64* %6
%56 = and i64 %55, 7
%57 = call i32 @byte_copy(i8* %50, i8* %54, i64 %56)
br label %63
58: ; preds = %3
%59 = load i8*, i8** %4
%60 = load i8*, i8** %5
%61 = load i64, i64* %6
%62 = call i32 @byte_copy(i8* %59, i8* %60, i64 %61)
br label %63
63: ; preds = %58, %23
%64 = load i8*, i8** %7
ret i8* %64
}

LLVM IR (-O2):
define i8* @memcpy(i8* returned %0, i8* %1, i64 %2) {
%4 = ptrtoint i8* %0 to i64
%5 = and i64 %4, 7
%6 = sub nuw nsw i64 8, %5
%7 = ptrtoint i8* %1 to i64
%8 = and i64 %7, 7
%9 = icmp eq i64 %5, %8
br i1 %9, label %10, label %26
10: ; preds = %3
%11 = tail call i32 @byte_copy(i8* %0, i8* %1, i64 %6)
%12 = bitcast i8* %0 to i8**
%13 = load i8*, i8** %12
%14 = getelementptr inbounds i8, i8* %13, i64 %6
store i8* %14, i8** %12
%15 = bitcast i8* %1 to i8**
%16 = load i8*, i8** %15
%17 = getelementptr inbounds i8, i8* %16, i64 %6
store i8* %17, i8** %15
%18 = sub i64 %2, %6
%19 = lshr i64 %18, 3
%20 = tail call i32 @word_copy(i8* %0, i8* %1, i64 %19)
%21 = and i64 %18, -8
%22 = getelementptr i8, i8* %0, i64 %21
%23 = getelementptr i8, i8* %1, i64 %21
%24 = and i64 %18, 7
%25 = tail call i32 @byte_copy(i8* %22, i8* %23, i64 %24)
br label %28
26: ; preds = %3
%27 = tail call i32 @byte_copy(i8* %0, i8* %1, i64 %2)
br label %28
28: ; preds = %26, %10
ret i8* %0
}