#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 atoi(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strtol(unsigned char *, int /*<<< orphan*/ *, int);
int main(int argc, char *argv[]) {
  if (argc == 2) {
    unsigned char *hex;
    hex = argv[1];
    int addr = (int)strtol(hex, NULL, 16);
    int pg = addr / 4096;
    int offset = addr - (pg * 4096);
    printf("Page #: %d\n", pg);
    printf("Offset: %d\n", offset);
  } else if (argc == 3) {
    int pg = atoi(argv[1]);
    int offset = atoi(argv[2]);
    int addr = 4096 * pg + offset;
    printf("Virtual Address-> %d\n", addr);
  } else {
    printf("Incorrect Number of Arguments!");
    return 0;
  }
}


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   $0x2,-0x8(%rbp)
jne    11b4 <main+0x84>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1040 <strtol@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x1000,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x20(%rbp),%ecx
shl    $0xc,%ecx
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x24(%rbp),%esi
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    121f <main+0xef>
cmpl   $0x3,-0x8(%rbp)
jne    1200 <main+0xd0>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%eax
shl    $0xc,%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%esi
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    121a <main+0xea>
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    121f <main+0xef>
jmp    121f <main+0xef>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x3,%edi
je     1182 <main+0x52>
cmp    $0x2,%edi
jne    11ad <main+0x7d>
mov    0x8(%rbx),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <strtol@plt>
mov    %rax,%rbx
add    $0xfff,%eax
test   %ebx,%ebx
cmovns %ebx,%eax
mov    %eax,%esi
sar    $0xc,%esi
and    $0xfffff000,%eax
sub    %eax,%ebx
lea    0xe90(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xe8e(%rip),%rdi
mov    %ebx,%esi
jmp    11a4 <main+0x74>
mov    0x8(%rbx),%rdi
call   1050 <atoi@plt>
mov    %eax,%ebp
mov    0x10(%rbx),%rdi
call   1050 <atoi@plt>
shl    $0xc,%ebp
add    %eax,%ebp
lea    0xe76(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    11bb <main+0x8b>
lea    0xe7a(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"Page #: %d\0A\00"
@.str.1 = [12 x i8] c"Offset: %d\0A\00"
@.str.2 = [22 x i8] c"Virtual Address-> %d\0A\00"
@.str.3 = [31 x i8] c"Incorrect Number of Arguments!\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, 2
br i1 %14, label %15, label %32
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
store i8* %18, i8** %6
%19 = load i8*, i8** %6
%20 = call i64 @strtol(i8* %19, i32* null, i32 16)
%21 = trunc i64 %20 to i32
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = sdiv i32 %22, 4096
store i32 %23, i32* %8
%24 = load i32, i32* %7
%25 = load i32, i32* %8
%26 = mul nsw i32 %25, 4096
%27 = sub nsw i32 %24, %26
store i32 %27, i32* %9
%28 = load i32, i32* %8
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %9
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %30)
br label %53
32: ; preds = %2
%33 = load i32, i32* %4
%34 = icmp eq i32 %33, 3
br i1 %34, label %35, label %50
35: ; preds = %32
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i32 @atoi(i8* %38)
store i32 %39, i32* %10
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 2
%42 = load i8*, i8** %41
%43 = call i32 @atoi(i8* %42)
store i32 %43, i32* %11
%44 = load i32, i32* %10
%45 = mul nsw i32 4096, %44
%46 = load i32, i32* %11
%47 = add nsw i32 %45, %46
store i32 %47, i32* %12
%48 = load i32, i32* %12
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %48)
br label %52
50: ; preds = %32
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %53
52: ; preds = %35
br label %53
53: ; preds = %50, %52, %15
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@.str = [12 x i8] c"Page #: %d\0A\00"
@.str.1 = [12 x i8] c"Offset: %d\0A\00"
@.str.2 = [22 x i8] c"Virtual Address-> %d\0A\00"
@.str.3 = [31 x i8] c"Incorrect Number of Arguments!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
switch i32 %0, label %23 [
i32 2, label %3
i32 3, label %13
]
3: ; preds = %2
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = tail call i64 @strtol(i8* nocapture %5, i32* null, i32 16)
%7 = trunc i64 %6 to i32
%8 = sdiv i32 %7, 4096
%9 = mul i32 %8, -4096
%10 = add i32 %9, %7
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %8)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %10)
br label %25
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32 @atoi(i8* %15)
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = tail call i32 @atoi(i8* %18)
%20 = shl nsw i32 %16, 12
%21 = add nsw i32 %19, %20
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %21)
br label %25
23: ; preds = %2
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %23, %13, %3
ret i32 0
}