#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int O_RDWR;
int O_SYNC;
int /*<<< orphan*/ errno;
void *getVirtualAddress(int, int);
int /*<<< orphan*/ munmap(void *, unsigned long);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int /*<<< orphan*/);
int main() {
  int fd;
  if ((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) {
    printf("Char device /dev/mem could not be opened: %s\n", strerror(errno));
    return EXIT_FAILURE;
  }
  void *va_bootkey_in = getVirtualAddress(0x48310038UL, fd);
  void *va_bootkey_dir = getVirtualAddress(0x48310034UL, fd);
  void *va_sysled4_out = getVirtualAddress(0x4905803CUL, fd);
  void *va_sysled4_dir = getVirtualAddress(0x49058034UL, fd);
  if ((va_bootkey_in == (void *)-1) || (va_bootkey_dir == (void *)-1) ||
      (va_sysled4_out == (void *)-1) || (va_sysled4_dir == (void *)-1)) {
    printf("Program halted, problem with assignment of virtual addresses.\n");
    munmap(va_bootkey_in, 4096UL);
    munmap(va_bootkey_dir, 4096UL);
    munmap(va_sysled4_out, 4096UL);
    munmap(va_sysled4_dir, 4096UL);
    return EXIT_FAILURE;
  }
  *(unsigned long *)va_bootkey_dir |= 0x80UL;
  *(unsigned long *)va_sysled4_dir &= 0xffffffefUL;
  for (;;) {
    if ((*(unsigned long *)va_bootkey_in & (0x80UL)) > 7) {
      printf("BOOT_KEY: pressed.\n");
      *(unsigned long *)va_sysled4_out &= 0xffffffefUL;
    } else {
      printf("BOOT_KEY: not pressed.\n");
      *(unsigned long *)va_sysled4_out |= (0x10UL);
    }
  }
  munmap(va_bootkey_in, 4096UL);
  munmap(va_bootkey_dir, 4096UL);
  munmap(va_sysled4_out, 4096UL);
  munmap(va_sysled4_dir, 4096UL);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
mov    0x2e71(%rip),%rax
or     (%rax),%esi
lea    0xe88(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11b9 <main+0x69>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %rax,%rsi
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    131c <main+0x1cc>
mov    -0x8(%rbp),%esi
mov    $0x48310038,%edi
call   1070 <getVirtualAddress@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%esi
mov    $0x48310034,%edi
call   1070 <getVirtualAddress@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%esi
mov    $0x4905803c,%edi
call   1070 <getVirtualAddress@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%esi
mov    $0x49058034,%edi
call   1070 <getVirtualAddress@plt>
mov    %rax,-0x28(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x10(%rbp)
je     1241 <main+0xf1>
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x18(%rbp)
je     1241 <main+0xf1>
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x20(%rbp)
je     1241 <main+0xf1>
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x28(%rbp)
jne    1298 <main+0x148>
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    $0x1000,%esi
call   1040 <munmap@plt>
mov    -0x18(%rbp),%rdi
mov    $0x1000,%esi
call   1040 <munmap@plt>
mov    -0x20(%rbp),%rdi
mov    $0x1000,%esi
call   1040 <munmap@plt>
mov    -0x28(%rbp),%rdi
mov    $0x1000,%esi
call   1040 <munmap@plt>
mov    0x2d3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    131c <main+0x1cc>
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
or     $0x80,%rcx
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
movabs $0xffffffef,%rcx
and    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
and    $0x80,%rax
cmp    $0x7,%rax
jbe    12fb <main+0x1ab>
lea    0xd9b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
movabs $0xffffffef,%rcx
and    (%rax),%rcx
mov    %rcx,(%rax)
jmp    1317 <main+0x1c7>
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
or     $0x10,%rcx
mov    %rcx,(%rax)
jmp    12bd <main+0x16d>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e79(%rip),%rax
mov    0x2e6a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe7f(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1251 <main+0xf1>
mov    %eax,%ebp
mov    $0x48310038,%edi
mov    %eax,%esi
call   1080 <getVirtualAddress@plt>
mov    %rax,%r14
mov    $0x48310034,%edi
mov    %ebp,%esi
call   1080 <getVirtualAddress@plt>
mov    %rax,%r15
mov    $0x4905803c,%edi
mov    %ebp,%esi
call   1080 <getVirtualAddress@plt>
mov    %rax,%rbx
mov    $0x49058034,%edi
mov    %ebp,%esi
call   1080 <getVirtualAddress@plt>
mov    %rax,%r12
cmp    $0xffffffffffffffff,%r14
je     1272 <main+0x112>
cmp    $0xffffffffffffffff,%r15
je     1272 <main+0x112>
cmp    $0xffffffffffffffff,%rbx
je     1272 <main+0x112>
cmp    $0xffffffffffffffff,%r12
je     1272 <main+0x112>
orb    $0x80,(%r15)
mov    $0xffffffef,%eax
and    %rax,(%r12)
lea    0xe2e(%rip),%r15
lea    0xe3e(%rip),%rbp
testb  $0x80,(%r14)
je     1237 <main+0xd7>
cs nopw 0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1030 <puts@plt>
mov    (%rbx),%rax
and    $0xffffffef,%eax
mov    %rax,(%rbx)
testb  $0x80,(%r14)
jne    1220 <main+0xc0>
mov    %r15,%rdi
call   1030 <puts@plt>
mov    (%rbx),%rax
or     $0x10,%rax
mov    %rax,(%rbx)
testb  $0x80,(%r14)
je     1237 <main+0xd7>
jmp    1220 <main+0xc0>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xda3(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12b2 <main+0x152>
lea    0xde8(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1000,%esi
mov    %r14,%rdi
call   1050 <munmap@plt>
mov    $0x1000,%esi
mov    %r15,%rdi
call   1050 <munmap@plt>
mov    $0x1000,%esi
mov    %rbx,%rdi
call   1050 <munmap@plt>
mov    $0x1000,%esi
mov    %r12,%rdi
call   1050 <munmap@plt>
mov    0x2d0f(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [46 x i8] c"Char device /dev/mem could not be opened: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.2 = [63 x i8] c"Program halted, problem with assignment of virtual addresses.\0A\00"
@.str.3 = [20 x i8] c"BOOT_KEY: pressed.\0A\00"
@.str.4 = [24 x i8] c"BOOT_KEY: not pressed.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i32 0, i32* %1
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_SYNC
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %2
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %17
12: ; preds = %0
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EXIT_FAILURE
store i32 %16, i32* %1
br label %76
17: ; preds = %0
%18 = load i32, i32* %2
%19 = call i8* @getVirtualAddress(i32 1211170872, i32 %18)
store i8* %19, i8** %3
%20 = load i32, i32* %2
%21 = call i8* @getVirtualAddress(i32 1211170868, i32 %20)
store i8* %21, i8** %4
%22 = load i32, i32* %2
%23 = call i8* @getVirtualAddress(i32 1225097276, i32 %22)
store i8* %23, i8** %5
%24 = load i32, i32* %2
%25 = call i8* @getVirtualAddress(i32 1225097268, i32 %24)
store i8* %25, i8** %6
%26 = load i8*, i8** %3
%27 = icmp eq i8* %26, inttoptr (i64 -1 to i8*)
br i1 %27, label %37, label %28
28: ; preds = %17
%29 = load i8*, i8** %4
%30 = icmp eq i8* %29, inttoptr (i64 -1 to i8*)
br i1 %30, label %37, label %31
31: ; preds = %28
%32 = load i8*, i8** %5
%33 = icmp eq i8* %32, inttoptr (i64 -1 to i8*)
br i1 %33, label %37, label %34
34: ; preds = %31
%35 = load i8*, i8** %6
%36 = icmp eq i8* %35, inttoptr (i64 -1 to i8*)
br i1 %36, label %37, label %48
37: ; preds = %34, %31, %28, %17
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.2, i64 0, i64 0))
%39 = load i8*, i8** %3
%40 = call i32 @munmap(i8* %39, i64 4096)
%41 = load i8*, i8** %4
%42 = call i32 @munmap(i8* %41, i64 4096)
%43 = load i8*, i8** %5
%44 = call i32 @munmap(i8* %43, i64 4096)
%45 = load i8*, i8** %6
%46 = call i32 @munmap(i8* %45, i64 4096)
%47 = load i32, i32* @EXIT_FAILURE
store i32 %47, i32* %1
br label %76
48: ; preds = %34
%49 = load i8*, i8** %4
%50 = bitcast i8* %49 to i64*
%51 = load i64, i64* %50
%52 = or i64 %51, 128
store i64 %52, i64* %50
%53 = load i8*, i8** %6
%54 = bitcast i8* %53 to i64*
%55 = load i64, i64* %54
%56 = and i64 %55, 4294967279
store i64 %56, i64* %54
br label %57
57: ; preds = %75, %48
%58 = load i8*, i8** %3
%59 = bitcast i8* %58 to i64*
%60 = load i64, i64* %59
%61 = and i64 %60, 128
%62 = icmp ugt i64 %61, 7
br i1 %62, label %63, label %69
63: ; preds = %57
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%65 = load i8*, i8** %5
%66 = bitcast i8* %65 to i64*
%67 = load i64, i64* %66
%68 = and i64 %67, 4294967279
store i64 %68, i64* %66
br label %75
69: ; preds = %57
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%71 = load i8*, i8** %5
%72 = bitcast i8* %71 to i64*
%73 = load i64, i64* %72
%74 = or i64 %73, 16
store i64 %74, i64* %72
br label %75
75: ; preds = %69, %63
br label %57
76: ; preds = %37, %12
%77 = load i32, i32* %1
ret i32 %77
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [46 x i8] c"Char device /dev/mem could not be opened: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@str = [23 x i8] c"BOOT_KEY: not pressed.\00"
@str.5 = [19 x i8] c"BOOT_KEY: pressed.\00"
@str.6 = [62 x i8] c"Program halted, problem with assignment of virtual addresses.\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_SYNC
%3 = or i32 %2, %1
%4 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %10
6: ; preds = %0
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i8* %8)
br label %51
10: ; preds = %0
%11 = tail call i8* @getVirtualAddress(i32 1211170872, i32 %4)
%12 = tail call i8* @getVirtualAddress(i32 1211170868, i32 %4)
%13 = tail call i8* @getVirtualAddress(i32 1225097276, i32 %4)
%14 = tail call i8* @getVirtualAddress(i32 1225097268, i32 %4)
%15 = icmp eq i8* %11, inttoptr (i64 -1 to i8*)
%16 = icmp eq i8* %12, inttoptr (i64 -1 to i8*)
%17 = select i1 %15, i1 true, i1 %16
%18 = icmp eq i8* %13, inttoptr (i64 -1 to i8*)
%19 = select i1 %17, i1 true, i1 %18
%20 = icmp eq i8* %14, inttoptr (i64 -1 to i8*)
%21 = select i1 %19, i1 true, i1 %20
br i1 %21, label %22, label %28
22: ; preds = %10
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @str.6, i64 0, i64 0))
%24 = tail call i32 @munmap(i8* %11, i64 4096)
%25 = tail call i32 @munmap(i8* %12, i64 4096)
%26 = tail call i32 @munmap(i8* %13, i64 4096)
%27 = tail call i32 @munmap(i8* %14, i64 4096)
br label %51
28: ; preds = %10
%29 = bitcast i8* %12 to i64*
%30 = load i64, i64* %29
%31 = or i64 %30, 128
store i64 %31, i64* %29
%32 = bitcast i8* %14 to i64*
%33 = load i64, i64* %32
%34 = and i64 %33, 4294967279
store i64 %34, i64* %32
%35 = bitcast i8* %11 to i64*
%36 = bitcast i8* %13 to i64*
br label %37
37: ; preds = %49, %28
%38 = load i64, i64* %35
%39 = and i64 %38, 128
%40 = icmp eq i64 %39, 0
br i1 %40, label %45, label %41
41: ; preds = %37
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.5, i64 0, i64 0))
%43 = load i64, i64* %36
%44 = and i64 %43, 4294967279
br label %49
45: ; preds = %37
%46 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%47 = load i64, i64* %36
%48 = or i64 %47, 16
br label %49
49: ; preds = %45, %41
%50 = phi i64 [ %44, %41 ], [ %48, %45 ]
store i64 %50, i64* %36
br label %37
51: ; preds = %22, %6
%52 = load i32, i32* @EXIT_FAILURE
ret i32 %52
}