#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;
  timeout.tv_sec = timeout.tv_usec = 0;
  while (size > 0) {
    FD_ZERO(&fds);
    FD_SET(s, &fds);
    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)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x18(%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>
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    11a6 <flush_recv_buffer+0x26>
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    11a6 <flush_recv_buffer+0x26>
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
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
test   %esi,%esi
jle    1255 <flush_recv_buffer+0xd5>
mov    %esi,%r15d
mov    %edi,%r12d
lea    0x1(%r12),%ebp
xor    %r14d,%r14d
lea    0xc(%rsp),%rbx
lea    0x10(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1070 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
call   10a0 <FD_SET@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r13,%r8
call   1080 <select@plt>
test   %eax,%eax
js     11c0 <flush_recv_buffer+0x40>
je     1248 <flush_recv_buffer+0xc8>
mov    %r12d,%edi
mov    %rbx,%rsi
call   1050 <FD_ISSET@plt>
test   %eax,%eax
je     1248 <flush_recv_buffer+0xc8>
test   %r14,%r14
jne    121d <flush_recv_buffer+0x9d>
mov    %r15d,%edi
call   1090 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    121d <flush_recv_buffer+0x9d>
lea    0xdeb(%rip),%rdi
call   1060 <error_exit@plt>
xor    %r14d,%r14d
mov    %r12d,%edi
mov    %r14,%rsi
mov    %r15d,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %eax,%eax
jns    1248 <flush_recv_buffer+0xc8>
mov    %eax,%r15d
lea    0xdd5(%rip),%rdi
call   1060 <error_exit@plt>
neg    %r15d
jmp    11c0 <flush_recv_buffer+0x40>
test   %r14,%r14
je     1255 <flush_recv_buffer+0xd5>
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
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
store i64 0, i64* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
store i64 0, i64* %10
br label %11
11: ; preds = %59, %23, %2
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %62
14: ; preds = %11
%15 = call i32 @FD_ZERO(i32* %5)
%16 = load i32, i32* %3
%17 = call i32 @FD_SET(i32 %16, i32* %5)
%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 = %14
br label %11
24: ; preds = %14
%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 %11
62: ; preds = %58, %34, %11
%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
%9 = bitcast %struct.timeval* %4 to i8*
br i1 %8, label %10, label %42
10: ; preds = %2, %34
%11 = phi i8* [ %31, %34 ], [ null, %2 ]
%12 = phi i32 [ %36, %34 ], [ %1, %2 ]
br label %13
13: ; preds = %10, %13
%14 = call i32 @FD_ZERO(i32* %3)
%15 = call i32 @FD_SET(i32 %0, i32* %3)
%16 = call i32 @select(i32 %7, i32* %3, i32* null, i32* null, %struct.timeval* %4)
%17 = icmp slt i32 %16, 0
br i1 %17, label %13, label %18
18: ; preds = %13
%19 = icmp eq i32 %16, 0
br i1 %19, label %37, label %20
20: ; preds = %18
%21 = call i32 @FD_ISSET(i32 %0, i32* %3)
%22 = icmp eq i32 %21, 0
br i1 %22, label %37, label %23
23: ; preds = %20
%24 = icmp eq i8* %11, null
br i1 %24, label %25, label %30
25: ; preds = %23
%26 = call i8* @malloc(i32 %12)
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @error_exit(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %25, %28, %23
%31 = phi i8* [ null, %28 ], [ %26, %25 ], [ %11, %23 ]
%32 = call i32 @recv(i32 %0, i8* %31, i32 %12, i32 0)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = call i32 @error_exit(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%36 = sub nsw i32 0, %32
br label %10
37: ; preds = %30, %18, %20
%38 = phi i8* [ %11, %20 ], [ %11, %18 ], [ %31, %30 ]
%39 = icmp eq i8* %38, null
br i1 %39, label %42, label %40
40: ; preds = %37
%41 = call i32 @free(i8* %38)
br label %42
42: ; preds = %2, %40, %37
ret i32 0
}