#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 */
scalar_t__ ECONNRESET;
int /*<<< orphan*/ assert(int);
scalar_t__ errno;
int /*<<< orphan*/ perror_and_exit(char *);
int write(int, void const *, size_t);
ssize_t write_wrapper(int fd, const void *buf, size_t count) {
  ssize_t write_bytes = write(fd, buf, count);
  if (write_bytes < 0) {
    if (errno == ECONNRESET)
      return -1;
    else
      perror_and_exit("write");
  }
  if (write_bytes == 0)
    return -1;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-compare"
  assert(write_bytes <= count);
#pragma GCC diagnostic pop
  return write_bytes;
}


x86 assembly (clang, -O0):
<write_wrapper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1197 <write_wrapper+0x67>
mov    0x2e71(%rip),%rax
mov    (%rax),%rax
mov    0x2e6f(%rip),%rcx
cmp    (%rcx),%rax
jne    1186 <write_wrapper+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c8 <write_wrapper+0x98>
lea    0xe73(%rip),%rdi
call   1050 <perror_and_exit@plt>
jmp    1197 <write_wrapper+0x67>
cmpl   $0x0,-0x1c(%rbp)
jne    11ad <write_wrapper+0x7d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c8 <write_wrapper+0x98>
movslq -0x1c(%rbp),%rax
cmp    -0x18(%rbp),%rax
setbe  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_wrapper>:
push   %rbp
push   %rbx
push   %rax
mov    %rdx,%rbx
call   1030 <write@plt>
mov    %eax,%ebp
test   %eax,%eax
js     114a <write_wrapper+0x1a>
jne    1171 <write_wrapper+0x41>
mov    $0xffffffff,%eax
jmp    1184 <write_wrapper+0x54>
mov    0x2e87(%rip),%rax
mov    (%rax),%rcx
mov    0x2e85(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%rcx
je     1184 <write_wrapper+0x54>
lea    0xe94(%rip),%rdi
call   1050 <perror_and_exit@plt>
movslq %ebp,%rax
xor    %edi,%edi
cmp    %rbx,%rax
setbe  %dil
call   1040 <assert@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@ECONNRESET = global i64 0
@.str = [6 x i8] c"write\00"
define i32 @write_wrapper(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 i8*, i8** %6
%11 = load i64, i64* %7
%12 = call i32 @write(i32 %9, i8* %10, i64 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %3
%16 = load i64, i64* @errno
%17 = load i64, i64* @ECONNRESET
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 -1, i32* %4
br label %35
20: ; preds = %15
%21 = call i32 @perror_and_exit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20
br label %23
23: ; preds = %22, %3
%24 = load i32, i32* %8
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %23
store i32 -1, i32* %4
br label %35
27: ; preds = %23
%28 = load i32, i32* %8
%29 = sext i32 %28 to i64
%30 = load i64, i64* %7
%31 = icmp ule i64 %29, %30
%32 = zext i1 %31 to i32
%33 = call i32 @assert(i32 %32)
%34 = load i32, i32* %8
store i32 %34, i32* %4
br label %35
35: ; preds = %27, %26, %19
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
@errno = global i64 0
@ECONNRESET = global i64 0
@.str = [6 x i8] c"write\00"
define i32 @write_wrapper(i32 %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32 @write(i32 %0, i8* %1, i64 %2)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %12
6: ; preds = %3
%7 = load i64, i64* @errno
%8 = load i64, i64* @ECONNRESET
%9 = icmp eq i64 %7, %8
br i1 %9, label %19, label %10
10: ; preds = %6
%11 = tail call i32 @perror_and_exit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %14
12: ; preds = %3
%13 = icmp eq i32 %4, 0
br i1 %13, label %19, label %14
14: ; preds = %10, %12
%15 = sext i32 %4 to i64
%16 = icmp ule i64 %15, %2
%17 = zext i1 %16 to i32
%18 = tail call i32 @assert(i32 %17)
br label %19
19: ; preds = %12, %6, %14
%20 = phi i32 [ %4, %14 ], [ -1, %6 ], [ -1, %12 ]
ret i32 %20
}