#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 /*<<< orphan*/ EAGAIN;
int /*<<< orphan*/ ECONNRESET;
int /*<<< orphan*/ errno;
int read(int, char *, int);
int m_recvdata(int socket, char *recvbp, int count) {
  int rlen;
  if (count == 0) {
    return (0);
  }
restart:
  rlen = read(socket, recvbp, count);
  switch (rlen) {
  case -1:
    if (errno == ECONNRESET) {
      return (-4);
    }
    if (errno == EAGAIN) {
      goto restart;
    }
    return (-1);
  case 0:
    return (-2);
  default:
    return (rlen);
  }
}


x86 assembly (clang, -O0):
<m_recvdata>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1138 <m_recvdata+0x28>
movl   $0x0,-0x4(%rbp)
jmp    11d2 <m_recvdata+0xc2>
jmp    113d <m_recvdata+0x2d>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0xffffffff,%eax
je     1173 <m_recvdata+0x63>
jmp    1163 <m_recvdata+0x53>
mov    -0x1c(%rbp),%eax
test   %eax,%eax
je     11c0 <m_recvdata+0xb0>
jmp    11cc <m_recvdata+0xbc>
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
mov    0x2e5d(%rip),%rcx
cmp    (%rcx),%eax
jne    1197 <m_recvdata+0x87>
movl   $0xfffffffc,-0x4(%rbp)
jmp    11d2 <m_recvdata+0xc2>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    0x2e21(%rip),%rcx
cmp    (%rcx),%eax
jne    11b4 <m_recvdata+0xa4>
jmp    113d <m_recvdata+0x2d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d2 <m_recvdata+0xc2>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11d2 <m_recvdata+0xc2>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<m_recvdata>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
je     1167 <m_recvdata+0x57>
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2eab(%rip),%r15
mov    0x2eac(%rip),%r12
mov    0x2e8d(%rip),%r13
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
jne    116b <m_recvdata+0x5b>
mov    (%r15),%eax
cmp    (%r12),%eax
je     117a <m_recvdata+0x6a>
cmp    0x0(%r13),%eax
je     1140 <m_recvdata+0x30>
mov    $0xffffffff,%eax
jmp    117f <m_recvdata+0x6f>
xor    %eax,%eax
jmp    117f <m_recvdata+0x6f>
mov    %eax,%ecx
mov    $0xfffffffe,%eax
test   %ecx,%ecx
je     117f <m_recvdata+0x6f>
mov    %ecx,%eax
jmp    117f <m_recvdata+0x6f>
mov    $0xfffffffc,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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