#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 */
typedef void *pid_t;

/* Variables and functions */
#define EFAULT 130
#define EINVAL 129
#define ESRCH 128
int errno;
int /*<<< orphan*/ printf(char *, ...);
void *strtol(char *, int /*<<< orphan*/ *, int);
int syscall(int, void *, unsigned long, unsigned long long *);
int main(int argc, char *argv[]) {
  if (argc != 3) {
    printf("Usage:\t%s pid address\n", argv[0]);
    return EINVAL;
  }
  pid_t pid = strtol(argv[1], NULL, 10);
  unsigned long addr = strtol(argv[2], NULL, 16);
  unsigned long long phys;
  if (syscall(325, pid, addr, &phys) == -1) {
    switch (errno) {
    case ESRCH:
      printf("No such process\n");
      break;
    case EINVAL:
      printf("Invalid address\n");
      break;
    case EFAULT:
      printf("Unable to copy memory\n");
      break;
    }
    return errno;
  }
  if (phys & (1ULL << 62)) {
    phys &= ~(1ULL << 62);
    printf("Swap identifier:\t%llu\n", phys);
  } else {
    printf("Physical address:\t0x%llx\n", phys);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1171 <main+0x41>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xea2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x81,-0x4(%rbp)
jmp    129e <main+0x16e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    $0x145,%edi
lea    -0x28(%rbp),%rcx
call   1040 <syscall@plt>
cmp    $0xffffffff,%eax
jne    124b <main+0x11b>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x80,%eax
je     1206 <main+0xd6>
jmp    11e0 <main+0xb0>
mov    -0x2c(%rbp),%eax
sub    $0x81,%eax
je     1219 <main+0xe9>
jmp    11f3 <main+0xc3>
mov    -0x2c(%rbp),%eax
sub    $0x82,%eax
je     122c <main+0xfc>
jmp    123a <main+0x10a>
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    123a <main+0x10a>
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    123a <main+0x10a>
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    129e <main+0x16e>
movabs $0x4000000000000000,%rax
and    -0x28(%rbp),%rax
cmp    $0x0,%rax
je     128c <main+0x15c>
movabs $0xbfffffffffffffff,%rax
ff ff bf
and    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    129e <main+0x16e>
mov    -0x28(%rbp),%rsi
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    119b <main+0x5b>
mov    0x8(%rbx),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %rax,%r14
mov    0x10(%rbx),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <strtol@plt>
mov    %rsp,%rcx
mov    $0x145,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1050 <syscall@plt>
cmp    $0xffffffff,%eax
je     11b3 <main+0x73>
mov    (%rsp),%rsi
bt     $0x3e,%rsi
jb     11dd <main+0x9d>
lea    0xe95(%rip),%rdi
jmp    11f5 <main+0xb5>
mov    (%rbx),%rsi
lea    0xe5b(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x81,%ebx
jmp    11fe <main+0xbe>
mov    0x2e26(%rip),%rbx
mov    (%rbx),%eax
add    $0xffffff80,%eax
cmp    $0x2,%eax
ja     11d9 <main+0x99>
cltq
lea    0xeb3(%rip),%rcx
movslq (%rcx,%rax,4),%rdi
add    %rcx,%rdi
call   1030 <puts@plt>
mov    (%rbx),%ebx
jmp    11fe <main+0xbe>
movabs $0xbfffffffffffffff,%rax
ff ff bf
and    %rax,%rsi
mov    %rsi,(%rsp)
lea    0xe22(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [23 x i8] c"Usage:\09%s pid address\0A\00"
@errno = global i32 0
@.str.1 = [17 x i8] c"No such process\0A\00"
@.str.2 = [17 x i8] c"Invalid address\0A\00"
@.str.3 = [23 x i8] c"Unable to copy memory\0A\00"
@.str.4 = [23 x i8] c"Swap identifier:\09%llu\0A\00"
@.str.5 = [26 x i8] c"Physical address:\090x%llx\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 3
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %14)
store i32 129, i32* %3
br label %52
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i8* @strtol(i8* %19, i32* null, i32 10)
store i8* %20, i8** %6
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 2
%23 = load i8*, i8** %22
%24 = call i8* @strtol(i8* %23, i32* null, i32 16)
%25 = ptrtoint i8* %24 to i64
store i64 %25, i64* %7
%26 = load i8*, i8** %6
%27 = load i64, i64* %7
%28 = call i32 @syscall(i32 325, i8* %26, i64 %27, i64* %8)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %40
30: ; preds = %16
%31 = load i32, i32* @errno
switch i32 %31, label %38 [
i32 128, label %32
i32 129, label %34
i32 130, label %36
]
32: ; preds = %30
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %38
34: ; preds = %30
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %38
36: ; preds = %30
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %30, %36, %34, %32
%39 = load i32, i32* @errno
store i32 %39, i32* %3
br label %52
40: ; preds = %16
%41 = load i64, i64* %8
%42 = and i64 %41, 4611686018427387904
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %40
%45 = load i64, i64* %8
%46 = and i64 %45, -4611686018427387905
store i64 %46, i64* %8
%47 = load i64, i64* %8
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i64 %47)
br label %52
49: ; preds = %40
%50 = load i64, i64* %8
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i64 %50)
br label %52
52: ; preds = %11, %38, %49, %44
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@.str = [23 x i8] c"Usage:\09%s pid address\0A\00"
@errno = global i32 0
@.str.4 = [23 x i8] c"Swap identifier:\09%llu\0A\00"
@.str.5 = [26 x i8] c"Physical address:\090x%llx\0A\00"
@str = [22 x i8] c"Unable to copy memory\00"
@str.6 = [16 x i8] c"Invalid address\00"
@str.7 = [16 x i8] c"No such process\00"
@reltable.main = [3 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([16 x i8]* @str.7 to i64), i64 ptrtoint ([3 x i32]* @reltable.main to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([16 x i8]* @str.6 to i64), i64 ptrtoint ([3 x i32]* @reltable.main to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([22 x i8]* @str to i64), i64 ptrtoint ([3 x i32]* @reltable.main to i64)) to i32)]
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i64
%4 = icmp eq i32 %0, 3
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %41
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i8* @strtol(i8* nocapture %10, i32* null, i32 10)
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i8* @strtol(i8* nocapture %13, i32* null, i32 16)
%15 = ptrtoint i8* %14 to i64
%16 = bitcast i64* %3 to i8*
%17 = call i32 @syscall(i32 325, i8* %11, i64 %15, i64* %3)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %30
19: ; preds = %8
%20 = load i32, i32* @errno
%21 = add i32 %20, -128
%22 = icmp ult i32 %21, 3
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = sext i32 %21 to i64
%25 = shl i64 %24, 2
%26 = call i8* @llvm.load.relative.i64(i8* bitcast ([3 x i32]* @reltable.main to i8*), i64 %25)
%27 = call i32 @puts(i8* dereferenceable(1) %26)
br label %28
28: ; preds = %19, %23
%29 = load i32, i32* @errno
br label %39
30: ; preds = %8
%31 = load i64, i64* %3
%32 = and i64 %31, 4611686018427387904
%33 = icmp eq i64 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %30
%35 = and i64 %31, -4611686018427387905
store i64 %35, i64* %3
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i64 %35)
br label %39
37: ; preds = %30
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i64 %31)
br label %39
39: ; preds = %34, %37, %28
%40 = phi i32 [ %29, %28 ], [ 0, %37 ], [ 0, %34 ]
br label %41
41: ; preds = %39, %5
%42 = phi i32 [ 129, %5 ], [ %40, %39 ]
ret i32 %42
}