#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 scalar_t__ clock_t;

/* Variables and functions */
scalar_t__ clock();
void *malloc(int const);
int /*<<< orphan*/ memcpy(void *, void *, int const);
int /*<<< orphan*/ memmove(void *, void *, int const);
int /*<<< orphan*/ printf(char *, long double);
int main(int argc, char **argv) {
  const int N = 1000000;
  const int SIZE = 4;
  void *src = malloc(SIZE);
  void *dst = malloc(SIZE);
  int i;
  long double memcpy_total = 0;
  for (i = 0; i < N; ++i) {
    clock_t start = clock();
    memcpy(dst, src, SIZE);
    memcpy_total += (clock() - start);
  }
  long double memmove_total = 0;
  for (i = 0; i < N; ++i) {
    clock_t start = clock();
    memmove(dst, src, SIZE);
    memmove_total += (clock() - start);
  }
  if (memmove_total > memcpy_total)
    printf("memcpy is %Lf times faster\n", memmove_total / memcpy_total);
  else
    printf("Surprise!\nmemmove is %Lf times faster then memcpy\n",
           memcpy_total / memmove_total);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xf4240,-0x14(%rbp)
movl   $0x4,-0x18(%rbp)
mov    $0x4,%edi
mov    %edi,-0x7c(%rbp)
call   1060 <malloc@plt>
mov    -0x7c(%rbp),%edi
mov    %rax,-0x20(%rbp)
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
fldz
fstpt  -0x40(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0xf4240,-0x2c(%rbp)
jge    11f9 <main+0xa9>
xor    %eax,%eax
mov    %al,-0x7d(%rbp)
call   1030 <clock@plt>
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    -0x7d(%rbp),%al
call   1030 <clock@plt>
mov    -0x48(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x78(%rbp)
fildll -0x78(%rbp)
fldt   -0x40(%rbp)
faddp  %st,%st(1)
fstpt  -0x40(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11a0 <main+0x50>
fldz
fstpt  -0x60(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0xf4240,-0x2c(%rbp)
jge    125e <main+0x10e>
xor    %eax,%eax
mov    %al,-0x7e(%rbp)
call   1030 <clock@plt>
mov    %rax,-0x68(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memmove@plt>
mov    -0x7e(%rbp),%al
call   1030 <clock@plt>
mov    -0x68(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x70(%rbp)
fildll -0x70(%rbp)
fldt   -0x60(%rbp)
faddp  %st,%st(1)
fstpt  -0x60(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1205 <main+0xb5>
fldt   -0x60(%rbp)
fldt   -0x40(%rbp)
fxch   %st(1)
fucomip %st(1),%st
fstp   %st(0)
jbe    1293 <main+0x143>
jmp    1275 <main+0x125>
fldt   -0x60(%rbp)
fldt   -0x40(%rbp)
fdivrp %st,%st(1)
mov    %rsp,%rax
fstpt  (%rax)
lea    0xd77(%rip),%rdi
call   1040 <printf@plt>
jmp    12ac <main+0x15c>
fldt   -0x40(%rbp)
fldt   -0x60(%rbp)
fdivrp %st,%st(1)
mov    %rsp,%rax
fstpt  (%rax)
lea    0xd75(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
fldz
mov    $0xf4240,%ebp
nopl   0x0(%rax,%rax,1)
fstpt  0x1c(%rsp)
xor    %eax,%eax
call   1030 <clock@plt>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
xor    %eax,%eax
call   1030 <clock@plt>
sub    %rbx,%rax
mov    %rax,0x30(%rsp)
fildll 0x30(%rsp)
fldt   0x1c(%rsp)
faddp  %st,%st(1)
fstpt  0x1c(%rsp)
fldt   0x1c(%rsp)
add    $0xffffffff,%ebp
jne    1180 <main+0x30>
fstp   %st(0)
fldz
mov    $0xf4240,%ebp
nopl   (%rax)
fstpt  0x3c(%rsp)
xor    %eax,%eax
call   1030 <clock@plt>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x4,%edx
call   1070 <memmove@plt>
xor    %eax,%eax
call   1030 <clock@plt>
fldt   0x3c(%rsp)
sub    %rbx,%rax
mov    %rax,0x28(%rsp)
fildll 0x28(%rsp)
faddp  %st,%st(1)
add    $0xffffffff,%ebp
jne    11d0 <main+0x80>
fldt   0x1c(%rsp)
fxch   %st(1)
fucomi %st(1),%st
jbe    1224 <main+0xd4>
fdivp  %st,%st(1)
fstpt  (%rsp)
lea    0xdde(%rip),%rdi
jmp    1230 <main+0xe0>
fdivrp %st,%st(1)
fstpt  (%rsp)
lea    0xdec(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"memcpy is %Lf times faster\0A\00"
@.str.1 = [51 x i8] c"Surprise!\0Amemmove is %Lf times faster then memcpy\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca x86_fp80
%12 = alloca i64
%13 = alloca x86_fp80
%14 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1000000, i32* %6
store i32 4, i32* %7
%15 = call i8* @malloc(i32 4)
store i8* %15, i8** %8
%16 = call i8* @malloc(i32 4)
store i8* %16, i8** %9
store x86_fp80 0xK00000000000000000000, x86_fp80* %11
store i32 0, i32* %10
br label %17
17: ; preds = %31, %2
%18 = load i32, i32* %10
%19 = icmp slt i32 %18, 1000000
br i1 %19, label %20, label %34
20: ; preds = %17
%21 = call i64 (...) @clock()
store i64 %21, i64* %12
%22 = load i8*, i8** %9
%23 = load i8*, i8** %8
%24 = call i32 @memcpy(i8* %22, i8* %23, i32 4)
%25 = call i64 (...) @clock()
%26 = load i64, i64* %12
%27 = sub nsw i64 %25, %26
%28 = sitofp i64 %27 to x86_fp80
%29 = load x86_fp80, x86_fp80* %11
%30 = fadd x86_fp80 %29, %28
store x86_fp80 %30, x86_fp80* %11
br label %31
31: ; preds = %20
%32 = load i32, i32* %10
%33 = add nsw i32 %32, 1
store i32 %33, i32* %10
br label %17
34: ; preds = %17
store x86_fp80 0xK00000000000000000000, x86_fp80* %13
store i32 0, i32* %10
br label %35
35: ; preds = %49, %34
%36 = load i32, i32* %10
%37 = icmp slt i32 %36, 1000000
br i1 %37, label %38, label %52
38: ; preds = %35
%39 = call i64 (...) @clock()
store i64 %39, i64* %14
%40 = load i8*, i8** %9
%41 = load i8*, i8** %8
%42 = call i32 @memmove(i8* %40, i8* %41, i32 4)
%43 = call i64 (...) @clock()
%44 = load i64, i64* %14
%45 = sub nsw i64 %43, %44
%46 = sitofp i64 %45 to x86_fp80
%47 = load x86_fp80, x86_fp80* %13
%48 = fadd x86_fp80 %47, %46
store x86_fp80 %48, x86_fp80* %13
br label %49
49: ; preds = %38
%50 = load i32, i32* %10
%51 = add nsw i32 %50, 1
store i32 %51, i32* %10
br label %35
52: ; preds = %35
%53 = load x86_fp80, x86_fp80* %13
%54 = load x86_fp80, x86_fp80* %11
%55 = fcmp ogt x86_fp80 %53, %54
br i1 %55, label %56, label %61
56: ; preds = %52
%57 = load x86_fp80, x86_fp80* %13
%58 = load x86_fp80, x86_fp80* %11
%59 = fdiv x86_fp80 %57, %58
%60 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), x86_fp80 %59)
br label %66
61: ; preds = %52
%62 = load x86_fp80, x86_fp80* %11
%63 = load x86_fp80, x86_fp80* %13
%64 = fdiv x86_fp80 %62, %63
%65 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), x86_fp80 %64)
br label %66
66: ; preds = %61, %56
ret i32 0
}

LLVM IR (-O2):
@.str = [28 x i8] c"memcpy is %Lf times faster\0A\00"
@.str.1 = [51 x i8] c"Surprise!\0Amemmove is %Lf times faster then memcpy\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call dereferenceable_or_null(4) i8* @malloc(i32 4)
%4 = tail call dereferenceable_or_null(4) i8* @malloc(i32 4)
br label %5
5: ; preds = %2, %5
%6 = phi i32 [ 0, %2 ], [ %14, %5 ]
%7 = phi x86_fp80 [ 0xK00000000000000000000, %2 ], [ %13, %5 ]
%8 = tail call i64 (...) @clock()
%9 = tail call i32 @memcpy(i8* %4, i8* %3, i32 4)
%10 = tail call i64 (...) @clock()
%11 = sub nsw i64 %10, %8
%12 = sitofp i64 %11 to x86_fp80
%13 = fadd x86_fp80 %7, %12
%14 = add nuw nsw i32 %6, 1
%15 = icmp eq i32 %14, 1000000
br i1 %15, label %16, label %5
16: ; preds = %5, %16
%17 = phi i32 [ %25, %16 ], [ 0, %5 ]
%18 = phi x86_fp80 [ %24, %16 ], [ 0xK00000000000000000000, %5 ]
%19 = tail call i64 (...) @clock()
%20 = tail call i32 @memmove(i8* %4, i8* %3, i32 4)
%21 = tail call i64 (...) @clock()
%22 = sub nsw i64 %21, %19
%23 = sitofp i64 %22 to x86_fp80
%24 = fadd x86_fp80 %18, %23
%25 = add nuw nsw i32 %17, 1
%26 = icmp eq i32 %25, 1000000
br i1 %26, label %27, label %16
27: ; preds = %16
%28 = fcmp ogt x86_fp80 %24, %13
br i1 %28, label %29, label %32
29: ; preds = %27
%30 = fdiv x86_fp80 %24, %13
%31 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), x86_fp80 %30)
br label %35
32: ; preds = %27
%33 = fdiv x86_fp80 %13, %24
%34 = tail call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), x86_fp80 %33)
br label %35
35: ; preds = %32, %29
ret i32 0
}