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

/* Variables and functions */
char volatile *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, size_t, ...);
scalar_t__ getpagesize();
char *mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ munmap(void *, size_t);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int main() {
  int fd;
  uint32_t value = 0;
  uint32_t flag = 0;
  volatile uint8_t *mm;
  uint8_t str[256];
  int rcnt;
  fd = open("/dev/mem", O_RDWR | O_SYNC);
  if (fd < 0) {
    fprintf(stderr, "open(/dev/mem) failed (%d)\n", errno);
    return 1;
  }
  mm = mmap(NULL, ((size_t)getpagesize()), PROT_READ | PROT_WRITE, MAP_SHARED,
            fd, 0xFFFF9000);
  if (mm == MAP_FAILED) {
    fprintf(stderr, "mmap64(0x%x@0x%x) failed (%d)\n", ((size_t)getpagesize()),
            (uint32_t)(0xFFFF9000), errno);
    return 1;
  }
  rcnt = 0;
  while (1) {
    if ((flag = *(volatile uint32_t *)(mm + 0x00))) {
      value = *(volatile uint32_t *)(mm + 0x04);
      if (flag > 1) {
        printf("CPU1: 0x%08x = 0x%08x\n", (uint32_t)(mm + 0x04), value);
      } else {
        if (rcnt < 256) {
          str[rcnt++] = value;
        }
        if (value == '\n') {
          if (rcnt != 0) {
            str[rcnt - 1] = '\0';
          } else {
            str[0] = '\0';
          }
          printf("CPU1: %s\n", str);
          rcnt = 0;
        }
      }
      *(volatile uint32_t *)(mm + 0x00) = 0;
    }
  }
  munmap((void *)mm, ((size_t)getpagesize()));
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    0x2e50(%rip),%rax
or     (%rax),%esi
lea    0xe77(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11c8 <main+0x78>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
mov    0x2e0d(%rip),%rax
movslq (%rax),%rdx
lea    0xe54(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1348 <main+0x1f8>
mov    $0x0,%al
call   1070 <getpagesize@plt>
mov    %rax,%rsi
mov    0x2e1f(%rip),%rax
mov    (%rax),%edx
mov    0x2dce(%rip),%rax
or     (%rax),%edx
mov    0x2dfd(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffff9000,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d99(%rip),%rcx
cmp    (%rcx),%rax
jne    1260 <main+0x110>
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x128(%rbp)
mov    $0x0,%al
call   1070 <getpagesize@plt>
mov    -0x128(%rbp),%edi
mov    %rax,%rdx
mov    0x2d7a(%rip),%rax
mov    (%rax),%r8d
lea    0xddd(%rip),%rsi
mov    $0xffff9000,%ecx
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1348 <main+0x1f8>
movl   $0x0,-0x124(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
je     1343 <main+0x1f3>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x1,-0x10(%rbp)
jle    12b0 <main+0x160>
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %eax,%esi
mov    -0xc(%rbp),%edx
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1339 <main+0x1e9>
cmpl   $0x100,-0x124(%rbp)
jge    12df <main+0x18f>
mov    -0xc(%rbp),%eax
mov    %al,%cl
mov    -0x124(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x124(%rbp)
cltq
mov    %cl,-0x120(%rbp,%rax,1)
cmpl   $0xa,-0xc(%rbp)
jne    1334 <main+0x1e4>
cmpl   $0x0,-0x124(%rbp)
je     130e <main+0x1be>
mov    -0x124(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x120(%rbp,%rax,1)
jmp    1315 <main+0x1c5>
movb   $0x0,-0x120(%rbp)
lea    -0x120(%rbp),%rsi
lea    0xd38(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x124(%rbp)
jmp    1339 <main+0x1e9>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
jmp    126a <main+0x11a>
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    0x2e70(%rip),%rax
mov    0x2e61(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe86(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     124e <main+0xfe>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <getpagesize@plt>
mov    0x2e61(%rip),%rcx
mov    0x2e12(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e3f(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    %rax,%rsi
mov    %ebx,%r8d
mov    $0xffff9000,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2de0(%rip),%rax
cmp    (%rax),%rbx
je     1271 <main+0x121>
mov    %rbx,%r14
add    $0x4,%r14
lea    0xe65(%rip),%r15
lea    0xe75(%rip),%r12
mov    %rsp,%r13
xor    %ebp,%ebp
jmp    1200 <main+0xb0>
mov    %r15,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x0,(%rbx)
mov    (%rbx),%eax
test   %eax,%eax
je     1200 <main+0xb0>
mov    (%r14),%edx
cmp    $0x2,%eax
jge    11ed <main+0x9d>
cmp    $0xff,%ebp
jg     121f <main+0xcf>
movslq %ebp,%rax
add    $0x1,%ebp
mov    %dl,(%rsp,%rax,1)
cmp    $0xa,%edx
jne    1246 <main+0xf6>
test   %ebp,%ebp
je     1233 <main+0xe3>
movslq %ebp,%rax
movb   $0x0,-0x1(%rax,%r13,1)
jmp    1237 <main+0xe7>
movb   $0x0,(%rsp)
xor    %ebp,%ebp
mov    %r12,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x0,(%rbx)
jmp    1200 <main+0xb0>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
mov    0x2d5a(%rip),%rax
movslq (%rax),%rdx
lea    0xda1(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    12a3 <main+0x153>
mov    0x2d48(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1070 <getpagesize@plt>
mov    0x2d30(%rip),%rcx
mov    (%rcx),%r8d
lea    0xd93(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
mov    $0xffff9000,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
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
@stderr = global i32 0
@.str.1 = [28 x i8] c"open(/dev/mem) failed (%d)\0A\00"
@errno = 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 = [31 x i8] c"mmap64(0x%x@0x%x) failed (%d)\0A\00"
@.str.3 = [23 x i8] c"CPU1: 0x%08x = 0x%08x\0A\00"
@.str.4 = [10 x i8] c"CPU1: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [256 x i8]
%7 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
store i32 0, i32* %4
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_SYNC
%10 = or i32 %8, %9
%11 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %0
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = sext i32 %16 to i64
%18 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i64 %17)
store i32 1, i32* %1
br label %87
19: ; preds = %0
%20 = call i64 (...) @getpagesize()
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @PROT_WRITE
%23 = or i32 %21, %22
%24 = load i32, i32* @MAP_SHARED
%25 = load i32, i32* %2
%26 = call i8* @mmap(i32* null, i64 %20, i32 %23, i32 %24, i32 %25, i32 -28672)
store i8* %26, i8** %5
%27 = load i8*, i8** %5
%28 = load i8*, i8** @MAP_FAILED
%29 = icmp eq i8* %27, %28
br i1 %29, label %30, label %35
30: ; preds = %19
%31 = load i32, i32* @stderr
%32 = call i64 (...) @getpagesize()
%33 = load i32, i32* @errno
%34 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i64 %32, i32 -28672, i32 %33)
store i32 1, i32* %1
br label %87
35: ; preds = %19
store i32 0, i32* %7
br label %36
36: ; preds = %35, %86
%37 = load i8*, i8** %5
%38 = getelementptr inbounds i8, i8* %37, i64 0
%39 = bitcast i8* %38 to i32*
%40 = load volatile i32, i32* %39
store i32 %40, i32* %4
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %86
42: ; preds = %36
%43 = load i8*, i8** %5
%44 = getelementptr inbounds i8, i8* %43, i64 4
%45 = bitcast i8* %44 to i32*
%46 = load volatile i32, i32* %45
store i32 %46, i32* %3
%47 = load i32, i32* %4
%48 = icmp sgt i32 %47, 1
br i1 %48, label %49, label %55
49: ; preds = %42
%50 = load i8*, i8** %5
%51 = getelementptr inbounds i8, i8* %50, i64 4
%52 = ptrtoint i8* %51 to i32
%53 = load i32, i32* %3
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %52, i32 %53)
br label %82
55: ; preds = %42
%56 = load i32, i32* %7
%57 = icmp slt i32 %56, 256
br i1 %57, label %58, label %65
58: ; preds = %55
%59 = load i32, i32* %3
%60 = trunc i32 %59 to i8
%61 = load i32, i32* %7
%62 = add nsw i32 %61, 1
store i32 %62, i32* %7
%63 = sext i32 %61 to i64
%64 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %63
store i8 %60, i8* %64
br label %65
65: ; preds = %58, %55
%66 = load i32, i32* %3
%67 = icmp eq i32 %66, 10
br i1 %67, label %68, label %81
68: ; preds = %65
%69 = load i32, i32* %7
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %76
71: ; preds = %68
%72 = load i32, i32* %7
%73 = sub nsw i32 %72, 1
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %74
store i8 0, i8* %75
br label %78
76: ; preds = %68
%77 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
store i8 0, i8* %77
br label %78
78: ; preds = %76, %71
%79 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %79)
store i32 0, i32* %7
br label %81
81: ; preds = %78, %65
br label %82
82: ; preds = %81, %49
%83 = load i8*, i8** %5
%84 = getelementptr inbounds i8, i8* %83, i64 0
%85 = bitcast i8* %84 to i32*
store volatile i32 0, i32* %85
br label %86
86: ; preds = %82, %36
br label %36
87: ; preds = %30, %14
%88 = load i32, i32* %1
ret i32 %88
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@stderr = global i32 0
@.str.1 = [28 x i8] c"open(/dev/mem) failed (%d)\0A\00"
@errno = 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 = [31 x i8] c"mmap64(0x%x@0x%x) failed (%d)\0A\00"
@.str.3 = [23 x i8] c"CPU1: 0x%08x = 0x%08x\0A\00"
@.str.4 = [10 x i8] c"CPU1: %s\0A\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_SYNC
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = sext i32 %10 to i64
%12 = tail call i32 (i32, i8*, i64, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i64 %11)
br label %64
13: ; preds = %0
%14 = tail call i64 (...) @getpagesize()
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @PROT_WRITE
%17 = or i32 %16, %15
%18 = load i32, i32* @MAP_SHARED
%19 = tail call i8* @mmap(i32* null, i64 %14, i32 %17, i32 %18, i32 %6, i32 -28672)
%20 = load i8*, i8** @MAP_FAILED
%21 = icmp eq i8* %19, %20
br i1 %21, label %30, label %22
22: ; preds = %13
%23 = bitcast i8* %19 to i32*
%24 = getelementptr inbounds i8, i8* %19, i64 4
%25 = bitcast i8* %24 to i32*
%26 = ptrtoint i8* %24 to i64
%27 = trunc i64 %26 to i32
br label %28
28: ; preds = %62, %22
%29 = phi i32 [ %63, %62 ], [ 0, %22 ]
br label %35
30: ; preds = %13
%31 = load i32, i32* @stderr
%32 = tail call i64 (...) @getpagesize()
%33 = load i32, i32* @errno
%34 = tail call i32 (i32, i8*, i64, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i64 %32, i32 -28672, i32 %33)
br label %64
35: ; preds = %28, %35
%36 = load volatile i32, i32* %23
%37 = icmp eq i32 %36, 0
br i1 %37, label %35, label %38
38: ; preds = %35
%39 = load volatile i32, i32* %25
%40 = icmp sgt i32 %36, 1
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %27, i32 %39)
br label %62
43: ; preds = %38
%44 = icmp slt i32 %29, 256
br i1 %44, label %45, label %50
45: ; preds = %43
%46 = trunc i32 %39 to i8
%47 = add nsw i32 %29, 1
%48 = sext i32 %29 to i64
%49 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %48
store i8 %46, i8* %49
br label %50
50: ; preds = %45, %43
%51 = phi i32 [ %47, %45 ], [ %29, %43 ]
%52 = icmp eq i32 %39, 10
br i1 %52, label %53, label %62
53: ; preds = %50
%54 = icmp eq i32 %51, 0
br i1 %54, label %59, label %55
55: ; preds = %53
%56 = add nsw i32 %51, -1
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %57
store i8 0, i8* %58
br label %60
59: ; preds = %53
store i8 0, i8* %2
br label %60
60: ; preds = %59, %55
%61 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %2)
br label %62
62: ; preds = %50, %60, %41
%63 = phi i32 [ %29, %41 ], [ 0, %60 ], [ %51, %50 ]
store volatile i32 0, i32* %23
br label %28
64: ; preds = %30, %8
ret i32 1
}