#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 */
void touch_mem(const void *mem, size_t size, size_t nmemb) {
  size_t i;
  char *last = ((char *)mem) + size * nmemb - 1;
  for (char *c = (char *)mem; c <= last; c += 4096) {
    volatile char touched = *c;
  }
  volatile char touched = *last;
}


x86 assembly (clang, -O0):
<touch_mem>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
imul   -0x18(%rbp),%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
ja     115a <touch_mem+0x5a>
mov    -0x30(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x31(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1000,%rax
mov    %rax,-0x30(%rbp)
jmp    1130 <touch_mem+0x30>
mov    -0x28(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x32(%rbp)
pop    %rbp
ret

x86 assembly (clang, -O2):
<touch_mem>:
imul   %rdx,%rsi
lea    (%rdi,%rsi,1),%rax
add    $0xffffffffffffffff,%rax
cmp    %rdi,%rax
jb     1133 <touch_mem+0x33>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl (%rdi),%ecx
mov    %cl,-0x1(%rsp)
add    $0x1000,%rdi
cmp    %rax,%rdi
jbe    1120 <touch_mem+0x20>
mov    (%rax),%al
mov    %al,-0x2(%rsp)
ret

LLVM IR (-O0):
define void @touch_mem(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8
%11 = alloca i8
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 %2, i64* %6
%12 = load i8*, i8** %4
%13 = load i64, i64* %5
%14 = load i64, i64* %6
%15 = mul i64 %13, %14
%16 = getelementptr inbounds i8, i8* %12, i64 %15
%17 = getelementptr inbounds i8, i8* %16, i64 -1
store i8* %17, i8** %8
%18 = load i8*, i8** %4
store i8* %18, i8** %9
br label %19
19: ; preds = %26, %3
%20 = load i8*, i8** %9
%21 = load i8*, i8** %8
%22 = icmp ule i8* %20, %21
br i1 %22, label %23, label %29
23: ; preds = %19
%24 = load i8*, i8** %9
%25 = load i8, i8* %24
store volatile i8 %25, i8* %10
br label %26
26: ; preds = %23
%27 = load i8*, i8** %9
%28 = getelementptr inbounds i8, i8* %27, i64 4096
store i8* %28, i8** %9
br label %19
29: ; preds = %19
%30 = load i8*, i8** %8
%31 = load i8, i8* %30
store volatile i8 %31, i8* %11
ret void
}

LLVM IR (-O2):
define void @touch_mem(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8
%5 = alloca i8
%6 = mul i64 %2, %1
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = getelementptr inbounds i8, i8* %7, i64 -1
%9 = icmp ult i8* %8, %0
br i1 %9, label %10, label %12
10: ; preds = %12, %3
%11 = load i8, i8* %8
store volatile i8 %11, i8* %5
ret void
12: ; preds = %3, %12
%13 = phi i8* [ %15, %12 ], [ %0, %3 ]
%14 = load i8, i8* %13
store volatile i8 %14, i8* %4
%15 = getelementptr inbounds i8, i8* %13, i64 4096
%16 = icmp ugt i8* %15, %8
br i1 %16, label %10, label %12
}