#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 */
scalar_t__ 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 getpagesize();
scalar_t__ mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
                unsigned int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int);
int /*<<< orphan*/ toggle_io(unsigned int volatile *);
int main(int argc, char *argv[]) {
  int fd;
  int page_size = getpagesize();
  unsigned int *mem_addr = NULL;
  unsigned int mem_phys = 0x72A00000;
  volatile unsigned int *vga_reg;
  volatile unsigned int *enable_vga;
  volatile unsigned int *dio2_reg;
  volatile unsigned int *enable_dio2;
  volatile unsigned int *misc_reg;
  volatile unsigned int *enable_misc;
  fd = open("/dev/mem", O_RDWR | O_SYNC);
  if (fd < 0) {
    printf("Open /dev/mem failed - %s %d\n", strerror(errno), errno);
    return -1;
  }
  mem_addr = (int *)mmap(0, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
                         mem_phys);
  if (mem_addr == (unsigned int *)MAP_FAILED) {
    printf("Error: mmap failed\n");
    close(fd);
    return -1;
  }
  printf("%x: %x \n", (int)mem_addr, *(mem_addr));
  vga_reg = mem_addr + 0x00;
  enable_vga = mem_addr + 0x01;
  dio2_reg = mem_addr + 0x02;
  enable_dio2 = mem_addr + 0x03;
  misc_reg = mem_addr + 0x04;
  enable_misc = mem_addr + 0x05;
  *enable_vga = 0x00007FFF;
  *enable_dio2 = 0x0000FDFF;
  *enable_misc = 0x0000000F;
  printf("Toggling XDIO#1, use Ctrl-C to exit\n");
  toggle_io(misc_reg);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1080 <getpagesize@plt>
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x72a00000,-0x24(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    0x2e21(%rip),%rax
or     (%rax),%esi
lea    0xe48(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11fe <main+0x8e>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1302 <main+0x192>
mov    -0x18(%rbp),%esi
mov    0x2df0(%rip),%rax
mov    (%rax),%edx
mov    0x2da7(%rip),%rax
or     (%rax),%edx
mov    0x2dce(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x24(%rbp),%r9d
xor    %edi,%edi
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d76(%rip),%rcx
mov    (%rcx),%rcx
cmp    %rcx,%rax
jne    1268 <main+0xf8>
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1302 <main+0x192>
mov    -0x20(%rbp),%rax
mov    %eax,%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
add    $0xc,%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rax
add    $0x14,%rax
mov    %rax,-0x58(%rbp)
mov    -0x38(%rbp),%rax
movl   $0x7fff,(%rax)
mov    -0x48(%rbp),%rax
movl   $0xfdff,(%rax)
mov    -0x58(%rbp),%rax
movl   $0xf,(%rax)
lea    0xd59(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x50(%rbp),%rdi
call   1030 <toggle_io@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   1090 <getpagesize@plt>
mov    %eax,%ebx
mov    0x2e44(%rip),%rax
mov    0x2e35(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe5a(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1238 <main+0xb8>
mov    %eax,%ebp
mov    0x2e3c(%rip),%rax
mov    0x2df5(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e1a(%rip),%rax
mov    (%rax),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    %ebx,%esi
mov    %ebp,%r8d
mov    $0x72a00000,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    0x2dc1(%rip),%rax
cmp    (%rax),%rbx
je     125b <main+0xdb>
mov    (%rbx),%edx
lea    0xe2a(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rbp
add    $0x10,%rbp
movl   $0x7fff,0x4(%rbx)
movl   $0xfdff,0xc(%rbx)
movl   $0xf,0x14(%rbx)
lea    0xe07(%rip),%rdi
call   1030 <puts@plt>
mov    %rbp,%rdi
call   1040 <toggle_io@plt>
jmp    1274 <main+0xf4>
mov    0x2d81(%rip),%rbx
mov    (%rbx),%edi
call   10a0 <strerror@plt>
mov    (%rbx),%edx
lea    0xdba(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    126e <main+0xee>
lea    0xdf2(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
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 = [30 x i8] c"Open /dev/mem failed - %s %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 i64 0
@.str.2 = [20 x i8] c"Error: mmap failed\0A\00"
@.str.3 = [9 x i8] c"%x: %x \0A\00"
@.str.4 = [37 x i8] c"Toggling XDIO, use Ctrl-C to exit\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 i32*
%14 = alloca i32*
%15 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = call i32 (...) @getpagesize()
store i32 %16, i32* %7
store i32* null, i32** %8
store i32 1923088384, i32* %9
%17 = load i32, i32* @O_RDWR
%18 = load i32, i32* @O_SYNC
%19 = or i32 %17, %18
%20 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %2
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = load i32, i32* @errno
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %25, i32 %26)
store i32 -1, i32* %3
br label %70
28: ; preds = %2
%29 = load i32, i32* %7
%30 = load i32, i32* @PROT_READ
%31 = load i32, i32* @PROT_WRITE
%32 = or i32 %30, %31
%33 = load i32, i32* @MAP_SHARED
%34 = load i32, i32* %6
%35 = load i32, i32* %9
%36 = call i64 @mmap(i32 0, i32 %29, i32 %32, i32 %33, i32 %34, i32 %35)
%37 = inttoptr i64 %36 to i32*
store i32* %37, i32** %8
%38 = load i32*, i32** %8
%39 = load i64, i64* @MAP_FAILED
%40 = inttoptr i64 %39 to i32*
%41 = icmp eq i32* %38, %40
br i1 %41, label %42, label %46
42: ; preds = %28
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%44 = load i32, i32* %6
%45 = call i32 @close(i32 %44)
store i32 -1, i32* %3
br label %70
46: ; preds = %28
%47 = load i32*, i32** %8
%48 = ptrtoint i32* %47 to i32
%49 = load i32*, i32** %8
%50 = load i32, i32* %49
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %48, i32 %50)
%52 = load i32*, i32** %8
%53 = getelementptr inbounds i32, i32* %52, i64 0
store i32* %53, i32** %10
%54 = load i32*, i32** %8
%55 = getelementptr inbounds i32, i32* %54, i64 1
store i32* %55, i32** %11
%56 = load i32*, i32** %8
%57 = getelementptr inbounds i32, i32* %56, i64 2
store i32* %57, i32** %12
%58 = load i32*, i32** %8
%59 = getelementptr inbounds i32, i32* %58, i64 3
store i32* %59, i32** %13
%60 = load i32*, i32** %8
%61 = getelementptr inbounds i32, i32* %60, i64 4
store i32* %61, i32** %14
%62 = load i32*, i32** %8
%63 = getelementptr inbounds i32, i32* %62, i64 5
store i32* %63, i32** %15
%64 = load i32*, i32** %11
store volatile i32 32767, i32* %64
%65 = load i32*, i32** %13
store volatile i32 65023, i32* %65
%66 = load i32*, i32** %15
store volatile i32 15, i32* %66
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%68 = load i32*, i32** %14
%69 = call i32 @toggle_io(i32* %68)
store i32 0, i32* %3
br label %70
70: ; preds = %46, %42, %23
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [30 x i8] c"Open /dev/mem failed - %s %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 i64 0
@.str.3 = [9 x i8] c"%x: %x \0A\00"
@str = [36 x i8] c"Toggling XDIO, use Ctrl-C to exit\00"
@str.5 = [19 x i8] c"Error: mmap failed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @getpagesize()
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_SYNC
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = load i32, i32* @errno
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %11, i32 %12)
br label %37
14: ; preds = %2
%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 i64 @mmap(i32 0, i32 %3, i32 %17, i32 %18, i32 %7, i32 1923088384)
%20 = inttoptr i64 %19 to i32*
%21 = load i64, i64* @MAP_FAILED
%22 = inttoptr i64 %21 to i32*
%23 = icmp eq i32* %20, %22
br i1 %23, label %24, label %27
24: ; preds = %14
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.5, i64 0, i64 0))
%26 = tail call i32 @close(i32 %7)
br label %37
27: ; preds = %14
%28 = trunc i64 %19 to i32
%29 = load i32, i32* %20
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %28, i32 %29)
%31 = getelementptr inbounds i32, i32* %20, i64 1
%32 = getelementptr inbounds i32, i32* %20, i64 3
%33 = getelementptr inbounds i32, i32* %20, i64 4
%34 = getelementptr inbounds i32, i32* %20, i64 5
store volatile i32 32767, i32* %31
store volatile i32 65023, i32* %32
store volatile i32 15, i32* %34
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%36 = tail call i32 @toggle_io(i32* %33)
br label %37
37: ; preds = %27, %24, %9
%38 = phi i32 [ -1, %9 ], [ -1, %24 ], [ 0, %27 ]
ret i32 %38
}