#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 */
struct timeval {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ error_exit(char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int recv(int, char *, int, int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int flush_recv_buffer(int s, int size) {
  fd_set fds;
  struct timeval timeout;
  char *buffer = NULL;
  int r;
  while (size > 0) {
    FD_ZERO(&fds);
    FD_SET(s, &fds);
    timeout.tv_sec = timeout.tv_usec = 0;
    r = select(s + 1, &fds, NULL, NULL, &timeout);
    if (r < 0)
      continue;
    if ((r == 0) || ((r > 0) && !FD_ISSET(s, &fds)))
      break;
    if (buffer == NULL) {
      buffer = malloc(size);
      if (buffer == NULL)
        error_exit("Out of memory.\n");
    }
    r = recv(s, buffer, size, 0);
    if (r < 0)
      error_exit("Cannot flush buffer.\n");
    if (r == 0)
      break;
    size = -r;
  }
  if (buffer)
    free(buffer);
  return 0;
}


x86 assembly (clang, -O0):
<flush_recv_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movq   $0x0,-0x28(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    1298 <flush_recv_buffer+0x118>
lea    -0xc(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   10a0 <FD_SET@plt>
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1080 <select@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11f1 <flush_recv_buffer+0x71>
jmp    1196 <flush_recv_buffer+0x16>
cmpl   $0x0,-0x2c(%rbp)
je     121a <flush_recv_buffer+0x9a>
cmpl   $0x0,-0x2c(%rbp)
jle    121f <flush_recv_buffer+0x9f>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    121f <flush_recv_buffer+0x9f>
jmp    1298 <flush_recv_buffer+0x118>
cmpq   $0x0,-0x28(%rbp)
jne    1252 <flush_recv_buffer+0xd2>
mov    -0x8(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    124d <flush_recv_buffer+0xcd>
lea    0xdb8(%rip),%rdi
call   1060 <error_exit@plt>
jmp    1252 <flush_recv_buffer+0xd2>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x8(%rbp),%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    127c <flush_recv_buffer+0xfc>
lea    0xd99(%rip),%rdi
call   1060 <error_exit@plt>
cmpl   $0x0,-0x2c(%rbp)
jne    128b <flush_recv_buffer+0x10b>
jmp    1298 <flush_recv_buffer+0x118>
xor    %eax,%eax
sub    -0x2c(%rbp),%eax
mov    %eax,-0x8(%rbp)
jmp    1196 <flush_recv_buffer+0x16>
cmpq   $0x0,-0x28(%rbp)
je     12ac <flush_recv_buffer+0x12c>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flush_recv_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
test   %esi,%esi
jle    124b <flush_recv_buffer+0xcb>
mov    %esi,%r15d
mov    %edi,%ebx
lea    0x1(%rbx),%r12d
xor    %r14d,%r14d
lea    0xc(%rsp),%rbp
lea    0x10(%rsp),%r13
nopl   0x0(%rax)
mov    %rbp,%rdi
call   1070 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10a0 <FD_SET@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
mov    %r12d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r13,%r8
call   1080 <select@plt>
test   %eax,%eax
js     11b0 <flush_recv_buffer+0x30>
je     123e <flush_recv_buffer+0xbe>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1050 <FD_ISSET@plt>
test   %eax,%eax
je     123e <flush_recv_buffer+0xbe>
test   %r14,%r14
jne    1214 <flush_recv_buffer+0x94>
mov    %r15d,%edi
call   1090 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1214 <flush_recv_buffer+0x94>
lea    0xe04(%rip),%rdi
call   1060 <error_exit@plt>
xor    %r14d,%r14d
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15d,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %eax,%eax
jns    123e <flush_recv_buffer+0xbe>
mov    %eax,%r15d
lea    0xdef(%rip),%rdi
call   1060 <error_exit@plt>
neg    %r15d
jmp    11b0 <flush_recv_buffer+0x30>
test   %r14,%r14
je     124b <flush_recv_buffer+0xcb>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@.str = [16 x i8] c"Out of memory.\0A\00"
@.str.1 = [22 x i8] c"Cannot flush buffer.\0A\00"
define i32 @flush_recv_buffer(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca i8*
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i8* null, i8** %7
br label %9
9: ; preds = %59, %23, %2
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %62
12: ; preds = %9
%13 = call i32 @FD_ZERO(i32* %5)
%14 = load i32, i32* %3
%15 = call i32 @FD_SET(i32 %14, i32* %5)
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
store i64 0, i64* %16
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
store i64 0, i64* %17
%18 = load i32, i32* %3
%19 = add nsw i32 %18, 1
%20 = call i32 @select(i32 %19, i32* %5, i32* null, i32* null, %struct.timeval* %6)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %12
br label %9
24: ; preds = %12
%25 = load i32, i32* %8
%26 = icmp eq i32 %25, 0
br i1 %26, label %34, label %27
27: ; preds = %24
%28 = load i32, i32* %8
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = load i32, i32* %3
%32 = call i32 @FD_ISSET(i32 %31, i32* %5)
%33 = icmp ne i32 %32, 0
br i1 %33, label %35, label %34
34: ; preds = %30, %24
br label %62
35: ; preds = %30, %27
%36 = load i8*, i8** %7
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %46
38: ; preds = %35
%39 = load i32, i32* %4
%40 = call i8* @malloc(i32 %39)
store i8* %40, i8** %7
%41 = load i8*, i8** %7
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = call i32 @error_exit(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %45
45: ; preds = %43, %38
br label %46
46: ; preds = %45, %35
%47 = load i32, i32* %3
%48 = load i8*, i8** %7
%49 = load i32, i32* %4
%50 = call i32 @recv(i32 %47, i8* %48, i32 %49, i32 0)
store i32 %50, i32* %8
%51 = load i32, i32* %8
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %46
%54 = call i32 @error_exit(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %55
55: ; preds = %53, %46
%56 = load i32, i32* %8
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %55
br label %62
59: ; preds = %55
%60 = load i32, i32* %8
%61 = sub nsw i32 0, %60
store i32 %61, i32* %4
br label %9
62: ; preds = %58, %34, %9
%63 = load i8*, i8** %7
%64 = icmp ne i8* %63, null
br i1 %64, label %65, label %68
65: ; preds = %62
%66 = load i8*, i8** %7
%67 = call i32 @free(i8* %66)
br label %68
68: ; preds = %65, %62
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@.str = [16 x i8] c"Out of memory.\0A\00"
@.str.1 = [22 x i8] c"Cannot flush buffer.\0A\00"
define i32 @flush_recv_buffer(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.timeval* %4 to i8*
%7 = add nsw i32 %0, 1
%8 = icmp sgt i32 %1, 0
br i1 %8, label %9, label %43
9: ; preds = %2
%10 = bitcast %struct.timeval* %4 to i8*
br label %11
11: ; preds = %35, %9
%12 = phi i8* [ null, %9 ], [ %32, %35 ]
%13 = phi i32 [ %1, %9 ], [ %37, %35 ]
br label %14
14: ; preds = %11, %14
%15 = call i32 @FD_ZERO(i32* %3)
%16 = call i32 @FD_SET(i32 %0, i32* %3)
%17 = call i32 @select(i32 %7, i32* %3, i32* null, i32* null, %struct.timeval* %4)
%18 = icmp slt i32 %17, 0
br i1 %18, label %14, label %19
19: ; preds = %14
%20 = icmp eq i32 %17, 0
br i1 %20, label %38, label %21
21: ; preds = %19
%22 = call i32 @FD_ISSET(i32 %0, i32* %3)
%23 = icmp eq i32 %22, 0
br i1 %23, label %38, label %24
24: ; preds = %21
%25 = icmp eq i8* %12, null
br i1 %25, label %26, label %31
26: ; preds = %24
%27 = call i8* @malloc(i32 %13)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 @error_exit(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %31
31: ; preds = %26, %29, %24
%32 = phi i8* [ null, %29 ], [ %27, %26 ], [ %12, %24 ]
%33 = call i32 @recv(i32 %0, i8* %32, i32 %13, i32 0)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 @error_exit(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%37 = sub nsw i32 0, %33
br label %11
38: ; preds = %31, %19, %21
%39 = phi i8* [ %12, %21 ], [ %12, %19 ], [ %32, %31 ]
%40 = icmp eq i8* %39, null
br i1 %40, label %43, label %41
41: ; preds = %38
%42 = call i32 @free(i8* %39)
br label %43
43: ; preds = %2, %41, %38
ret i32 0
}