#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*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ write(int, void const *, int);
int socket_write(int fd, const void *bf, int len) {
  if ((fd < 0) || !bf || (len < 0)) {
    return -1;
  }
  if (write(fd, bf, len) < 0) {
    fprintf(stderr, "write error: [%s]\n", strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<socket_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1161 <socket_write+0x31>
cmpq   $0x0,-0x10(%rbp)
je     1161 <socket_write+0x31>
cmpl   $0x0,-0x14(%rbp)
jge    116d <socket_write+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c5 <socket_write+0x95>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    11be <socket_write+0x8e>
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xe53(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c5 <socket_write+0x95>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_write>:
push   %rbx
mov    $0xffffffff,%eax
test   %rsi,%rsi
je     1150 <socket_write+0x20>
mov    %edx,%ecx
or     %edi,%ecx
js     1150 <socket_write+0x20>
call   1030 <write@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
js     1152 <socket_write+0x22>
pop    %rbx
ret
mov    0x2e87(%rip),%rax
mov    (%rax),%ebx
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe90(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [19 x i8] c"write error: [%s]\0A\00"
@errno = global i32 0
define i32 @socket_write(i32 %0, i8* %1, i32 %2) {
%4 = icmp eq i8* %1, null
%5 = or i32 %2, %0
%6 = icmp slt i32 %5, 0
%7 = or i1 %4, %6
br i1 %7, label %16, label %8
8: ; preds = %3
%9 = tail call i64 @write(i32 %0, i8* %1, i32 %2)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %8, %3, %11
%17 = phi i32 [ -1, %11 ], [ -1, %3 ], [ 0, %8 ]
ret i32 %17
}