#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 uint32_t;

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
char *get_file_type(short);
int open(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, int, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, unsigned char *, int);
void phx_write_file(unsigned char *BIOSImage, char *filename, short filetype,
                    int offset, uint32_t length) {
  int fd;
  if (filename[0] == '\0') {
    sprintf(filename, "%s_0x%08x-0x%08x", get_file_type(filetype), offset,
            offset + length);
  }
  fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
  if (fd < 0) {
    fprintf(stderr, "Error: unable to open %s: %s\n\n", filename,
            strerror(errno));
    return;
  }
  write(fd, BIOSImage + offset + 0x18, length - 0x18);
  close(fd);
}


x86 assembly (clang, -O0):
<phx_write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %dx,%ax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11cd <phx_write_file+0x5d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movswl -0x12(%rbp),%edi
call   1060 <get_file_type@plt>
mov    -0x28(%rbp),%rdi
mov    %rax,%rdx
mov    -0x18(%rbp),%ecx
mov    -0x18(%rbp),%r8d
add    -0x1c(%rbp),%r8d
lea    0xe38(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
mov    0x2e17(%rip),%rax
or     (%rax),%esi
mov    0x2e06(%rip),%rax
or     (%rax),%esi
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
mov    0x2dd4(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    124d <phx_write_file+0xdd>
mov    0x2da9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xdce(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1272 <phx_write_file+0x102>
mov    -0x20(%rbp),%edi
mov    -0x8(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
add    $0x18,%rsi
mov    -0x1c(%rbp),%edx
sub    $0x18,%edx
call   1030 <write@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<phx_write_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8d,%r14d
mov    %ecx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r12
cmpb   $0x0,(%rsi)
jne    11aa <phx_write_file+0x3a>
movswl %dx,%edi
call   1060 <get_file_type@plt>
lea    (%r14,%r15,1),%r8d
lea    0xe64(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
mov    %r15d,%ecx
call   1080 <sprintf@plt>
mov    0x2e27(%rip),%rax
mov    0x2e40(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e2d(%rip),%rax
or     (%rax),%esi
mov    0x2de4(%rip),%rax
mov    0x2dfd(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     120d <phx_write_file+0x9d>
mov    %eax,%ebp
movslq %r15d,%rax
lea    (%r12,%rax,1),%rsi
add    $0x18,%rsi
add    $0xffffffe8,%r14d
mov    %ebp,%edi
mov    %r14d,%edx
call   1030 <write@plt>
mov    %ebp,%edi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <close@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%ebp
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xde6(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fprintf@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"%s_0x%08x-0x%08x\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@stderr = global i32 0
@.str.1 = [31 x i8] c"Error: unable to open %s: %s\0A\0A\00"
@errno = global i32 0
define void @phx_write_file(i8* %0, i8* %1, i16 signext %2, i32 %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i16
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i16 %2, i16* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%12 = load i8*, i8** %7
%13 = getelementptr inbounds i8, i8* %12, i64 0
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %26
17: ; preds = %5
%18 = load i8*, i8** %7
%19 = load i16, i16* %8
%20 = call i8* @get_file_type(i16 signext %19)
%21 = load i32, i32* %9
%22 = load i32, i32* %9
%23 = load i32, i32* %10
%24 = add nsw i32 %22, %23
%25 = call i32 @sprintf(i8* %18, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %20, i32 %21, i32 %24)
br label %26
26: ; preds = %17, %5
%27 = load i8*, i8** %7
%28 = load i32, i32* @O_RDWR
%29 = load i32, i32* @O_CREAT
%30 = or i32 %28, %29
%31 = load i32, i32* @O_TRUNC
%32 = or i32 %30, %31
%33 = load i32, i32* @S_IRUSR
%34 = load i32, i32* @S_IWUSR
%35 = or i32 %33, %34
%36 = call i32 @open(i8* %27, i32 %32, i32 %35)
store i32 %36, i32* %11
%37 = load i32, i32* %11
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %26
%40 = load i32, i32* @stderr
%41 = load i8*, i8** %7
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %41, i8* %43)
br label %57
45: ; preds = %26
%46 = load i32, i32* %11
%47 = load i8*, i8** %6
%48 = load i32, i32* %9
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
%51 = getelementptr inbounds i8, i8* %50, i64 24
%52 = load i32, i32* %10
%53 = sub nsw i32 %52, 24
%54 = call i32 @write(i32 %46, i8* %51, i32 %53)
%55 = load i32, i32* %11
%56 = call i32 @close(i32 %55)
br label %57
57: ; preds = %45, %39
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"%s_0x%08x-0x%08x\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@stderr = global i32 0
@.str.1 = [31 x i8] c"Error: unable to open %s: %s\0A\0A\00"
@errno = global i32 0
define void @phx_write_file(i8* nocapture %0, i8* %1, i16 signext %2, i32 %3, i32 %4) {
%6 = load i8, i8* %1
%7 = icmp eq i8 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %5
%9 = tail call i8* @get_file_type(i16 signext %2)
%10 = add nsw i32 %4, %3
%11 = tail call i32 @sprintf(i8* %1, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %9, i32 %3, i32 %10)
br label %12
12: ; preds = %8, %5
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_CREAT
%15 = or i32 %14, %13
%16 = load i32, i32* @O_TRUNC
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IRUSR
%19 = load i32, i32* @S_IWUSR
%20 = or i32 %19, %18
%21 = tail call i32 @open(i8* %1, i32 %17, i32 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %12
%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 ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %26)
br label %35
28: ; preds = %12
%29 = sext i32 %3 to i64
%30 = getelementptr inbounds i8, i8* %0, i64 %29
%31 = getelementptr inbounds i8, i8* %30, i64 24
%32 = add nsw i32 %4, -24
%33 = tail call i32 @write(i32 %21, i8* %31, i32 %32)
%34 = tail call i32 @close(i32 %21)
br label %35
35: ; preds = %28, %23
ret void
}