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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
scalar_t__ lseek(int, scalar_t__, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
size_t read(int, char *, size_t);
int copy_sys_bios(void) {
  int mem_fd;
  if ((mem_fd = open("/dev/mem", O_RDONLY)) < 0) {
    perror("opening memory");
    return (0);
  }
  if (lseek(mem_fd, (off_t)0xF0000, SEEK_SET) != (off_t)0xF0000)
    goto Error;
  if (read(mem_fd, (char *)0xF0000, (size_t)0xFFFF) != (size_t)0xFFFF)
    goto Error;
  close(mem_fd);
  return (1);
Error:
  perror("sys_bios");
  close(mem_fd);
  return (0);
}


x86 assembly (clang, -O0):
<copy_sys_bios>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1191 <copy_sys_bios+0x41>
lea    0xe89(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    120a <copy_sys_bios+0xba>
mov    -0x8(%rbp),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%edx
mov    $0xf0000,%esi
call   1030 <lseek@plt>
cmp    $0xf0000,%rax
je     11b8 <copy_sys_bios+0x68>
jmp    11ef <copy_sys_bios+0x9f>
mov    -0x8(%rbp),%edi
mov    $0xf0000,%esi
mov    $0xffff,%edx
call   1050 <read@plt>
cmp    $0xffff,%rax
je     11db <copy_sys_bios+0x8b>
jmp    11ef <copy_sys_bios+0x9f>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    120a <copy_sys_bios+0xba>
lea    0xe22(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_sys_bios>:
push   %rbx
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
lea    0xe9f(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11c5 <copy_sys_bios+0x75>
mov    %eax,%ebx
mov    0x2e65(%rip),%rax
mov    (%rax),%edx
mov    $0xf0000,%esi
mov    %ebx,%edi
call   1030 <lseek@plt>
cmp    $0xf0000,%rax
jne    11b0 <copy_sys_bios+0x60>
mov    $0xf0000,%esi
mov    $0xffff,%edx
mov    %ebx,%edi
call   1050 <read@plt>
cmp    $0xffff,%rax
jne    11b0 <copy_sys_bios+0x60>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0x1,%eax
pop    %rbx
ret
lea    0xe61(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    11d1 <copy_sys_bios+0x81>
lea    0xe3d(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@.str.1 = [15 x i8] c"opening memory\00"
@SEEK_SET = global i32 0
@.str.2 = [9 x i8] c"sys_bios\00"
define i32 @copy_sys_bios() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @O_RDONLY
%4 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3)
store i32 %4, i32* %2
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %26
8: ; preds = %0
%9 = load i32, i32* %2
%10 = load i32, i32* @SEEK_SET
%11 = call i64 @lseek(i32 %9, i64 983040, i32 %10)
%12 = icmp ne i64 %11, 983040
br i1 %12, label %13, label %14
13: ; preds = %8
br label %22
14: ; preds = %8
%15 = load i32, i32* %2
%16 = call i64 @read(i32 %15, i8* inttoptr (i64 983040 to i8*), i64 65535)
%17 = icmp ne i64 %16, 65535
br i1 %17, label %18, label %19
18: ; preds = %14
br label %22
19: ; preds = %14
%20 = load i32, i32* %2
%21 = call i32 @close(i32 %20)
store i32 1, i32* %1
br label %26
22: ; preds = %18, %13
%23 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* %2
%25 = call i32 @close(i32 %24)
store i32 0, i32* %1
br label %26
26: ; preds = %22, %19, %6
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@.str.1 = [15 x i8] c"opening memory\00"
@SEEK_SET = global i32 0
@.str.2 = [9 x i8] c"sys_bios\00"
define i32 @copy_sys_bios() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %18
6: ; preds = %0
%7 = load i32, i32* @SEEK_SET
%8 = tail call i64 @lseek(i32 %2, i64 983040, i32 %7)
%9 = icmp eq i64 %8, 983040
br i1 %9, label %10, label %15
10: ; preds = %6
%11 = tail call i64 @read(i32 %2, i8* inttoptr (i64 983040 to i8*), i64 65535)
%12 = icmp eq i64 %11, 65535
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @close(i32 %2)
br label %18
15: ; preds = %10, %6
%16 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @close(i32 %2)
br label %18
18: ; preds = %15, %13, %4
%19 = phi i32 [ 0, %4 ], [ 0, %15 ], [ 1, %13 ]
ret i32 %19
}