#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 */
int MAP_FIXED;
int MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
void *mmap(void *, int, int, int, int, int);
int /*<<< orphan*/ munmap(void *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ real_mem_deinit();
int /*<<< orphan*/ real_mem_init();
int LRMI_common_init(void) {
  void *m;
  int fd_mem;
  if (!real_mem_init())
    return 0;
  fd_mem = open("/dev/mem", O_RDWR);
  if (fd_mem == -1) {
    real_mem_deinit();
    return 0;
  }
  m = mmap((void *)0, 0x502, PROT_READ | PROT_WRITE | PROT_EXEC,
           MAP_FIXED | MAP_SHARED, fd_mem, 0);
  if (m == (void *)-1) {
    close(fd_mem);
    real_mem_deinit();
    return 0;
  }
  m = mmap((void *)0xa0000, 0x100000 - 0xa0000, PROT_READ | PROT_WRITE,
           MAP_FIXED | MAP_SHARED, fd_mem, 0xa0000);
  if (m == (void *)-1) {
    munmap((void *)0, 0x502);
    close(fd_mem);
    real_mem_deinit();
    return 0;
  }
  close(fd_mem);
  return 1;
}


x86 assembly (clang, -O0):
<LRMI_common_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1060 <real_mem_init@plt>
cmp    $0x0,%eax
jne    1184 <LRMI_common_init+0x24>
movl   $0x0,-0x4(%rbp)
jmp    12b5 <LRMI_common_init+0x155>
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
lea    0xe6c(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b9 <LRMI_common_init+0x59>
mov    $0x0,%al
call   1050 <real_mem_deinit@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b5 <LRMI_common_init+0x155>
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
mov    0x2df7(%rip),%rax
or     (%rax),%edx
mov    0x2dfe(%rip),%rax
or     (%rax),%edx
mov    0x2ddd(%rip),%rax
mov    (%rax),%ecx
mov    0x2e04(%rip),%rax
or     (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x502,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x10(%rbp)
jne    122b <LRMI_common_init+0xcb>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    $0x0,%al
call   1050 <real_mem_deinit@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b5 <LRMI_common_init+0x155>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edx
mov    0x2d85(%rip),%rax
or     (%rax),%edx
mov    0x2d74(%rip),%rax
mov    (%rax),%ecx
mov    0x2d9b(%rip),%rax
or     (%rax),%ecx
mov    -0x14(%rbp),%r8d
mov    $0xa0000,%edi
mov    $0x60000,%esi
mov    $0xa0000,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x10(%rbp)
jne    12a6 <LRMI_common_init+0x146>
xor    %eax,%eax
mov    %eax,%edi
mov    $0x502,%esi
call   1070 <munmap@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    $0x0,%al
call   1050 <real_mem_deinit@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b5 <LRMI_common_init+0x155>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<LRMI_common_init>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <real_mem_init@plt>
test   %eax,%eax
je     1243 <LRMI_common_init+0xe3>
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
lea    0xe74(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1223 <LRMI_common_init+0xc3>
mov    %eax,%ebp
mov    0x2e55(%rip),%r14
mov    0x2e16(%rip),%r15
mov    (%r15),%edx
or     (%r14),%edx
mov    0x2e19(%rip),%rax
or     (%rax),%edx
mov    0x2df8(%rip),%r12
mov    0x2e21(%rip),%r13
mov    0x0(%r13),%ecx
or     (%r12),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    $0x502,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     1235 <LRMI_common_init+0xd5>
mov    (%r15),%edx
or     (%r14),%edx
mov    0x0(%r13),%ecx
or     (%r12),%ecx
mov    $0xa0000,%edi
mov    $0x60000,%esi
mov    %ebp,%r8d
mov    $0xa0000,%r9d
call   1030 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     1227 <LRMI_common_init+0xc7>
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0x1,%ebx
jmp    1243 <LRMI_common_init+0xe3>
xor    %ebx,%ebx
jmp    123c <LRMI_common_init+0xdc>
xor    %ebx,%ebx
xor    %edi,%edi
mov    $0x502,%esi
call   1070 <munmap@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
call   1050 <real_mem_deinit@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
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
@PROT_EXEC = global i32 0
@MAP_FIXED = global i32 0
@MAP_SHARED = global i32 0
define i32 @LRMI_common_init() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = call i32 (...) @real_mem_init()
%5 = icmp ne i32 %4, 0
br i1 %5, label %7, label %6
6: ; preds = %0
store i32 0, i32* %1
br label %50
7: ; preds = %0
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = call i32 (...) @real_mem_deinit()
store i32 0, i32* %1
br label %50
14: ; preds = %7
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @PROT_WRITE
%17 = or i32 %15, %16
%18 = load i32, i32* @PROT_EXEC
%19 = or i32 %17, %18
%20 = load i32, i32* @MAP_FIXED
%21 = load i32, i32* @MAP_SHARED
%22 = or i32 %20, %21
%23 = load i32, i32* %3
%24 = call i8* @mmap(i8* null, i32 1282, i32 %19, i32 %22, i32 %23, i32 0)
store i8* %24, i8** %2
%25 = load i8*, i8** %2
%26 = icmp eq i8* %25, inttoptr (i64 -1 to i8*)
br i1 %26, label %27, label %31
27: ; preds = %14
%28 = load i32, i32* %3
%29 = call i32 @close(i32 %28)
%30 = call i32 (...) @real_mem_deinit()
store i32 0, i32* %1
br label %50
31: ; preds = %14
%32 = load i32, i32* @PROT_READ
%33 = load i32, i32* @PROT_WRITE
%34 = or i32 %32, %33
%35 = load i32, i32* @MAP_FIXED
%36 = load i32, i32* @MAP_SHARED
%37 = or i32 %35, %36
%38 = load i32, i32* %3
%39 = call i8* @mmap(i8* inttoptr (i64 655360 to i8*), i32 393216, i32 %34, i32 %37, i32 %38, i32 655360)
store i8* %39, i8** %2
%40 = load i8*, i8** %2
%41 = icmp eq i8* %40, inttoptr (i64 -1 to i8*)
br i1 %41, label %42, label %47
42: ; preds = %31
%43 = call i32 @munmap(i8* null, i32 1282)
%44 = load i32, i32* %3
%45 = call i32 @close(i32 %44)
%46 = call i32 (...) @real_mem_deinit()
store i32 0, i32* %1
br label %50
47: ; preds = %31
%48 = load i32, i32* %3
%49 = call i32 @close(i32 %48)
store i32 1, i32* %1
br label %50
50: ; preds = %47, %42, %27, %12, %6
%51 = load i32, i32* %1
ret i32 %51
}

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
@PROT_EXEC = global i32 0
@MAP_FIXED = global i32 0
@MAP_SHARED = global i32 0
define i32 @LRMI_common_init() {
%1 = tail call i32 (...) @real_mem_init()
%2 = icmp eq i32 %1, 0
br i1 %2, label %38, label %3
3: ; preds = %0
%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 eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 (...) @real_mem_deinit()
br label %38
9: ; preds = %3
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %11, %10
%13 = load i32, i32* @PROT_EXEC
%14 = or i32 %12, %13
%15 = load i32, i32* @MAP_FIXED
%16 = load i32, i32* @MAP_SHARED
%17 = or i32 %16, %15
%18 = tail call i8* @mmap(i8* null, i32 1282, i32 %14, i32 %17, i32 %5, i32 0)
%19 = icmp eq i8* %18, inttoptr (i64 -1 to i8*)
br i1 %19, label %20, label %23
20: ; preds = %9
%21 = tail call i32 @close(i32 %5)
%22 = tail call i32 (...) @real_mem_deinit()
br label %38
23: ; preds = %9
%24 = load i32, i32* @PROT_READ
%25 = load i32, i32* @PROT_WRITE
%26 = or i32 %25, %24
%27 = load i32, i32* @MAP_FIXED
%28 = load i32, i32* @MAP_SHARED
%29 = or i32 %28, %27
%30 = tail call i8* @mmap(i8* inttoptr (i64 655360 to i8*), i32 393216, i32 %26, i32 %29, i32 %5, i32 655360)
%31 = icmp eq i8* %30, inttoptr (i64 -1 to i8*)
br i1 %31, label %32, label %36
32: ; preds = %23
%33 = tail call i32 @munmap(i8* null, i32 1282)
%34 = tail call i32 @close(i32 %5)
%35 = tail call i32 (...) @real_mem_deinit()
br label %38
36: ; preds = %23
%37 = tail call i32 @close(i32 %5)
br label %38
38: ; preds = %0, %36, %32, %20, %7
%39 = phi i32 [ 0, %7 ], [ 0, %20 ], [ 0, %32 ], [ 1, %36 ], [ 0, %0 ]
ret i32 %39
}