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

/* Variables and functions */
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ errno;
int my_strlen(char *);
char *strerror(int /*<<< orphan*/);
int write(int, char *, int);
ssize_t xwrite(int fd, const void *buf, size_t count) {
  ssize_t ret;
  char *err;
  ret = write(fd, buf, count);
  if (ret == -1) {
    err = strerror(errno);
    write(STDERR_FILENO, "write failed: ", sizeof("write failed: "));
    write(STDERR_FILENO, err, my_strlen(err));
    write(STDERR_FILENO, "\n", 1);
  }
  return (ret);
}


x86 assembly (clang, -O0):
<xwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11d3 <xwrite+0xa3>
mov    0x2e77(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
lea    0xe7c(%rip),%rsi
mov    $0xf,%edx
call   1040 <write@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <my_strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xwrite>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
call   1040 <write@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    119a <xwrite+0x6a>
mov    0x2e96(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %rax,%r14
mov    0x2e7d(%rip),%r15
mov    (%r15),%edi
lea    0xe9b(%rip),%rsi
mov    $0xf,%edx
call   1040 <write@plt>
mov    (%r15),%ebp
mov    %r14,%rdi
call   1030 <my_strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    (%r15),%edi
lea    0xe7f(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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