#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 */
unsigned short volatile *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
unsigned short *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
                     int);
int /*<<< orphan*/ munmap(void *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int pollux_set(unsigned short volatile *, char *);
int /*<<< orphan*/ usage(char *);
int main(int argc, char *argv[]) {
  volatile unsigned short *memregs;
  int ret, memdev;
  if (argc != 2) {
    usage(argv[0]);
    return 1;
  }
  memdev = open("/dev/mem", O_RDWR);
  if (memdev == -1) {
    perror("open(/dev/mem) failed");
    return 1;
  }
  memregs =
      mmap(0, 0x20000, PROT_READ | PROT_WRITE, MAP_SHARED, memdev, 0xc0000000);
  if (memregs == MAP_FAILED) {
    perror("mmap(memregs) failed");
    close(memdev);
    return 1;
  }
  ret = pollux_set(memregs, argv[1]);
  munmap((void *)memregs, 0x20000);
  close(memdev);
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11a8 <main+0x38>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <usage@plt>
movl   $0x1,-0x4(%rbp)
jmp    127b <main+0x10b>
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
lea    0xe48(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11e2 <main+0x72>
lea    0xe38(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    127b <main+0x10b>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
mov    0x2dd6(%rip),%rax
or     (%rax),%edx
mov    0x2ded(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%r8d
xor    %edi,%edi
mov    $0x20000,%esi
mov    $0xc0000000,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d9e(%rip),%rcx
cmp    (%rcx),%rax
jne    124b <main+0xdb>
lea    0xded(%rip),%rdi
call   1090 <perror@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    127b <main+0x10b>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <pollux_set@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x20000,%esi
call   1060 <munmap@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1203 <main+0x93>
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
lea    0xe70(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1215 <main+0xa5>
mov    %eax,%r14d
mov    0x2e54(%rip),%rax
mov    0x2e1d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e32(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x20000,%esi
mov    %r14d,%r8d
mov    $0xc0000000,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbp
mov    0x2de9(%rip),%rax
cmp    (%rax),%rbp
je     1223 <main+0xb3>
mov    0x8(%rbx),%rsi
mov    %rbp,%rdi
call   1030 <pollux_set@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
mov    $0x20000,%esi
call   1060 <munmap@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %ebx,%eax
jmp    1210 <main+0xa0>
mov    (%rbx),%rdi
call   1070 <usage@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xded(%rip),%rdi
call   1090 <perror@plt>
jmp    120b <main+0x9b>
lea    0xdf5(%rip),%rdi
call   1090 <perror@plt>
mov    %r14d,%edi
call   1050 <close@plt>
jmp    120b <main+0x9b>

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@.str.1 = [22 x i8] c"open(/dev/mem) failed\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i16* null
@.str.2 = [21 x i8] c"mmap(memregs) failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i16*
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @usage(i8* %14)
store i32 1, i32* %3
br label %49
16: ; preds = %2
%17 = load i32, i32* @O_RDWR
%18 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %49
23: ; preds = %16
%24 = load i32, i32* @PROT_READ
%25 = load i32, i32* @PROT_WRITE
%26 = or i32 %24, %25
%27 = load i32, i32* @MAP_SHARED
%28 = load i32, i32* %8
%29 = call i16* @mmap(i32 0, i32 131072, i32 %26, i32 %27, i32 %28, i32 -1073741824)
store i16* %29, i16** %6
%30 = load i16*, i16** %6
%31 = load i16*, i16** @MAP_FAILED
%32 = icmp eq i16* %30, %31
br i1 %32, label %33, label %37
33: ; preds = %23
%34 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32, i32* %8
%36 = call i32 @close(i32 %35)
store i32 1, i32* %3
br label %49
37: ; preds = %23
%38 = load i16*, i16** %6
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
%42 = call i32 @pollux_set(i16* %38, i8* %41)
store i32 %42, i32* %7
%43 = load i16*, i16** %6
%44 = bitcast i16* %43 to i8*
%45 = call i32 @munmap(i8* %44, i32 131072)
%46 = load i32, i32* %8
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %7
store i32 %48, i32* %3
br label %49
49: ; preds = %37, %33, %21, %11
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@.str.1 = [22 x i8] c"open(/dev/mem) failed\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i16* null
@.str.2 = [21 x i8] c"mmap(memregs) failed\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @usage(i8* %5)
br label %31
7: ; preds = %2
%8 = load i32, i32* @O_RDWR
%9 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %31
13: ; preds = %7
%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 i16* @mmap(i32 0, i32 131072, i32 %16, i32 %17, i32 %9, i32 -1073741824)
%19 = load i16*, i16** @MAP_FAILED
%20 = icmp eq i16* %18, %19
br i1 %20, label %21, label %24
21: ; preds = %13
%22 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%23 = tail call i32 @close(i32 %9)
br label %31
24: ; preds = %13
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = tail call i32 @pollux_set(i16* %18, i8* %26)
%28 = bitcast i16* %18 to i8*
%29 = tail call i32 @munmap(i8* %28, i32 131072)
%30 = tail call i32 @close(i32 %9)
br label %31
31: ; preds = %24, %21, %11, %4
%32 = phi i32 [ 1, %4 ], [ 1, %11 ], [ 1, %21 ], [ %27, %24 ]
ret i32 %32
}