#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 */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
unsigned int atoi(char *);
int /*<<< orphan*/ close(int);
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int, int);
scalar_t__ munmap(void *, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  void *virtual_base;
  void *h2p_lw_accel_addr;
  int fd;
  unsigned int a1, a2;
  if (argc != 3) {
    printf("Wring arguments");
    return -1;
  }
  a1 = atoi(argv[1]);
  a2 = atoi(argv[2]);
  if ((fd = open("/dev/mem", (O_RDWR | O_SYNC))) == -1) {
    printf("ERROR: could not open \"/dev/mem\"...\n");
    return (1);
  }
  virtual_base = mmap(NULL, 0x04000000, (PROT_READ | PROT_WRITE), MAP_SHARED,
                      fd, 0xFC000000);
  if (virtual_base == MAP_FAILED) {
    printf("ERROR: mmap() failed...\n");
    close(fd);
    return (1);
  }
  h2p_lw_accel_addr = virtual_base + (unsigned int)(0x03200000 + 0x00040000);
  printf("Writing 1st argument\n");
  *(unsigned int *)(h2p_lw_accel_addr + 0) = a1;
  printf("Writing 2nd argument\n");
  *(unsigned int *)(h2p_lw_accel_addr + 4) = a2;
  sleep(1);
  printf("Reading result\n");
  printf("Res: %d\n", *(unsigned int *)(h2p_lw_accel_addr + 0));
  if (munmap(virtual_base, 0x04000000) != 0) {
    printf("ERROR: munmap() failed...\n");
    close(fd);
    return (1);
  }
  close(fd);
  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)
