#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 */
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;
  if (argc == 2) {
    fd = open(argv[1], O_RDONLY);
    ptr = mmap(0, 65536, PROT_READ, MAP_SHARED, fd, 0);
  } else {
    fd = open("/dev/mem", O_RDONLY);
    ptr = mmap(0, 65536, PROT_READ, MAP_SHARED, fd, 0xc0000);
  }
  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    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    11c8 <main+0x68>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e58(%rip),%rax
mov    (%rax),%edx
mov    0x2e3f(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x10000,%esi
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
jmp    120c <main+0xac>
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
lea    0xe28(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
mov    0x2df8(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %edi,%edi
mov    $0x10000,%esi
mov    $0xc0000,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2da1(%rip),%rcx
cmp    (%rcx),%rax
jne    1258 <main+0xf8>
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xdc2(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1270 <main+0x110>
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    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x2,%edi
jne    119b <main+0x3b>
mov    0x8(%rsi),%rdi
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ebx
mov    0x2e77(%rip),%rax
mov    (%rax),%edx
mov    0x2e5e(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x10000,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
jmp    11d4 <main+0x74>
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
lea    0xe55(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    0x2e26(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x10000,%esi
mov    %ebx,%r8d
mov    $0xc0000,%r9d
call   1030 <mmap@plt>
mov    0x2dd8(%rip),%rcx
cmp    (%rcx),%rax
je     11f8 <main+0x98>
mov    %rax,%rdi
call   1060 <parse_vbios@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    0x2dc9(%rip),%rax
mov    (%rax),%ebx
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdf3(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%eax
pop    %rbx
ret

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

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