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

/* Variables and functions */
int /*<<< orphan*/ err(char *);
int /*<<< orphan*/ free(char *);
int gettimeofday(struct timeval *, int /*<<< orphan*/ *);
char *malloc(int const);
int /*<<< orphan*/ shutdown();
int calibrate_page_dirty() {
  int dirty_rate = 1;
  const int numpages = 1024;
  const int bufsize = 4096 * numpages;
  char *mem_buf = malloc(bufsize);
  if (!mem_buf) {
    err("Cant allocate buffer to calibrate page dirty \n");
    shutdown();
  }
  struct timeval tv, tv2;
  if (gettimeofday(&tv, NULL) == -1)
    shutdown();
  int i, j;
  for (i = 0; i < 10; i++) {
    for (j = 0; j < bufsize - 1; j++) {
      mem_buf[j] = 'a';
    }
  }
  if (gettimeofday(&tv2, NULL) == -1)
    shutdown();
  long long elapsed =
      (tv2.tv_sec - tv.tv_sec) * 1000000 + (tv2.tv_usec - tv.tv_usec);
  dirty_rate = (bufsize * 10) / elapsed;
  free(mem_buf);
  return dirty_rate;
}


x86 assembly (clang, -O0):
<calibrate_page_dirty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x1,-0x4(%rbp)
movl   $0x400,-0x8(%rbp)
movl   $0x400000,-0xc(%rbp)
mov    $0x400000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1199 <calibrate_page_dirty+0x49>
lea    0xe73(%rip),%rdi
call   1060 <err@plt>
mov    $0x0,%al
call   1040 <shutdown@plt>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
cmp    $0xffffffff,%eax
jne    11b6 <calibrate_page_dirty+0x66>
mov    $0x0,%al
call   1040 <shutdown@plt>
movl   $0x0,-0x2c(%rbp)
cmpl   $0xa,-0x2c(%rbp)
jge    1208 <calibrate_page_dirty+0xb8>
movl   $0x0,-0x30(%rbp)
cmpl   $0x3fffff,-0x30(%rbp)
jge    11f5 <calibrate_page_dirty+0xa5>
mov    -0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
movb   $0x61,(%rax,%rcx,1)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11ce <calibrate_page_dirty+0x7e>
jmp    11fa <calibrate_page_dirty+0xaa>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11bd <calibrate_page_dirty+0x6d>
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
cmp    $0xffffffff,%eax
jne    1225 <calibrate_page_dirty+0xd5>
mov    $0x0,%al
call   1040 <shutdown@plt>
mov    -0x28(%rbp),%eax
sub    -0x20(%rbp),%eax
imul   $0xf4240,%eax,%eax
mov    -0x24(%rbp),%ecx
sub    -0x1c(%rbp),%ecx
add    %ecx,%eax
cltq
mov    %rax,-0x38(%rbp)
mov    $0x2800000,%eax
cqto
idivq  -0x38(%rbp)
mov    %eax,-0x4(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calibrate_page_dirty>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    $0x400000,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    118b <calibrate_page_dirty+0x2b>
lea    0xe81(%rip),%rdi
call   1070 <err@plt>
xor    %eax,%eax
call   1040 <shutdown@plt>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
cmp    $0xffffffff,%eax
jne    11a3 <calibrate_page_dirty+0x43>
xor    %eax,%eax
call   1040 <shutdown@plt>
mov    $0x3fffff,%edx
mov    %rbx,%rdi
mov    $0x61,%esi
call   1060 <memset@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
cmp    $0xffffffff,%eax
jne    11cd <calibrate_page_dirty+0x6d>
xor    %eax,%eax
call   1040 <shutdown@plt>
mov    0x8(%rsp),%eax
sub    0x10(%rsp),%eax
mov    0xc(%rsp),%ecx
imul   $0xf4240,%eax,%eax
sub    0x14(%rsp),%ecx
add    %eax,%ecx
mov    $0x2800000,%eax
xor    %edx,%edx
idiv   %ecx
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [47 x i8] c"Cant allocate buffer to calibrate page dirty \0A\00"
define i32 @calibrate_page_dirty() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.timeval
%6 = alloca %struct.timeval
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
store i32 1, i32* %1
store i32 1024, i32* %2
store i32 4194304, i32* %3
%10 = call i8* @malloc(i32 4194304)
store i8* %10, i8** %4
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %16, label %13
13: ; preds = %0
%14 = call i32 @err(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
%15 = call i32 (...) @shutdown()
br label %16
16: ; preds = %13, %0
%17 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 (...) @shutdown()
br label %21
21: ; preds = %19, %16
store i32 0, i32* %7
br label %22
22: ; preds = %38, %21
%23 = load i32, i32* %7
%24 = icmp slt i32 %23, 10
br i1 %24, label %25, label %41
25: ; preds = %22
store i32 0, i32* %8
br label %26
26: ; preds = %34, %25
%27 = load i32, i32* %8
%28 = icmp slt i32 %27, 4194303
br i1 %28, label %29, label %37
29: ; preds = %26
%30 = load i8*, i8** %4
%31 = load i32, i32* %8
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 97, i8* %33
br label %34
34: ; preds = %29
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
store i32 %36, i32* %8
br label %26
37: ; preds = %26
br label %38
38: ; preds = %37
%39 = load i32, i32* %7
%40 = add nsw i32 %39, 1
store i32 %40, i32* %7
br label %22
41: ; preds = %22
%42 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = call i32 (...) @shutdown()
br label %46
46: ; preds = %44, %41
%47 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%48 = load i32, i32* %47
%49 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%50 = load i32, i32* %49
%51 = sub nsw i32 %48, %50
%52 = mul nsw i32 %51, 1000000
%53 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%54 = load i32, i32* %53
%55 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%56 = load i32, i32* %55
%57 = sub nsw i32 %54, %56
%58 = add nsw i32 %52, %57
%59 = sext i32 %58 to i64
store i64 %59, i64* %9
%60 = load i64, i64* %9
%61 = sdiv i64 41943040, %60
%62 = trunc i64 %61 to i32
store i32 %62, i32* %1
%63 = load i8*, i8** %4
%64 = call i32 @free(i8* %63)
%65 = load i32, i32* %1
ret i32 %65
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str = [47 x i8] c"Cant allocate buffer to calibrate page dirty \0A\00"
define i32 @calibrate_page_dirty() {
%1 = alloca %struct.timeval
%2 = alloca %struct.timeval
%3 = tail call dereferenceable_or_null(4194304) i8* @malloc(i32 4194304)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @err(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 (...) @shutdown()
br label %8
8: ; preds = %5, %0
%9 = bitcast %struct.timeval* %1 to i8*
%10 = bitcast %struct.timeval* %2 to i8*
%11 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 (...) @shutdown()
br label %15
15: ; preds = %13, %8
%16 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 (...) @shutdown()
br label %20
20: ; preds = %18, %15
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%24 = load i32, i32* %23
%25 = sub nsw i32 %22, %24
%26 = mul nsw i32 %25, 1000000
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%30 = load i32, i32* %29
%31 = sub i32 %28, %30
%32 = add nsw i32 %31, %26
%33 = sdiv i32 41943040, %32
%34 = tail call i32 @free(i8* %3)
ret i32 %33
}