#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*/ memset(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_memset(int iters, int sz) {
  struct timeval tv1;
  struct timeval tv2;
  int i, j;
  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++)
      memset(b, 0, sz);
    gettimeofday(&tv2, NULL);
    tv_sub(&tv2, &tv1);
    printf("memset %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_memset>:
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_memset+0x31>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <do_memset+0x11e>
mov    $0x3b9aca00,%eax
cltd
idivl  -0xc(%rbp)
mov    %eax,-0x34(%rbp)
movl   $0x0,-0x24(%rbp)
mov    $0x1,%al
cmpl   $0xffffffff,-0x8(%rbp)
mov    %al,-0x35(%rbp)
je     11bf <do_memset+0x5f>
mov    -0x24(%rbp),%eax
cmp    -0x8(%rbp),%eax
setl   %al
mov    %al,-0x35(%rbp)
mov    -0x35(%rbp),%al
test   $0x1,%al
jne    11cf <do_memset+0x6f>
jmp    1277 <do_memset+0x117>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    120b <do_memset+0xab>
mov    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11e3 <do_memset+0x83>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
lea    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <tv_sub@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x34(%rbp),%edi
imul   -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1070 <mb_sec@plt>
mov    -0x48(%rbp),%esi
mov    -0x44(%rbp),%edx
mov    -0x40(%rbp),%ecx
mov    -0x3c(%rbp),%r8d
mov    %eax,%r9d
lea    0xd9c(%rip),%rdi
call   1030 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a4 <do_memset+0x44>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_memset>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
mov    %esi,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1268 <do_memset+0x108>
mov    %rax,%r15
xor    %eax,%eax
mov    %rax,0x10(%rsp)
mov    $0x3b9aca00,%eax
xor    %edx,%edx
idiv   %ebx
mov    %eax,0xc(%rsp)
cmp    $0xffffffff,%ebp
je     11a6 <do_memset+0x46>
test   %ebp,%ebp
jle    1272 <do_memset+0x112>
mov    0xc(%rsp),%eax
imul   %ebx,%eax
mov    %eax,0x18(%rsp)
xor    %eax,%eax
lea    0x30(%rsp),%r14
mov    %ebp,0x1c(%rsp)
nopl   0x0(%rax)
mov    %rax,0x28(%rsp)
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
cmpl   $0x0,0xc(%rsp)
jle    11f1 <do_memset+0x91>
mov    0xc(%rsp),%ebp
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1050 <memset@plt>
add    $0xffffffff,%ebp
jne    11e0 <do_memset+0x80>
xor    %eax,%eax
mov    %rax,0x10(%rsp)
lea    0x20(%rsp),%r13
mov    %r13,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    %r13,%rdi
mov    %r14,%rsi
call   1060 <tv_sub@plt>
mov    %r14,%rbp
mov    0x20(%rsp),%r14d
mov    0x24(%rsp),%r12d
mov    0x18(%rsp),%edi
mov    %r13,%rsi
call   1070 <mb_sec@plt>
lea    0xdce(%rip),%rdi
mov    0xc(%rsp),%esi
mov    %ebx,%edx
mov    %r12d,%ecx
mov    %r14d,%r8d
mov    %rbp,%r14
mov    %eax,%r9d
call   1030 <printf@plt>
mov    0x28(%rsp),%rax
add    $0x1,%eax
mov    0x1c(%rsp),%ebp
cmp    $0xffffffff,%ebp
je     11c0 <do_memset+0x60>
cmp    %ebp,%eax
jl     11c0 <do_memset+0x60>
jmp    1272 <do_memset+0x112>
mov    $0xffffffff,%eax
mov    %rax,0x10(%rsp)
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"memset %dx%d bytes took %ld.%06ld seconds (%f MB/s)\0A\00"
define i32 @do_memset(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
store i32 %0, i32* %4
store i32 %1, i32* %5
%12 = load i32, i32* %5
%13 = call i32* @malloc(i32 %12)
store i32* %13, i32** %10
%14 = load i32*, i32** %10
%15 = icmp ne i32* %14, null
br i1 %15, label %17, label %16
16: ; preds = %2
store i32 -1, i32* %3
br label %60
17: ; preds = %2
%18 = load i32, i32* %5
%19 = sdiv i32 1000000000, %18
store i32 %19, i32* %11
store i32 0, i32* %8
br label %20
20: ; preds = %56, %17
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, -1
br i1 %22, label %27, label %23
23: ; preds = %20
%24 = load i32, i32* %8
%25 = load i32, i32* %4
%26 = icmp slt i32 %24, %25
br label %27
27: ; preds = %23, %20
%28 = phi i1 [ true, %20 ], [ %26, %23 ]
br i1 %28, label %29, label %59
29: ; preds = %27
%30 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
store i32 0, i32* %9
br label %31
31: ; preds = %39, %29
%32 = load i32, i32* %9
%33 = load i32, i32* %11
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %42
35: ; preds = %31
%36 = load i32*, i32** %10
%37 = load i32, i32* %5
%38 = call i32 @memset(i32* %36, i32 0, i32 %37)
br label %39
39: ; preds = %35
%40 = load i32, i32* %9
%41 = add nsw i32 %40, 1
store i32 %41, i32* %9
br label %31
42: ; preds = %31
%43 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%44 = call i32 @tv_sub(%struct.timeval* %7, %struct.timeval* %6)
%45 = load i32, i32* %11
%46 = load i32, i32* %5
%47 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%48 = load i32, i32* %47
%49 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%50 = load i32, i32* %49
%51 = load i32, i32* %11
%52 = load i32, i32* %5
%53 = mul nsw i32 %51, %52
%54 = call i32 @mb_sec(i32 %53, %struct.timeval* %7)
%55 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %45, i32 %46, i32 %48, i32 %50, i32 %54)
br label %56
56: ; preds = %42
%57 = load i32, i32* %8
%58 = add nsw i32 %57, 1
store i32 %58, i32* %8
br label %20
59: ; preds = %27
store i32 0, i32* %3
br label %60
60: ; preds = %59, %16
%61 = load i32, i32* %3
ret i32 %61
}

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