#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 int /*<<< orphan*/ IRQEnable;

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ fclose(int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/ *, unsigned int, int, int /*<<< orphan*/, int,
           unsigned int);
int /*<<< orphan*/ munmap(void *, unsigned int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, int *, int);
int /*<<< orphan*/ stdout;
unsigned int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, int *, int);
int main(int argc, char *argv[]) {
  printf("OPEN Device\n");
  int fd = open("/dev/uio0", O_RDWR);
  if (fd < 1) {
    perror(argv[0]);
    return -1;
  }
  unsigned pageSize = sysconf(_SC_PAGESIZE);
  printf("Map Memory\n");
  unsigned offset = pageSize * (1 - 1);
  void *ptr;
  ptr = mmap(NULL, pageSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
  int IRQEnable = 1;
  write(fd, &IRQEnable, sizeof(IRQEnable));
  printf("WAIT for Interrupt\n");
  while (1) {
    read(fd, &IRQEnable, sizeof(IRQEnable));
    printf("READ: left channel: %d \n", *((unsigned *)(ptr + 0)));
    printf("READ: right channel: %d \n", *((unsigned *)(ptr + 4)));
    int IRQEnable = 1;
    write(fd, &IRQEnable, sizeof(IRQEnable));
  }
  printf("DEBUG_USERSPACE : IRQ\n");
  printf("Interrupt count: = %d \n", IRQEnable);
  munmap(ptr, pageSize);
  fclose(stdout);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
lea    0xe69(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    11cd <main+0x5d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
add    $0x30,%rsp
pop    %rbp
ret
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %eax,-0x18(%rbp)
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
imul   $0x0,-0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%esi
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
mov    0x2dba(%rip),%rax
or     (%rax),%edx
mov    0x2dd9(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x1c(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x30(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
jmp    124c <main+0xdc>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
lea    0xea8(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    0xe56(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
jle    1280 <main+0x100>
mov    %eax,%ebp
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
mov    %eax,%ebx
lea    0xe79(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e1c(%rip),%rax
mov    0x2ddd(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dfa(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
movl   $0x1,0x4(%rsp)
lea    0x4(%rsp),%r14
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1040 <write@plt>
lea    0xe2f(%rip),%rdi
call   1030 <puts@plt>
lea    0xdd9(%rip),%r15
lea    0xdeb(%rip),%r12
mov    %rsp,%r13
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1070 <read@plt>
mov    (%rbx),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x4(%rbx),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
movl   $0x1,(%rsp)
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x4,%edx
call   1040 <write@plt>
jmp    1240 <main+0xc0>
mov    (%rbx),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"OPEN Device\0A\00"
@.str.1 = [10 x i8] c"/dev/uio0\00"
@O_RDWR = global i32 0
@_SC_PAGESIZE = global i32 0
@.str.2 = [12 x i8] c"Map Memory\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [20 x i8] c"WAIT for Interrupt\0A\00"
@.str.4 = [25 x i8] c"READ: left channel: %d \0A\00"
@.str.5 = [26 x i8] c"READ: right channel: %d \0A\00"
@stdout = global i32 0
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 i8*
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @O_RDWR
%14 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 1
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 @perror(i8* %20)
ret i32 -1
22: ; preds = %2
%23 = load i32, i32* @_SC_PAGESIZE
%24 = call i32 @sysconf(i32 %23)
store i32 %24, i32* %7
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* %7
%27 = mul i32 %26, 0
store i32 %27, i32* %8
%28 = load i32, i32* %7
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @PROT_WRITE
%31 = or i32 %29, %30
%32 = load i32, i32* @MAP_SHARED
%33 = load i32, i32* %6
%34 = load i32, i32* %8
%35 = call i8* @mmap(i32* null, i32 %28, i32 %31, i32 %32, i32 %33, i32 %34)
store i8* %35, i8** %9
store i32 1, i32* %10
%36 = load i32, i32* %6
%37 = call i32 @write(i32 %36, i32* %10, i32 4)
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %22, %39
%40 = load i32, i32* %6
%41 = call i32 @read(i32 %40, i32* %10, i32 4)
%42 = load i8*, i8** %9
%43 = getelementptr i8, i8* %42, i64 0
%44 = bitcast i8* %43 to i32*
%45 = load i32, i32* %44
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %45)
%47 = load i8*, i8** %9
%48 = getelementptr i8, i8* %47, i64 4
%49 = bitcast i8* %48 to i32*
%50 = load i32, i32* %49
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %50)
store i32 1, i32* %11
%52 = load i32, i32* %6
%53 = call i32 @write(i32 %52, i32* %11, i32 4)
br label %39
}

LLVM IR (-O2):
@.str.1 = [10 x i8] c"/dev/uio0\00"
@O_RDWR = global i32 0
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.4 = [25 x i8] c"READ: left channel: %d \0A\00"
@.str.5 = [26 x i8] c"READ: right channel: %d \0A\00"
@stdout = global i32 0
@str = [12 x i8] c"OPEN Device\00"
@str.6 = [11 x i8] c"Map Memory\00"
@str.7 = [19 x i8] c"WAIT for Interrupt\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%6 = load i32, i32* @O_RDWR
%7 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 1
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i8*, i8** %1
%11 = tail call i32 @perror(i8* %10)
ret i32 -1
12: ; preds = %2
%13 = load i32, i32* @_SC_PAGESIZE
%14 = tail call i32 @sysconf(i32 %13)
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.6, i64 0, i64 0))
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @PROT_WRITE
%18 = or i32 %17, %16
%19 = load i32, i32* @MAP_SHARED
%20 = tail call i8* @mmap(i32* null, i32 %14, i32 %18, i32 %19, i32 %7, i32 0)
%21 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%22 = call i32 @write(i32 %7, i32* %3, i32 4)
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.7, i64 0, i64 0))
%24 = bitcast i8* %20 to i32*
%25 = getelementptr i8, i8* %20, i64 4
%26 = bitcast i8* %25 to i32*
%27 = bitcast i32* %4 to i8*
br label %28
28: ; preds = %28, %12
%29 = call i32 @read(i32 %7, i32* %3, i32 4)
%30 = load i32, i32* %24
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %30)
%32 = load i32, i32* %26
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %32)
store i32 1, i32* %4
%34 = call i32 @write(i32 %7, i32* %4, i32 4)
br label %28
}