#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;
void *mmap(int /*<<< orphan*/ *, unsigned int, int, int /*<<< orphan*/, int,
           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);
unsigned int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, int *, int);
void *copy_thread() {
  printf("copy thread started\n");
  int fd_src = open("/dev/uio0", O_RDWR);
  if (fd_src < 1) {
    perror("Error open /dev/uio0/");
  }
  int fd_dst1 = open("/dev/mem", O_RDWR);
  if (fd_dst1 < 1) {
    perror("Error open /dev/mem/");
  }
  unsigned pageSize = sysconf(_SC_PAGESIZE);
  void *ptr_src;
  ptr_src = mmap(NULL, pageSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd_src, 0);
  void *ptr_dst1;
  ptr_dst1 = mmap(NULL, pageSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd_dst1,
                  0x43C20000);
  int IRQEnable = 1;
  write(fd_src, &IRQEnable, sizeof(IRQEnable));
  while (1) {
    read(fd_src, &IRQEnable, sizeof(IRQEnable));
    *((unsigned *)(ptr_dst1 + 0)) = *((unsigned *)(ptr_src + 0));
    *((unsigned *)(ptr_dst1 + 4)) = *((unsigned *)(ptr_src + 4));
    int IRQEnable = 1;
    write(fd_src, &IRQEnable, sizeof(IRQEnable));
  }
  munmap(ptr_src, pageSize);
  munmap(ptr_dst1, pageSize);
}


