#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, char *, int, int /*<<< orphan*/);
int discard(int fd, int len) {
  char bin[4096];
  do {
    int amt = sizeof bin;
    int n;
    if (amt > len)
      amt = len;
  restart:
    n = recv(fd, bin, amt, 0);
    if (n == -1 && errno == EINTR)
      goto restart;
    if (n <= 0)
      return 1;
    len -= n;
  } while (len > 0);
  return 0;
}


x86 assembly (clang, -O0):
<discard>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x1000,-0x1014(%rbp)
mov    -0x1014(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    1143 <discard+0x33>
mov    -0xc(%rbp),%eax
mov    %eax,-0x1014(%rbp)
jmp    1148 <discard+0x38>
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1014(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1018(%rbp)
cmpl   $0xffffffff,-0x1018(%rbp)
jne    1191 <discard+0x81>
mov    0x2e67(%rip),%rax
mov    (%rax),%rax
mov    0x2e55(%rip),%rcx
cmp    (%rcx),%rax
jne    1191 <discard+0x81>
jmp    1148 <discard+0x38>
cmpl   $0x0,-0x1018(%rbp)
jg     11aa <discard+0x9a>
movl   $0x1,-0x4(%rbp)
jmp    11c9 <discard+0xb9>
mov    -0x1018(%rbp),%ecx
mov    -0xc(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jg     1121 <discard+0x11>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<discard>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
mov    %rsp,%r15
mov    0x2eb0(%rip),%r12
mov    0x2ea1(%rip),%r13
cmp    $0x1000,%r14d
mov    $0x1000,%ebx
cmovl  %r14d,%ebx
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r15,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
jne    1170 <discard+0x60>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1150 <discard+0x40>
jmp    1180 <discard+0x70>
nop
test   %eax,%eax
jle    1180 <discard+0x70>
sub    %eax,%r14d
test   %r14d,%r14d
jg     1137 <discard+0x27>
xor    %eax,%eax
jmp    1185 <discard+0x75>
mov    $0x1,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
define i32 @discard(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [4096 x i8]
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
br label %9
9: ; preds = %36, %2
store i32 4096, i32* %7
%10 = load i32, i32* %7
%11 = load i32, i32* %5
%12 = icmp sgt i32 %10, %11
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = load i32, i32* %5
store i32 %14, i32* %7
br label %15
15: ; preds = %13, %9
br label %16
16: ; preds = %27, %15
%17 = load i32, i32* %4
%18 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%19 = load i32, i32* %7
%20 = call i32 @recv(i32 %17, i8* %18, i32 %19, i32 0)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %28
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 %16
28: ; preds = %23, %16
%29 = load i32, i32* %8
%30 = icmp sle i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %28
store i32 1, i32* %3
br label %40
32: ; preds = %28
%33 = load i32, i32* %8
%34 = load i32, i32* %5
%35 = sub nsw i32 %34, %33
store i32 %35, i32* %5
br label %36
36: ; preds = %32
%37 = load i32, i32* %5
%38 = icmp sgt i32 %37, 0
br i1 %38, label %9, label %39
39: ; preds = %36
store i32 0, i32* %3
br label %40
40: ; preds = %39, %31
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i32 @discard(i32 %0, i32 %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
br label %5
5: ; preds = %19, %2
%6 = phi i32 [ %1, %2 ], [ %18, %19 ]
%7 = icmp slt i32 %6, 4096
%8 = select i1 %7, i32 %6, i32 4096
br label %9
9: ; preds = %12, %5
%10 = call i32 @recv(i32 %0, i8* %4, i32 %8, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
br i1 %15, label %9, label %21
16: ; preds = %9
%17 = icmp slt i32 %10, 1
%18 = sub nsw i32 %6, %10
br i1 %17, label %21, label %19
19: ; preds = %16
%20 = icmp sgt i32 %18, 0
br i1 %20, label %5, label %21
21: ; preds = %19, %16, %12
%22 = phi i32 [ 1, %12 ], [ 0, %19 ], [ 1, %16 ]
ret i32 %22
}