#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 MAP_ANONYMOUS;
int MAP_NORESERVE;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int atoi(char *);
int *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, unsigned long);
char *mmap(int /*<<< orphan*/ *, size_t, int, int, int, int /*<<< orphan*/);
int munmap(char *, size_t);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  int n = (argc > 1) ? atoi(argv[1]) : 30;
  size_t bytes = 1UL << n;
  int *pa = malloc(4096);
  printf("pa=%p\n", pa);
  intptr_t ipa = (intptr_t)pa;
  printf("ipa=%zu\n", ipa);
  intptr_t xipa = ipa | (intptr_t)0xFFFF000000000000ULL;
  printf("xipa=%zu\n", xipa);
  int *xpa = (int *)xipa;
  printf("xpa=%p\n", xpa);
  {
    int prot = PROT_READ | PROT_WRITE;
    int flags = MAP_ANONYMOUS | MAP_NORESERVE | MAP_PRIVATE;
    char *ptr = mmap(NULL, bytes, prot, flags, -1, 0);
    printf("ptr=%p\n", ptr);
    printf("(int)ptr=%zu\n", (intptr_t)ptr);
    memset(ptr, 0, 1UL << 12);
    int rc = munmap(ptr, bytes);
    printf("munmap=%d\n", rc);
  }
  int *zpa = (int *)((intptr_t)xpa & (intptr_t)0x0000FFFFFFFFFFFFULL);
  printf("zpa=%p\n", zpa);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1195 <main+0x35>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x64(%rbp)
