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

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ PROT_WRITE;
int /*<<< orphan*/ _SC_PAGESIZE;
int close(int);
int /*<<< orphan*/ memcpy(char *, void *, size_t);
char *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int);
int munmap(char *, size_t);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int sysconf(int /*<<< orphan*/);
int mem_write(off_t ptr, void *buffer, size_t len) {
  int errno;
  off_t mapped_offset = ptr & ~(sysconf(_SC_PAGESIZE) - 1);
  size_t mapped_len = len + ptr - mapped_offset;
  int fd = open("/dev/mem", O_RDWR, 0);
  if (fd == -1)
    return -1;
  char *mapped =
      mmap(NULL, mapped_len, PROT_WRITE, MAP_SHARED, fd, mapped_offset);
  if (mapped == MAP_FAILED)
    return -1;
  memcpy(mapped + ptr - mapped_offset, buffer, len);
  errno = munmap(mapped, mapped_len);
  if (errno)
    return errno;
  errno = close(fd);
  if (errno)
    return errno;
  return 0;
}


x86 assembly (clang, -O0):
<mem_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
sub    $0x1,%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
add    %rcx,%rax
movslq -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
lea    0xe43(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11dd <mem_write+0x7d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1298 <mem_write+0x138>
mov    -0x28(%rbp),%rsi
mov    0x2de0(%rip),%rax
mov    (%rax),%edx
mov    0x2dff(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
mov    -0x20(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    0x2dad(%rip),%rcx
cmp    (%rcx),%rax
jne    1228 <mem_write+0xc8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1298 <mem_write+0x138>
mov    -0x38(%rbp),%rdi
movslq -0x8(%rbp),%rax
add    %rax,%rdi
movslq -0x20(%rbp),%rcx
xor    %eax,%eax
sub    %rcx,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <munmap@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1271 <mem_write+0x111>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1298 <mem_write+0x138>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1291 <mem_write+0x131>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1298 <mem_write+0x138>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,%ebp
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
mov    $0xffffffff,%r13d
cmp    $0xffffffff,%eax
je     1224 <mem_write+0xc4>
mov    %eax,%r12d
mov    %r15,0x8(%rsp)
neg    %ebp
and    %ebx,%ebp
movslq %ebx,%rbx
mov    %r14,0x10(%rsp)
lea    (%rbx,%r14,1),%r15
movslq %ebp,%r14
sub    %r14,%r15
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
mov    0x2e1a(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15,%rsi
mov    %r12d,%r8d
mov    %r14d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    0x2dce(%rip),%rax
cmp    (%rax),%rbp
je     1224 <mem_write+0xc4>
neg    %r14
add    %rbp,%rbx
add    %r14,%rbx
mov    %rbx,%rdi
mov    0x8(%rsp),%rsi
mov    0x10(%rsp),%rdx
call   1050 <memcpy@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1060 <munmap@plt>
mov    %eax,%r13d
test   %eax,%eax
je     1236 <mem_write+0xd6>
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12d,%edi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <close@plt>

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

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