#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 */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
scalar_t__ write(int, void const *, size_t);
ssize_t tcp_write(int fd, const void *buf, size_t count) {
  ssize_t nwrite = write(fd, buf, count);
  if (nwrite == 0) {
    return 0;
  }
  if (nwrite < 0) {
    if (errno == EINTR || errno == EAGAIN) {
      return 0;
    }
    return -1;
  }
  return nwrite;
}


x86 assembly (clang, -O0):
<tcp_write>:
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
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    114f <tcp_write+0x3f>
movq   $0x0,-0x8(%rbp)
jmp    11b0 <tcp_write+0xa0>
cmpq   $0x0,-0x28(%rbp)
jge    11a8 <tcp_write+0x98>
mov    0x2e7f(%rip),%rax
mov    (%rax),%rax
mov    0x2e6d(%rip),%rcx
cmp    (%rcx),%rax
je     118e <tcp_write+0x7e>
mov    0x2e65(%rip),%rax
mov    (%rax),%rax
mov    0x2e43(%rip),%rcx
cmp    (%rcx),%rax
jne    119b <tcp_write+0x8b>
movq   $0x0,-0x8(%rbp)
jmp    11b0 <tcp_write+0xa0>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11b0 <tcp_write+0xa0>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_write>:
push   %rax
call   1030 <write@plt>
test   %rax,%rax
je     111f <tcp_write+0xf>
js     1123 <tcp_write+0x13>
pop    %rcx
ret
xor    %eax,%eax
pop    %rcx
ret
mov    0x2eb6(%rip),%rax
mov    (%rax),%rax
mov    0x2ea4(%rip),%rcx
cmp    (%rcx),%rax
setne  %cl
mov    0x2e87(%rip),%rdx
cmp    (%rdx),%rax
setne  %al
and    %cl,%al
movzbl %al,%eax
neg    %rax
pop    %rcx
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i64 @tcp_write(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 = call i64 @write(i32 %9, i8* %10, i64 %11)
store i64 %12, i64* %8
%13 = load i64, i64* %8
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %3
store i64 0, i64* %4
br label %31
16: ; preds = %3
%17 = load i64, i64* %8
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %29
19: ; preds = %16
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = load i64, i64* @errno
%25 = load i64, i64* @EAGAIN
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %23, %19
store i64 0, i64* %4
br label %31
28: ; preds = %23
store i64 -1, i64* %4
br label %31
29: ; preds = %16
%30 = load i64, i64* %8
store i64 %30, i64* %4
br label %31
31: ; preds = %29, %28, %27, %15
%32 = load i64, i64* %4
ret i64 %32
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i64 @tcp_write(i32 %0, i8* nocapture %1, i64 %2) {
%4 = tail call i64 @write(i32 %0, i8* %1, i64 %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %16, label %6
6: ; preds = %3
%7 = icmp slt i64 %4, 0
br i1 %7, label %8, label %16
8: ; preds = %6
%9 = load i64, i64* @errno
%10 = load i64, i64* @EINTR
%11 = icmp ne i64 %9, %10
%12 = load i64, i64* @EAGAIN
%13 = icmp ne i64 %9, %12
%14 = select i1 %11, i1 %13, i1 false
%15 = sext i1 %14 to i64
br label %16
16: ; preds = %8, %6, %3
%17 = phi i64 [ 0, %3 ], [ %4, %6 ], [ %15, %8 ]
ret i64 %17
}