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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int /*<<< orphan*/ stderr;
int write(int, void *, size_t);
int xwrite(int *fd, void *buf, size_t bufsz) {
  int n;
  if (*fd > -1) {
    n = write(*fd, buf, bufsz);
    if (n != bufsz) {
      fprintf(stderr, "short write: %d, errno=%d\n", n, errno);
      close(*fd);
      *fd = -1;
    }
  }
}


x86 assembly (clang, -O0):
<xwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jle    11b0 <xwrite+0x80>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     11ab <xwrite+0x7b>
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
mov    0x2e50(%rip),%rax
mov    (%rax),%ecx
lea    0xe6f(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
movl   $0xffffffff,(%rax)
jmp    11b0 <xwrite+0x80>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xwrite>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    (%rdi),%edi
test   %edi,%edi
js     117c <xwrite+0x4c>
mov    %rdx,%rbx
call   1030 <write@plt>
movslq %eax,%rcx
cmp    %rbx,%rcx
je     117c <xwrite+0x4c>
mov    0x2e8c(%rip),%rcx
mov    (%rcx),%edi
mov    0x2e7b(%rip),%rcx
mov    (%rcx),%ecx
lea    0xe9a(%rip),%rsi
mov    %eax,%edx
call   1050 <fprintf@plt>
mov    (%r14),%edi
call   1040 <close@plt>
movl   $0xffffffff,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"short write: %d, errno=%d\0A\00"
@errno = global i32 0
define i32 @xwrite(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i32*, i32** %5
%10 = load i32, i32* %9
%11 = icmp sgt i32 %10, -1
br i1 %11, label %12, label %32
12: ; preds = %3
%13 = load i32*, i32** %5
%14 = load i32, i32* %13
%15 = load i8*, i8** %6
%16 = load i64, i64* %7
%17 = call i32 @write(i32 %14, i8* %15, i64 %16)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = sext i32 %18 to i64
%20 = load i64, i64* %7
%21 = icmp ne i64 %19, %20
br i1 %21, label %22, label %31
22: ; preds = %12
%23 = load i32, i32* @stderr
%24 = load i32, i32* %8
%25 = load i32, i32* @errno
%26 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %24, i32 %25)
%27 = load i32*, i32** %5
%28 = load i32, i32* %27
%29 = call i32 @close(i32 %28)
%30 = load i32*, i32** %5
store i32 -1, i32* %30
br label %31
31: ; preds = %22, %12
br label %32
32: ; preds = %31, %3
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"short write: %d, errno=%d\0A\00"
@errno = global i32 0
define i32 @xwrite(i32* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = load i32, i32* %0
%5 = icmp sgt i32 %4, -1
br i1 %5, label %6, label %16
6: ; preds = %3
%7 = tail call i32 @write(i32 %4, i8* %1, i64 %2)
%8 = sext i32 %7 to i64
%9 = icmp eq i64 %8, %2
br i1 %9, label %16, label %10
10: ; preds = %6
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %12)
%14 = load i32, i32* %0
%15 = tail call i32 @close(i32 %14)
store i32 -1, i32* %0
br label %16
16: ; preds = %6, %10, %3
ret i32 undef
}