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

/* Variables and functions */
int O_CLOEXEC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ ksft_exit_fail_msg(char *, char const *, ...);
int open(char const *, int);
int /*<<< orphan*/ strerror(int);
scalar_t__ write_nointr(int, void const *, size_t);
void write_to_file(const char *filename, const void *buf, size_t count,
                   int allowed_errno) {
  int fd, saved_errno;
  ssize_t ret;
  fd = open(filename, O_WRONLY | O_CLOEXEC);
  if (fd < 0)
    ksft_exit_fail_msg("%s - Failed to open file %s\n", strerror(errno),
                       filename);
  ret = write_nointr(fd, buf, count);
  if (ret < 0) {
    if (allowed_errno && (errno == allowed_errno)) {
      close(fd);
      return;
    }
    goto on_error;
  }
  if ((size_t)ret != count)
    goto on_error;
  close(fd);
  return;
on_error:
  saved_errno = errno;
  close(fd);
  errno = saved_errno;
  if (ret < 0)
    ksft_exit_fail_msg("%s - Failed to write to file %s\n", strerror(errno),
                       filename);
  ksft_exit_fail_msg("Failed to write to file %s\n", filename);
}


x86 assembly (clang, -O0):
<write_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    0x2e4d(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11b2 <write_to_file+0x62>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
movslq %eax,%rsi
mov    -0x8(%rbp),%rdx
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1030 <ksft_exit_fail_msg@plt>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <write_nointr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    11ff <write_to_file+0xaf>
cmpl   $0x0,-0x1c(%rbp)
je     11fa <write_to_file+0xaa>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
cmp    -0x1c(%rbp),%eax
jne    11fa <write_to_file+0xaa>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
jmp    127f <write_to_file+0x12f>
jmp    121f <write_to_file+0xcf>
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     1212 <write_to_file+0xc2>
jmp    121f <write_to_file+0xcf>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
jmp    127f <write_to_file+0x12f>
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2da3(%rip),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x30(%rbp)
jge    126d <write_to_file+0x11d>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
movslq %eax,%rsi
mov    -0x8(%rbp),%rdx
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1030 <ksft_exit_fail_msg@plt>
mov    -0x8(%rbp),%rsi
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1030 <ksft_exit_fail_msg@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_to_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r14d
mov    %rdx,%rbp
mov    %rsi,%r12
mov    %rdi,%r15
mov    0x2e65(%rip),%rax
mov    0x2e56(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11a3 <write_to_file+0x53>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
movslq %eax,%rsi
lea    0xe67(%rip),%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1030 <ksft_exit_fail_msg@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %rbp,%rdx
call   1050 <write_nointr@plt>
test   %rax,%rax
js     11c9 <write_to_file+0x79>
cmp    %rbp,%rax
jne    11ee <write_to_file+0x9e>
mov    %ebx,%edi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <close@plt>
mov    0x2e10(%rip),%r12
mov    (%r12),%ebp
mov    %ebx,%edi
call   1040 <close@plt>
test   %r14d,%r14d
je     1204 <write_to_file+0xb4>
cmp    %r14d,%ebp
jne    1204 <write_to_file+0xb4>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2deb(%rip),%r14
mov    (%r14),%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,(%r14)
jmp    1223 <write_to_file+0xd3>
mov    %ebp,(%r12)
mov    %ebp,%edi
call   1070 <strerror@plt>
movslq %eax,%rsi
lea    0xe04(%rip),%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1030 <ksft_exit_fail_msg@plt>
lea    0xdf8(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <ksft_exit_fail_msg@plt>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CLOEXEC = global i32 0
@.str = [29 x i8] c"%s - Failed to open file %s\0A\00"
@errno = global i32 0
@.str.1 = [33 x i8] c"%s - Failed to write to file %s\0A\00"
@.str.2 = [28 x i8] c"Failed to write to file %s\0A\00"
define void @write_to_file(i8* %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 %3, i32* %8
%12 = load i8*, i8** %5
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_CLOEXEC
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %12, i32 %15)
store i32 %16, i32* %9
%17 = load i32, i32* %9
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %4
%20 = load i32, i32* @errno
%21 = call i32 @strerror(i32 %20)
%22 = sext i32 %21 to i64
%23 = inttoptr i64 %22 to i8*
%24 = load i8*, i8** %5
%25 = call i32 (i8*, i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %23, i8* %24)
br label %26
26: ; preds = %19, %4
%27 = load i32, i32* %9
%28 = load i8*, i8** %6
%29 = load i64, i64* %7
%30 = call i64 @write_nointr(i32 %27, i8* %28, i64 %29)
store i64 %30, i64* %11
%31 = load i64, i64* %11
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %44
33: ; preds = %26
%34 = load i32, i32* %8
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %43
36: ; preds = %33
%37 = load i32, i32* @errno
%38 = load i32, i32* %8
%39 = icmp eq i32 %37, %38
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i32, i32* %9
%42 = call i32 @close(i32 %41)
br label %69
43: ; preds = %36, %33
br label %52
44: ; preds = %26
%45 = load i64, i64* %11
%46 = load i64, i64* %7
%47 = icmp ne i64 %45, %46
br i1 %47, label %48, label %49
48: ; preds = %44
br label %52
49: ; preds = %44
%50 = load i32, i32* %9
%51 = call i32 @close(i32 %50)
br label %69
52: ; preds = %48, %43
%53 = load i32, i32* @errno
store i32 %53, i32* %10
%54 = load i32, i32* %9
%55 = call i32 @close(i32 %54)
%56 = load i32, i32* %10
store i32 %56, i32* @errno
%57 = load i64, i64* %11
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %66
59: ; preds = %52
%60 = load i32, i32* @errno
%61 = call i32 @strerror(i32 %60)
%62 = sext i32 %61 to i64
%63 = inttoptr i64 %62 to i8*
%64 = load i8*, i8** %5
%65 = call i32 (i8*, i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %63, i8* %64)
br label %66
66: ; preds = %59, %52
%67 = load i8*, i8** %5
%68 = call i32 (i8*, i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %67)
br label %69
69: ; preds = %66, %49, %40
ret void
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CLOEXEC = global i32 0
@.str = [29 x i8] c"%s - Failed to open file %s\0A\00"
@errno = global i32 0
@.str.1 = [33 x i8] c"%s - Failed to write to file %s\0A\00"
@.str.2 = [28 x i8] c"Failed to write to file %s\0A\00"
define void @write_to_file(i8* %0, i8* %1, i64 %2, i32 %3) {
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CLOEXEC
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %4
%11 = load i32, i32* @errno
%12 = tail call i32 @strerror(i32 %11)
%13 = sext i32 %12 to i64
%14 = inttoptr i64 %13 to i8*
%15 = tail call i32 (i8*, i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %14, i8* %0)
br label %16
16: ; preds = %10, %4
%17 = tail call i64 @write_nointr(i32 %8, i8* %1, i64 %2)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %16
%20 = icmp ne i32 %3, 0
%21 = load i32, i32* @errno
%22 = icmp eq i32 %21, %3
%23 = select i1 %20, i1 %22, i1 false
%24 = tail call i32 @close(i32 %8)
br i1 %23, label %39, label %32
25: ; preds = %16
%26 = icmp eq i64 %17, %2
br i1 %26, label %27, label %29
27: ; preds = %25
%28 = tail call i32 @close(i32 %8)
br label %39
29: ; preds = %25
%30 = load i32, i32* @errno
%31 = tail call i32 @close(i32 %8)
store i32 %30, i32* @errno
br label %37
32: ; preds = %19
store i32 %21, i32* @errno
%33 = tail call i32 @strerror(i32 %21)
%34 = sext i32 %33 to i64
%35 = inttoptr i64 %34 to i8*
%36 = tail call i32 (i8*, i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %35, i8* %0)
br label %37
37: ; preds = %29, %32
%38 = tail call i32 (i8*, i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %39
39: ; preds = %19, %37, %27
ret void
}