#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 */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int send(int, unsigned char *, int, int /*<<< orphan*/);
int send_comm(int socket, unsigned char *buffer, int length) {
  unsigned char *ptr;
  int bytes_left;
  int bytes_sent;
  ptr = buffer;
  bytes_left = length;
  while (bytes_left) {
    bytes_sent = send(socket, ptr, bytes_left, 0);
    if (bytes_sent < 0) {
      if (errno == EINTR) {
        continue;
      } else {
        perror("write");
        return -2;
      }
    } else if (bytes_sent == 0) {
      return -1;
    }
    ptr += bytes_sent;
    bytes_left -= bytes_sent;
  }
  return 0;
}


x86 assembly (clang, -O0):
<send_comm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11db <send_comm+0xbb>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    119f <send_comm+0x7f>
mov    0x2e71(%rip),%rax
mov    (%rax),%rax
mov    0x2e5f(%rip),%rcx
cmp    (%rcx),%rax
jne    1187 <send_comm+0x67>
jmp    1140 <send_comm+0x20>
lea    0xe72(%rip),%rdi
call   1040 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11e2 <send_comm+0xc2>
cmpl   $0x0,-0x28(%rbp)
jne    11b5 <send_comm+0x95>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e2 <send_comm+0xc2>
jmp    11ba <send_comm+0x9a>
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%ecx
mov    -0x24(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
jmp    1140 <send_comm+0x20>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_comm>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %edx,%edx
je     1177 <send_comm+0x57>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r14d
mov    0x2ea5(%rip),%r15
mov    0x2e96(%rip),%r12
jmp    1159 <send_comm+0x39>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r15),%rax
cmp    (%r12),%rax
jne    117b <send_comm+0x5b>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1030 <send@plt>
test   %eax,%eax
js     1150 <send_comm+0x30>
je     118e <send_comm+0x6e>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jne    1159 <send_comm+0x39>
xor    %eax,%eax
jmp    1193 <send_comm+0x73>
lea    0xe7e(%rip),%rdi
call   1040 <perror@plt>
mov    $0xfffffffe,%eax
jmp    1193 <send_comm+0x73>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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