#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;
struct timeval {
  int /*<<< orphan*/ tv_usec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(int);
int /*<<< orphan*/ mb_sec(int, struct timeval *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ printf(char *, int, int, int /*<<< orphan*/,
                          int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ tv_sub(struct timeval *, struct timeval *);
int do_memcpy(int iters, int sz) {
  struct timeval tv1;
  struct timeval tv2;
  int i, j;
  uint8_t *a = malloc(sz);
  if (!a)
    return -1;
  uint8_t *b = malloc(sz);
  if (!b)
    return -1;
  int c = 1000000000 / sz;
  for (i = 0; iters == -1 || i < iters; i++) {
    gettimeofday(&tv1, NULL);
    for (j = 0; j < c; j++)
      memcpy(b, a, sz);
    gettimeofday(&tv2, NULL);
    tv_sub(&tv2, &tv1);
    printf("memcpy %dx%d bytes took %ld.%06ld seconds (%f MB/s)\n", c, sz,
           tv2.tv_sec, tv2.tv_usec, mb_sec(c * sz, &tv2));
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_memcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1191 <do_memcpy+0x31>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a3 <do_memcpy+0x143>
mov    -0xc(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11b4 <do_memcpy+0x54>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a3 <do_memcpy+0x143>
mov    $0x3b9aca00,%eax
cltd
idivl  -0xc(%rbp)
mov    %eax,-0x3c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    $0x1,%al
cmpl   $0xffffffff,-0x8(%rbp)
mov    %al,-0x3d(%rbp)
je     11e2 <do_memcpy+0x82>
mov    -0x24(%rbp),%eax
cmp    -0x8(%rbp),%eax
setl   %al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
test   $0x1,%al
jne    11f2 <do_memcpy+0x92>
jmp    129c <do_memcpy+0x13c>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jge    1230 <do_memcpy+0xd0>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1206 <do_memcpy+0xa6>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
lea    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <tv_sub@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x3c(%rbp),%edi
imul   -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <mb_sec@plt>
mov    -0x50(%rbp),%esi
mov    -0x4c(%rbp),%edx
mov    -0x48(%rbp),%ecx
mov    -0x44(%rbp),%r8d
mov    %eax,%r9d
lea    0xd77(%rip),%rdi
call   1030 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11c7 <do_memcpy+0x67>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_memcpy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %esi,%r12d
mov    %edi,0x8(%rsp)
mov    %esi,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    $0xffffffff,%eax
mov    %rax,0x10(%rsp)
test   %r15,%r15
je     1288 <do_memcpy+0x128>
mov    %r12d,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1288 <do_memcpy+0x128>
mov    %rax,%rbp
xor    %eax,%eax
mov    %rax,0x10(%rsp)
mov    $0x3b9aca00,%eax
xor    %edx,%edx
idiv   %r12d
mov    %eax,0xc(%rsp)
cmpl   $0xffffffff,0x8(%rsp)
je     11cd <do_memcpy+0x6d>
cmpl   $0x0,0x8(%rsp)
jle    1288 <do_memcpy+0x128>
mov    0xc(%rsp),%eax
imul   %r12d,%eax
mov    %eax,0x24(%rsp)
xor    %eax,%eax
lea    0x30(%rsp),%rbx
mov    %rax,0x28(%rsp)
mov    %rbx,%r14
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
cmpl   $0x0,0xc(%rsp)
jle    1213 <do_memcpy+0xb3>
mov    0xc(%rsp),%ebx
nopl   (%rax)
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1070 <memcpy@plt>
add    $0xffffffff,%ebx
jne    1200 <do_memcpy+0xa0>
xor    %eax,%eax
mov    %rax,0x10(%rsp)
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    %rbx,%rdi
mov    %r14,%rbx
mov    %r14,%rsi
call   1050 <tv_sub@plt>
mov    0x18(%rsp),%r14d
mov    0x1c(%rsp),%r13d
mov    0x24(%rsp),%edi
lea    0x18(%rsp),%rsi
call   1060 <mb_sec@plt>
lea    0xdaa(%rip),%rdi
mov    0xc(%rsp),%esi
mov    %r12d,%edx
mov    %r13d,%ecx
mov    %r14d,%r8d
mov    %eax,%r9d
call   1030 <printf@plt>
mov    0x28(%rsp),%rax
add    $0x1,%eax
cmpl   $0xffffffff,0x8(%rsp)
je     11e0 <do_memcpy+0x80>
cmp    0x8(%rsp),%eax
jl     11e0 <do_memcpy+0x80>
mov    0x10(%rsp),%rax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [53 x i8] c"memcpy %dx%d bytes took %ld.%06ld seconds (%f MB/s)\0A\00"
define i32 @do_memcpy(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca %struct.timeval
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%13 = load i32, i32* %5
%14 = call i32* @malloc(i32 %13)
store i32* %14, i32** %10
%15 = load i32*, i32** %10
%16 = icmp ne i32* %15, null
br i1 %16, label %18, label %17
17: ; preds = %2
store i32 -1, i32* %3
br label %68
18: ; preds = %2
%19 = load i32, i32* %5
%20 = call i32* @malloc(i32 %19)
store i32* %20, i32** %11
%21 = load i32*, i32** %11
%22 = icmp ne i32* %21, null
br i1 %22, label %24, label %23
23: ; preds = %18
store i32 -1, i32* %3
br label %68
24: ; preds = %18
%25 = load i32, i32* %5
%26 = sdiv i32 1000000000, %25
store i32 %26, i32* %12
store i32 0, i32* %8
br label %27
27: ; preds = %64, %24
%28 = load i32, i32* %4
%29 = icmp eq i32 %28, -1
br i1 %29, label %34, label %30
30: ; preds = %27
%31 = load i32, i32* %8
%32 = load i32, i32* %4
%33 = icmp slt i32 %31, %32
br label %34
34: ; preds = %30, %27
%35 = phi i1 [ true, %27 ], [ %33, %30 ]
br i1 %35, label %36, label %67
36: ; preds = %34
%37 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
store i32 0, i32* %9
br label %38
38: ; preds = %47, %36
%39 = load i32, i32* %9
%40 = load i32, i32* %12
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %50
42: ; preds = %38
%43 = load i32*, i32** %11
%44 = load i32*, i32** %10
%45 = load i32, i32* %5
%46 = call i32 @memcpy(i32* %43, i32* %44, i32 %45)
br label %47
47: ; preds = %42
%48 = load i32, i32* %9
%49 = add nsw i32 %48, 1
store i32 %49, i32* %9
br label %38
50: ; preds = %38
%51 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%52 = call i32 @tv_sub(%struct.timeval* %7, %struct.timeval* %6)
%53 = load i32, i32* %12
%54 = load i32, i32* %5
%55 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%56 = load i32, i32* %55
%57 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%58 = load i32, i32* %57
%59 = load i32, i32* %12
%60 = load i32, i32* %5
%61 = mul nsw i32 %59, %60
%62 = call i32 @mb_sec(i32 %61, %struct.timeval* %7)
%63 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %53, i32 %54, i32 %56, i32 %58, i32 %62)
br label %64
64: ; preds = %50
%65 = load i32, i32* %8
%66 = add nsw i32 %65, 1
store i32 %66, i32* %8
br label %27
67: ; preds = %34
store i32 0, i32* %3
br label %68
68: ; preds = %67, %23, %17
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str = [53 x i8] c"memcpy %dx%d bytes took %ld.%06ld seconds (%f MB/s)\0A\00"
define i32 @do_memcpy(i32 %0, i32 %1) {
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = bitcast %struct.timeval* %3 to i8*
%6 = bitcast %struct.timeval* %4 to i8*
%7 = tail call i32* @malloc(i32 %1)
%8 = icmp eq i32* %7, null
br i1 %8, label %40, label %9
9: ; preds = %2
%10 = tail call i32* @malloc(i32 %1)
%11 = icmp eq i32* %10, null
br i1 %11, label %40, label %12
12: ; preds = %9
%13 = sdiv i32 1000000000, %1
%14 = icmp eq i32 %0, -1
%15 = icmp sgt i32 %0, 0
%16 = or i1 %14, %15
br i1 %16, label %17, label %40
17: ; preds = %12
%18 = icmp sgt i32 %13, 0
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%21 = mul nsw i32 %13, %1
br label %22
22: ; preds = %17, %30
%23 = phi i32 [ 0, %17 ], [ %37, %30 ]
%24 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
br i1 %18, label %25, label %30
25: ; preds = %22, %25
%26 = phi i32 [ %28, %25 ], [ 0, %22 ]
%27 = call i32 @memcpy(i32* %10, i32* %7, i32 %1)
%28 = add nuw nsw i32 %26, 1
%29 = icmp eq i32 %28, %13
br i1 %29, label %30, label %25
30: ; preds = %25, %22
%31 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%32 = call i32 @tv_sub(%struct.timeval* %4, %struct.timeval* %3)
%33 = load i32, i32* %19
%34 = load i32, i32* %20
%35 = call i32 @mb_sec(i32 %21, %struct.timeval* %4)
%36 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %1, i32 %33, i32 %34, i32 %35)
%37 = add nuw nsw i32 %23, 1
%38 = icmp slt i32 %37, %0
%39 = select i1 %14, i1 true, i1 %38
br i1 %39, label %22, label %40
40: ; preds = %30, %12, %9, %2
%41 = phi i32 [ -1, %2 ], [ -1, %9 ], [ 0, %12 ], [ 0, %30 ]
ret i32 %41
}