x86 assembly (clang, -O0):
<copy_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe81(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x1,-0x4(%rbp)
jge    11b2 <copy_thread+0x42>
lea    0xe72(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
lea    0xe73(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x1,-0x8(%rbp)
jge    11e0 <copy_thread+0x70>
lea    0xe63(%rip),%rdi
call   1080 <perror@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
mov    0x2dbc(%rip),%rax
or     (%rax),%edx
mov    0x2ddb(%rip),%rax
mov    (%rax),%ecx
mov    -0x4(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%esi
mov    0x2dcb(%rip),%rax
mov    (%rax),%edx
mov    0x2d8a(%rip),%rax
or     (%rax),%edx
mov    0x2da9(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x43c20000,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x4(%rax)
movl   $0x1,-0x28(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
jmp    1270 <copy_thread+0x100>

x86 assembly (clang, -O2):
<copy_thread>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xebc(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e4a(%rip),%rbx
mov    (%rbx),%esi
lea    0xe69(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jle    1284 <copy_thread+0x114>
mov    (%rbx),%esi
lea    0xe70(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
jle    1295 <copy_thread+0x125>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %eax,%ebx
mov    0x2e21(%rip),%rax
mov    0x2de2(%rip),%r13
mov    0x0(%r13),%edx
or     (%rax),%edx
mov    0x2dfd(%rip),%r12
mov    (%r12),%ecx
xor    %edi,%edi
mov    %ebx,%esi
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbp
mov    0x0(%r13),%edx
mov    0x2dec(%rip),%rax
or     (%rax),%edx
mov    (%r12),%ecx
xor    %edi,%edi
mov    %ebx,%esi
mov    %r14d,%r8d
mov    $0x43c20000,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
movl   $0x1,0x4(%rsp)
lea    0x4(%rsp),%r14
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    %rsp,%r12
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    0x0(%rbp),%eax
mov    %eax,(%rbx)
mov    0x4(%rbp),%eax
mov    %eax,0x4(%rbx)
movl   $0x1,(%rsp)
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x4,%edx
call   1040 <write@plt>
jmp    1250 <copy_thread+0xe0>
lea    0xd7f(%rip),%rdi
call   1080 <perror@plt>
jmp    11a7 <copy_thread+0x37>
lea    0xd8d(%rip),%rdi
call   1080 <perror@plt>
jmp    11c0 <copy_thread+0x50>

LLVM IR (-O0):
@.str = [21 x i8] c"copy thread started\0A\00"
@.str.1 = [10 x i8] c"/dev/uio0\00"
@O_RDWR = global i32 0
@.str.2 = [22 x i8] c"Error open /dev/uio0/\00"
@.str.3 = [9 x i8] c"/dev/mem\00"
@.str.4 = [21 x i8] c"Error open /dev/mem/\00"
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define i8* @copy_thread() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %9)
store i32 %10, i32* %1
%11 = load i32, i32* %1
%12 = icmp slt i32 %11, 1
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %0
%16 = load i32, i32* @O_RDWR
%17 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %16)
store i32 %17, i32* %2
%18 = load i32, i32* %2
%19 = icmp slt i32 %18, 1
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %22
22: ; preds = %20, %15
%23 = load i32, i32* @_SC_PAGESIZE
%24 = call i32 @sysconf(i32 %23)
store i32 %24, i32* %3
%25 = load i32, i32* %3
%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* %1
%31 = call i8* @mmap(i32* null, i32 %25, i32 %28, i32 %29, i32 %30, i32 0)
store i8* %31, i8** %4
%32 = load i32, i32* %3
%33 = load i32, i32* @PROT_READ
%34 = load i32, i32* @PROT_WRITE
%35 = or i32 %33, %34
%36 = load i32, i32* @MAP_SHARED
%37 = load i32, i32* %2
%38 = call i8* @mmap(i32* null, i32 %32, i32 %35, i32 %36, i32 %37, i32 1136787456)
store i8* %38, i8** %5
store i32 1, i32* %6
%39 = load i32, i32* %1
%40 = call i32 @write(i32 %39, i32* %6, i32 4)
br label %41
41: ; preds = %22, %41
%42 = load i32, i32* %1
%43 = call i32 @read(i32 %42, i32* %6, i32 4)
%44 = load i8*, i8** %4
%45 = getelementptr i8, i8* %44, i64 0
%46 = bitcast i8* %45 to i32*
%47 = load i32, i32* %46
%48 = load i8*, i8** %5
%49 = getelementptr i8, i8* %48, i64 0
%50 = bitcast i8* %49 to i32*
store i32 %47, i32* %50
%51 = load i8*, i8** %4
%52 = getelementptr i8, i8* %51, i64 4
%53 = bitcast i8* %52 to i32*
%54 = load i32, i32* %53
%55 = load i8*, i8** %5
%56 = getelementptr i8, i8* %55, i64 4
%57 = bitcast i8* %56 to i32*
store i32 %54, i32* %57
store i32 1, i32* %7
%58 = load i32, i32* %1
%59 = call i32 @write(i32 %58, i32* %7, i32 4)
br label %41
}

LLVM IR (-O2):
@.str.1 = [10 x i8] c"/dev/uio0\00"
@O_RDWR = global i32 0
@.str.2 = [22 x i8] c"Error open /dev/uio0/\00"
@.str.3 = [9 x i8] c"/dev/mem\00"
@.str.4 = [21 x i8] c"Error open /dev/mem/\00"
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@str = [20 x i8] c"copy thread started\00"
define noalias i8* @copy_thread() {
%1 = alloca i32
%2 = alloca i32
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %9
9: ; preds = %7, %0
%10 = load i32, i32* @O_RDWR
%11 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %10)
%12 = icmp slt i32 %11, 1
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %15
15: ; preds = %13, %9
%16 = load i32, i32* @_SC_PAGESIZE
%17 = tail call i32 @sysconf(i32 %16)
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @PROT_WRITE
%20 = or i32 %19, %18
%21 = load i32, i32* @MAP_SHARED
%22 = tail call i8* @mmap(i32* null, i32 %17, i32 %20, i32 %21, i32 %5, i32 0)
%23 = load i32, i32* @PROT_READ
%24 = load i32, i32* @PROT_WRITE
%25 = or i32 %24, %23
%26 = load i32, i32* @MAP_SHARED
%27 = tail call i8* @mmap(i32* null, i32 %17, i32 %25, i32 %26, i32 %11, i32 1136787456)
%28 = bitcast i32* %1 to i8*
store i32 1, i32* %1
%29 = call i32 @write(i32 %5, i32* %1, i32 4)
%30 = bitcast i8* %22 to i32*
%31 = bitcast i8* %27 to i32*
%32 = getelementptr i8, i8* %22, i64 4
%33 = bitcast i8* %32 to i32*
%34 = getelementptr i8, i8* %27, i64 4
%35 = bitcast i8* %34 to i32*
%36 = bitcast i32* %2 to i8*
br label %37
37: ; preds = %37, %15
%38 = call i32 @read(i32 %5, i32* %1, i32 4)
%39 = load i32, i32* %30
store i32 %39, i32* %31
%40 = load i32, i32* %33
store i32 %40, i32* %35
store i32 1, i32* %2
%41 = call i32 @write(i32 %5, i32* %2, i32 4)
br label %37
}