#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 uint32_t;

/* Variables and functions */
int *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_RDWR;
int O_SYNC;
int /*<<< orphan*/ PROT_WRITE;
int /*<<< orphan*/ _SC_PAGESIZE;
int close(int);
int *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
          int, int);
int munmap(int *, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int sysconf(int /*<<< orphan*/);
int setMem(uint32_t addr, uint32_t val) {
  uint32_t *pmem = NULL;
  uint32_t page_addr = addr & ~(sysconf(_SC_PAGESIZE) - 1);
  uint32_t offs = addr - page_addr;
  int ret = -1;
  int fd;
  if ((fd = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {
    perror("error open /dev/mem:");
    return -1;
  }
  pmem =
      mmap(NULL, sysconf(_SC_PAGESIZE), PROT_WRITE, MAP_SHARED, fd, page_addr);
  if (pmem == MAP_FAILED) {
    perror("error mmap:");
    goto error;
  }
  pmem[offs / sizeof(uint32_t)] = val;
  ret = 0;
error:
  if (pmem) {
    ret = munmap(pmem, sysconf(_SC_PAGESIZE));
    if (ret)
      perror("error munmap:");
  }
  ret = close(fd);
  if (ret)
    perror("error close:");
  return ret;
}


x86 assembly (clang, -O0):
<setMem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
sub    $0x1,%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    0x2e1e(%rip),%rax
or     (%rax),%esi
lea    0xe3d(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    11ec <setMem+0x8c>
lea    0xe2e(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <setMem+0x172>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,%esi
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
mov    0x2de4(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%r8d
mov    -0x1c(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d8a(%rip),%rcx
cmp    (%rcx),%rax
jne    1248 <setMem+0xe8>
lea    0xde0(%rip),%rdi
call   1070 <perror@plt>
jmp    1261 <setMem+0x101>
mov    -0xc(%rbp),%edx
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
shr    $0x2,%rcx
mov    %edx,(%rax,%rcx,4)
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12ab <setMem+0x14b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
call   1050 <munmap@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     12a6 <setMem+0x146>
lea    0xd89(%rip),%rdi
call   1070 <perror@plt>
jmp    12ab <setMem+0x14b>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     12cc <setMem+0x16c>
lea    0xd71(%rip),%rdi
call   1070 <perror@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setMem>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%r12d
mov    0x2e58(%rip),%r13
mov    0x0(%r13),%edi
call   1080 <sysconf@plt>
mov    %eax,%ebx
mov    0x2e56(%rip),%rax
mov    0x2e47(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe64(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     122f <setMem+0xcf>
mov    %eax,%r15d
neg    %ebx
and    %r12d,%ebx
mov    0x0(%r13),%edi
call   1080 <sysconf@plt>
mov    0x2dff(%rip),%rcx
mov    (%rcx),%edx
mov    0x2e26(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    %eax,%esi
mov    %r15d,%r8d
mov    %ebx,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    0x2dd3(%rip),%rax
cmp    (%rax),%rbp
je     123d <setMem+0xdd>
sub    %ebx,%r12d
movslq %r12d,%rax
and    $0xfffffffffffffffc,%rax
mov    %r14d,0x0(%rbp,%rax,1)
mov    0x0(%r13),%edi
call   1080 <sysconf@plt>
mov    %rbp,%rdi
mov    %eax,%esi
call   1050 <munmap@plt>
test   %eax,%eax
jne    1250 <setMem+0xf0>
mov    %r15d,%edi
call   1040 <close@plt>
xor    %ebx,%ebx
test   %eax,%eax
jne    125e <setMem+0xfe>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%ebx
lea    0xdce(%rip),%rdi
jmp    1267 <setMem+0x107>
lea    0xdda(%rip),%rdi
call   1070 <perror@plt>
test   %rbp,%rbp
jne    11f9 <setMem+0x99>
jmp    1210 <setMem+0xb0>
lea    0xdd3(%rip),%rdi
call   1070 <perror@plt>
jmp    1210 <setMem+0xb0>
lea    0xdd3(%rip),%rdi
mov    %eax,%ebx
call   1070 <perror@plt>
jmp    121e <setMem+0xbe>

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [21 x i8] c"error open /dev/mem:\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.2 = [12 x i8] c"error mmap:\00"
@.str.3 = [14 x i8] c"error munmap:\00"
@.str.4 = [13 x i8] c"error close:\00"
define i32 @setMem(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32* null, i32** %6
%11 = load i32, i32* %4
%12 = load i32, i32* @_SC_PAGESIZE
%13 = call i32 @sysconf(i32 %12)
%14 = sub nsw i32 %13, 1
%15 = xor i32 %14, -1
%16 = and i32 %11, %15
store i32 %16, i32* %7
%17 = load i32, i32* %4
%18 = load i32, i32* %7
%19 = sub nsw i32 %17, %18
store i32 %19, i32* %8
store i32 -1, i32* %9
%20 = load i32, i32* @O_RDWR
%21 = load i32, i32* @O_SYNC
%22 = or i32 %20, %21
%23 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %22)
store i32 %23, i32* %10
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %2
%26 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %69
27: ; preds = %2
%28 = load i32, i32* @_SC_PAGESIZE
%29 = call i32 @sysconf(i32 %28)
%30 = load i32, i32* @PROT_WRITE
%31 = load i32, i32* @MAP_SHARED
%32 = load i32, i32* %10
%33 = load i32, i32* %7
%34 = call i32* @mmap(i32* null, i32 %29, i32 %30, i32 %31, i32 %32, i32 %33)
store i32* %34, i32** %6
%35 = load i32*, i32** %6
%36 = load i32*, i32** @MAP_FAILED
%37 = icmp eq i32* %35, %36
br i1 %37, label %38, label %40
38: ; preds = %27
%39 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %47
40: ; preds = %27
%41 = load i32, i32* %5
%42 = load i32*, i32** %6
%43 = load i32, i32* %8
%44 = sext i32 %43 to i64
%45 = udiv i64 %44, 4
%46 = getelementptr inbounds i32, i32* %42, i64 %45
store i32 %41, i32* %46
store i32 0, i32* %9
br label %47
47: ; preds = %40, %38
%48 = load i32*, i32** %6
%49 = icmp ne i32* %48, null
br i1 %49, label %50, label %60
50: ; preds = %47
%51 = load i32*, i32** %6
%52 = load i32, i32* @_SC_PAGESIZE
%53 = call i32 @sysconf(i32 %52)
%54 = call i32 @munmap(i32* %51, i32 %53)
store i32 %54, i32* %9
%55 = load i32, i32* %9
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %50
%58 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %59
59: ; preds = %57, %50
br label %60
60: ; preds = %59, %47
%61 = load i32, i32* %10
%62 = call i32 @close(i32 %61)
store i32 %62, i32* %9
%63 = load i32, i32* %9
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %60
%66 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %67
67: ; preds = %65, %60
%68 = load i32, i32* %9
store i32 %68, i32* %3
br label %69
69: ; preds = %67, %25
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [21 x i8] c"error open /dev/mem:\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.2 = [12 x i8] c"error mmap:\00"
@.str.3 = [14 x i8] c"error munmap:\00"
@.str.4 = [13 x i8] c"error close:\00"
define i32 @setMem(i32 %0, i32 %1) {
%3 = load i32, i32* @_SC_PAGESIZE
%4 = tail call i32 @sysconf(i32 %3)
%5 = sub i32 0, %4
%6 = and i32 %5, %0
%7 = sub nsw i32 %0, %6
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_SYNC
%10 = or i32 %9, %8
%11 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %38, label %13
13: ; preds = %2
%14 = load i32, i32* @_SC_PAGESIZE
%15 = tail call i32 @sysconf(i32 %14)
%16 = load i32, i32* @PROT_WRITE
%17 = load i32, i32* @MAP_SHARED
%18 = tail call i32* @mmap(i32* null, i32 %15, i32 %16, i32 %17, i32 %11, i32 %6)
%19 = load i32*, i32** @MAP_FAILED
%20 = icmp eq i32* %18, %19
br i1 %20, label %25, label %21
21: ; preds = %13
%22 = sext i32 %7 to i64
%23 = lshr i64 %22, 2
%24 = getelementptr inbounds i32, i32* %18, i64 %23
store i32 %1, i32* %24
br label %28
25: ; preds = %13
%26 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%27 = icmp eq i32* %18, null
br i1 %27, label %35, label %28
28: ; preds = %21, %25
%29 = load i32, i32* @_SC_PAGESIZE
%30 = tail call i32 @sysconf(i32 %29)
%31 = tail call i32 @munmap(i32* %18, i32 %30)
%32 = icmp eq i32 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %28
%34 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %28, %33, %25
%36 = tail call i32 @close(i32 %11)
%37 = icmp eq i32 %36, 0
br i1 %37, label %42, label %38
38: ; preds = %35, %2
%39 = phi i8* [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), %2 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), %35 ]
%40 = phi i32 [ -1, %2 ], [ %36, %35 ]
%41 = tail call i32 @perror(i8* %39)
br label %42
42: ; preds = %38, %35
%43 = phi i32 [ 0, %35 ], [ %40, %38 ]
ret i32 %43
}