#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 CHAR_BIT;
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ same_page(void *, void *);
int scanf(char *, void **, void **);
int main(void) {
  printf("Page size = %d; Pointers are %zu bits\n", 4096,
         sizeof(void *) * CHAR_BIT);
  printf(
      "Enter 2 memory addresses to see if they are on the same memory page\n");
  printf("> ");
  void *ptr1, *ptr2;
  while (scanf("%p%p", &ptr1, &ptr2) == 2) {
    if (same_page(ptr1, ptr2)) {
      printf("They are on the same page\n");
    } else {
      printf("They are not on the same page\n");
    }
    printf("> ");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e9a(%rip),%rax
movslq (%rax),%rdx
shl    $0x3,%rdx
lea    0xeac(%rip),%rdi
mov    $0x1000,%esi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xec0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xef7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeec(%rip),%rdi
lea    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1040 <scanf@plt>
cmp    $0x2,%eax
jne    11e4 <main+0xb4>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <same_page@plt>
cmp    $0x0,%rax
je     11c3 <main+0x93>
lea    0xebd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11d1 <main+0xa1>
lea    0xec5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe94(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    117c <main+0x4c>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e8b(%rip),%rax
movslq (%rax),%rdx
shl    $0x3,%rdx
lea    0xe9d(%rip),%rdi
mov    $0x1000,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xeb9(%rip),%rdi
call   1030 <puts@plt>
lea    0xea5(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe9a(%rip),%rdi
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
call   1050 <scanf@plt>
cmp    $0x2,%eax
jne    120b <main+0xcb>
lea    0xec8(%rip),%r13
lea    0xedf(%rip),%rbp
lea    0xe6e(%rip),%r14
lea    0xe6a(%rip),%r15
lea    0x10(%rsp),%r12
lea    0x8(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    0x10(%rsp),%rdi
mov    0x8(%rsp),%rsi
call   1060 <same_page@plt>
test   %rax,%rax
mov    %rbp,%rdi
cmove  %r13,%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1050 <scanf@plt>
cmp    $0x2,%eax
je     11d0 <main+0x90>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [39 x i8] c"Page size = %d; Pointers are %zu bits\0A\00"
@CHAR_BIT = global i32 0
@.str.1 = [69 x i8] c"Enter 2 memory addresses to see if they are on the same memory page\0A\00"
@.str.2 = [3 x i8] c"> \00"
@.str.3 = [5 x i8] c"%p%p\00"
@.str.4 = [27 x i8] c"They are on the same page\0A\00"
@.str.5 = [31 x i8] c"They are not on the same page\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
store i32 0, i32* %1
%4 = load i32, i32* @CHAR_BIT
%5 = sext i32 %4 to i64
%6 = mul i64 8, %5
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 4096, i64 %6)
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %10
10: ; preds = %22, %0
%11 = call i32 @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8** %2, i8** %3)
%12 = icmp eq i32 %11, 2
br i1 %12, label %13, label %24
13: ; preds = %10
%14 = load i8*, i8** %2
%15 = load i8*, i8** %3
%16 = call i64 @same_page(i8* %14, i8* %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
br label %22
20: ; preds = %13
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
br label %22
22: ; preds = %20, %18
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %10
24: ; preds = %10
ret i32 0
}

LLVM IR (-O2):
@.str = [39 x i8] c"Page size = %d; Pointers are %zu bits\0A\00"
@CHAR_BIT = global i32 0
@.str.2 = [3 x i8] c"> \00"
@.str.3 = [5 x i8] c"%p%p\00"
@str = [68 x i8] c"Enter 2 memory addresses to see if they are on the same memory page\00"
@str.6 = [30 x i8] c"They are not on the same page\00"
@str.7 = [26 x i8] c"They are on the same page\00"
define i32 @main() {
%1 = alloca i8*
%2 = alloca i8*
%3 = load i32, i32* @CHAR_BIT
%4 = sext i32 %3 to i64
%5 = shl nsw i64 %4, 3
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 4096, i64 %5)
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([68 x i8], [68 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%9 = bitcast i8** %1 to i8*
%10 = bitcast i8** %2 to i8*
%11 = call i32 @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8** %1, i8** %2)
%12 = icmp eq i32 %11, 2
br i1 %12, label %13, label %23
13: ; preds = %0, %13
%14 = load i8*, i8** %1
%15 = load i8*, i8** %2
%16 = call i64 @same_page(i8* %14, i8* %15)
%17 = icmp eq i64 %16, 0
%18 = select i1 %17, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @str.6, i64 0, i64 0), i8* getelementptr inbounds ([26 x i8], [26 x i8]* @str.7, i64 0, i64 0)
%19 = call i32 @puts(i8* dereferenceable(1) %18)
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8** %1, i8** %2)
%22 = icmp eq i32 %21, 2
br i1 %22, label %13, label %23
23: ; preds = %13, %0
ret i32 0
}