#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 */
char **MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
char *mmap(int /*<<< orphan*/, size_t, int, int /*<<< orphan*/, int,
           unsigned long);
int open(char *, int /*<<< orphan*/);
int memget(unsigned long addr, size_t size, char **buf) {
  int desc = open("/dev/mem", O_RDWR);
  if (desc < 0)
    return -1;
  *buf = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, desc, addr);
  if (!buf || (MAP_FAILED == buf)) {
    close(desc);
    return -1;
  }
  return desc;
}


x86 assembly (clang, -O0):
<memget>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
lea    0xeac(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1172 <memget+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e3 <memget+0xb3>
mov    -0x18(%rbp),%rsi
mov    0x2e7b(%rip),%rax
mov    (%rax),%edx
mov    0x2e42(%rip),%rax
or     (%rax),%edx
mov    0x2e59(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
mov    -0x10(%rbp),%r9
xor    %edi,%edi
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x20(%rbp)
je     11c9 <memget+0x99>
mov    0x2e04(%rip),%rax
mov    (%rax),%rax
cmp    -0x20(%rbp),%rax
jne    11dd <memget+0xad>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e3 <memget+0xb3>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memget>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
lea    0xeaf(%rip),%rdi
call   1050 <open@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     11a6 <memget+0x76>
mov    %eax,%ebp
mov    0x2e90(%rip),%rax
mov    0x2e59(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e6e(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r12,%rsi
mov    %ebp,%r8d
mov    %r15,%r9
call   1030 <mmap@plt>
mov    %rax,(%r14)
mov    0x2e2a(%rip),%rax
cmp    %r14,(%rax)
je     119f <memget+0x6f>
mov    %ebp,%ebx
jmp    11a6 <memget+0x76>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%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
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8** null
define i32 @memget(i64 %0, i64 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8**
%8 = alloca i32
store i64 %0, i64* %5
store i64 %1, i64* %6
store i8** %2, i8*** %7
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %8
%11 = load i32, i32* %8
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 -1, i32* %4
br label %35
14: ; preds = %3
%15 = load i64, i64* %6
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @PROT_WRITE
%18 = or i32 %16, %17
%19 = load i32, i32* @MAP_SHARED
%20 = load i32, i32* %8
%21 = load i64, i64* %5
%22 = call i8* @mmap(i32 0, i64 %15, i32 %18, i32 %19, i32 %20, i64 %21)
%23 = load i8**, i8*** %7
store i8* %22, i8** %23
%24 = load i8**, i8*** %7
%25 = icmp ne i8** %24, null
br i1 %25, label %26, label %30
26: ; preds = %14
%27 = load i8**, i8*** @MAP_FAILED
%28 = load i8**, i8*** %7
%29 = icmp eq i8** %27, %28
br i1 %29, label %30, label %33
30: ; preds = %26, %14
%31 = load i32, i32* %8
%32 = call i32 @close(i32 %31)
store i32 -1, i32* %4
br label %35
33: ; preds = %26
%34 = load i32, i32* %8
store i32 %34, i32* %4
br label %35
35: ; preds = %33, %30, %13
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8** null
define i32 @memget(i64 %0, i64 %1, i8** nocapture writeonly %2) {
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %17, label %7
7: ; preds = %3
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @PROT_WRITE
%10 = or i32 %9, %8
%11 = load i32, i32* @MAP_SHARED
%12 = tail call i8* @mmap(i32 0, i64 %1, i32 %10, i32 %11, i32 %5, i64 %0)
store i8* %12, i8** %2
%13 = load i8**, i8*** @MAP_FAILED
%14 = icmp eq i8** %13, %2
br i1 %14, label %15, label %17
15: ; preds = %7
%16 = tail call i32 @close(i32 %5)
br label %17
17: ; preds = %7, %3, %15
%18 = phi i32 [ -1, %15 ], [ -1, %3 ], [ %5, %7 ]
ret i32 %18
}