#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*/ perror(char *);
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();
    perror("open /dev/mem");
    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();
    perror("mmap /dev/mem");
    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();
    perror("mmap /dev/mem");
    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    1194 <LRMI_common_init+0x24>
movl   $0x0,-0x4(%rbp)
jmp    12e9 <LRMI_common_init+0x179>
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
lea    0xe61(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11d5 <LRMI_common_init+0x65>
mov    $0x0,%al
call   1050 <real_mem_deinit@plt>
lea    0xe3c(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e9 <LRMI_common_init+0x179>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
mov    0x2ddb(%rip),%rax
or     (%rax),%edx
mov    0x2de2(%rip),%rax
or     (%rax),%edx
mov    0x2dc1(%rip),%rax
mov    (%rax),%ecx
mov    0x2de8(%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    1253 <LRMI_common_init+0xe3>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    $0x0,%al
call   1050 <real_mem_deinit@plt>
lea    0xdcc(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e9 <LRMI_common_init+0x179>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edx
mov    0x2d5d(%rip),%rax
or     (%rax),%edx
mov    0x2d4c(%rip),%rax
mov    (%rax),%ecx
mov    0x2d73(%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    12da <LRMI_common_init+0x16a>
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>
lea    0xd45(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e9 <LRMI_common_init+0x179>
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     1231 <LRMI_common_init+0xc1>
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
lea    0xe69(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1242 <LRMI_common_init+0xd2>
mov    %eax,%ebp
mov    0x2e45(%rip),%r14
mov    0x2e06(%rip),%r15
mov    (%r15),%edx
or     (%r14),%edx
mov    0x2e09(%rip),%rax
or     (%rax),%edx
mov    0x2de8(%rip),%r12
mov    0x2e11(%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     1267 <LRMI_common_init+0xf7>
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     1259 <LRMI_common_init+0xe9>
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <real_mem_deinit@plt>
lea    0xdae(%rip),%rdi
call   1090 <perror@plt>
jmp    1231 <LRMI_common_init+0xc1>
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>
lea    0xd92(%rip),%rdi
call   1090 <perror@plt>
jmp    1231 <LRMI_common_init+0xc1>

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@.str.1 = [14 x i8] c"open /dev/mem\00"
@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
@.str.2 = [14 x i8] c"mmap /dev/mem\00"
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 %53
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 %15
12: ; preds = %7
%13 = call i32 (...) @real_mem_deinit()
%14 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %53
15: ; preds = %7
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @PROT_WRITE
%18 = or i32 %16, %17
%19 = load i32, i32* @PROT_EXEC
%20 = or i32 %18, %19
%21 = load i32, i32* @MAP_FIXED
%22 = load i32, i32* @MAP_SHARED
%23 = or i32 %21, %22
%24 = load i32, i32* %3
%25 = call i8* @mmap(i8* null, i32 1282, i32 %20, i32 %23, i32 %24, i32 0)
store i8* %25, i8** %2
%26 = load i8*, i8** %2
%27 = icmp eq i8* %26, inttoptr (i64 -1 to i8*)
br i1 %27, label %28, label %33
28: ; preds = %15
%29 = load i32, i32* %3
%30 = call i32 @close(i32 %29)
%31 = call i32 (...) @real_mem_deinit()
%32 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %53
33: ; preds = %15
%34 = load i32, i32* @PROT_READ
%35 = load i32, i32* @PROT_WRITE
%36 = or i32 %34, %35
%37 = load i32, i32* @MAP_FIXED
%38 = load i32, i32* @MAP_SHARED
%39 = or i32 %37, %38
%40 = load i32, i32* %3
%41 = call i8* @mmap(i8* inttoptr (i64 655360 to i8*), i32 393216, i32 %36, i32 %39, i32 %40, i32 655360)
store i8* %41, i8** %2
%42 = load i8*, i8** %2
%43 = icmp eq i8* %42, inttoptr (i64 -1 to i8*)
br i1 %43, label %44, label %50
44: ; preds = %33
%45 = call i32 @munmap(i8* null, i32 1282)
%46 = load i32, i32* %3
%47 = call i32 @close(i32 %46)
%48 = call i32 (...) @real_mem_deinit()
%49 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %53
50: ; preds = %33
%51 = load i32, i32* %3
%52 = call i32 @close(i32 %51)
store i32 1, i32* %1
br label %53
53: ; preds = %50, %44, %28, %12, %6
%54 = load i32, i32* %1
ret i32 %54
}

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