#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 int uint64_t;
typedef int uint32_t;

/* Variables and functions */
scalar_t__ dynamic_mmap(void *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("Enter an address to read or enter an address and value to write\n");
    exit(-1);
  } else {
    uint64_t address = 0;
    uint64_t value = 0;
    volatile uint32_t *ptr;
    bool is_read = true;
    sscanf(argv[1], "%lli", &address);
    address &= 0xFFFFFFFF;
    switch (argc) {
    case 2:
      is_read = true;
      break;
    case 3:
      sscanf(argv[2], "%lli", &value);
      is_read = false;
      break;
    default:
      printf("Unknown error\n");
      exit(-1);
    }
    ptr = (uint32_t *)dynamic_mmap((void *)(uint32_t)address);
    if (is_read) {
      printf("Value = 0x%x\n", *ptr);
    } else {
      volatile uint32_t read_value;
      *ptr = 0xFFFFFFFF & value;
      read_value = *ptr;
      printf("Value = 0x%x, Set Value = 0x%x\n", 0xFFFFFFFF & value,
             read_value);
      printf("Write : %s\n", read_value == value ? "Passed" : "Failed");
    }
  }
}


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)
jge    1178 <main+0x38>
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xea5(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <sscanf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x2,%eax
je     11ca <main+0x8a>
jmp    11b9 <main+0x79>
mov    -0x2c(%rbp),%eax
sub    $0x3,%eax
je     11d6 <main+0x96>
jmp    11fa <main+0xba>
movl   $0x1,-0x24(%rbp)
jmp    1212 <main+0xd2>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe5c(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1040 <sscanf@plt>
movl   $0x0,-0x24(%rbp)
jmp    1212 <main+0xd2>
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
movslq -0x14(%rbp),%rdi
call   1060 <dynamic_mmap@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1242 <main+0x102>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    129c <main+0x15c>
mov    -0x18(%rbp),%ecx
and    $0xffffffff,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%esi
and    $0xffffffff,%esi
mov    -0x28(%rbp),%edx
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%ecx
mov    -0x18(%rbp),%edx
lea    0xe07(%rip),%rsi
lea    0xdf9(%rip),%rax
cmp    %edx,%ecx
cmove  %rax,%rsi
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12a1 <main+0x161>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
cmp    $0x1,%edi
jle    1225 <main+0xd5>
mov    %rsi,%rbx
mov    %edi,%ebp
movl   $0x0,0x10(%rsp)
movl   $0x0,0xc(%rsp)
mov    0x8(%rsi),%rdi
lea    0xe81(%rip),%rsi
lea    0x10(%rsp),%rdx
call   1050 <sscanf@plt>
cmp    $0x2,%ebp
je     1202 <main+0xb2>
cmp    $0x3,%ebp
jne    122e <main+0xde>
mov    0x10(%rbx),%rdi
lea    0xe5e(%rip),%rsi
lea    0xc(%rsp),%rdx
call   1050 <sscanf@plt>
movslq 0x10(%rsp),%rdi
call   1070 <dynamic_mmap@plt>
mov    0xc(%rsp),%ecx
mov    %ecx,(%rax)
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0xc(%rsp),%esi
mov    0x14(%rsp),%edx
lea    0xe34(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x14(%rsp),%eax
cmp    0xc(%rsp),%eax
lea    0xe4a(%rip),%rax
lea    0xe4a(%rip),%rsi
cmove  %rax,%rsi
lea    0xe2c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    121c <main+0xcc>
movslq 0x10(%rsp),%rdi
call   1070 <dynamic_mmap@plt>
mov    (%rax),%esi
lea    0xe02(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe21(%rip),%rdi
jmp    1235 <main+0xe5>
lea    0xe0a(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [65 x i8] c"Enter an address to read or enter an address and value to write\0A\00"
@.str.1 = [5 x i8] c"%lli\00"
@.str.2 = [15 x i8] c"Unknown error\0A\00"
@.str.3 = [14 x i8] c"Value = 0x%x\0A\00"
@.str.4 = [32 x i8] c"Value = 0x%x, Set Value = 0x%x\0A\00"
@.str.5 = [12 x i8] c"Write : %s\0A\00"
@.str.6 = [7 x i8] c"Passed\00"
@.str.7 = [7 x i8] c"Failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %2
store i32 0, i32* %6
store i32 0, i32* %7
store i32 1, i32* %9
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @sscanf(i8* %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32* %6)
%21 = load i32, i32* %6
%22 = and i32 %21, -1
store i32 %22, i32* %6
%23 = load i32, i32* %4
switch i32 %23, label %30 [
i32 2, label %24
i32 3, label %25
]
24: ; preds = %16
store i32 1, i32* %9
br label %33
25: ; preds = %16
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i32 @sscanf(i8* %28, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32* %7)
store i32 0, i32* %9
br label %33
30: ; preds = %16
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 -1)
unreachable
33: ; preds = %25, %24
%34 = load i32, i32* %6
%35 = sext i32 %34 to i64
%36 = inttoptr i64 %35 to i8*
%37 = call i64 @dynamic_mmap(i8* %36)
%38 = inttoptr i64 %37 to i32*
store i32* %38, i32** %8
%39 = load i32, i32* %9
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %33
%42 = load i32*, i32** %8
%43 = load volatile i32, i32* %42
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %43)
br label %61
45: ; preds = %33
%46 = load i32, i32* %7
%47 = and i32 -1, %46
%48 = load i32*, i32** %8
store volatile i32 %47, i32* %48
%49 = load i32*, i32** %8
%50 = load volatile i32, i32* %49
store volatile i32 %50, i32* %10
%51 = load i32, i32* %7
%52 = and i32 -1, %51
%53 = load volatile i32, i32* %10
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %52, i32 %53)
%55 = load volatile i32, i32* %10
%56 = load i32, i32* %7
%57 = icmp eq i32 %55, %56
%58 = zext i1 %57 to i64
%59 = select i1 %57, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0)
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %59)
br label %61
61: ; preds = %45, %41
br label %62
62: ; preds = %61
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c"%lli\00"
@.str.3 = [14 x i8] c"Value = 0x%x\0A\00"
@.str.4 = [32 x i8] c"Value = 0x%x, Set Value = 0x%x\0A\00"
@.str.5 = [12 x i8] c"Write : %s\0A\00"
@.str.6 = [7 x i8] c"Passed\00"
@.str.7 = [7 x i8] c"Failed\00"
@str = [14 x i8] c"Unknown error\00"
@str.8 = [64 x i8] c"Enter an address to read or enter an address and value to write\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = icmp slt i32 %0, 2
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @str.8, i64 0, i64 0))
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %2
%11 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%12 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @sscanf(i8* %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32* %3)
switch i32 %0, label %16 [
i32 2, label %19
i32 3, label %27
]
16: ; preds = %10
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %10
%20 = load i32, i32* %3
%21 = sext i32 %20 to i64
%22 = inttoptr i64 %21 to i8*
%23 = call i64 @dynamic_mmap(i8* %22)
%24 = inttoptr i64 %23 to i32*
%25 = load volatile i32, i32* %24
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %25)
br label %47
27: ; preds = %10
%28 = getelementptr inbounds i8*, i8** %1, i64 2
%29 = load i8*, i8** %28
%30 = call i32 @sscanf(i8* %29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%31 = load i32, i32* %3
%32 = sext i32 %31 to i64
%33 = inttoptr i64 %32 to i8*
%34 = call i64 @dynamic_mmap(i8* %33)
%35 = inttoptr i64 %34 to i32*
%36 = bitcast i32* %5 to i8*
%37 = load i32, i32* %4
store volatile i32 %37, i32* %35
%38 = load volatile i32, i32* %35
store volatile i32 %38, i32* %5
%39 = load i32, i32* %4
%40 = load volatile i32, i32* %5
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %39, i32 %40)
%42 = load volatile i32, i32* %5
%43 = load i32, i32* %4
%44 = icmp eq i32 %42, %43
%45 = select i1 %44, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0)
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %45)
br label %47
47: ; preds = %27, %19
ret i32 0
}