#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 /*<<< orphan*/ 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 /*<<< orphan*/);
int /*<<< orphan*/ toggle_io(unsigned char volatile *);
int main(int argc, char *argv[]) {
  int fd;
  int page_size = getpagesize();
  unsigned char *mem_addr = NULL;
  unsigned int mem_phys = 0x72000000;
  volatile unsigned char *port_ctrl;
  volatile unsigned char *port;
  volatile unsigned char *port_dir;
  fd = open("/dev/mem", O_RDWR | O_SYNC);
  if (fd < 0) {
    printf("Open /dev/mem failed - %s\n", strerror(errno));
    return -1;
  }
  mem_addr = (char *)mmap(0, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
                          mem_phys);
  if (mem_addr == (unsigned char *)MAP_FAILED) {
    printf("Error: mmap failed\n");
    close(fd);
    return -1;
  }
  port_ctrl = mem_addr + 0x40;
  port_dir = mem_addr + 0x41;
  port = mem_addr + 0x42;
  *port_ctrl = 0;
  *port_dir = 0xFF;
  printf("Toggling XDIO#1, use Ctrl-C to exit\n");
  toggle_io(port);
  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    $0x0,%al
call   1080 <getpagesize@plt>
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x72000000,-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    11f5 <main+0x85>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <main+0x13f>
mov    -0x18(%rbp),%esi
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
mov    0x2db0(%rip),%rax
or     (%rax),%edx
mov    0x2dd7(%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    0x2d7f(%rip),%rcx
mov    (%rcx),%rcx
cmp    %rcx,%rax
jne    125f <main+0xef>
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <main+0x13f>
mov    -0x20(%rbp),%rax
add    $0x40,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
add    $0x41,%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
add    $0x42,%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x40(%rbp),%rax
movb   $0xff,(%rax)
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <toggle_io@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   1090 <getpagesize@plt>
mov    %eax,%r14d
mov    0x2e43(%rip),%rax
mov    0x2e34(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe59(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1213 <main+0x93>
mov    %eax,%ebp
mov    0x2e3f(%rip),%rax
mov    0x2df8(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e1d(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    %r14d,%esi
mov    %ebp,%r8d
mov    $0x72000000,%r9d
call   1050 <mmap@plt>
mov    0x2dc7(%rip),%rcx
cmp    (%rcx),%rax
je     1234 <main+0xb4>
mov    %rax,%rbp
add    $0x42,%rbp
movb   $0x0,0x40(%rax)
movb   $0xff,0x41(%rax)
lea    0xe20(%rip),%rdi
call   1030 <puts@plt>
mov    %rbp,%rdi
call   1040 <toggle_io@plt>
jmp    124c <main+0xcc>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xde1(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1247 <main+0xc7>
lea    0xe0d(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%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 = [27 x i8] c"Open /dev/mem failed - %s\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 = [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 i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = call i32 (...) @getpagesize()
store i32 %13, i32* %7
store i8* null, i8** %8
store i32 1912602624, i32* %9
%14 = load i32, i32* @O_RDWR
%15 = load i32, i32* @O_SYNC
%16 = or i32 %14, %15
%17 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %2
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %22)
store i32 -1, i32* %3
br label %54
24: ; preds = %2
%25 = load i32, i32* %7
%26 = load i32, i32* @PROT_READ
%27 = load i32, i32* @PROT_WRITE
%28 = or i32 %26, %27
%29 = load i32, i32* @MAP_SHARED
%30 = load i32, i32* %6
%31 = load i32, i32* %9
%32 = call i64 @mmap(i32 0, i32 %25, i32 %28, i32 %29, i32 %30, i32 %31)
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %8
%34 = load i8*, i8** %8
%35 = load i64, i64* @MAP_FAILED
%36 = inttoptr i64 %35 to i8*
%37 = icmp eq i8* %34, %36
br i1 %37, label %38, label %42
38: ; preds = %24
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%40 = load i32, i32* %6
%41 = call i32 @close(i32 %40)
store i32 -1, i32* %3
br label %54
42: ; preds = %24
%43 = load i8*, i8** %8
%44 = getelementptr inbounds i8, i8* %43, i64 64
store i8* %44, i8** %10
%45 = load i8*, i8** %8
%46 = getelementptr inbounds i8, i8* %45, i64 65
store i8* %46, i8** %12
%47 = load i8*, i8** %8
%48 = getelementptr inbounds i8, i8* %47, i64 66
store i8* %48, i8** %11
%49 = load i8*, i8** %10
store volatile i8 0, i8* %49
%50 = load i8*, i8** %12
store volatile i8 -1, i8* %50
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%52 = load i8*, i8** %11
%53 = call i32 @toggle_io(i8* %52)
store i32 0, i32* %3
br label %54
54: ; preds = %42, %38, %20
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [27 x i8] c"Open /dev/mem failed - %s\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 = [36 x i8] c"Toggling XDIO, use Ctrl-C to exit\00"
@str.4 = [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 %13
9: ; preds = %2
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %11)
br label %32
13: ; preds = %2
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_WRITE
%16 = or i32 %15, %14
%17 = load i32, i32* @MAP_SHARED
%18 = tail call i64 @mmap(i32 0, i32 %3, i32 %16, i32 %17, i32 %7, i32 1912602624)
%19 = inttoptr i64 %18 to i8*
%20 = load i64, i64* @MAP_FAILED
%21 = inttoptr i64 %20 to i8*
%22 = icmp eq i8* %19, %21
br i1 %22, label %23, label %26
23: ; preds = %13
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.4, i64 0, i64 0))
%25 = tail call i32 @close(i32 %7)
br label %32
26: ; preds = %13
%27 = getelementptr inbounds i8, i8* %19, i64 64
%28 = getelementptr inbounds i8, i8* %19, i64 65
%29 = getelementptr inbounds i8, i8* %19, i64 66
store volatile i8 0, i8* %27
store volatile i8 -1, i8* %28
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%31 = tail call i32 @toggle_io(i8* %29)
br label %32
32: ; preds = %26, %23, %9
%33 = phi i32 [ -1, %9 ], [ -1, %23 ], [ 0, %26 ]
ret i32 %33
}