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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int const);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int write(int, unsigned char const *, size_t);
void header2bin(const unsigned char *data, size_t len, const int productid) {
  char buf[32];
  int fd;
  int ret;
  snprintf(buf, sizeof(buf) - 1, "moxa-%04x.fw", productid);
  fd = open(buf, O_WRONLY | O_CREAT | O_TRUNC,
            S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  if (fd < 0) {
    fprintf(stderr, "Creating file %s: %s\n", buf, strerror(errno));
    exit(EXIT_FAILURE);
  }
  ret = write(fd, data, len);
  if (ret != len) {
    fprintf(stderr, "Write truncated!\n");
    exit(EXIT_FAILURE);
  }
  if (ret < 0) {
    fprintf(stderr, "Writing data to file: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  close(fd);
}


x86 assembly (clang, -O0):
<header2bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x1f,%esi
lea    0xe6a(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    0x2e49(%rip),%rax
or     (%rax),%esi
mov    0x2e38(%rip),%rax
or     (%rax),%esi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
mov    0x2e0e(%rip),%rax
or     (%rax),%edx
mov    0x2de5(%rip),%rax
or     (%rax),%edx
mov    0x2dd4(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1238 <header2bin+0xc8>
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    %rax,%rcx
lea    0xdea(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x44(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x48(%rbp)
movslq -0x48(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     127e <header2bin+0x10e>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x48(%rbp)
jge    12c4 <header2bin+0x154>
mov    0x2d39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x58(%rbp),%edi
mov    %rax,%rdx
lea    0xd86(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x44(%rbp),%edi
call   1050 <close@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<header2bin>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edx,%ecx
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe79(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x1f,%esi
call   1040 <snprintf@plt>
mov    0x2df2(%rip),%rax
mov    0x2e53(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e40(%rip),%rax
or     (%rax),%esi
mov    0x2de7(%rip),%rax
mov    0x2e18(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2ded(%rip),%rax
or     (%rax),%edx
mov    0x2ddc(%rip),%rax
or     (%rax),%edx
mov    %rbp,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     120d <header2bin+0x9d>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
movslq %eax,%rcx
cmp    %rbx,%rcx
jne    123c <header2bin+0xcc>
test   %eax,%eax
js     1255 <header2bin+0xe5>
mov    %ebp,%edi
call   1050 <close@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2db4(%rip),%rax
mov    (%rax),%ebx
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xde2(%rip),%rsi
mov    %rsp,%rdx
mov    %ebx,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    127f <header2bin+0x10f>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
lea    0xdd7(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    127f <header2bin+0x10f>
mov    0x2d6c(%rip),%rax
mov    (%rax),%ebx
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdc2(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"moxa-%04x.fw\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@stderr = global i32 0
@.str.1 = [22 x i8] c"Creating file %s: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.2 = [18 x i8] c"Write truncated!\0A\00"
@.str.3 = [26 x i8] c"Writing data to file: %s\0A\00"
define void @header2bin(i8* %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca [32 x i8]
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%10 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%11 = load i32, i32* %6
%12 = call i32 @snprintf(i8* %10, i32 31, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_CREAT
%16 = or i32 %14, %15
%17 = load i32, i32* @O_TRUNC
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IRUSR
%20 = load i32, i32* @S_IWUSR
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IRGRP
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IROTH
%25 = or i32 %23, %24
%26 = call i32 @open(i8* %13, i32 %18, i32 %25)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %37
29: ; preds = %3
%30 = load i32, i32* @stderr
%31 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %31, i8* %33)
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %3
%38 = load i32, i32* %8
%39 = load i8*, i8** %4
%40 = load i64, i64* %5
%41 = call i32 @write(i32 %38, i8* %39, i64 %40)
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = sext i32 %42 to i64
%44 = load i64, i64* %5
%45 = icmp ne i64 %43, %44
br i1 %45, label %46, label %51
46: ; preds = %37
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%49 = load i32, i32* @EXIT_FAILURE
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %37
%52 = load i32, i32* %9
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %61
54: ; preds = %51
%55 = load i32, i32* @stderr
%56 = load i32, i32* @errno
%57 = call i8* @strerror(i32 %56)
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %57)
%59 = load i32, i32* @EXIT_FAILURE
%60 = call i32 @exit(i32 %59)
unreachable
61: ; preds = %51
%62 = load i32, i32* %8
%63 = call i32 @close(i32 %62)
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"moxa-%04x.fw\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@stderr = global i32 0
@.str.1 = [22 x i8] c"Creating file %s: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.2 = [18 x i8] c"Write truncated!\0A\00"
@.str.3 = [26 x i8] c"Writing data to file: %s\0A\00"
define void @header2bin(i8* nocapture %0, i64 %1, i32 %2) {
%4 = alloca [32 x i8]
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 31, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %2)
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_CREAT
%9 = or i32 %8, %7
%10 = load i32, i32* @O_TRUNC
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IRUSR
%13 = load i32, i32* @S_IWUSR
%14 = or i32 %13, %12
%15 = load i32, i32* @S_IRGRP
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IROTH
%18 = or i32 %16, %17
%19 = call i32 @open(i8* %5, i32 %11, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %3
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %5, i8* %24)
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %3
%29 = call i32 @write(i32 %19, i8* %0, i64 %1)
%30 = sext i32 %29 to i64
%31 = icmp eq i64 %30, %1
br i1 %31, label %37, label %32
32: ; preds = %28
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %28
%38 = icmp slt i32 %29, 0
br i1 %38, label %39, label %46
39: ; preds = %37
%40 = load i32, i32* @stderr
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %42)
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %37
%47 = call i32 @close(i32 %19)
ret void
}