#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 /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int);
size_t strlen(char const *);
int write(int, char const *, size_t);
ssize_t file_write(const char *path, const char *val, ssize_t sz) {
  int fd = open(path, O_WRONLY);
  int ret = -1;
  if (fd >= 0) {
    ret = write(fd, val, (sz < 0) ? strlen(val) : (size_t)sz);
    close(fd);
  } else {
    fprintf(stderr,
            "[WARN]"
            "failed to open \"%s\" for writing:%d(%s)"
            "\n",
            path ?: "<NULL>", errno, strerror(errno));
    ;
  }
  return ret;
}


x86 assembly (clang, -O0):
<file_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     11ed <file_write+0x8d>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11c5 <file_write+0x65>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
jmp    11cd <file_write+0x6d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edi
mov    -0x38(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
jmp    1263 <file_write+0x103>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     1218 <file_write+0xb8>
mov    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
jmp    1228 <file_write+0xc8>
lea    0xe0f(%rip),%rax
mov    %rax,-0x50(%rbp)
jmp    1228 <file_write+0xc8>
mov    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x60(%rbp),%rdx
mov    -0x54(%rbp),%ecx
mov    %rax,%r8
lea    0xda2(%rip),%rsi
call   1060 <fprintf@plt>
movslq -0x20(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11af <file_write+0x4f>
mov    %eax,%ebp
test   %r15,%r15
jns    1194 <file_write+0x34>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <write@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
movslq %ebx,%rax
jmp    11f5 <file_write+0x95>
mov    0x2e2a(%rip),%rax
mov    (%rax),%r14d
test   %rbx,%rbx
lea    0xe6b(%rip),%rbp
cmovne %rbx,%rbp
mov    0x2e0a(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1080 <strerror@plt>
lea    0xe22(%rip),%rsi
mov    %r14d,%edi
mov    %rbp,%rdx
mov    %ebx,%ecx
mov    %rax,%r8
call   1060 <fprintf@plt>
mov    $0xffffffffffffffff,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str = [46 x i8] c"[WARN]failed to open \22%s\22 for writing:%d(%s)\0A\00"
@.str.1 = [7 x i8] c"<NULL>\00"
@errno = global i32 0
define i64 @file_write(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_WRONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %7
store i32 -1, i32* %8
%12 = load i32, i32* %7
%13 = icmp sge i32 %12, 0
br i1 %13, label %14, label %29
14: ; preds = %3
%15 = load i32, i32* %7
%16 = load i8*, i8** %5
%17 = load i64, i64* %6
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i8*, i8** %5
%21 = call i64 @strlen(i8* %20)
br label %24
22: ; preds = %14
%23 = load i64, i64* %6
br label %24
24: ; preds = %22, %19
%25 = phi i64 [ %21, %19 ], [ %23, %22 ]
%26 = call i32 @write(i32 %15, i8* %16, i64 %25)
store i32 %26, i32* %8
%27 = load i32, i32* %7
%28 = call i32 @close(i32 %27)
br label %41
29: ; preds = %3
%30 = load i32, i32* @stderr
%31 = load i8*, i8** %4
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %34
33: ; preds = %29
br label %35
34: ; preds = %29
br label %35
35: ; preds = %34, %33
%36 = phi i8* [ %31, %33 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %34 ]
%37 = load i32, i32* @errno
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %36, i32 %37, i8* %39)
br label %41
41: ; preds = %35, %24
%42 = load i32, i32* %8
%43 = sext i32 %42 to i64
ret i64 %43
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str = [46 x i8] c"[WARN]failed to open \22%s\22 for writing:%d(%s)\0A\00"
@.str.1 = [7 x i8] c"<NULL>\00"
@errno = global i32 0
define i64 @file_write(i8* %0, i8* nocapture %1, i64 %2) {
%4 = load i32, i32* @O_WRONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp sgt i32 %5, -1
br i1 %6, label %7, label %16
7: ; preds = %3
%8 = icmp slt i64 %2, 0
br i1 %8, label %9, label %11
9: ; preds = %7
%10 = tail call i64 @strlen(i8* dereferenceable(1) %1)
br label %11
11: ; preds = %7, %9
%12 = phi i64 [ %10, %9 ], [ %2, %7 ]
%13 = tail call i32 @write(i32 %5, i8* %1, i64 %12)
%14 = tail call i32 @close(i32 %5)
%15 = sext i32 %13 to i64
br label %23
16: ; preds = %3
%17 = load i32, i32* @stderr
%18 = icmp eq i8* %0, null
%19 = select i1 %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %0
%20 = load i32, i32* @errno
%21 = tail call i8* @strerror(i32 %20)
%22 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %19, i32 %20, i8* %21)
br label %23
23: ; preds = %16, %11
%24 = phi i64 [ %15, %11 ], [ -1, %16 ]
ret i64 %24
}