#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__ EPIPE;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int recv(int, void *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int nbrecv(int s, void *buf, int len) {
  int oldfl, ret, comp = len;
  oldfl = fcntl(s, F_GETFL, 0);
  if (!(oldfl & O_NONBLOCK))
    fcntl(s, F_SETFL, oldfl | O_NONBLOCK);
  while (comp) {
  nbrecv_restart:
    ret = recv(s, buf, comp, 0);
    if (!ret) {
      errno = EPIPE;
      return (-1);
    }
    if (ret == -1 && errno == EWOULDBLOCK) {
      fprintf(stderr, "nbrecv: would block\n");
      return (len - comp);
    }
    if (ret == -1 && errno == EINTR) {
      goto nbrecv_restart;
    } else if (ret == -1) {
      perror("nbrecv: recv");
      return (-1);
    }
    fprintf(stderr, "nbrecv: s = %d, expect = %d, actual = %d\n", s, comp, ret);
    comp -= ret;
    buf = (char *)buf + ret;
  }
  return (len - comp);
}


x86 assembly (clang, -O0):
<nbrecv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2e70(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11a0 <nbrecv+0x60>
mov    -0x8(%rbp),%edi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
mov    0x2e4f(%rip),%rax
or     (%rax),%edx
call   1040 <fcntl@plt>
jmp    11a5 <nbrecv+0x65>
cmpl   $0x0,-0x20(%rbp)
je     12cb <nbrecv+0x18b>
jmp    11b4 <nbrecv+0x74>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11f2 <nbrecv+0xb2>
mov    0x2df7(%rip),%rax
mov    (%rax),%rcx
mov    0x2ddd(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12d4 <nbrecv+0x194>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    123b <nbrecv+0xfb>
mov    0x2dbd(%rip),%rax
mov    (%rax),%rax
mov    0x2dab(%rip),%rcx
cmp    (%rcx),%rax
jne    123b <nbrecv+0xfb>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
lea    0xdda(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12d4 <nbrecv+0x194>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1264 <nbrecv+0x124>
mov    0x2d74(%rip),%rax
mov    (%rax),%rax
mov    0x2d52(%rip),%rcx
cmp    (%rcx),%rax
jne    1264 <nbrecv+0x124>
jmp    11b4 <nbrecv+0x74>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1286 <nbrecv+0x146>
lea    0xda0(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d4 <nbrecv+0x194>
jmp    128b <nbrecv+0x14b>
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
lea    0xd7d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    11a5 <nbrecv+0x65>
mov    -0x14(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nbrecv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r13
mov    %edi,%r15d
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    0x2e7c(%rip),%rcx
mov    (%rcx),%edx
test   %eax,%edx
jne    1185 <nbrecv+0x45>
mov    0x2e4f(%rip),%rcx
mov    (%rcx),%esi
or     %eax,%edx
mov    %r15d,%edi
call   1040 <fcntl@plt>
test   %ebx,%ebx
je     124d <nbrecv+0x10d>
mov    0x2e24(%rip),%r14
mov    0x2e0d(%rip),%r12
mov    %ebx,0x4(%rsp)
mov    %ebx,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %r13,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
jne    11de <nbrecv+0x9e>
mov    0x2df5(%rip),%rax
mov    (%rax),%rax
cmp    (%r14),%rax
je     122e <nbrecv+0xee>
cmp    (%r12),%rax
je     11b0 <nbrecv+0x70>
jmp    125e <nbrecv+0x11e>
mov    %eax,%ebx
test   %eax,%eax
je     1213 <nbrecv+0xd3>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
mov    %r15d,%edx
mov    %ebp,%ecx
mov    %ebx,%r8d
xor    %eax,%eax
call   1050 <fprintf@plt>
movslq %ebx,%rax
add    %rax,%r13
sub    %ebx,%ebp
jne    11b0 <nbrecv+0x70>
mov    0x4(%rsp),%ebp
jmp    124d <nbrecv+0x10d>
mov    0x2db6(%rip),%rax
mov    (%rax),%rax
mov    0x2d9c(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%ebp
jmp    124d <nbrecv+0x10d>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
lea    0xdc2(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x4(%rsp),%eax
sub    %ebp,%eax
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb0(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebp
jmp    124d <nbrecv+0x10d>

LLVM IR (-O0):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@EPIPE = global i64 0
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@stderr = global i32 0
@.str = [21 x i8] c"nbrecv: would block\0A\00"
@EINTR = global i64 0
@.str.1 = [13 x i8] c"nbrecv: recv\00"
@.str.2 = [42 x i8] c"nbrecv: s = %d, expect = %d, actual = %d\0A\00"
define i32 @nbrecv(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 i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i32, i32* %7
store i32 %11, i32* %10
%12 = load i32, i32* %5
%13 = load i32, i32* @F_GETFL
%14 = call i32 @fcntl(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = load i32, i32* @O_NONBLOCK
%17 = and i32 %15, %16
%18 = icmp ne i32 %17, 0
br i1 %18, label %26, label %19
19: ; preds = %3
%20 = load i32, i32* %5
%21 = load i32, i32* @F_SETFL
%22 = load i32, i32* %8
%23 = load i32, i32* @O_NONBLOCK
%24 = or i32 %22, %23
%25 = call i32 @fcntl(i32 %20, i32 %21, i32 %24)
br label %26
26: ; preds = %19, %3
br label %27
27: ; preds = %67, %26
%28 = load i32, i32* %10
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %80
30: ; preds = %27
br label %31
31: ; preds = %60, %30
%32 = load i32, i32* %5
%33 = load i8*, i8** %6
%34 = load i32, i32* %10
%35 = call i32 @recv(i32 %32, i8* %33, i32 %34, i32 0)
store i32 %35, i32* %9
%36 = load i32, i32* %9
%37 = icmp ne i32 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %31
%39 = load i64, i64* @EPIPE
store i64 %39, i64* @errno
store i32 -1, i32* %4
br label %84
40: ; preds = %31
%41 = load i32, i32* %9
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %53
43: ; preds = %40
%44 = load i64, i64* @errno
%45 = load i64, i64* @EWOULDBLOCK
%46 = icmp eq i64 %44, %45
br i1 %46, label %47, label %53
47: ; preds = %43
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%50 = load i32, i32* %7
%51 = load i32, i32* %10
%52 = sub nsw i32 %50, %51
store i32 %52, i32* %4
br label %84
53: ; preds = %43, %40
%54 = load i32, i32* %9
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %61
56: ; preds = %53
%57 = load i64, i64* @errno
%58 = load i64, i64* @EINTR
%59 = icmp eq i64 %57, %58
br i1 %59, label %60, label %61
60: ; preds = %56
br label %31
61: ; preds = %56, %53
%62 = load i32, i32* %9
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %66
64: ; preds = %61
%65 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %84
66: ; preds = %61
br label %67
67: ; preds = %66
%68 = load i32, i32* @stderr
%69 = load i32, i32* %5
%70 = load i32, i32* %10
%71 = load i32, i32* %9
%72 = call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %69, i32 %70, i32 %71)
%73 = load i32, i32* %9
%74 = load i32, i32* %10
%75 = sub nsw i32 %74, %73
store i32 %75, i32* %10
%76 = load i8*, i8** %6
%77 = load i32, i32* %9
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i8, i8* %76, i64 %78
store i8* %79, i8** %6
br label %27
80: ; preds = %27
%81 = load i32, i32* %7
%82 = load i32, i32* %10
%83 = sub nsw i32 %81, %82
store i32 %83, i32* %4
br label %84
84: ; preds = %80, %64, %47, %38
%85 = load i32, i32* %4
ret i32 %85
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@EPIPE = global i64 0
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@stderr = global i32 0
@.str = [21 x i8] c"nbrecv: would block\0A\00"
@EINTR = global i64 0
@.str.1 = [13 x i8] c"nbrecv: recv\00"
@.str.2 = [42 x i8] c"nbrecv: s = %d, expect = %d, actual = %d\0A\00"
define i32 @nbrecv(i32 %0, i8* %1, i32 %2) {
%4 = load i32, i32* @F_GETFL
%5 = tail call i32 @fcntl(i32 %0, i32 %4, i32 0)
%6 = load i32, i32* @O_NONBLOCK
%7 = and i32 %6, %5
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %3
%10 = load i32, i32* @F_SETFL
%11 = or i32 %6, %5
%12 = tail call i32 @fcntl(i32 %0, i32 %10, i32 %11)
br label %13
13: ; preds = %9, %3
%14 = icmp eq i32 %2, 0
br i1 %14, label %42, label %15
15: ; preds = %13, %35
%16 = phi i32 [ %38, %35 ], [ %2, %13 ]
%17 = phi i8* [ %40, %35 ], [ %1, %13 ]
br label %18
18: ; preds = %15, %30
%19 = tail call i32 @recv(i32 %0, i8* %17, i32 %16, i32 0)
switch i32 %19, label %35 [
i32 0, label %20
i32 -1, label %22
]
20: ; preds = %18
%21 = load i64, i64* @EPIPE
store i64 %21, i64* @errno
br label %42
22: ; preds = %18
%23 = load i64, i64* @errno
%24 = load i64, i64* @EWOULDBLOCK
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%29 = sub nsw i32 %2, %16
br label %42
30: ; preds = %22
%31 = load i64, i64* @EINTR
%32 = icmp eq i64 %23, %31
br i1 %32, label %18, label %33
33: ; preds = %30
%34 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %42
35: ; preds = %18
%36 = load i32, i32* @stderr
%37 = tail call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %0, i32 %16, i32 %19)
%38 = sub nsw i32 %16, %19
%39 = sext i32 %19 to i64
%40 = getelementptr inbounds i8, i8* %17, i64 %39
%41 = icmp eq i32 %38, 0
br i1 %41, label %42, label %15
42: ; preds = %35, %13, %33, %26, %20
%43 = phi i32 [ %29, %26 ], [ -1, %33 ], [ -1, %20 ], [ 0, %13 ], [ %2, %35 ]
ret i32 %43
}