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

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int, int);
int /*<<< orphan*/ printf(char *, char const *, ...);
char *strerror(int);
int write(int, void *, int);
int write_fw(const char *name, void *fw, ssize_t size) {
  int fd, n;
  if ((fd = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) {
    printf("Open error: %s\n", strerror(fd));
    return fd;
  }
  n = write(fd, fw, size);
  if (n < 0)
    printf("Error writing %s: %s\n", name, strerror(fd));
  else if (n < size)
    printf("Error writing %s: file is incomplete (%s)\n", name, strerror(fd));
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<write_fw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    0x2e81(%rip),%rax
or     (%rax),%esi
mov    0x2e70(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11bc <write_fw+0x6c>
mov    -0x20(%rbp),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1247 <write_fw+0xf7>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1202 <write_fw+0xb2>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%edi
call   1070 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1238 <write_fw+0xe8>
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1233 <write_fw+0xe3>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%edi
call   1070 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1238 <write_fw+0xe8>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_fw>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e6a(%rip),%rcx
mov    0x2e93(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e78(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
mov    $0x1a4,%edx
call   1060 <open@plt>
mov    %eax,%ebx
mov    %eax,%edi
test   %eax,%eax
js     11a9 <write_fw+0x59>
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
js     11c1 <write_fw+0x71>
cmp    %ebp,%eax
jge    11df <write_fw+0x8f>
lea    0xe7f(%rip),%rbp
jmp    11c8 <write_fw+0x78>
call   1070 <strerror@plt>
lea    0xe4b(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11e8 <write_fw+0x98>
lea    0xe48(%rip),%rbp
mov    %ebx,%edi
call   1070 <strerror@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str = [16 x i8] c"Open error: %s\0A\00"
@.str.1 = [22 x i8] c"Error writing %s: %s\0A\00"
@.str.2 = [43 x i8] c"Error writing %s: file is incomplete (%s)\0A\00"
define i32 @write_fw(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_CREAT
%13 = or i32 %11, %12
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %10, i32 %15, i32 420)
store i32 %16, i32* %8
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %3
%19 = load i32, i32* %8
%20 = call i8* @strerror(i32 %19)
%21 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i32, i32* %8
store i32 %22, i32* %4
br label %48
23: ; preds = %3
%24 = load i32, i32* %8
%25 = load i8*, i8** %6
%26 = load i32, i32* %7
%27 = call i32 @write(i32 %24, i8* %25, i32 %26)
store i32 %27, i32* %9
%28 = load i32, i32* %9
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %23
%31 = load i8*, i8** %5
%32 = load i32, i32* %8
%33 = call i8* @strerror(i32 %32)
%34 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %31, i8* %33)
br label %45
35: ; preds = %23
%36 = load i32, i32* %9
%37 = load i32, i32* %7
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %44
39: ; preds = %35
%40 = load i8*, i8** %5
%41 = load i32, i32* %8
%42 = call i8* @strerror(i32 %41)
%43 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %40, i8* %42)
br label %44
44: ; preds = %39, %35
br label %45
45: ; preds = %44, %30
%46 = load i32, i32* %8
%47 = call i32 @close(i32 %46)
store i32 0, i32* %4
br label %48
48: ; preds = %45, %18
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str = [16 x i8] c"Open error: %s\0A\00"
@.str.1 = [22 x i8] c"Error writing %s: %s\0A\00"
@.str.2 = [43 x i8] c"Error writing %s: file is incomplete (%s)\0A\00"
define i32 @write_fw(i8* %0, i8* nocapture %1, i32 %2) {
%4 = load i32, i32* @O_WRONLY
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @O_TRUNC
%8 = or i32 %6, %7
%9 = tail call i32 @open(i8* %0, i32 %8, i32 420)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = tail call i8* @strerror(i32 %9)
%13 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %25
14: ; preds = %3
%15 = tail call i32 @write(i32 %9, i8* %1, i32 %2)
%16 = icmp slt i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = icmp slt i32 %15, %2
br i1 %18, label %19, label %23
19: ; preds = %17, %14
%20 = phi i8* [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), %14 ], [ getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), %17 ]
%21 = tail call i8* @strerror(i32 %9)
%22 = tail call i32 (i8*, i8*, ...) @printf(i8* %20, i8* %0, i8* %21)
br label %23
23: ; preds = %19, %17
%24 = tail call i32 @close(i32 %9)
br label %25
25: ; preds = %23, %11
%26 = phi i32 [ %9, %11 ], [ 0, %23 ]
ret i32 %26
}