#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*/ uint8_t;
typedef int ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EINVAL;
int EIO;
int ENODEV;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int fwrite(int /*<<< orphan*/ const *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
ssize_t __file_write(const char *fname, const uint8_t *buff, size_t len) {
  int rc;
  FILE *fp;
  if ((fname == NULL) || (buff == NULL) || (len == 0))
    return -EINVAL;
  fp = fopen(fname, "w+");
  if (!fp) {
    fprintf(stderr, "err: Cannot open file %s: %s\n", fname, strerror(errno));
    return -ENODEV;
  }
  rc = fwrite(buff, len, 1, fp);
  if (rc == -1) {
    fprintf(stderr, "err: Cannot write to %s: %s\n", fname, strerror(errno));
    fclose(fp);
    return -EIO;
  }
  fclose(fp);
  return rc;
}


x86 assembly (clang, -O0):
<__file_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1185 <__file_write+0x35>
cmpq   $0x0,-0x18(%rbp)
je     1185 <__file_write+0x35>
cmpq   $0x0,-0x20(%rbp)
jne    1198 <__file_write+0x48>
xor    %eax,%eax
mov    0x2e3a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1288 <__file_write+0x138>
mov    -0x10(%rbp),%rdi
lea    0xe5d(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1202 <__file_write+0xb2>
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xe19(%rip),%rsi
call   1040 <fprintf@plt>
xor    %eax,%eax
mov    0x2dc8(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1288 <__file_write+0x138>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rcx
mov    $0x1,%edx
call   1060 <fwrite@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1279 <__file_write+0x129>
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xdc9(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
mov    0x2d71(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1288 <__file_write+0x138>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__file_write>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     11a8 <__file_write+0x58>
mov    %rsi,%rbx
test   %rsi,%rsi
je     11a8 <__file_write+0x58>
mov    %rdx,%r12
test   %rdx,%rdx
je     11a8 <__file_write+0x58>
mov    %rdi,%r15
lea    0xe89(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11b1 <__file_write+0x61>
mov    %rax,%r14
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
mov    %rax,%rcx
call   1060 <fwrite@plt>
cmp    $0xffffffff,%eax
je     11e5 <__file_write+0x95>
mov    %eax,%ebx
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    1223 <__file_write+0xd3>
mov    0x2e19(%rip),%rax
jmp    121f <__file_write+0xcf>
mov    0x2e20(%rip),%rax
mov    (%rax),%ebx
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe34(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    0x2ddd(%rip),%rax
jmp    121f <__file_write+0xcf>
mov    0x2dec(%rip),%rax
mov    (%rax),%ebx
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe1e(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    0x2dc1(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@.str = [3 x i8] c"w+\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"err: Cannot open file %s: %s\0A\00"
@errno = global i32 0
@ENODEV = global i32 0
@.str.2 = [29 x i8] c"err: Cannot write to %s: %s\0A\00"
@EIO = global i32 0
define i32 @__file_write(i8* %0, i32* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32*
store i8* %0, i8** %5
store i32* %1, i32** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %5
%11 = icmp eq i8* %10, null
br i1 %11, label %18, label %12
12: ; preds = %3
%13 = load i32*, i32** %6
%14 = icmp eq i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = load i64, i64* %7
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15, %12, %3
%19 = load i32, i32* @EINVAL
%20 = sub nsw i32 0, %19
store i32 %20, i32* %4
br label %55
21: ; preds = %15
%22 = load i8*, i8** %5
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %23, i32** %9
%24 = load i32*, i32** %9
%25 = icmp ne i32* %24, null
br i1 %25, label %34, label %26
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %5
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %28, i8* %30)
%32 = load i32, i32* @ENODEV
%33 = sub nsw i32 0, %32
store i32 %33, i32* %4
br label %55
34: ; preds = %21
%35 = load i32*, i32** %6
%36 = load i64, i64* %7
%37 = load i32*, i32** %9
%38 = call i32 @fwrite(i32* %35, i64 %36, i32 1, i32* %37)
store i32 %38, i32* %8
%39 = load i32, i32* %8
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %51
41: ; preds = %34
%42 = load i32, i32* @stderr
%43 = load i8*, i8** %5
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %43, i8* %45)
%47 = load i32*, i32** %9
%48 = call i32 @fclose(i32* %47)
%49 = load i32, i32* @EIO
%50 = sub nsw i32 0, %49
store i32 %50, i32* %4
br label %55
51: ; preds = %34
%52 = load i32*, i32** %9
%53 = call i32 @fclose(i32* %52)
%54 = load i32, i32* %8
store i32 %54, i32* %4
br label %55
55: ; preds = %51, %41, %26, %18
%56 = load i32, i32* %4
ret i32 %56
}

LLVM IR (-O2):
@EINVAL = global i32 0
@.str = [3 x i8] c"w+\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"err: Cannot open file %s: %s\0A\00"
@errno = global i32 0
@ENODEV = global i32 0
@.str.2 = [29 x i8] c"err: Cannot write to %s: %s\0A\00"
@EIO = global i32 0
define i32 @__file_write(i8* %0, i32* %1, i64 %2) {
%4 = icmp eq i8* %0, null
%5 = icmp eq i32* %1, null
%6 = or i1 %4, %5
%7 = icmp eq i64 %2, 0
%8 = or i1 %6, %7
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = load i32, i32* @EINVAL
%11 = sub nsw i32 0, %10
br label %35
12: ; preds = %3
%13 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %22
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %18)
%20 = load i32, i32* @ENODEV
%21 = sub nsw i32 0, %20
br label %35
22: ; preds = %12
%23 = tail call i32 @fwrite(i32* %1, i64 %2, i32 1, i32* %13)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %33
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = tail call i8* @strerror(i32 %27)
%29 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %28)
%30 = tail call i32 @fclose(i32* %13)
%31 = load i32, i32* @EIO
%32 = sub nsw i32 0, %31
br label %35
33: ; preds = %22
%34 = tail call i32 @fclose(i32* %13)
br label %35
35: ; preds = %33, %25, %15, %9
%36 = phi i32 [ %11, %9 ], [ %32, %25 ], [ %23, %33 ], [ %21, %15 ]
ret i32 %36
}