#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 EAGAIN;
int EWOULDBLOCK;
int errno;
int write(int, void *, unsigned int);
int WriteData(int sock, void *data, unsigned BytesOut) {
  int byteswritten;
  byteswritten = 0;
  if ((byteswritten = write(sock, data, BytesOut)) > 0)
    ;
  else if (byteswritten < 0) {
    if (errno == EAGAIN)
      return (0);
    if (errno == EWOULDBLOCK)
      return (0);
    else
      return (-errno);
  }
  return (byteswritten);
}


x86 assembly (clang, -O0):
<WriteData>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    1149 <WriteData+0x39>
jmp    11b3 <WriteData+0xa3>
cmpl   $0x0,-0x18(%rbp)
jge    11ae <WriteData+0x9e>
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
mov    0x2e65(%rip),%rcx
cmp    (%rcx),%eax
jne    1177 <WriteData+0x67>
movl   $0x0,-0x4(%rbp)
jmp    11b9 <WriteData+0xa9>
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    0x2e51(%rip),%rcx
cmp    (%rcx),%eax
jne    119b <WriteData+0x8b>
movl   $0x0,-0x4(%rbp)
jmp    11b9 <WriteData+0xa9>
xor    %eax,%eax
mov    0x2e3c(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11b9 <WriteData+0xa9>
jmp    11b3 <WriteData+0xa3>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WriteData>:
push   %rax
call   1030 <write@plt>
test   %eax,%eax
js     111c <WriteData+0xc>
pop    %rcx
ret
mov    0x2ebd(%rip),%rax
mov    (%rax),%ecx
mov    0x2e9c(%rip),%rdx
mov    0x2ea5(%rip),%rsi
mov    %ecx,%eax
neg    %eax
xor    %edi,%edi
cmp    (%rsi),%ecx
cmove  %edi,%eax
cmp    (%rdx),%ecx
cmove  %edi,%eax
pop    %rcx
ret

LLVM IR (-O0):
@errno = global i32 0
@EAGAIN = global i32 0
@EWOULDBLOCK = global i32 0
define i32 @WriteData(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
%9 = load i32, i32* %5
%10 = load i8*, i8** %6
%11 = load i32, i32* %7
%12 = call i32 @write(i32 %9, i8* %10, i32 %11)
store i32 %12, i32* %8
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %3
br label %32
15: ; preds = %3
%16 = load i32, i32* %8
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %31
18: ; preds = %15
%19 = load i32, i32* @errno
%20 = load i32, i32* @EAGAIN
%21 = icmp eq i32 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 0, i32* %4
br label %34
23: ; preds = %18
%24 = load i32, i32* @errno
%25 = load i32, i32* @EWOULDBLOCK
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %23
store i32 0, i32* %4
br label %34
28: ; preds = %23
%29 = load i32, i32* @errno
%30 = sub nsw i32 0, %29
store i32 %30, i32* %4
br label %34
31: ; preds = %15
br label %32
32: ; preds = %31, %14
%33 = load i32, i32* %8
store i32 %33, i32* %4
br label %34
34: ; preds = %32, %28, %27, %22
%35 = load i32, i32* %4
ret i32 %35
}

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