#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*/ uint16_t;
typedef unsigned long off_t;

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
void *mmap(int /*<<< orphan*/, unsigned long, int, int /*<<< orphan*/, int,
           unsigned long);
int /*<<< orphan*/ munmap(void *, unsigned long);
int open(char *, int);
int /*<<< orphan*/ write16(void *, int /*<<< orphan*/);
unsigned int write_mem16(off_t target, uint16_t val) {
  int fd;
  void *map_base, *virt_addr;
  fd = open("/dev/mem", O_RDWR | O_SYNC);
  if (fd < 0)
    return (unsigned int)1;
  map_base = mmap(0, 4096UL, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
                  target & ~(4096UL - 1));
  if (map_base == (void *)-1)
    return (unsigned int)2;
  virt_addr = map_base + (target & (4096UL - 1));
  write16(virt_addr, val);
  munmap(map_base, 4096UL);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<write_mem16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
mov    0x2e61(%rip),%rax
or     (%rax),%esi
lea    0xe88(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1196 <write_mem16+0x46>
movl   $0x1,-0x4(%rbp)
jmp    122c <write_mem16+0xdc>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edx
mov    0x2e1a(%rip),%rax
or     (%rax),%edx
mov    0x2e39(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%r8d
mov    -0x10(%rbp),%r9
and    $0xfffffffffffff000,%r9
xor    %edi,%edi
mov    $0x1000,%esi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x20(%rbp)
jne    11ed <write_mem16+0x9d>
movl   $0x2,-0x4(%rbp)
jmp    122c <write_mem16+0xdc>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
and    $0xfff,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <write16@plt>
mov    -0x20(%rbp),%rdi
mov    $0x1000,%esi
call   1050 <munmap@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_mem16>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2e73(%rip),%rax
mov    0x2e64(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe89(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11ee <write_mem16+0x9e>
mov    %eax,%r12d
mov    0x2e6e(%rip),%rax
mov    0x2e2f(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e4c(%rip),%rax
mov    (%rax),%ecx
mov    %rbx,%r9
and    $0xfffffffffffff000,%r9
xor    %r15d,%r15d
mov    $0x1000,%esi
xor    %edi,%edi
mov    %r12d,%r8d
call   1030 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     11f6 <write_mem16+0xa6>
mov    %rax,%rbp
and    $0xfff,%ebx
add    %rax,%rbx
mov    %rbx,%rdi
mov    %r14d,%esi
call   1070 <write16@plt>
mov    $0x1000,%esi
mov    %rbp,%rdi
call   1050 <munmap@plt>
mov    %r12d,%edi
call   1040 <close@plt>
jmp    11fc <write_mem16+0xac>
mov    $0x1,%r15d
jmp    11fc <write_mem16+0xac>
mov    $0x2,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [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
define i32 @write_mem16(i64 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
store i64 %0, i64* %4
store i32 %1, i32* %5
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_SYNC
%11 = or i32 %9, %10
%12 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 1, i32* %3
br label %40
16: ; preds = %2
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @PROT_WRITE
%19 = or i32 %17, %18
%20 = load i32, i32* @MAP_SHARED
%21 = load i32, i32* %6
%22 = load i64, i64* %4
%23 = and i64 %22, -4096
%24 = call i8* @mmap(i32 0, i64 4096, i32 %19, i32 %20, i32 %21, i64 %23)
store i8* %24, i8** %7
%25 = load i8*, i8** %7
%26 = icmp eq i8* %25, inttoptr (i64 -1 to i8*)
br i1 %26, label %27, label %28
27: ; preds = %16
store i32 2, i32* %3
br label %40
28: ; preds = %16
%29 = load i8*, i8** %7
%30 = load i64, i64* %4
%31 = and i64 %30, 4095
%32 = getelementptr i8, i8* %29, i64 %31
store i8* %32, i8** %8
%33 = load i8*, i8** %8
%34 = load i32, i32* %5
%35 = call i32 @write16(i8* %33, i32 %34)
%36 = load i8*, i8** %7
%37 = call i32 @munmap(i8* %36, i64 4096)
%38 = load i32, i32* %6
%39 = call i32 @close(i32 %38)
store i32 0, i32* %3
br label %40
40: ; preds = %28, %27, %15
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@.str = [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
define i32 @write_mem16(i64 %0, i32 %1) {
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_SYNC
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %22, label %8
8: ; preds = %2
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @PROT_WRITE
%11 = or i32 %10, %9
%12 = load i32, i32* @MAP_SHARED
%13 = and i64 %0, -4096
%14 = tail call i8* @mmap(i32 0, i64 4096, i32 %11, i32 %12, i32 %6, i64 %13)
%15 = icmp eq i8* %14, inttoptr (i64 -1 to i8*)
br i1 %15, label %22, label %16
16: ; preds = %8
%17 = and i64 %0, 4095
%18 = getelementptr i8, i8* %14, i64 %17
%19 = tail call i32 @write16(i8* %18, i32 %1)
%20 = tail call i32 @munmap(i8* %14, i64 4096)
%21 = tail call i32 @close(i32 %6)
br label %22
22: ; preds = %8, %2, %16
%23 = phi i32 [ 0, %16 ], [ 1, %2 ], [ 2, %8 ]
ret i32 %23
}