#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 */
scalar_t__ close(int);
int creat(char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int write(int, unsigned char *, int);
void write_rom(char *path, unsigned char *buf, int len) {
  int fd = creat(path, 0666);
  if (fd == -1) {
    perror(path);
    exit(1);
  }
  if (write(fd, buf, len) != len) {
    perror(path);
    exit(1);
  }
  if (close(fd) != 0) {
    perror(path);
    exit(1);
  }
}


x86 assembly (clang, -O0):
<write_rom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x1b6,%esi
call   1060 <creat@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1191 <write_rom+0x41>
mov    -0x8(%rbp),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
cmp    -0x14(%rbp),%eax
je     11bc <write_rom+0x6c>
mov    -0x8(%rbp),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
je     11e1 <write_rom+0x91>
mov    -0x8(%rbp),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_rom>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r14
mov    $0x1b6,%esi
call   1060 <creat@plt>
cmp    $0xffffffff,%eax
je     1197 <write_rom+0x47>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
jne    1197 <write_rom+0x47>
mov    %ebx,%edi
call   1040 <close@plt>
test   %rax,%rax
jne    1197 <write_rom+0x47>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
define void @write_rom(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
%9 = call i32 @creat(i8* %8, i32 438)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = load i8*, i8** %4
%14 = call i32 @perror(i8* %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %3
%17 = load i32, i32* %7
%18 = load i8*, i8** %5
%19 = load i32, i32* %6
%20 = call i32 @write(i32 %17, i8* %18, i32 %19)
%21 = load i32, i32* %6
%22 = icmp ne i32 %20, %21
br i1 %22, label %23, label %27
23: ; preds = %16
%24 = load i8*, i8** %4
%25 = call i32 @perror(i8* %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %16
%28 = load i32, i32* %7
%29 = call i64 @close(i32 %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %27
%32 = load i8*, i8** %4
%33 = call i32 @perror(i8* %32)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %27
ret void
}

LLVM IR (-O2):
define void @write_rom(i8* %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32 @creat(i8* %0, i32 438)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = tail call i32 @perror(i8* %0)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %3
%10 = tail call i32 @write(i32 %4, i8* %1, i32 %2)
%11 = icmp eq i32 %10, %2
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = tail call i32 @perror(i8* %0)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %9
%16 = tail call i64 @close(i32 %4)
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = tail call i32 @perror(i8* %0)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %15
ret void
}