#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 */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int open(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int write(int, void *, int);
void save_bios_to_file(void) {
  static int num = 0;
  int size, count;
  char file_name[256];
  int fd;
  sprintf(file_name, "bios_%i.fil", num);
  if ((fd = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 00644)) == -1)
    return;
  size = (*(unsigned char *)(0xc0000 + 2)) * 512;
  if ((count = write(fd, (void *)(0xc0000), size)) != size)
    fprintf(stderr, "only saved %i of %i bytes\n", size, count);
  num++;
}


x86 assembly (clang, -O0):
<save_bios_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
lea    -0x110(%rbp),%rdi
mov    0x2eec(%rip),%edx
lea    0xea1(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    0x2e7d(%rip),%rax
or     (%rax),%esi
mov    0x2e6c(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1050 <open@plt>
mov    %eax,-0x114(%rbp)
cmp    $0xffffffff,%eax
jne    11a4 <save_bios_to_file+0x64>
jmp    11fb <save_bios_to_file+0xbb>
mov    $0xc0002,%eax
movzbl (%rax),%eax
shl    $0x9,%eax
mov    %eax,-0x4(%rbp)
mov    -0x114(%rbp),%edi
mov    -0x4(%rbp),%edx
mov    $0xc0000,%esi
call   1030 <write@plt>
mov    %eax,-0x8(%rbp)
cmp    -0x4(%rbp),%eax
je     11ec <save_bios_to_file+0xac>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xe25(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e52(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e49(%rip)
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_bios_to_file>:
push   %rbx
sub    $0x100,%rsp
mov    0x2ef6(%rip),%edx
lea    0xeab(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1060 <sprintf@plt>
mov    0x2e59(%rip),%rax
mov    0x2e8a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e77(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1a4,%edx
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11ca <save_bios_to_file+0x8a>
movzbl 0xc0002,%ebx
shl    $0x9,%ebx
mov    $0xc0000,%esi
mov    %eax,%edi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     11c3 <save_bios_to_file+0x83>
mov    0x2e1f(%rip),%rcx
mov    (%rcx),%edi
lea    0xe52(%rip),%rsi
mov    %ebx,%edx
mov    %eax,%ecx
call   1040 <fprintf@plt>
addl   $0x1,0x2e7a(%rip)
add    $0x100,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@save_bios_to_file.num = internal global i32 0
@.str = [12 x i8] c"bios_%i.fil\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"only saved %i of %i bytes\0A\00"
define void @save_bios_to_file() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = load i32, i32* @save_bios_to_file.num
%7 = call i32 @sprintf(i8* %5, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%9 = load i32, i32* @O_WRONLY
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
%12 = load i32, i32* @O_TRUNC
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %8, i32 %13, i32 420)
store i32 %14, i32* %4
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %0
br label %34
17: ; preds = %0
%18 = load i8, i8* inttoptr (i64 786434 to i8*)
%19 = zext i8 %18 to i32
%20 = mul nsw i32 %19, 512
store i32 %20, i32* %1
%21 = load i32, i32* %4
%22 = load i32, i32* %1
%23 = call i32 @write(i32 %21, i8* inttoptr (i64 786432 to i8*), i32 %22)
store i32 %23, i32* %2
%24 = load i32, i32* %1
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %31
26: ; preds = %17
%27 = load i32, i32* @stderr
%28 = load i32, i32* %1
%29 = load i32, i32* %2
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %28, i32 %29)
br label %31
31: ; preds = %26, %17
%32 = load i32, i32* @save_bios_to_file.num
%33 = add nsw i32 %32, 1
store i32 %33, i32* @save_bios_to_file.num
br label %34
34: ; preds = %31, %16
ret void
}

LLVM IR (-O2):
@save_bios_to_file.num = internal unnamed_addr global i32 0
@.str = [12 x i8] c"bios_%i.fil\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"only saved %i of %i bytes\0A\00"
define void @save_bios_to_file() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @save_bios_to_file.num
%4 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %7, %8
%10 = call i32 @open(i8* %2, i32 %9, i32 420)
%11 = icmp eq i32 %10, -1
br i1 %11, label %24, label %12
12: ; preds = %0
%13 = load i8, i8* inttoptr (i64 786434 to i8*)
%14 = zext i8 %13 to i32
%15 = shl nuw nsw i32 %14, 9
%16 = tail call i32 @write(i32 %10, i8* inttoptr (i64 786432 to i8*), i32 %15)
%17 = icmp eq i32 %16, %15
br i1 %17, label %21, label %18
18: ; preds = %12
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %16)
br label %21
21: ; preds = %18, %12
%22 = load i32, i32* @save_bios_to_file.num
%23 = add nsw i32 %22, 1
store i32 %23, i32* @save_bios_to_file.num
br label %24
24: ; preds = %0, %21
ret void
}