#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int map_mem(char *, int, int, int);
int /*<<< orphan*/ scan_rom(char *, char *);
int /*<<< orphan*/ scan_sysfs(char *, char *, int, int, int);
int /*<<< orphan*/ stderr;
int main() {
  int rc;
  if (map_mem("/dev/mem", 0, 0xA0000, 1) == 0)
    fprintf(stderr, "PASS: /dev/mem 0x0-0xa0000 is readable\n");
  else
    fprintf(stderr, "FAIL: /dev/mem 0x0-0xa0000 not accessible\n");
  if (map_mem("/dev/mem", 0xA0000, 0x20000, 0) == 0)
    fprintf(stderr, "PASS: /dev/mem 0xa0000-0xc0000 is mappable\n");
  else
    fprintf(stderr, "FAIL: /dev/mem 0xa0000-0xc0000 not accessible\n");
  if (map_mem("/dev/mem", 0xC0000, 0x40000, 1) == 0)
    fprintf(stderr, "PASS: /dev/mem 0xc0000-0x100000 is readable\n");
  else
    fprintf(stderr, "FAIL: /dev/mem 0xc0000-0x100000 not accessible\n");
  rc = map_mem("/dev/mem", 0, 1024 * 1024, 0);
  if (rc == 0)
    fprintf(stderr, "PASS: /dev/mem 0x0-0x100000 is mappable\n");
  else if (rc > 0)
    fprintf(stderr, "PASS: /dev/mem 0x0-0x100000 not mappable\n");
  else
    fprintf(stderr, "FAIL: /dev/mem 0x0-0x100000 not accessible\n");
  scan_sysfs("/sys/class/pci_bus", "legacy_mem", 0, 0xA0000, 1);
  scan_sysfs("/sys/class/pci_bus", "legacy_mem", 0xA0000, 0x20000, 0);
  scan_sysfs("/sys/class/pci_bus", "legacy_mem", 0xC0000, 0x40000, 1);
  scan_sysfs("/sys/class/pci_bus", "legacy_mem", 0, 1024 * 1024, 0);
  scan_rom("/sys/devices", "rom");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
xor    %esi,%esi
mov    $0xa0000,%edx
mov    $0x1,%ecx
call   1030 <map_mem@plt>
cmp    $0x0,%eax
jne    118a <main+0x4a>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
lea    0xe89(%rip),%rsi
call   1040 <fprintf@plt>
jmp    119f <main+0x5f>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
lea    0xe97(%rip),%rsi
call   1040 <fprintf@plt>
lea    0xe5a(%rip),%rdi
mov    $0xa0000,%esi
mov    $0x20000,%edx
xor    %ecx,%ecx
call   1030 <map_mem@plt>
cmp    $0x0,%eax
jne    11da <main+0x9a>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe8c(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11ef <main+0xaf>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe9e(%rip),%rsi
call   1040 <fprintf@plt>
lea    0xe0a(%rip),%rdi
mov    $0xc0000,%esi
mov    $0x40000,%edx
mov    $0x1,%ecx
call   1030 <map_mem@plt>
cmp    $0x0,%eax
jne    122d <main+0xed>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xe94(%rip),%rsi
call   1040 <fprintf@plt>
jmp    1242 <main+0x102>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
lea    0xea7(%rip),%rsi
call   1040 <fprintf@plt>
lea    0xdb7(%rip),%rdi
xor    %ecx,%ecx
mov    $0x100000,%edx
mov    %ecx,%esi
call   1030 <map_mem@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    127e <main+0x13e>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
lea    0xea0(%rip),%rsi
call   1040 <fprintf@plt>
jmp    12bc <main+0x17c>
cmpl   $0x0,-0x8(%rbp)
jle    12a2 <main+0x162>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
lea    0xea5(%rip),%rsi
call   1040 <fprintf@plt>
jmp    12b7 <main+0x177>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
lea    0xeb5(%rip),%rsi
call   1040 <fprintf@plt>
jmp    12bc <main+0x17c>
lea    0xed0(%rip),%rdi
lea    0xedc(%rip),%rsi
xor    %edx,%edx
mov    $0xa0000,%ecx
mov    $0x1,%r8d
call   1050 <scan_sysfs@plt>
lea    0xeb0(%rip),%rdi
lea    0xebc(%rip),%rsi
mov    $0xa0000,%edx
mov    $0x20000,%ecx
xor    %r8d,%r8d
call   1050 <scan_sysfs@plt>
lea    0xe90(%rip),%rdi
lea    0xe9c(%rip),%rsi
mov    $0xc0000,%edx
mov    $0x40000,%ecx
mov    $0x1,%r8d
call   1050 <scan_sysfs@plt>
lea    0xe6d(%rip),%rdi
lea    0xe79(%rip),%rsi
xor    %r8d,%r8d
mov    $0x100000,%ecx
mov    %r8d,%edx
call   1050 <scan_sysfs@plt>
lea    0xe6d(%rip),%rdi
lea    0xe73(%rip),%rsi
call   1060 <scan_rom@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xeb5(%rip),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xa0000,%edx
mov    $0x1,%ecx
call   1030 <map_mem@plt>
test   %eax,%eax
mov    0x2e78(%rip),%r14
mov    (%r14),%edi
lea    0xe97(%rip),%rax
lea    0xeb8(%rip),%rsi
cmove  %rax,%rsi
call   1040 <fprintf@plt>
mov    %rbx,%rdi
mov    $0xa0000,%esi
mov    $0x20000,%edx
xor    %ecx,%ecx
call   1030 <map_mem@plt>
test   %eax,%eax
lea    0xebd(%rip),%rax
lea    0xee2(%rip),%rsi
cmove  %rax,%rsi
mov    (%r14),%edi
call   1040 <fprintf@plt>
mov    %rbx,%rdi
mov    $0xc0000,%esi
mov    $0x40000,%edx
mov    $0x1,%ecx
call   1030 <map_mem@plt>
test   %eax,%eax
lea    0xee5(%rip),%rax
lea    0xf0b(%rip),%rsi
cmove  %rax,%rsi
mov    (%r14),%edi
call   1040 <fprintf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100000,%edx
xor    %ecx,%ecx
call   1030 <map_mem@plt>
test   %eax,%eax
je     1208 <main+0xc8>
mov    (%r14),%edi
jle    1214 <main+0xd4>
lea    0xf37(%rip),%rsi
jmp    121b <main+0xdb>
mov    (%r14),%edi
lea    0xf02(%rip),%rsi
jmp    121b <main+0xdb>
lea    0xf4c(%rip),%rsi
call   1040 <fprintf@plt>
lea    0xf6c(%rip),%rbx
lea    0xf78(%rip),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
mov    $0xa0000,%ecx
mov    $0x1,%r8d
call   1050 <scan_sysfs@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0xa0000,%edx
mov    $0x20000,%ecx
xor    %r8d,%r8d
call   1050 <scan_sysfs@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0xc0000,%edx
mov    $0x40000,%ecx
mov    $0x1,%r8d
call   1050 <scan_sysfs@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
mov    $0x100000,%ecx
xor    %r8d,%r8d
call   1050 <scan_sysfs@plt>
lea    0xf1c(%rip),%rdi
lea    0xf22(%rip),%rsi
call   1060 <scan_rom@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@stderr = global i32 0
@.str.1 = [40 x i8] c"PASS: /dev/mem 0x0-0xa0000 is readable\0A\00"
@.str.2 = [43 x i8] c"FAIL: /dev/mem 0x0-0xa0000 not accessible\0A\00"
@.str.3 = [44 x i8] c"PASS: /dev/mem 0xa0000-0xc0000 is mappable\0A\00"
@.str.4 = [47 x i8] c"FAIL: /dev/mem 0xa0000-0xc0000 not accessible\0A\00"
@.str.5 = [45 x i8] c"PASS: /dev/mem 0xc0000-0x100000 is readable\0A\00"
@.str.6 = [48 x i8] c"FAIL: /dev/mem 0xc0000-0x100000 not accessible\0A\00"
@.str.7 = [41 x i8] c"PASS: /dev/mem 0x0-0x100000 is mappable\0A\00"
@.str.8 = [42 x i8] c"PASS: /dev/mem 0x0-0x100000 not mappable\0A\00"
@.str.9 = [44 x i8] c"FAIL: /dev/mem 0x0-0x100000 not accessible\0A\00"
@.str.10 = [19 x i8] c"/sys/class/pci_bus\00"
@.str.11 = [11 x i8] c"legacy_mem\00"
@.str.12 = [13 x i8] c"/sys/devices\00"
@.str.13 = [4 x i8] c"rom\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i32 @map_mem(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 0, i32 655360, i32 1)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %11
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
br label %11
11: ; preds = %8, %5
%12 = call i32 @map_mem(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 655360, i32 131072, i32 0)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
br label %20
17: ; preds = %11
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0))
br label %20
20: ; preds = %17, %14
%21 = call i32 @map_mem(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 786432, i32 262144, i32 1)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0))
br label %29
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.6, i64 0, i64 0))
br label %29
29: ; preds = %26, %23
%30 = call i32 @map_mem(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 0, i32 1048576, i32 0)
store i32 %30, i32* %2
%31 = load i32, i32* %2
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0))
br label %46
36: ; preds = %29
%37 = load i32, i32* %2
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.8, i64 0, i64 0))
br label %45
42: ; preds = %36
%43 = load i32, i32* @stderr
%44 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.9, i64 0, i64 0))
br label %45
45: ; preds = %42, %39
br label %46
46: ; preds = %45, %33
%47 = call i32 @scan_sysfs(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.11, i64 0, i64 0), i32 0, i32 655360, i32 1)
%48 = call i32 @scan_sysfs(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.11, i64 0, i64 0), i32 655360, i32 131072, i32 0)
%49 = call i32 @scan_sysfs(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.11, i64 0, i64 0), i32 786432, i32 262144, i32 1)
%50 = call i32 @scan_sysfs(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.11, i64 0, i64 0), i32 0, i32 1048576, i32 0)
%51 = call i32 @scan_rom(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.12, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.13, i64 0, i64 0))
%52 = load i32, i32* %1
ret i32 %52
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@stderr = global i32 0
@.str.1 = [40 x i8] c"PASS: /dev/mem 0x0-0xa0000 is readable\0A\00"
@.str.2 = [43 x i8] c"FAIL: /dev/mem 0x0-0xa0000 not accessible\0A\00"
@.str.3 = [44 x i8] c"PASS: /dev/mem 0xa0000-0xc0000 is mappable\0A\00"
@.str.4 = [47 x i8] c"FAIL: /dev/mem 0xa0000-0xc0000 not accessible\0A\00"
@.str.5 = [45 x i8] c"PASS: /dev/mem 0xc0000-0x100000 is readable\0A\00"
@.str.6 = [48 x i8] c"FAIL: /dev/mem 0xc0000-0x100000 not accessible\0A\00"
@.str.7 = [41 x i8] c"PASS: /dev/mem 0x0-0x100000 is mappable\0A\00"
@.str.8 = [42 x i8] c"PASS: /dev/mem 0x0-0x100000 not mappable\0A\00"
@.str.9 = [44 x i8] c"FAIL: /dev/mem 0x0-0x100000 not accessible\0A\00"
@.str.10 = [19 x i8] c"/sys/class/pci_bus\00"
@.str.11 = [11 x i8] c"legacy_mem\00"
@.str.12 = [13 x i8] c"/sys/devices\00"
@.str.13 = [4 x i8] c"rom\00"
define i32 @main() {
%1 = tail call i32 @map_mem(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 0, i32 655360, i32 1)
%2 = icmp eq i32 %1, 0
%3 = load i32, i32* @stderr
%4 = select i1 %2, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0)
%5 = tail call i32 @fprintf(i32 %3, i8* %4)
%6 = tail call i32 @map_mem(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 655360, i32 131072, i32 0)
%7 = icmp eq i32 %6, 0
%8 = select i1 %7, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0)
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* %8)
%11 = tail call i32 @map_mem(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 786432, i32 262144, i32 1)
%12 = icmp eq i32 %11, 0
%13 = select i1 %12, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.6, i64 0, i64 0)
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* %13)
%16 = tail call i32 @map_mem(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 0, i32 1048576, i32 0)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %0
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0))
br label %28
21: ; preds = %0
%22 = icmp sgt i32 %16, 0
%23 = load i32, i32* @stderr
br i1 %22, label %24, label %26
24: ; preds = %21
%25 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.8, i64 0, i64 0))
br label %28
26: ; preds = %21
%27 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.9, i64 0, i64 0))
br label %28
28: ; preds = %24, %26, %18
%29 = tail call i32 @scan_sysfs(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.11, i64 0, i64 0), i32 0, i32 655360, i32 1)
%30 = tail call i32 @scan_sysfs(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.11, i64 0, i64 0), i32 655360, i32 131072, i32 0)
%31 = tail call i32 @scan_sysfs(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.11, i64 0, i64 0), i32 786432, i32 262144, i32 1)
%32 = tail call i32 @scan_sysfs(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.11, i64 0, i64 0), i32 0, i32 1048576, i32 0)
%33 = tail call i32 @scan_rom(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.12, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.13, i64 0, i64 0))
ret i32 0
}