#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*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int recv(int, unsigned char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int fullrecv(int sock, void *vbuffer, int size) {
  int cur = 0;
  int count = 0;
  unsigned char *buffer = (unsigned char *)vbuffer;
  while (cur < size) {
    count = recv(sock, &buffer[cur], size - cur, 0);
    if (count <= 0) {
      if (count == 0) {
        fprintf(stderr, "Connection has been terminated by server\n");
      } else {
        perror("recv");
      }
      fflush(stdin);
      fflush(stderr);
      return count;
    }
    cur += count;
  }
  return size;
}


x86 assembly (clang, -O0):
<fullrecv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1201 <fullrecv+0xc1>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     11f3 <fullrecv+0xb3>
cmpl   $0x0,-0x1c(%rbp)
jne    11c0 <fullrecv+0x80>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11cc <fullrecv+0x8c>
lea    0xe63(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1207 <fullrecv+0xc7>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1168 <fullrecv+0x28>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fullrecv>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r12d
test   %edx,%edx
jle    11bb <fullrecv+0x7b>
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movslq %ebx,%rax
lea    (%r14,%rax,1),%rsi
mov    %r12d,%edx
sub    %eax,%edx
mov    %r15d,%edi
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1185 <fullrecv+0x45>
add    %ebp,%ebx
cmp    %r12d,%ebx
jl     1160 <fullrecv+0x20>
jmp    11bb <fullrecv+0x7b>
jne    11c7 <fullrecv+0x87>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
lea    0xe69(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    %ebp,%r12d
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe5c(%rip),%rdi
call   1060 <perror@plt>
jmp    119c <fullrecv+0x5c>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [42 x i8] c"Connection has been terminated by server\0A\00"
@.str.1 = [5 x i8] c"recv\00"
@stdin = global i32 0
define i32 @fullrecv(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
%11 = load i8*, i8** %6
store i8* %11, i8** %10
br label %12
12: ; preds = %42, %3
%13 = load i32, i32* %8
%14 = load i32, i32* %7
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %46
16: ; preds = %12
%17 = load i32, i32* %5
%18 = load i8*, i8** %10
%19 = load i32, i32* %8
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i32, i32* %7
%23 = load i32, i32* %8
%24 = sub nsw i32 %22, %23
%25 = call i32 @recv(i32 %17, i8* %21, i32 %24, i32 0)
store i32 %25, i32* %9
%26 = load i32, i32* %9
%27 = icmp sle i32 %26, 0
br i1 %27, label %28, label %42
28: ; preds = %16
%29 = load i32, i32* %9
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %36
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %34, %31
%37 = load i32, i32* @stdin
%38 = call i32 @fflush(i32 %37)
%39 = load i32, i32* @stderr
%40 = call i32 @fflush(i32 %39)
%41 = load i32, i32* %9
store i32 %41, i32* %4
br label %48
42: ; preds = %16
%43 = load i32, i32* %9
%44 = load i32, i32* %8
%45 = add nsw i32 %44, %43
store i32 %45, i32* %8
br label %12
46: ; preds = %12
%47 = load i32, i32* %7
store i32 %47, i32* %4
br label %48
48: ; preds = %46, %36
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [42 x i8] c"Connection has been terminated by server\0A\00"
@.str.1 = [5 x i8] c"recv\00"
@stdin = global i32 0
define i32 @fullrecv(i32 %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %27
5: ; preds = %3, %24
%6 = phi i32 [ %25, %24 ], [ 0, %3 ]
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %1, i64 %7
%9 = sub nsw i32 %2, %6
%10 = tail call i32 @recv(i32 %0, i8* %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %24
12: ; preds = %5
%13 = icmp eq i32 %10, 0
br i1 %13, label %14, label %17
14: ; preds = %12
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %19
17: ; preds = %12
%18 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %14
%20 = load i32, i32* @stdin
%21 = tail call i32 @fflush(i32 %20)
%22 = load i32, i32* @stderr
%23 = tail call i32 @fflush(i32 %22)
br label %27
24: ; preds = %5
%25 = add nsw i32 %10, %6
%26 = icmp slt i32 %25, %2
br i1 %26, label %5, label %27
27: ; preds = %24, %3, %19
%28 = phi i32 [ %10, %19 ], [ %2, %3 ], [ %2, %24 ]
ret i32 %28
}