#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 /*<<< orphan*/ MAP_SHARED;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
int atoi(char *);
int /*<<< orphan*/ close(int);
scalar_t__ mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ munmap(void *, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  int fd = open("/dev/mem", (O_RDWR | O_SYNC));
  if (fd < 1) {
    perror("error opening /dev/mem\n");
    close(fd);
    return -1;
  }
  printf("/dev/mem opened successfully!\n");
  void *virtualBase = (void *)mmap(NULL, (0x00001000), (PROT_READ | PROT_WRITE),
                                   MAP_SHARED, fd, (0xff200000));
  printf("virtualBase = 0x%x\n", (unsigned int)virtualBase);
  printf("read sys id:\n");
  int *sysIDPtr = (int *)(virtualBase + (0x00000800) + (0x00000200));
  printf("0x%x\n", *sysIDPtr);
  int *fringeAddrPtr = (int *)(virtualBase + (0x00000800));
  printf("%d, %s\n", argc, argv[1]);
  *(fringeAddrPtr + 2) = argc > 1 ? atoi(argv[1]) : 100;
  *(fringeAddrPtr) = 1;
  while (1) {
    int done = *(fringeAddrPtr + 1);
    printf("done = %d\n", done);
    if (done)
      break;
  }
  int result = *(fringeAddrPtr + 3);
  printf("result = %d\n", result);
  munmap(virtualBase, (0x00001000));
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
mov    0x2e3a(%rip),%rax
or     (%rax),%esi
lea    0xe61(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    11d1 <main+0x61>
lea    0xe51(%rip),%rdi
call   1080 <perror@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132e <main+0x1be>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
mov    0x2dd1(%rip),%rax
or     (%rax),%edx
mov    0x2df0(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1000,%esi
mov    $0xff200000,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %eax,%esi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
add    $0x800,%rax
add    $0x200,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
add    $0x800,%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x1,-0x8(%rbp)
jle    12a6 <main+0x136>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x3c(%rbp)
jmp    12b3 <main+0x143>
mov    $0x64,%eax
mov    %eax,-0x3c(%rbp)
jmp    12b3 <main+0x143>
mov    -0x3c(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x30(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xd8f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x34(%rbp)
je     12f1 <main+0x181>
jmp    12f6 <main+0x186>
jmp    12c7 <main+0x157>
mov    -0x30(%rbp),%rax
mov    0xc(%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%esi
lea    0xd6b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    $0x1000,%esi
call   1060 <munmap@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2e44(%rip),%rax
mov    0x2e35(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe5a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jle    12c0 <main+0x140>
lea    0xe98(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e2f(%rip),%rax
mov    0x2df0(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e0d(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %r15d,%r8d
mov    $0xff200000,%r9d
call   1040 <mmap@plt>
mov    %rax,%r12
lea    0xe25(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe66(%rip),%rdi
call   1030 <puts@plt>
mov    0xa00(%r12),%esi
lea    0xe0e(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%r14),%rdx
lea    0xe02(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x64,%eax
cmp    $0x2,%ebp
jl     124f <main+0xcf>
mov    0x8(%r14),%rdi
call   10a0 <atoi@plt>
mov    %eax,0x808(%r12)
movl   $0x1,0x800(%r12)
lea    0xdd3(%rip),%rbp
nopw   0x0(%rax,%rax,1)
mov    0x804(%r12),%ebx
mov    %rbp,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebx,%ebx
je     1270 <main+0xf0>
mov    0x80c(%r12),%esi
lea    0xdb1(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12,%rdi
mov    $0x1000,%esi
call   1070 <munmap@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd42(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebp
jmp    12ad <main+0x12d>

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [24 x i8] c"error opening /dev/mem\0A\00"
@.str.2 = [31 x i8] c"/dev/mem opened successfully!\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [20 x i8] c"virtualBase = 0x%x\0A\00"
@.str.4 = [14 x i8] c"read sys id:\0A\00"
@.str.5 = [6 x i8] c"0x%x\0A\00"
@.str.6 = [8 x i8] c"%d, %s\0A\00"
@.str.7 = [11 x i8] c"done = %d\0A\00"
@.str.8 = [13 x i8] c"result = %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_SYNC
%14 = or i32 %12, %13
%15 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 1
br i1 %17, label %18, label %22
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* %6
%21 = call i32 @close(i32 %20)
store i32 -1, i32* %3
br label %83
22: ; preds = %2
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* @PROT_READ
%25 = load i32, i32* @PROT_WRITE
%26 = or i32 %24, %25
%27 = load i32, i32* @MAP_SHARED
%28 = load i32, i32* %6
%29 = call i64 @mmap(i32* null, i32 4096, i32 %26, i32 %27, i32 %28, i32 -14680064)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %7
%31 = load i8*, i8** %7
%32 = ptrtoint i8* %31 to i32
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %32)
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%35 = load i8*, i8** %7
%36 = getelementptr i8, i8* %35, i64 2048
%37 = getelementptr i8, i8* %36, i64 512
%38 = bitcast i8* %37 to i32*
store i32* %38, i32** %8
%39 = load i32*, i32** %8
%40 = load i32, i32* %39
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %40)
%42 = load i8*, i8** %7
%43 = getelementptr i8, i8* %42, i64 2048
%44 = bitcast i8* %43 to i32*
store i32* %44, i32** %9
%45 = load i32, i32* %4
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = load i8*, i8** %47
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 %45, i8* %48)
%50 = load i32, i32* %4
%51 = icmp sgt i32 %50, 1
br i1 %51, label %52, label %57
52: ; preds = %22
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 1
%55 = load i8*, i8** %54
%56 = call i32 @atoi(i8* %55)
br label %58
57: ; preds = %22
br label %58
58: ; preds = %57, %52
%59 = phi i32 [ %56, %52 ], [ 100, %57 ]
%60 = load i32*, i32** %9
%61 = getelementptr inbounds i32, i32* %60, i64 2
store i32 %59, i32* %61
%62 = load i32*, i32** %9
store i32 1, i32* %62
br label %63
63: ; preds = %58, %72
%64 = load i32*, i32** %9
%65 = getelementptr inbounds i32, i32* %64, i64 1
%66 = load i32, i32* %65
store i32 %66, i32* %10
%67 = load i32, i32* %10
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i32 %67)
%69 = load i32, i32* %10
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %63
br label %73
72: ; preds = %63
br label %63
73: ; preds = %71
%74 = load i32*, i32** %9
%75 = getelementptr inbounds i32, i32* %74, i64 3
%76 = load i32, i32* %75
store i32 %76, i32* %11
%77 = load i32, i32* %11
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0), i32 %77)
%79 = load i8*, i8** %7
%80 = call i32 @munmap(i8* %79, i32 4096)
%81 = load i32, i32* %6
%82 = call i32 @close(i32 %81)
store i32 0, i32* %3
br label %83
83: ; preds = %73, %18
%84 = load i32, i32* %3
ret i32 %84
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [24 x i8] c"error opening /dev/mem\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [20 x i8] c"virtualBase = 0x%x\0A\00"
@.str.5 = [6 x i8] c"0x%x\0A\00"
@.str.6 = [8 x i8] c"%d, %s\0A\00"
@.str.7 = [11 x i8] c"done = %d\0A\00"
@.str.8 = [13 x i8] c"result = %d\0A\00"
@str = [30 x i8] c"/dev/mem opened successfully!\00"
@str.9 = [13 x i8] c"read sys id:\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%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, 1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %50
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @PROT_WRITE
%14 = or i32 %13, %12
%15 = load i32, i32* @MAP_SHARED
%16 = tail call i64 @mmap(i32* null, i32 4096, i32 %14, i32 %15, i32 %6, i32 -14680064)
%17 = inttoptr i64 %16 to i8*
%18 = trunc i64 %16 to i32
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.9, i64 0, i64 0))
%21 = getelementptr i8, i8* %17, i64 2048
%22 = getelementptr i8, i8* %17, i64 2560
%23 = bitcast i8* %22 to i32*
%24 = load i32, i32* %23
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %24)
%26 = bitcast i8* %21 to i32*
%27 = getelementptr inbounds i8*, i8** %1, i64 1
%28 = load i8*, i8** %27
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 %0, i8* %28)
%30 = icmp sgt i32 %0, 1
br i1 %30, label %31, label %34
31: ; preds = %10
%32 = load i8*, i8** %27
%33 = tail call i32 @atoi(i8* %32)
br label %34
34: ; preds = %10, %31
%35 = phi i32 [ %33, %31 ], [ 100, %10 ]
%36 = getelementptr i8, i8* %17, i64 2056
%37 = bitcast i8* %36 to i32*
store i32 %35, i32* %37
store i32 1, i32* %26
%38 = getelementptr i8, i8* %17, i64 2052
%39 = bitcast i8* %38 to i32*
br label %40
40: ; preds = %40, %34
%41 = load i32, i32* %39
%42 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i32 %41)
%43 = icmp eq i32 %41, 0
br i1 %43, label %40, label %44
44: ; preds = %40
%45 = getelementptr i8, i8* %17, i64 2060
%46 = bitcast i8* %45 to i32*
%47 = load i32, i32* %46
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0), i32 %47)
%49 = tail call i32 @munmap(i8* %17, i32 4096)
br label %50
50: ; preds = %44, %8
%51 = phi i32 [ -1, %8 ], [ 0, %44 ]
%52 = tail call i32 @close(i32 %6)
ret i32 %51
}