#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 recv(int, unsigned char *, int, int /*<<< orphan*/);
int recv_comm(int socket, unsigned char *buffer, int length) {
  unsigned char *ptr;
  int bytes_left;
  int bytes_read;
  ptr = buffer;
  bytes_left = length;
  while (bytes_left) {
    bytes_read = recv(socket, ptr, bytes_left, 0);
    if (bytes_read < 0) {
      if (errno == EINTR) {
        continue;
      } else {
        return -2;
      }
    } else if (bytes_read == 0) {
      return -1;
    }
    ptr += bytes_read;
    bytes_left -= bytes_read;
  }
  return 0;
}


x86 assembly (clang, -O0):
<recv_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     11bf <recv_comm+0xaf>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1183 <recv_comm+0x73>
mov    0x2e81(%rip),%rax
mov    (%rax),%rax
mov    0x2e6f(%rip),%rcx
cmp    (%rcx),%rax
jne    1177 <recv_comm+0x67>
jmp    1130 <recv_comm+0x20>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11c6 <recv_comm+0xb6>
cmpl   $0x0,-0x28(%rbp)
jne    1199 <recv_comm+0x89>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c6 <recv_comm+0xb6>
jmp    119e <recv_comm+0x8e>
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    1130 <recv_comm+0x20>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_comm>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %edx,%edx
je     1168 <recv_comm+0x58>
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2eb6(%rip),%r15
mov    0x2ea7(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jns    115d <recv_comm+0x4d>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1140 <recv_comm+0x30>
jmp    116c <recv_comm+0x5c>
je     117a <recv_comm+0x6a>
mov    %eax,%ecx
add    %rcx,%r14
sub    %eax,%ebx
jne    1140 <recv_comm+0x30>
xor    %eax,%eax
jmp    1171 <recv_comm+0x61>
mov    $0xfffffffe,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%eax
jmp    1171 <recv_comm+0x61>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
define i32 @recv_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 = %34, %27, %3
%14 = load i32, i32* %9
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %42
16: ; preds = %13
%17 = load i32, i32* %5
%18 = load i8*, i8** %8
%19 = load i32, i32* %9
%20 = call i32 @recv(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 %29
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
store i32 -2, i32* %4
br label %43
29: ; preds = %16
%30 = load i32, i32* %10
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %29
store i32 -1, i32* %4
br label %43
33: ; preds = %29
br label %34
34: ; preds = %33
%35 = load i32, i32* %10
%36 = load i8*, i8** %8
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds i8, i8* %36, i64 %37
store i8* %38, i8** %8
%39 = load i32, i32* %10
%40 = load i32, i32* %9
%41 = sub nsw i32 %40, %39
store i32 %41, i32* %9
br label %13
42: ; preds = %13
store i32 0, i32* %4
br label %43
43: ; preds = %42, %32, %28
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i32 @recv_comm(i32 %0, i8* %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %22, label %5
5: ; preds = %3, %17
%6 = phi i32 [ %20, %17 ], [ %2, %3 ]
%7 = phi i8* [ %19, %17 ], [ %1, %3 ]
br label %8
8: ; preds = %5, %11
%9 = tail call i32 @recv(i32 %0, i8* %7, i32 %6, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
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 %22
15: ; preds = %8
%16 = icmp eq i32 %9, 0
br i1 %16, label %22, label %17
17: ; preds = %15
%18 = zext i32 %9 to i64
%19 = getelementptr inbounds i8, i8* %7, i64 %18
%20 = sub nsw i32 %6, %9
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %5
22: ; preds = %17, %15, %11, %3
%23 = phi i32 [ 0, %3 ], [ -2, %11 ], [ -1, %15 ], [ 0, %17 ]
ret i32 %23
}