#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 */
int getpagesize();
void touch(char *buf, int nbytes) {
  static int psize;
  if (!psize) {
    psize = getpagesize();
  }
  while (nbytes > 0) {
    *buf = 1;
    buf += psize;
    nbytes -= psize;
  }
}


x86 assembly (clang, -O0):
<touch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,0x2f06(%rip)
jne    1139 <touch+0x29>
mov    $0x0,%al
call   1030 <getpagesize@plt>
mov    %eax,0x2ef3(%rip)
jmp    113e <touch+0x2e>
cmpl   $0x0,-0xc(%rbp)
jle    1176 <touch+0x66>
mov    -0x8(%rbp),%rax
movb   $0x1,(%rax)
mov    0x2ed7(%rip),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    0x2ec3(%rip),%ecx
mov    -0xc(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
jmp    113e <touch+0x2e>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<touch>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    0x2f0e(%rip),%eax
test   %eax,%eax
je     1128 <touch+0x18>
test   %ebp,%ebp
jg     1139 <touch+0x29>
jmp    114c <touch+0x3c>
xor    %eax,%eax
call   1030 <getpagesize@plt>
mov    %eax,0x2ef7(%rip)
test   %ebp,%ebp
jle    114c <touch+0x3c>
cltq
nopl   0x0(%rax,%rax,1)
movb   $0x1,(%rbx)
add    %rax,%rbx
sub    %eax,%ebp
test   %ebp,%ebp
jg     1140 <touch+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@touch.psize = internal global i32 0
define void @touch(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%5 = load i32, i32* @touch.psize
%6 = icmp ne i32 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = call i32 (...) @getpagesize()
store i32 %8, i32* @touch.psize
br label %9
9: ; preds = %7, %2
br label %10
10: ; preds = %13, %9
%11 = load i32, i32* %4
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %22
13: ; preds = %10
%14 = load i8*, i8** %3
store i8 1, i8* %14
%15 = load i32, i32* @touch.psize
%16 = load i8*, i8** %3
%17 = sext i32 %15 to i64
%18 = getelementptr inbounds i8, i8* %16, i64 %17
store i8* %18, i8** %3
%19 = load i32, i32* @touch.psize
%20 = load i32, i32* %4
%21 = sub nsw i32 %20, %19
store i32 %21, i32* %4
br label %10
22: ; preds = %10
ret void
}

LLVM IR (-O2):
@touch.psize = internal unnamed_addr global i32 0
define void @touch(i8* nocapture writeonly %0, i32 %1) {
%3 = load i32, i32* @touch.psize
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 (...) @getpagesize()
store i32 %6, i32* @touch.psize
br label %7
7: ; preds = %5, %2
%8 = phi i32 [ %6, %5 ], [ %3, %2 ]
%9 = icmp sgt i32 %1, 0
br i1 %9, label %10, label %18
10: ; preds = %7
%11 = sext i32 %8 to i64
br label %12
12: ; preds = %10, %12
%13 = phi i32 [ %1, %10 ], [ %16, %12 ]
%14 = phi i8* [ %0, %10 ], [ %15, %12 ]
store i8 1, i8* %14
%15 = getelementptr inbounds i8, i8* %14, i64 %11
%16 = sub nsw i32 %13, %8
%17 = icmp sgt i32 %16, 0
br i1 %17, label %12, label %18
18: ; preds = %12, %7
ret void
}