#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 */
#define EAGAIN 129
#define ECONNRESET 128
int /*<<< orphan*/ MSG_NOSIGNAL;
int errno;
int /*<<< orphan*/ perror(char *);
scalar_t__ send(int, void *, size_t, int /*<<< orphan*/);
ssize_t write_wrapper(int fd, void *buf, size_t count) {
  ssize_t bytes = send(fd, buf, count, MSG_NOSIGNAL);
  if (bytes < 0) {
    switch (errno) {
    case EAGAIN:
      return 0;
    case ECONNRESET:
      return -errno;
    default:
      perror("send");
      return -errno;
    }
  } else if (bytes == 0) {
    return -ECONNRESET;
  } else {
    return bytes;
  }
}


x86 assembly (clang, -O0):
<write_wrapper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    0x2e93(%rip),%rax
mov    (%rax),%ecx
call   1030 <send@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    11cf <write_wrapper+0xaf>
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x80,%eax
je     1197 <write_wrapper+0x77>
jmp    1177 <write_wrapper+0x57>
mov    -0x2c(%rbp),%eax
sub    $0x81,%eax
jne    11ad <write_wrapper+0x8d>
jmp    118a <write_wrapper+0x6a>
movq   $0x0,-0x8(%rbp)
jmp    11ef <write_wrapper+0xcf>
xor    %eax,%eax
mov    0x2e40(%rip),%rcx
sub    (%rcx),%eax
cltq
mov    %rax,-0x8(%rbp)
jmp    11ef <write_wrapper+0xcf>
lea    0xe4c(%rip),%rdi
call   1040 <perror@plt>
xor    %eax,%eax
mov    0x2e1e(%rip),%rcx
sub    (%rcx),%eax
cltq
mov    %rax,-0x8(%rbp)
jmp    11ef <write_wrapper+0xcf>
cmpq   $0x0,-0x28(%rbp)
jne    11e7 <write_wrapper+0xc7>
movq   $0xffffffffffffff80,-0x8(%rbp)
ff
jmp    11ef <write_wrapper+0xcf>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_wrapper>:
push   %rbx
mov    0x2eb0(%rip),%rax
mov    (%rax),%ecx
call   1030 <send@plt>
test   %rax,%rax
js     1141 <write_wrapper+0x21>
mov    $0xffffffffffffff80,%rcx
cmove  %rcx,%rax
pop    %rbx
ret
mov    0x2e98(%rip),%rbx
mov    (%rbx),%eax
cmp    $0x81,%eax
je     1161 <write_wrapper+0x41>
cmp    $0x80,%eax
jne    1165 <write_wrapper+0x45>
mov    $0xffffffffffffff80,%rax
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret
lea    0xe94(%rip),%rdi
call   1040 <perror@plt>
movslq (%rbx),%rax
neg    %rax
pop    %rbx
ret

LLVM IR (-O0):
@MSG_NOSIGNAL = global i32 0
@errno = global i32 0
@.str = [5 x i8] c"send\00"
define i64 @write_wrapper(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i32, i32* %5
%10 = load i8*, i8** %6
%11 = load i64, i64* %7
%12 = load i32, i32* @MSG_NOSIGNAL
%13 = call i64 @send(i32 %9, i8* %10, i64 %11, i32 %12)
store i64 %13, i64* %8
%14 = load i64, i64* %8
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %28
16: ; preds = %3
%17 = load i32, i32* @errno
switch i32 %17, label %23 [
i32 129, label %18
i32 128, label %19
]
18: ; preds = %16
store i64 0, i64* %4
br label %34
19: ; preds = %16
%20 = load i32, i32* @errno
%21 = sub nsw i32 0, %20
%22 = sext i32 %21 to i64
store i64 %22, i64* %4
br label %34
23: ; preds = %16
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* @errno
%26 = sub nsw i32 0, %25
%27 = sext i32 %26 to i64
store i64 %27, i64* %4
br label %34
28: ; preds = %3
%29 = load i64, i64* %8
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %28
store i64 -128, i64* %4
br label %34
32: ; preds = %28
%33 = load i64, i64* %8
store i64 %33, i64* %4
br label %34
34: ; preds = %32, %31, %23, %19, %18
%35 = load i64, i64* %4
ret i64 %35
}

LLVM IR (-O2):
@MSG_NOSIGNAL = global i32 0
@errno = global i32 0
@.str = [5 x i8] c"send\00"
define i64 @write_wrapper(i32 %0, i8* %1, i64 %2) {
%4 = load i32, i32* @MSG_NOSIGNAL
%5 = tail call i64 @send(i32 %0, i8* %1, i64 %2, i32 %4)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %15
7: ; preds = %3
%8 = load i32, i32* @errno
switch i32 %8, label %10 [
i32 129, label %18
i32 128, label %9
]
9: ; preds = %7
br label %18
10: ; preds = %7
%11 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @errno
%13 = sub nsw i32 0, %12
%14 = sext i32 %13 to i64
br label %18
15: ; preds = %3
%16 = icmp eq i64 %5, 0
%17 = select i1 %16, i64 -128, i64 %5
br label %18
18: ; preds = %15, %7, %10, %9
%19 = phi i64 [ %14, %10 ], [ -128, %9 ], [ 0, %7 ], [ %17, %15 ]
ret i64 %19
}