jmp    11a2 <main+0x42>
mov    $0x1e,%eax
mov    %eax,-0x64(%rbp)
jmp    11a2 <main+0x42>
mov    -0x64(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
mov    $0x1,%eax
shl    %cl,%rax
mov    %rax,-0x20(%rbp)
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movabs $0xffff000000000000,%rax
or     -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rsi
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    0x2d87(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x44(%rbp)
mov    0x2d8b(%rip),%rax
mov    (%rax),%eax
mov    0x2d8a(%rip),%rcx
or     (%rcx),%eax
mov    0x2d61(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x48(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x44(%rbp),%edx
mov    -0x48(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rsi
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x50(%rbp),%rsi
lea    0xd79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    -0x50(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <munmap@plt>
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%esi
lea    0xd56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rax
movabs $0xffffffffffff,%rcx
and    %rcx,%rax
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rsi
lea    0xd3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    $0x1e,%ecx
cmp    $0x2,%edi
jl     117a <main+0x1a>
mov    0x8(%rsi),%rdi
call   1080 <atoi@plt>
mov    %eax,%ecx
mov    $0x1,%r15d
shl    %cl,%r15
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
lea    0xe74(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe59(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
movabs $0xffff000000000000,%rbx
or     %r14,%rbx
lea    0xe3a(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe33(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e10(%rip),%rax
mov    0x2dd9(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dde(%rip),%rax
mov    0x2ddf(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2db4(%rip),%rax
or     (%rax),%ecx
xor    %edi,%edi
mov    %r15,%rsi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
lea    0xde7(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdde(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1000,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <munmap@plt>
lea    0xdc1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movabs $0xffffffffffff,%rsi
and    %r14,%rsi
lea    0xdaf(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [7 x i8] c"pa=%p\0A\00"
@.str.1 = [9 x i8] c"ipa=%zu\0A\00"
@.str.2 = [10 x i8] c"xipa=%zu\0A\00"
@.str.3 = [8 x i8] c"xpa=%p\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_NORESERVE = global i32 0
@MAP_PRIVATE = global i32 0
@.str.4 = [8 x i8] c"ptr=%p\0A\00"
@.str.5 = [14 x i8] c"(int)ptr=%zu\0A\00"
@.str.6 = [11 x i8] c"munmap=%d\0A\00"
@.str.7 = [8 x i8] c"zpa=%p\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%17 = load i32, i32* %4
%18 = icmp sgt i32 %17, 1
br i1 %18, label %19, label %24
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
br label %25
24: ; preds = %2
br label %25
25: ; preds = %24, %19
%26 = phi i32 [ %23, %19 ], [ 30, %24 ]
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = zext i32 %27 to i64
%29 = shl i64 1, %28
store i64 %29, i64* %7
%30 = call i32* @malloc(i32 4096)
store i32* %30, i32** %8
%31 = load i32*, i32** %8
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32* %31)
%33 = load i32*, i32** %8
%34 = ptrtoint i32* %33 to i64
store i64 %34, i64* %9
%35 = load i64, i64* %9
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i64 %35)
%37 = load i64, i64* %9
%38 = or i64 %37, -281474976710656
store i64 %38, i64* %10
%39 = load i64, i64* %10
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i64 %39)
%41 = load i64, i64* %10
%42 = inttoptr i64 %41 to i32*
store i32* %42, i32** %11
%43 = load i32*, i32** %11
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32* %43)
%45 = load i32, i32* @PROT_READ
%46 = load i32, i32* @PROT_WRITE
%47 = or i32 %45, %46
store i32 %47, i32* %12
%48 = load i32, i32* @MAP_ANONYMOUS
%49 = load i32, i32* @MAP_NORESERVE
%50 = or i32 %48, %49
%51 = load i32, i32* @MAP_PRIVATE
%52 = or i32 %50, %51
store i32 %52, i32* %13
%53 = load i64, i64* %7
%54 = load i32, i32* %12
%55 = load i32, i32* %13
%56 = call i8* @mmap(i32* null, i64 %53, i32 %54, i32 %55, i32 -1, i32 0)
store i8* %56, i8** %14
%57 = load i8*, i8** %14
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %57)
%59 = load i8*, i8** %14
%60 = ptrtoint i8* %59 to i64
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i64 %60)
%62 = load i8*, i8** %14
%63 = call i32 @memset(i8* %62, i32 0, i64 4096)
%64 = load i8*, i8** %14
%65 = load i64, i64* %7
%66 = call i32 @munmap(i8* %64, i64 %65)
store i32 %66, i32* %15
%67 = load i32, i32* %15
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %67)
%69 = load i32*, i32** %11
%70 = ptrtoint i32* %69 to i64
%71 = and i64 %70, 281474976710655
%72 = inttoptr i64 %71 to i32*
store i32* %72, i32** %16
%73 = load i32*, i32** %16
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32* %73)
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"pa=%p\0A\00"
@.str.1 = [9 x i8] c"ipa=%zu\0A\00"
@.str.2 = [10 x i8] c"xipa=%zu\0A\00"
@.str.3 = [8 x i8] c"xpa=%p\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_NORESERVE = global i32 0
@MAP_PRIVATE = global i32 0
@.str.4 = [8 x i8] c"ptr=%p\0A\00"
@.str.5 = [14 x i8] c"(int)ptr=%zu\0A\00"
@.str.6 = [11 x i8] c"munmap=%d\0A\00"
@.str.7 = [8 x i8] c"zpa=%p\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @atoi(i8* %6)
%8 = zext i32 %7 to i64
br label %9
9: ; preds = %2, %4
%10 = phi i64 [ %8, %4 ], [ 30, %2 ]
%11 = shl nuw i64 1, %10
%12 = tail call i32* @malloc(i32 4096)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32* %12)
%14 = ptrtoint i32* %12 to i64
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i64 %14)
%16 = or i64 %14, -281474976710656
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i64 %16)
%18 = inttoptr i64 %16 to i32*
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32* %18)
%20 = load i32, i32* @PROT_READ
%21 = load i32, i32* @PROT_WRITE
%22 = or i32 %21, %20
%23 = load i32, i32* @MAP_ANONYMOUS
%24 = load i32, i32* @MAP_NORESERVE
%25 = or i32 %24, %23
%26 = load i32, i32* @MAP_PRIVATE
%27 = or i32 %25, %26
%28 = tail call i8* @mmap(i32* null, i64 %11, i32 %22, i32 %27, i32 -1, i32 0)
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %28)
%30 = ptrtoint i8* %28 to i64
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i64 %30)
%32 = tail call i32 @memset(i8* %28, i32 0, i64 4096)
%33 = tail call i32 @munmap(i8* %28, i64 %11)
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %33)
%35 = and i64 %14, 281474976710655
%36 = inttoptr i64 %35 to i32*
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32* %36)
ret i32 0
}