#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 /*<<< orphan*/ uint8_t;
typedef int int32_t;

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
void *mmap(void *, int, int, int /*<<< orphan*/, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int32_t ece391_vidmap(uint8_t **screen_start) {
  static int mem_fd = -1;
  void *mem_image;
  if (mem_fd == -1) {
    mem_fd = open("/dev/mem", O_RDWR);
  }
  if ((mem_image = mmap((void *)0, 1024 * 1024, PROT_READ | PROT_WRITE,
                        MAP_SHARED, mem_fd, 0)) == MAP_FAILED) {
    perror("mmap low memory");
    return -1;
  }
  *screen_start = (uint8_t *)(mem_image + 0xb8000);
  return 0;
}


x86 assembly (clang, -O0):
<ece391_vidmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpl   $0xffffffff,0x2ef5(%rip)
jne    1164 <ece391_vidmap+0x34>
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
lea    0xea7(%rip),%rdi
call   1040 <open@plt>
mov    %eax,0x2ed4(%rip)
mov    0x2e8d(%rip),%rax
mov    (%rax),%edx
mov    0x2e54(%rip),%rax
or     (%rax),%edx
mov    0x2e6b(%rip),%rax
mov    (%rax),%ecx
mov    0x2eb2(%rip),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x100000,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e1e(%rip),%rcx
cmp    (%rcx),%rax
jne    11c3 <ece391_vidmap+0x93>
lea    0xe57(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dc <ece391_vidmap+0xac>
mov    -0x18(%rbp),%rcx
add    $0xb8000,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ece391_vidmap>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2efa(%rip),%r8d
cmp    $0xffffffff,%r8d
jne    1162 <ece391_vidmap+0x32>
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
lea    0xeac(%rip),%rdi
call   1040 <open@plt>
mov    %eax,%r8d
mov    %eax,0x2ed6(%rip)
mov    0x2e8f(%rip),%rax
mov    0x2e58(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e6d(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    $0x100000,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2e2b(%rip),%rcx
cmp    (%rcx),%rax
je     11ad <ece391_vidmap+0x7d>
add    $0xb8000,%rax
mov    %rax,(%r14)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe55(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11a3 <ece391_vidmap+0x73>

LLVM IR (-O0):
@ece391_vidmap.mem_fd = internal global i32 -1
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [16 x i8] c"mmap low memory\00"
define i32 @ece391_vidmap(i32** %0) {
%2 = alloca i32
%3 = alloca i32**
%4 = alloca i8*
store i32** %0, i32*** %3
%5 = load i32, i32* @ece391_vidmap.mem_fd
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %1
%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* @ece391_vidmap.mem_fd
br label %10
10: ; preds = %7, %1
%11 = load i32, i32* @PROT_READ
%12 = load i32, i32* @PROT_WRITE
%13 = or i32 %11, %12
%14 = load i32, i32* @MAP_SHARED
%15 = load i32, i32* @ece391_vidmap.mem_fd
%16 = call i8* @mmap(i8* null, i32 1048576, i32 %13, i32 %14, i32 %15, i32 0)
store i8* %16, i8** %4
%17 = load i8*, i8** @MAP_FAILED
%18 = icmp eq i8* %16, %17
br i1 %18, label %19, label %21
19: ; preds = %10
%20 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %26
21: ; preds = %10
%22 = load i8*, i8** %4
%23 = getelementptr i8, i8* %22, i64 753664
%24 = bitcast i8* %23 to i32*
%25 = load i32**, i32*** %3
store i32* %24, i32** %25
store i32 0, i32* %2
br label %26
26: ; preds = %21, %19
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
@ece391_vidmap.mem_fd = internal unnamed_addr global i32 -1
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [16 x i8] c"mmap low memory\00"
define i32 @ece391_vidmap(i32** nocapture writeonly %0) {
%2 = load i32, i32* @ece391_vidmap.mem_fd
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* @ece391_vidmap.mem_fd
br label %7
7: ; preds = %4, %1
%8 = phi i32 [ %6, %4 ], [ %2, %1 ]
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @PROT_WRITE
%11 = or i32 %10, %9
%12 = load i32, i32* @MAP_SHARED
%13 = tail call i8* @mmap(i8* null, i32 1048576, i32 %11, i32 %12, i32 %8, i32 0)
%14 = load i8*, i8** @MAP_FAILED
%15 = icmp eq i8* %13, %14
br i1 %15, label %16, label %18
16: ; preds = %7
%17 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %21
18: ; preds = %7
%19 = getelementptr i8, i8* %13, i64 753664
%20 = bitcast i32** %0 to i8**
store i8* %19, i8** %20
br label %21
21: ; preds = %18, %16
%22 = phi i32 [ -1, %16 ], [ 0, %18 ]
ret i32 %22
}