cmpl   $0x3,-0x8(%rbp)
je     11aa <main+0x3a>
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132d <main+0x1bd>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
mov    0x2df6(%rip),%rax
or     (%rax),%esi
lea    0xe2d(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    120e <main+0x9e>
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    132d <main+0x1bd>
mov    0x2de3(%rip),%rax
mov    (%rax),%edx
mov    0x2da2(%rip),%rax
or     (%rax),%edx
mov    0x2dc1(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4000000,%esi
mov    $0xfc000000,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d68(%rip),%rcx
cmp    (%rcx),%rax
jne    127b <main+0x10b>
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    132d <main+0x1bd>
mov    -0x18(%rbp),%rax
add    $0x3240000,%rax
mov    %rax,-0x20(%rbp)
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x2c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    $0x1,%edi
call   1090 <sleep@plt>
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    $0x4000000,%esi
call   1060 <munmap@plt>
cmp    $0x0,%rax
je     131e <main+0x1ae>
lea    0xd99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    132d <main+0x1bd>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    1293 <main+0x113>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
mov    0x10(%rbx),%rdi
call   1090 <atoi@plt>
mov    %eax,%r14d
mov    0x2e27(%rip),%rax
mov    0x2e18(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe4d(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     12a8 <main+0x128>
mov    %eax,%r15d
mov    0x2e1d(%rip),%rax
mov    0x2dde(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dfb(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x4000000,%esi
mov    %r15d,%r8d
mov    $0xfc000000,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2daa(%rip),%rax
cmp    (%rax),%rbx
je     12bb <main+0x13b>
lea    0xe04(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,0x3240000(%rbx)
lea    0xe07(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,0x3240004(%rbx)
mov    $0x1,%ebp
mov    $0x1,%edi
call   10a0 <sleep@plt>
lea    0xdfa(%rip),%rdi
call   1030 <puts@plt>
mov    0x3240000(%rbx),%esi
lea    0xdb5(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x4000000,%esi
call   1070 <munmap@plt>
test   %rax,%rax
je     12d6 <main+0x156>
lea    0xdd7(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%edi
call   1060 <close@plt>
jmp    12e0 <main+0x160>
lea    0xd66(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebp
jmp    12e0 <main+0x160>
lea    0xdde(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
jmp    12e0 <main+0x160>
lea    0xdb3(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    $0x1,%ebp
jmp    12e0 <main+0x160>
mov    %r15d,%edi
call   1060 <close@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"Wring arguments\00"
@.str.1 = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.2 = [37 x i8] c"ERROR: could not open \22/dev/mem\22...\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [25 x i8] c"ERROR: mmap() failed...\0A\00"
@.str.4 = [22 x i8] c"Writing 1st argument\0A\00"
@.str.5 = [22 x i8] c"Writing 2nd argument\0A\00"
@.str.6 = [16 x i8] c"Reading result\0A\00"
@.str.7 = [9 x i8] c"Res: %d\0A\00"
@.str.8 = [27 x i8] c"ERROR: munmap() failed...\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %75
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %9
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 2
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %10
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_SYNC
%26 = or i32 %24, %25
%27 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %26)
store i32 %27, i32* %8
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %15
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %75
31: ; preds = %15
%32 = load i32, i32* @PROT_READ
%33 = load i32, i32* @PROT_WRITE
%34 = or i32 %32, %33
%35 = load i32, i32* @MAP_SHARED
%36 = load i32, i32* %8
%37 = call i8* @mmap(i32* null, i32 67108864, i32 %34, i32 %35, i32 %36, i32 -67108864)
store i8* %37, i8** %6
%38 = load i8*, i8** %6
%39 = load i8*, i8** @MAP_FAILED
%40 = icmp eq i8* %38, %39
br i1 %40, label %41, label %45
41: ; preds = %31
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%43 = load i32, i32* %8
%44 = call i32 @close(i32 %43)
store i32 1, i32* %3
br label %75
45: ; preds = %31
%46 = load i8*, i8** %6
%47 = getelementptr i8, i8* %46, i64 52690944
store i8* %47, i8** %7
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%49 = load i32, i32* %9
%50 = load i8*, i8** %7
%51 = getelementptr i8, i8* %50, i64 0
%52 = bitcast i8* %51 to i32*
store i32 %49, i32* %52
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%54 = load i32, i32* %10
%55 = load i8*, i8** %7
%56 = getelementptr i8, i8* %55, i64 4
%57 = bitcast i8* %56 to i32*
store i32 %54, i32* %57
%58 = call i32 @sleep(i32 1)
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
%60 = load i8*, i8** %7
%61 = getelementptr i8, i8* %60, i64 0
%62 = bitcast i8* %61 to i32*
%63 = load i32, i32* %62
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 %63)
%65 = load i8*, i8** %6
%66 = call i64 @munmap(i8* %65, i32 67108864)
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %72
68: ; preds = %45
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0))
%70 = load i32, i32* %8
%71 = call i32 @close(i32 %70)
store i32 1, i32* %3
br label %75
72: ; preds = %45
%73 = load i32, i32* %8
%74 = call i32 @close(i32 %73)
store i32 0, i32* %3
br label %75
75: ; preds = %72, %68, %41, %29, %13
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
@.str = [16 x i8] c"Wring arguments\00"
@.str.1 = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.7 = [9 x i8] c"Res: %d\0A\00"
@str = [21 x i8] c"Writing 1st argument\00"
@str.9 = [21 x i8] c"Writing 2nd argument\00"
@str.10 = [15 x i8] c"Reading result\00"
@str.11 = [26 x i8] c"ERROR: munmap() failed...\00"
@str.12 = [24 x i8] c"ERROR: mmap() failed...\00"
@str.13 = [36 x i8] c"ERROR: could not open \22/dev/mem\22...\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %49
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_SYNC
%15 = or i32 %14, %13
%16 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %6
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.13, i64 0, i64 0))
br label %49
20: ; preds = %6
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @PROT_WRITE
%23 = or i32 %22, %21
%24 = load i32, i32* @MAP_SHARED
%25 = tail call i8* @mmap(i32* null, i32 67108864, i32 %23, i32 %24, i32 %16, i32 -67108864)
%26 = load i8*, i8** @MAP_FAILED
%27 = icmp eq i8* %25, %26
br i1 %27, label %28, label %31
28: ; preds = %20
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.12, i64 0, i64 0))
%30 = tail call i32 @close(i32 %16)
br label %49
31: ; preds = %20
%32 = getelementptr i8, i8* %25, i64 52690944
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%34 = bitcast i8* %32 to i32*
store i32 %9, i32* %34
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.9, i64 0, i64 0))
%36 = getelementptr i8, i8* %25, i64 52690948
%37 = bitcast i8* %36 to i32*
store i32 %12, i32* %37
%38 = tail call i32 @sleep(i32 1)
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.10, i64 0, i64 0))
%40 = load i32, i32* %34
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 %40)
%42 = tail call i64 @munmap(i8* %25, i32 67108864)
%43 = icmp eq i64 %42, 0
br i1 %43, label %47, label %44
44: ; preds = %31
%45 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.11, i64 0, i64 0))
%46 = tail call i32 @close(i32 %16)
br label %49
47: ; preds = %31
%48 = tail call i32 @close(i32 %16)
br label %49
49: ; preds = %47, %44, %28, %18, %4
%50 = phi i32 [ -1, %4 ], [ 1, %18 ], [ 1, %28 ], [ 1, %44 ], [ 0, %47 ]
ret i32 %50
}