#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 MSG_PEEK;
int O_NONBLOCK;
int /*<<< orphan*/ assert(int);
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int recv(int, void *, int, int);
int BMI_sockio_nbpeek(int s, void *buf, int len) {
  int ret;
  assert(fcntl(s, F_GETFL, 0) & O_NONBLOCK);
nbpeek_restart:
  ret = recv(s, buf, len, (MSG_PEEK | 0));
  if (ret == 0) {
    errno = EPIPE;
    return (-1);
  } else if (ret == -1 && errno == EWOULDBLOCK) {
    return (0);
  } else if (ret == -1 && errno == EINTR) {
    goto nbpeek_restart;
  } else if (ret == -1) {
    return (-1);
  }
  return (ret);
}


x86 assembly (clang, -O0):
<BMI_sockio_nbpeek>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,%edi
mov    0x2e8a(%rip),%rax
and    (%rax),%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    0x2e6a(%rip),%rax
mov    (%rax),%ecx
or     $0x0,%ecx
call   1030 <recv@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11ad <BMI_sockio_nbpeek+0x7d>
mov    0x2e3c(%rip),%rax
mov    (%rax),%rcx
mov    0x2e2a(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <BMI_sockio_nbpeek+0x101>
cmpl   $0xffffffff,-0x18(%rbp)
jne    11dd <BMI_sockio_nbpeek+0xad>
mov    0x2e0a(%rip),%rax
mov    (%rax),%rax
mov    0x2df8(%rip),%rcx
cmp    (%rcx),%rax
jne    11dd <BMI_sockio_nbpeek+0xad>
movl   $0x0,-0x4(%rbp)
jmp    1231 <BMI_sockio_nbpeek+0x101>
cmpl   $0xffffffff,-0x18(%rbp)
jne    1206 <BMI_sockio_nbpeek+0xd6>
mov    0x2dda(%rip),%rax
mov    (%rax),%rax
mov    0x2db8(%rip),%rcx
cmp    (%rcx),%rax
jne    1206 <BMI_sockio_nbpeek+0xd6>
jmp    1165 <BMI_sockio_nbpeek+0x35>
cmpl   $0xffffffff,-0x18(%rbp)
jne    121c <BMI_sockio_nbpeek+0xec>
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <BMI_sockio_nbpeek+0x101>
jmp    1221 <BMI_sockio_nbpeek+0xf1>
jmp    1226 <BMI_sockio_nbpeek+0xf6>
jmp    122b <BMI_sockio_nbpeek+0xfb>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BMI_sockio_nbpeek>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    0x2e8e(%rip),%rcx
and    (%rcx),%eax
mov    %eax,%edi
call   1050 <assert@plt>
mov    0x2e76(%rip),%r12
mov    0x2e4f(%rip),%r13
mov    0x2e38(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    (%r12),%ecx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
jne    11ad <BMI_sockio_nbpeek+0x7d>
mov    0x2e2b(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
je     11cc <BMI_sockio_nbpeek+0x9c>
cmp    (%r15),%rax
je     1180 <BMI_sockio_nbpeek+0x50>
jmp    11c5 <BMI_sockio_nbpeek+0x95>
test   %eax,%eax
jne    11ce <BMI_sockio_nbpeek+0x9e>
mov    0x2e18(%rip),%rax
mov    (%rax),%rax
mov    0x2e06(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%eax
jmp    11ce <BMI_sockio_nbpeek+0x9e>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@MSG_PEEK = global i32 0
@EPIPE = global i64 0
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@EINTR = global i64 0
define i32 @BMI_sockio_nbpeek(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i32, i32* %5
%10 = load i32, i32* @F_GETFL
%11 = call i32 @fcntl(i32 %9, i32 %10, i32 0)
%12 = load i32, i32* @O_NONBLOCK
%13 = and i32 %11, %12
%14 = call i32 @assert(i32 %13)
br label %15
15: ; preds = %41, %3
%16 = load i32, i32* %5
%17 = load i8*, i8** %6
%18 = load i32, i32* %7
%19 = load i32, i32* @MSG_PEEK
%20 = or i32 %19, 0
%21 = call i32 @recv(i32 %16, i8* %17, i32 %18, i32 %20)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %15
%25 = load i64, i64* @EPIPE
store i64 %25, i64* @errno
store i32 -1, i32* %4
br label %51
26: ; preds = %15
%27 = load i32, i32* %8
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = load i64, i64* @errno
%31 = load i64, i64* @EWOULDBLOCK
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %29
store i32 0, i32* %4
br label %51
34: ; preds = %29, %26
%35 = load i32, i32* %8
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %42
37: ; preds = %34
%38 = load i64, i64* @errno
%39 = load i64, i64* @EINTR
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %42
41: ; preds = %37
br label %15
42: ; preds = %37, %34
%43 = load i32, i32* %8
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %46
45: ; preds = %42
store i32 -1, i32* %4
br label %51
46: ; preds = %42
br label %47
47: ; preds = %46
br label %48
48: ; preds = %47
br label %49
49: ; preds = %48
%50 = load i32, i32* %8
store i32 %50, i32* %4
br label %51
51: ; preds = %49, %45, %33, %24
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@MSG_PEEK = global i32 0
@EPIPE = global i64 0
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@EINTR = global i64 0
define i32 @BMI_sockio_nbpeek(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 = tail call i32 @assert(i32 %7)
br label %9
9: ; preds = %18, %3
%10 = load i32, i32* @MSG_PEEK
%11 = tail call i32 @recv(i32 %0, i8* %1, i32 %2, i32 %10)
switch i32 %11, label %21 [
i32 0, label %12
i32 -1, label %14
]
12: ; preds = %9
%13 = load i64, i64* @EPIPE
store i64 %13, i64* @errno
br label %21
14: ; preds = %9
%15 = load i64, i64* @errno
%16 = load i64, i64* @EWOULDBLOCK
%17 = icmp eq i64 %15, %16
br i1 %17, label %21, label %18
18: ; preds = %14
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %15, %19
br i1 %20, label %9, label %21
21: ; preds = %9, %18, %14, %12
%22 = phi i32 [ -1, %12 ], [ %11, %9 ], [ -1, %18 ], [ 0, %14 ]
ret i32 %22
}