#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*/ block;

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
unsigned char get(int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, char *, ...);
scalar_t__ write(int, unsigned char *, int);
void readbios(char *name, int len) {
  int filefd;
  int filesize = 0;
  unsigned char block[256];
  int i, j;
  filefd = open(name, O_WRONLY | O_TRUNC | O_CREAT, 0644);
  if (filefd < 0) {
    printf("Couldn't create file %s for writing\n", name);
    return;
  }
  for (i = j = 0; i < len; ++i) {
    block[j++] = get(i);
    if (j < sizeof(block))
      continue;
    filesize += write(filefd, block, j);
    j = 0;
  }
  close(filefd);
  if (filesize != len) {
    printf("Error during write of %s file\n", name);
    return;
  }
  printf("BIOS contents saved to %s, $%x bytes\n", name, len);
}


x86 assembly (clang, -O0):
<readbios>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    0x2e73(%rip),%rax
or     (%rax),%esi
mov    0x2e72(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11b6 <readbios+0x66>
mov    -0x8(%rbp),%rsi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1298 <readbios+0x148>
movl   $0x0,-0x128(%rbp)
movl   $0x0,-0x124(%rbp)
mov    -0x124(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1258 <readbios+0x108>
mov    -0x124(%rbp),%edi
call   1060 <get@plt>
mov    %al,%cl
mov    -0x128(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x128(%rbp)
cltq
mov    %cl,-0x120(%rbp,%rax,1)
movslq -0x128(%rbp),%rax
cmp    $0x100,%rax
jae    1218 <readbios+0xc8>
jmp    1244 <readbios+0xf4>
mov    -0x10(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    -0x128(%rbp),%edx
call   1030 <write@plt>
mov    %rax,%rcx
movslq -0x14(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x128(%rbp)
mov    -0x124(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x124(%rbp)
jmp    11ca <readbios+0x7a>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     1283 <readbios+0x133>
mov    -0x8(%rbp),%rsi
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1298 <readbios+0x148>
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readbios>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    0x2e60(%rip),%rcx
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %esi,%r13d
mov    %rdi,%rbx
mov    0x2e78(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
test   %eax,%eax
js     11e4 <readbios+0x94>
mov    %eax,%r12d
mov    %rbx,0x8(%rsp)
test   %r13d,%r13d
jle    11f0 <readbios+0xa0>
xor    %ebp,%ebp
lea    0x10(%rsp),%r15
xor    %ebx,%ebx
xor    %r14d,%r14d
jmp    11b8 <readbios+0x68>
nopl   (%rax)
add    $0x1,%ebx
cmp    %ebx,%r13d
je     11f3 <readbios+0xa3>
mov    %ebx,%edi
call   1060 <get@plt>
mov    %ebp,%ecx
add    $0x1,%ebp
mov    %al,0x10(%rsp,%rcx,1)
cmp    $0x100,%ebp
jb     11b0 <readbios+0x60>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
add    %eax,%r14d
xor    %ebp,%ebp
jmp    11b0 <readbios+0x60>
lea    0xe15(%rip),%rdi
mov    %rbx,%rsi
jmp    1233 <readbios+0xe3>
xor    %r14d,%r14d
mov    %r12d,%edi
call   1050 <close@plt>
cmp    %r13d,%r14d
jne    1227 <readbios+0xd7>
lea    0xe3d(%rip),%rdi
mov    0x8(%rsp),%rsi
mov    %r13d,%edx
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@plt>
lea    0xdf7(%rip),%rdi
mov    0x8(%rsp),%rsi
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@plt>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str = [37 x i8] c"Couldn't create file %s for writing\0A\00"
@.str.1 = [31 x i8] c"Error during write of %s file\0A\00"
@.str.2 = [38 x i8] c"BIOS contents saved to %s, $%x bytes\0A\00"
define void @readbios(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %6
%10 = load i8*, i8** %3
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_TRUNC
%13 = or i32 %11, %12
%14 = load i32, i32* @O_CREAT
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %10, i32 %15, i32 420)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = load i8*, i8** %3
%21 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %63
22: ; preds = %2
store i32 0, i32* %9
store i32 0, i32* %8
br label %23
23: ; preds = %47, %22
%24 = load i32, i32* %8
%25 = load i32, i32* %4
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %50
27: ; preds = %23
%28 = load i32, i32* %8
%29 = call zeroext i8 @get(i32 %28)
%30 = load i32, i32* %9
%31 = add nsw i32 %30, 1
store i32 %31, i32* %9
%32 = sext i32 %30 to i64
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 %32
store i8 %29, i8* %33
%34 = load i32, i32* %9
%35 = sext i32 %34 to i64
%36 = icmp ult i64 %35, 256
br i1 %36, label %37, label %38
37: ; preds = %27
br label %47
38: ; preds = %27
%39 = load i32, i32* %5
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%41 = load i32, i32* %9
%42 = call i64 @write(i32 %39, i8* %40, i32 %41)
%43 = load i32, i32* %6
%44 = sext i32 %43 to i64
%45 = add nsw i64 %44, %42
%46 = trunc i64 %45 to i32
store i32 %46, i32* %6
store i32 0, i32* %9
br label %47
47: ; preds = %38, %37
%48 = load i32, i32* %8
%49 = add nsw i32 %48, 1
store i32 %49, i32* %8
br label %23
50: ; preds = %23
%51 = load i32, i32* %5
%52 = call i32 @close(i32 %51)
%53 = load i32, i32* %6
%54 = load i32, i32* %4
%55 = icmp ne i32 %53, %54
br i1 %55, label %56, label %59
56: ; preds = %50
%57 = load i8*, i8** %3
%58 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %57)
br label %63
59: ; preds = %50
%60 = load i8*, i8** %3
%61 = load i32, i32* %4
%62 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %60, i32 %61)
br label %63
63: ; preds = %59, %56, %19
ret void
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str = [37 x i8] c"Couldn't create file %s for writing\0A\00"
@.str.1 = [31 x i8] c"Error during write of %s file\0A\00"
@.str.2 = [38 x i8] c"BIOS contents saved to %s, $%x bytes\0A\00"
define void @readbios(i8* %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %6, %5
%8 = load i32, i32* @O_CREAT
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* %0, i32 %9, i32 420)
%11 = icmp slt i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %2
%13 = icmp sgt i32 %1, 0
br i1 %13, label %16, label %34
14: ; preds = %2
%15 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %42
16: ; preds = %12, %29
%17 = phi i32 [ %31, %29 ], [ 0, %12 ]
%18 = phi i32 [ %32, %29 ], [ 0, %12 ]
%19 = phi i32 [ %30, %29 ], [ 0, %12 ]
%20 = tail call zeroext i8 @get(i32 %18)
%21 = add nsw i32 %17, 1
%22 = zext i32 %17 to i64
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %22
store i8 %20, i8* %23
%24 = icmp ult i32 %21, 256
br i1 %24, label %29, label %25
25: ; preds = %16
%26 = call i64 @write(i32 %10, i8* %4, i32 %21)
%27 = trunc i64 %26 to i32
%28 = add i32 %19, %27
br label %29
29: ; preds = %16, %25
%30 = phi i32 [ %19, %16 ], [ %28, %25 ]
%31 = phi i32 [ %21, %16 ], [ 0, %25 ]
%32 = add nuw nsw i32 %18, 1
%33 = icmp eq i32 %32, %1
br i1 %33, label %34, label %16
34: ; preds = %29, %12
%35 = phi i32 [ 0, %12 ], [ %30, %29 ]
%36 = tail call i32 @close(i32 %10)
%37 = icmp eq i32 %35, %1
br i1 %37, label %40, label %38
38: ; preds = %34
%39 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %42
40: ; preds = %34
%41 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %1)
br label %42
42: ; preds = %40, %38, %14
ret void
}