#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 uint8_t;
typedef int uint64_t;
typedef int off_t;

/* Variables and functions */
int *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int getpagesize();
int *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ munmap(int *, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int volatile);
int show_usage();
int strtoull(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd, i, offset;
  uint8_t *map_base;
  volatile uint8_t *virt_addr;
  uint64_t mapped_size, page_size, offset_in_page, target;
  uint64_t width = 6;
  uint64_t config_offset = 0x100;
  uint64_t device_start_addr = 0x00000000;
  uint64_t mac = 0;
  if (argc != 3) {
    return show_usage();
  }
  device_start_addr = strtoull(argv[1], NULL, 0);
  mac = strtoull(argv[2], NULL, 0);
  fd = open("/dev/mem", O_RDWR | O_SYNC);
  if (fd < 0) {
    perror("Failed to open '/dev/mem'.");
    return 1;
  }
  target = device_start_addr + config_offset;
  mapped_size = page_size = getpagesize();
  offset_in_page = (unsigned)target & (page_size - 1);
  if (offset_in_page + width > page_size) {
    mapped_size *= 2;
  }
  map_base = mmap(NULL, mapped_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
                  target & ~(off_t)(page_size - 1));
  if (map_base == MAP_FAILED) {
    perror("Failed to mmap '/dev/mem'.");
    return 2;
  }
  virt_addr = (volatile uint8_t *)(map_base + offset_in_page);
  *virt_addr = (uint8_t)(mac >> 40);
  printf("%02x", *virt_addr);
  for (i = 1; i <= 5; i++) {
    *(virt_addr + i) = (uint8_t)(mac >> (5 - i) * 8);
    printf(":%02x", *(virt_addr + i));
  }
  munmap(map_base, mapped_size);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x6,-0x44(%rbp)
movl   $0x100,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
movl   $0x0,-0x50(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11db <main+0x4b>
mov    $0x0,%al
call   1080 <show_usage@plt>
mov    %eax,-0x4(%rbp)
jmp    1394 <main+0x204>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <strtoull@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <strtoull@plt>
mov    %eax,-0x50(%rbp)
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
mov    0x2db9(%rip),%rax
or     (%rax),%esi
lea    0xde0(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    124a <main+0xba>
lea    0xdd0(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1394 <main+0x204>
mov    -0x4c(%rbp),%eax
add    -0x48(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    $0x0,%al
call   10b0 <getpagesize@plt>
mov    %eax,-0x38(%rbp)
mov    %eax,-0x34(%rbp)
mov    -0x40(%rbp),%eax
mov    -0x38(%rbp),%ecx
sub    $0x1,%ecx
and    %ecx,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
add    -0x44(%rbp),%eax
cmp    -0x38(%rbp),%eax
jle    1286 <main+0xf6>
mov    -0x34(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
mov    0x2d68(%rip),%rax
mov    (%rax),%edx
mov    0x2d27(%rip),%rax
or     (%rax),%edx
mov    0x2d46(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x40(%rbp),%r9d
mov    -0x38(%rbp),%eax
sub    $0x1,%eax
xor    $0xffffffff,%eax
and    %eax,%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2ce8(%rip),%rcx
cmp    (%rcx),%rax
jne    12f1 <main+0x161>
lea    0xd44(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1394 <main+0x204>
mov    -0x28(%rbp),%rax
movslq -0x3c(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x50(%rbp),%eax
mov    $0x28,%ecx
sar    %cl,%eax
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
mov    (%rax),%esi
lea    0xd1d(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x18(%rbp)
cmpl   $0x5,-0x18(%rbp)
jg     1379 <main+0x1e9>
mov    -0x50(%rbp),%edx
mov    $0x5,%ecx
sub    -0x18(%rbp),%ecx
shl    $0x3,%ecx
sar    %cl,%edx
mov    -0x30(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x30(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xcd9(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    132f <main+0x19f>
mov    -0x28(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1070 <munmap@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
cmp    $0x3,%edi
jne    12fa <main+0x16a>
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <strtoull@plt>
mov    %eax,%ebx
mov    0x10(%rbp),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <strtoull@plt>
mov    %eax,%r12d
mov    0x2e0b(%rip),%rax
mov    0x2dfc(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe21(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1301 <main+0x171>
mov    %eax,%r15d
add    $0x100,%ebx
xor    %eax,%eax
call   10b0 <getpagesize@plt>
lea    -0x1(%rax),%ebp
and    %ebx,%ebp
lea    0x6(%rbp),%ecx
cmp    %eax,%ecx
setg   %cl
mov    %eax,%r13d
shl    %cl,%r13d
mov    0x2de2(%rip),%rcx
mov    0x2da3(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2dc0(%rip),%rcx
mov    (%rcx),%ecx
neg    %eax
and    %eax,%ebx
xor    %edi,%edi
mov    %r13d,%esi
mov    %r15d,%r8d
mov    %ebx,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2d70(%rip),%rax
cmp    (%rax),%rbx
je     1315 <main+0x185>
movslq %ebp,%r14
mov    (%rbx,%r14,4),%esi
lea    0xde1(%rip),%rdi
call   1040 <printf@plt>
mov    0x4(%rbx,%r14,4),%esi
lea    0xdcf(%rip),%rbp
mov    %rbp,%rdi
call   1040 <printf@plt>
mov    %r12d,%eax
sar    $0x18,%eax
mov    %eax,0x8(%rbx,%r14,4)
mov    0x8(%rbx,%r14,4),%esi
mov    %rbp,%rdi
call   1040 <printf@plt>
mov    %r12d,%eax
sar    $0x10,%eax
mov    %eax,0xc(%rbx,%r14,4)
mov    0xc(%rbx,%r14,4),%esi
mov    %rbp,%rdi
call   1040 <printf@plt>
mov    %r12d,%eax
sar    $0x8,%eax
mov    %eax,0x10(%rbx,%r14,4)
mov    0x10(%rbx,%r14,4),%esi
mov    %rbp,%rdi
call   1040 <printf@plt>
mov    %r12d,0x14(%rbx,%r14,4)
mov    0x14(%rbx,%r14,4),%esi
xor    %r14d,%r14d
mov    %rbp,%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    %r13d,%esi
call   1070 <munmap@plt>
mov    %r15d,%edi
call   1050 <close@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
jmp    1080 <show_usage@plt>
lea    0xd01(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%r14d
jmp    12e8 <main+0x158>
lea    0xd08(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x2,%r14d
jmp    12e8 <main+0x158>

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [27 x i8] c"Failed to open '/dev/mem'.\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.2 = [27 x i8] c"Failed to mmap '/dev/mem'.\00"
@.str.3 = [5 x i8] c"%02x\00"
@.str.4 = [6 x i8] c":%02x\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 6, i32* %15
store i32 256, i32* %16
store i32 0, i32* %17
store i32 0, i32* %18
%19 = load i32, i32* %4
%20 = icmp ne i32 %19, 3
br i1 %20, label %21, label %23
21: ; preds = %2
%22 = call i32 (...) @show_usage()
store i32 %22, i32* %3
br label %114
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @strtoull(i8* %26, i32* null, i32 0)
store i32 %27, i32* %17
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
%31 = call i32 @strtoull(i8* %30, i32* null, i32 0)
store i32 %31, i32* %18
%32 = load i32, i32* @O_RDWR
%33 = load i32, i32* @O_SYNC
%34 = or i32 %32, %33
%35 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %34)
store i32 %35, i32* %6
%36 = load i32, i32* %6
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %23
%39 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %114
40: ; preds = %23
%41 = load i32, i32* %17
%42 = load i32, i32* %16
%43 = add nsw i32 %41, %42
store i32 %43, i32* %14
%44 = call i32 (...) @getpagesize()
store i32 %44, i32* %12
store i32 %44, i32* %11
%45 = load i32, i32* %14
%46 = load i32, i32* %12
%47 = sub nsw i32 %46, 1
%48 = and i32 %45, %47
store i32 %48, i32* %13
%49 = load i32, i32* %13
%50 = load i32, i32* %15
%51 = add nsw i32 %49, %50
%52 = load i32, i32* %12
%53 = icmp sgt i32 %51, %52
br i1 %53, label %54, label %57
54: ; preds = %40
%55 = load i32, i32* %11
%56 = mul nsw i32 %55, 2
store i32 %56, i32* %11
br label %57
57: ; preds = %54, %40
%58 = load i32, i32* %11
%59 = load i32, i32* @PROT_READ
%60 = load i32, i32* @PROT_WRITE
%61 = or i32 %59, %60
%62 = load i32, i32* @MAP_SHARED
%63 = load i32, i32* %6
%64 = load i32, i32* %14
%65 = load i32, i32* %12
%66 = sub nsw i32 %65, 1
%67 = xor i32 %66, -1
%68 = and i32 %64, %67
%69 = call i32* @mmap(i32* null, i32 %58, i32 %61, i32 %62, i32 %63, i32 %68)
store i32* %69, i32** %9
%70 = load i32*, i32** %9
%71 = load i32*, i32** @MAP_FAILED
%72 = icmp eq i32* %70, %71
br i1 %72, label %73, label %75
73: ; preds = %57
%74 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %3
br label %114
75: ; preds = %57
%76 = load i32*, i32** %9
%77 = load i32, i32* %13
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i32, i32* %76, i64 %78
store i32* %79, i32** %10
%80 = load i32, i32* %18
%81 = ashr i32 %80, 40
%82 = load i32*, i32** %10
store volatile i32 %81, i32* %82
%83 = load i32*, i32** %10
%84 = load volatile i32, i32* %83
%85 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %84)
store i32 1, i32* %7
br label %86
86: ; preds = %105, %75
%87 = load i32, i32* %7
%88 = icmp sle i32 %87, 5
br i1 %88, label %89, label %108
89: ; preds = %86
%90 = load i32, i32* %18
%91 = load i32, i32* %7
%92 = sub nsw i32 5, %91
%93 = mul nsw i32 %92, 8
%94 = ashr i32 %90, %93
%95 = load i32*, i32** %10
%96 = load i32, i32* %7
%97 = sext i32 %96 to i64
%98 = getelementptr inbounds i32, i32* %95, i64 %97
store volatile i32 %94, i32* %98
%99 = load i32*, i32** %10
%100 = load i32, i32* %7
%101 = sext i32 %100 to i64
%102 = getelementptr inbounds i32, i32* %99, i64 %101
%103 = load volatile i32, i32* %102
%104 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %103)
br label %105
105: ; preds = %89
%106 = load i32, i32* %7
%107 = add nsw i32 %106, 1
store i32 %107, i32* %7
br label %86
108: ; preds = %86
%109 = load i32*, i32** %9
%110 = load i32, i32* %11
%111 = call i32 @munmap(i32* %109, i32 %110)
%112 = load i32, i32* %6
%113 = call i32 @close(i32 %112)
store i32 0, i32* %3
br label %114
114: ; preds = %108, %73, %38, %21
%115 = load i32, i32* %3
ret i32 %115
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [27 x i8] c"Failed to open '/dev/mem'.\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.2 = [27 x i8] c"Failed to mmap '/dev/mem'.\00"
@.str.3 = [5 x i8] c"%02x\00"
@.str.4 = [6 x i8] c":%02x\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 (...) @show_usage()
br label %65
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @strtoull(i8* nocapture %8, i32* null, i32 0)
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = tail call i32 @strtoull(i8* nocapture %11, i32* null, i32 0)
%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, i64 0, i64 0), i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %6
%19 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %65
20: ; preds = %6
%21 = add nsw i32 %9, 256
%22 = tail call i32 (...) @getpagesize()
%23 = add nsw i32 %22, -1
%24 = and i32 %23, %21
%25 = add nsw i32 %24, 6
%26 = icmp sgt i32 %25, %22
%27 = zext i1 %26 to i32
%28 = shl nsw i32 %22, %27
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @PROT_WRITE
%31 = or i32 %30, %29
%32 = load i32, i32* @MAP_SHARED
%33 = sub i32 0, %22
%34 = and i32 %21, %33
%35 = tail call i32* @mmap(i32* null, i32 %28, i32 %31, i32 %32, i32 %16, i32 %34)
%36 = load i32*, i32** @MAP_FAILED
%37 = icmp eq i32* %35, %36
br i1 %37, label %38, label %40
38: ; preds = %20
%39 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %65
40: ; preds = %20
%41 = sext i32 %24 to i64
%42 = getelementptr inbounds i32, i32* %35, i64 %41
store volatile i32 poison, i32* %42
%43 = load volatile i32, i32* %42
%44 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %43)
%45 = getelementptr inbounds i32, i32* %42, i64 1
store volatile i32 poison, i32* %45
%46 = load volatile i32, i32* %45
%47 = tail call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %46)
%48 = ashr i32 %12, 24
%49 = getelementptr inbounds i32, i32* %42, i64 2
store volatile i32 %48, i32* %49
%50 = load volatile i32, i32* %49
%51 = tail call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %50)
%52 = ashr i32 %12, 16
%53 = getelementptr inbounds i32, i32* %42, i64 3
store volatile i32 %52, i32* %53
%54 = load volatile i32, i32* %53
%55 = tail call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %54)
%56 = ashr i32 %12, 8
%57 = getelementptr inbounds i32, i32* %42, i64 4
store volatile i32 %56, i32* %57
%58 = load volatile i32, i32* %57
%59 = tail call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %58)
%60 = getelementptr inbounds i32, i32* %42, i64 5
store volatile i32 %12, i32* %60
%61 = load volatile i32, i32* %60
%62 = tail call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %61)
%63 = tail call i32 @munmap(i32* %35, i32 %28)
%64 = tail call i32 @close(i32 %16)
br label %65
65: ; preds = %40, %38, %18, %4
%66 = phi i32 [ %5, %4 ], [ 1, %18 ], [ 2, %38 ], [ 0, %40 ]
ret i32 %66
}