#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 off_t;

/* Variables and functions */
void const *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
void *mmap(int /*<<< orphan*/, int, int /*<<< orphan*/, int /*<<< orphan*/, int,
           int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ parse_vbios(void const *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  const void *ptr;
  int fd;
  off_t offset;
  if (argc == 2) {
    fd = open(argv[1], O_RDONLY);
    offset = 0;
  } else {
    fd = open("/dev/mem", O_RDONLY);
    offset = 0xc0000;
  }
  if (fd < 0) {
    fprintf(stderr, "open failed: %s\n", strerror(errno));
    return 1;
  }
  ptr = mmap(0, 65536, PROT_READ, MAP_SHARED, fd, offset);
  if (ptr == MAP_FAILED) {
    fprintf(stderr, "mmap failed: %s\n", strerror(errno));
    return 1;
  }
  parse_vbios(ptr);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    11a5 <main+0x45>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
jmp    11c4 <main+0x64>
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
lea    0xe4b(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
movl   $0xc0000,-0x20(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1206 <main+0xa6>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe14(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1294 <main+0x134>
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
mov    0x2dd2(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
mov    -0x20(%rbp),%r9d
xor    %edi,%edi
mov    $0x10000,%esi
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d7d(%rip),%rcx
cmp    (%rcx),%rax
jne    127c <main+0x11c>
mov    0x2d7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xdaf(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1294 <main+0x134>
mov    -0x18(%rbp),%rdi
call   1060 <parse_vbios@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
jne    1172 <main+0x12>
mov    0x8(%rsi),%rdi
xor    %r14d,%r14d
jmp    117f <main+0x1f>
mov    $0xc0000,%r14d
lea    0xe81(%rip),%rdi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11d6 <main+0x76>
mov    %eax,%ebx
mov    0x2e5e(%rip),%rax
mov    (%rax),%edx
mov    0x2e45(%rip),%rax
mov    (%rax),%ecx
xor    %ebp,%ebp
xor    %edi,%edi
mov    $0x10000,%esi
mov    %ebx,%r8d
mov    %r14d,%r9d
call   1030 <mmap@plt>
mov    0x2df8(%rip),%rcx
cmp    (%rcx),%rax
je     11f6 <main+0x96>
mov    %rax,%rdi
call   1060 <parse_vbios@plt>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    1223 <main+0xc3>
mov    0x2deb(%rip),%rax
mov    (%rax),%ebx
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe15(%rip),%rsi
jmp    1214 <main+0xb4>
mov    0x2dcb(%rip),%rax
mov    (%rax),%ebx
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe06(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [9 x i8] c"/dev/mem\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"open failed: %s\0A\00"
@errno = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [17 x i8] c"mmap failed: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 2
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %7
store i32 0, i32* %8
br label %20
17: ; preds = %2
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %18)
store i32 %19, i32* %7
store i32 786432, i32* %8
br label %20
20: ; preds = %17, %11
%21 = load i32, i32* %7
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %26)
store i32 1, i32* %3
br label %47
28: ; preds = %20
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @MAP_SHARED
%31 = load i32, i32* %7
%32 = load i32, i32* %8
%33 = call i8* @mmap(i32 0, i32 65536, i32 %29, i32 %30, i32 %31, i32 %32)
store i8* %33, i8** %6
%34 = load i8*, i8** %6
%35 = load i8*, i8** @MAP_FAILED
%36 = icmp eq i8* %34, %35
br i1 %36, label %37, label %42
37: ; preds = %28
%38 = load i32, i32* @stderr
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %40)
store i32 1, i32* %3
br label %47
42: ; preds = %28
%43 = load i8*, i8** %6
%44 = call i32 @parse_vbios(i8* %43)
%45 = load i32, i32* %7
%46 = call i32 @close(i32 %45)
store i32 0, i32* %3
br label %47
47: ; preds = %42, %37, %23
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [9 x i8] c"/dev/mem\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"open failed: %s\0A\00"
@errno = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [17 x i8] c"mmap failed: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
br label %7
7: ; preds = %2, %4
%8 = phi i8* [ %6, %4 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %2 ]
%9 = phi i32 [ 0, %4 ], [ 786432, %2 ]
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* %8, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %7
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %32
18: ; preds = %7
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @MAP_SHARED
%21 = tail call i8* @mmap(i32 0, i32 65536, i32 %19, i32 %20, i32 %11, i32 %9)
%22 = load i8*, i8** @MAP_FAILED
%23 = icmp eq i8* %21, %22
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = tail call i8* @strerror(i32 %26)
%28 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %27)
br label %32
29: ; preds = %18
%30 = tail call i32 @parse_vbios(i8* %21)
%31 = tail call i32 @close(i32 %11)
br label %32
32: ; preds = %29, %24, %13
%33 = phi i32 [ 1, %13 ], [ 1, %24 ], [ 0, %29 ]
ret i32 %33
}