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


x86 assembly (clang, -O0):
<nbpeek>:
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    0x2e6e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2e90(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1180 <nbpeek+0x60>
mov    -0x8(%rbp),%edi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
mov    0x2e6f(%rip),%rax
or     (%rax),%edx
call   1040 <fcntl@plt>
jmp    1185 <nbpeek+0x65>
cmpl   $0x0,-0x20(%rbp)
je     125f <nbpeek+0x13f>
jmp    1194 <nbpeek+0x74>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%edx
mov    0x2e3b(%rip),%rax
mov    (%rax),%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11d9 <nbpeek+0xb9>
mov    0x2e10(%rip),%rax
mov    (%rax),%rcx
mov    0x2df6(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <nbpeek+0x148>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    120b <nbpeek+0xeb>
mov    0x2dd6(%rip),%rax
mov    (%rax),%rax
mov    0x2dc4(%rip),%rcx
cmp    (%rcx),%rax
jne    120b <nbpeek+0xeb>
mov    -0x14(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1268 <nbpeek+0x148>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1234 <nbpeek+0x114>
mov    0x2da4(%rip),%rax
mov    (%rax),%rax
mov    0x2d82(%rip),%rcx
cmp    (%rcx),%rax
jne    1234 <nbpeek+0x114>
jmp    1194 <nbpeek+0x74>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    124a <nbpeek+0x12a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <nbpeek+0x148>
jmp    124f <nbpeek+0x12f>
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
jmp    1185 <nbpeek+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):
<nbpeek>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r12
mov    %edi,%ebp
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
xor    %r15d,%r15d
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    0x2e9c(%rip),%rcx
mov    (%rcx),%edx
test   %eax,%edx
jne    1164 <nbpeek+0x44>
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%esi
or     %eax,%edx
mov    %ebp,%edi
call   1040 <fcntl@plt>
test   %ebx,%ebx
je     11ee <nbpeek+0xce>
mov    0x2e6d(%rip),%r13
mov    0x2e3e(%rip),%r14
mov    0x2e27(%rip),%r15
mov    %ebx,0x4(%rsp)
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x0(%r13),%ecx
mov    %ebp,%edi
mov    %r12,%rsi
mov    %ebx,%edx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
jne    11bb <nbpeek+0x9b>
mov    0x2e14(%rip),%rax
mov    (%rax),%rax
cmp    (%r14),%rax
je     11e6 <nbpeek+0xc6>
cmp    (%r15),%rax
je     1190 <nbpeek+0x70>
jmp    11de <nbpeek+0xbe>
test   %eax,%eax
je     11ca <nbpeek+0xaa>
sub    %eax,%ebx
jne    1190 <nbpeek+0x70>
mov    0x4(%rsp),%r15d
jmp    11ee <nbpeek+0xce>
mov    0x2dff(%rip),%rax
mov    (%rax),%rax
mov    0x2de5(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%r15d
jmp    11ee <nbpeek+0xce>
mov    0x4(%rsp),%r15d
sub    %ebx,%r15d
mov    %r15d,%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
@F_SETFL = 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 @nbpeek(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 = %65, %26
%28 = load i32, i32* %10
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %69
30: ; preds = %27
br label %31
31: ; preds = %59, %30
%32 = load i32, i32* %5
%33 = load i8*, i8** %6
%34 = load i32, i32* %10
%35 = load i32, i32* @MSG_PEEK
%36 = call i32 @recv(i32 %32, i8* %33, i32 %34, i32 %35)
store i32 %36, i32* %9
%37 = load i32, i32* %9
%38 = icmp ne i32 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %31
%40 = load i64, i64* @EPIPE
store i64 %40, i64* @errno
store i32 -1, i32* %4
br label %73
41: ; preds = %31
%42 = load i32, i32* %9
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %52
44: ; preds = %41
%45 = load i64, i64* @errno
%46 = load i64, i64* @EWOULDBLOCK
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %52
48: ; preds = %44
%49 = load i32, i32* %7
%50 = load i32, i32* %10
%51 = sub nsw i32 %49, %50
store i32 %51, i32* %4
br label %73
52: ; preds = %44, %41
%53 = load i32, i32* %9
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %60
55: ; preds = %52
%56 = load i64, i64* @errno
%57 = load i64, i64* @EINTR
%58 = icmp eq i64 %56, %57
br i1 %58, label %59, label %60
59: ; preds = %55
br label %31
60: ; preds = %55, %52
%61 = load i32, i32* %9
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %64
63: ; preds = %60
store i32 -1, i32* %4
br label %73
64: ; preds = %60
br label %65
65: ; preds = %64
%66 = load i32, i32* %9
%67 = load i32, i32* %10
%68 = sub nsw i32 %67, %66
store i32 %68, i32* %10
br label %27
69: ; preds = %27
%70 = load i32, i32* %7
%71 = load i32, i32* %10
%72 = sub nsw i32 %70, %71
store i32 %72, i32* %4
br label %73
73: ; preds = %69, %63, %48, %39
%74 = load i32, i32* %4
ret i32 %74
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = 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 @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 = 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 %34, label %15
15: ; preds = %13, %31
%16 = phi i32 [ %32, %31 ], [ %2, %13 ]
br label %17
17: ; preds = %15, %28
%18 = load i32, i32* @MSG_PEEK
%19 = tail call i32 @recv(i32 %0, i8* %1, i32 %16, i32 %18)
switch i32 %19, label %31 [
i32 0, label %20
i32 -1, label %22
]
20: ; preds = %17
%21 = load i64, i64* @EPIPE
store i64 %21, i64* @errno
br label %34
22: ; preds = %17
%23 = load i64, i64* @errno
%24 = load i64, i64* @EWOULDBLOCK
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = sub nsw i32 %2, %16
br label %34
28: ; preds = %22
%29 = load i64, i64* @EINTR
%30 = icmp eq i64 %23, %29
br i1 %30, label %17, label %34
31: ; preds = %17
%32 = sub nsw i32 %16, %19
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %15
34: ; preds = %31, %28, %13, %26, %20
%35 = phi i32 [ %27, %26 ], [ -1, %20 ], [ 0, %13 ], [ -1, %28 ], [ %2, %31 ]
ret i32 %35
}