#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 uint32_t;

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ howto();
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ munmap(void *, int);
int open(char *, int /*<<< orphan*/);
int parse_args(int, char **, int *, int *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int sysconf(int /*<<< orphan*/);
int main(int argc, char **argv) {
  uint32_t gpio_addr = 0;
  uint32_t max_offset = 16;
  if (parse_args(argc, argv, &gpio_addr, &max_offset) == -1)
    return -1;
  if (gpio_addr == 0) {
    printf("è necessario specificare l'indirizzo di memoria del device.\n");
    howto();
    return -1;
  }
  int descriptor = open("/dev/mem", O_RDWR);
  if (descriptor < 1) {
    perror(argv[0]);
    return -1;
  }
  uint32_t page_size = sysconf(_SC_PAGESIZE);
  uint32_t page_mask = ~(page_size - 1);
  uint32_t page_addr = gpio_addr & page_mask;
  uint32_t offset = gpio_addr - page_addr;
  void *vrt_page_addr = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
                             MAP_SHARED, descriptor, page_addr);
  if (vrt_page_addr == MAP_FAILED) {
    printf("Mapping indirizzo fisico - indirizzo virtuale FALLITO!\n");
    return -1;
  }
  void *vrt_gpio_addr = vrt_page_addr + offset;
  printf("base address : %08X\n", (uint32_t)vrt_gpio_addr);
  uint32_t read_value = 0;
  uint32_t i;
  for (i = 0; i <= max_offset; i += 4) {
    read_value = *((uint32_t *)(vrt_gpio_addr + i));
    printf("\toffset : %08X => %08X\n", i, read_value);
  }
  munmap(vrt_page_addr, page_size);
  close(descriptor);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x10,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1070 <parse_args@plt>
cmp    $0xffffffff,%eax
jne    11dd <main+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    135a <main+0x1ca>
cmpl   $0x0,-0x14(%rbp)
jne    1208 <main+0x78>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <howto@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135a <main+0x1ca>
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
lea    0xe26(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jge    1242 <main+0xb2>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135a <main+0x1ca>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
xor    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%eax
and    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
sub    -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%esi
mov    0x2d7d(%rip),%rax
mov    (%rax),%edx
mov    0x2d3c(%rip),%rax
or     (%rax),%edx
mov    0x2d5b(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
mov    -0x28(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1040 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    0x2d09(%rip),%rcx
cmp    (%rcx),%rax
jne    12d2 <main+0x142>
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135a <main+0x1ca>
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %eax,%esi
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     133f <main+0x1af>
mov    -0x40(%rbp),%rax
movslq -0x48(%rbp),%rcx
mov    (%rax,%rcx,1),%eax
mov    %eax,-0x44(%rbp)
mov    -0x48(%rbp),%esi
mov    -0x44(%rbp),%edx
lea    0xd6a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x48(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x48(%rbp)
jmp    1303 <main+0x173>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1080 <munmap@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%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    %rsi,%rbx
movl   $0x0,0x10(%rsp)
movl   $0x10,0xc(%rsp)
lea    0x10(%rsp),%rdx
lea    0xc(%rsp),%rcx
call   1080 <parse_args@plt>
mov    $0xffffffff,%r12d
cmp    $0xffffffff,%eax
je     12f3 <main+0x153>
cmpl   $0x0,0x10(%rsp)
je     12d2 <main+0x132>
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
lea    0xe06(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
jle    1305 <main+0x165>
mov    %eax,%r14d
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
call   10c0 <sysconf@plt>
mov    %eax,%r15d
mov    %eax,%ebx
neg    %ebx
mov    0x10(%rsp),%ebp
and    %ebp,%ebx
mov    0x2dcc(%rip),%rax
mov    0x2d8d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2daa(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15d,%esi
mov    %r14d,%r8d
mov    %ebx,%r9d
call   1050 <mmap@plt>
mov    %rax,%r13
mov    0x2d5e(%rip),%rax
cmp    (%rax),%r13
je     12e7 <main+0x147>
mov    %r14d,0x14(%rsp)
sub    %ebx,%ebp
movslq %ebp,%rbx
add    %r13,%rbx
lea    0xd92(%rip),%rdi
xor    %r12d,%r12d
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
cmpl   $0x0,0xc(%rsp)
js     12bc <main+0x11c>
lea    0xd8d(%rip),%rbp
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%rbx,%r14,1),%edx
mov    %rbp,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x4,%r14
cmp    %r14d,0xc(%rsp)
jge    12a0 <main+0x100>
mov    %r13,%rdi
mov    %r15d,%esi
call   1090 <munmap@plt>
mov    0x14(%rsp),%edi
call   1070 <close@plt>
jmp    12f3 <main+0x153>
lea    0xd94(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1030 <howto@plt>
jmp    12f3 <main+0x153>
lea    0xd48(%rip),%rdi
call   1040 <puts@plt>
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%rdi
call   10b0 <perror@plt>
jmp    12f3 <main+0x153>

LLVM IR (-O0):
@.str = [62 x i8] c"\C3\A8 necessario specificare l'indirizzo di memoria del device.\0A\00"
@.str.1 = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [56 x i8] c"Mapping indirizzo fisico - indirizzo virtuale FALLITO!\0A\00"
@.str.3 = [21 x i8] c"base address : %08X\0A\00"
@.str.4 = [24 x i8] c"\09offset : %08X => %08X\0A\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
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 16, i32* %7
%17 = load i32, i32* %4
%18 = load i8**, i8*** %5
%19 = call i32 @parse_args(i32 %17, i8** %18, i32* %6, i32* %7)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %2
store i32 -1, i32* %3
br label %94
22: ; preds = %2
%23 = load i32, i32* %6
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str, i64 0, i64 0))
%27 = call i32 (...) @howto()
store i32 -1, i32* %3
br label %94
28: ; preds = %22
%29 = load i32, i32* @O_RDWR
%30 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %29)
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = icmp slt i32 %31, 1
br i1 %32, label %33, label %38
33: ; preds = %28
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 0
%36 = load i8*, i8** %35
%37 = call i32 @perror(i8* %36)
store i32 -1, i32* %3
br label %94
38: ; preds = %28
%39 = load i32, i32* @_SC_PAGESIZE
%40 = call i32 @sysconf(i32 %39)
store i32 %40, i32* %9
%41 = load i32, i32* %9
%42 = sub nsw i32 %41, 1
%43 = xor i32 %42, -1
store i32 %43, i32* %10
%44 = load i32, i32* %6
%45 = load i32, i32* %10
%46 = and i32 %44, %45
store i32 %46, i32* %11
%47 = load i32, i32* %6
%48 = load i32, i32* %11
%49 = sub nsw i32 %47, %48
store i32 %49, i32* %12
%50 = load i32, i32* %9
%51 = load i32, i32* @PROT_READ
%52 = load i32, i32* @PROT_WRITE
%53 = or i32 %51, %52
%54 = load i32, i32* @MAP_SHARED
%55 = load i32, i32* %8
%56 = load i32, i32* %11
%57 = call i8* @mmap(i32* null, i32 %50, i32 %53, i32 %54, i32 %55, i32 %56)
store i8* %57, i8** %13
%58 = load i8*, i8** %13
%59 = load i8*, i8** @MAP_FAILED
%60 = icmp eq i8* %58, %59
br i1 %60, label %61, label %63
61: ; preds = %38
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %94
63: ; preds = %38
%64 = load i8*, i8** %13
%65 = load i32, i32* %12
%66 = sext i32 %65 to i64
%67 = getelementptr i8, i8* %64, i64 %66
store i8* %67, i8** %14
%68 = load i8*, i8** %14
%69 = ptrtoint i8* %68 to i32
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %69)
store i32 0, i32* %15
store i32 0, i32* %16
br label %71
71: ; preds = %85, %63
%72 = load i32, i32* %16
%73 = load i32, i32* %7
%74 = icmp sle i32 %72, %73
br i1 %74, label %75, label %88
75: ; preds = %71
%76 = load i8*, i8** %14
%77 = load i32, i32* %16
%78 = sext i32 %77 to i64
%79 = getelementptr i8, i8* %76, i64 %78
%80 = bitcast i8* %79 to i32*
%81 = load i32, i32* %80
store i32 %81, i32* %15
%82 = load i32, i32* %16
%83 = load i32, i32* %15
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %82, i32 %83)
br label %85
85: ; preds = %75
%86 = load i32, i32* %16
%87 = add nsw i32 %86, 4
store i32 %87, i32* %16
br label %71
88: ; preds = %71
%89 = load i8*, i8** %13
%90 = load i32, i32* %9
%91 = call i32 @munmap(i8* %89, i32 %90)
%92 = load i32, i32* %8
%93 = call i32 @close(i32 %92)
store i32 0, i32* %3
br label %94
94: ; preds = %88, %61, %33, %25, %21
%95 = load i32, i32* %3
ret i32 %95
}

LLVM IR (-O2):
@.str.1 = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [21 x i8] c"base address : %08X\0A\00"
@.str.4 = [24 x i8] c"\09offset : %08X => %08X\0A\00"
@str = [55 x i8] c"Mapping indirizzo fisico - indirizzo virtuale FALLITO!\00"
@str.5 = [61 x i8] c"\C3\A8 necessario specificare l'indirizzo di memoria del device.\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%6 = bitcast i32* %4 to i8*
store i32 16, i32* %4
%7 = call i32 @parse_args(i32 %0, i8** %1, i32* %3, i32* %4)
%8 = icmp eq i32 %7, -1
br i1 %8, label %60, label %9
9: ; preds = %2
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @str.5, i64 0, i64 0))
%14 = call i32 (...) @howto()
br label %60
15: ; preds = %9
%16 = load i32, i32* @O_RDWR
%17 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = icmp slt i32 %17, 1
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i8*, i8** %1
%21 = call i32 @perror(i8* %20)
br label %60
22: ; preds = %15
%23 = load i32, i32* @_SC_PAGESIZE
%24 = call i32 @sysconf(i32 %23)
%25 = sub i32 0, %24
%26 = load i32, i32* %3
%27 = and i32 %26, %25
%28 = load i32, i32* @PROT_READ
%29 = load i32, i32* @PROT_WRITE
%30 = or i32 %29, %28
%31 = load i32, i32* @MAP_SHARED
%32 = call i8* @mmap(i32* null, i32 %24, i32 %30, i32 %31, i32 %17, i32 %27)
%33 = load i8*, i8** @MAP_FAILED
%34 = icmp eq i8* %32, %33
br i1 %34, label %35, label %37
35: ; preds = %22
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str, i64 0, i64 0))
br label %60
37: ; preds = %22
%38 = sub nsw i32 %26, %27
%39 = sext i32 %38 to i64
%40 = getelementptr i8, i8* %32, i64 %39
%41 = ptrtoint i8* %40 to i64
%42 = trunc i64 %41 to i32
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %42)
%44 = load i32, i32* %4
%45 = icmp slt i32 %44, 0
br i1 %45, label %57, label %46
46: ; preds = %37, %46
%47 = phi i64 [ %53, %46 ], [ 0, %37 ]
%48 = getelementptr i8, i8* %40, i64 %47
%49 = bitcast i8* %48 to i32*
%50 = load i32, i32* %49
%51 = trunc i64 %47 to i32
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %51, i32 %50)
%53 = add nuw i64 %47, 4
%54 = load i32, i32* %4
%55 = trunc i64 %53 to i32
%56 = icmp slt i32 %54, %55
br i1 %56, label %57, label %46
57: ; preds = %46, %37
%58 = call i32 @munmap(i8* %32, i32 %24)
%59 = call i32 @close(i32 %17)
br label %60
60: ; preds = %19, %57, %35, %2, %12
%61 = phi i32 [ -1, %12 ], [ -1, %2 ], [ -1, %19 ], [ -1, %35 ], [ 0, %57 ]
ret i32 %61
}