#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;
int /*<<< orphan*/ MSG_PEEK;
scalar_t__ errno;
int recv(int, char *, int, int /*<<< orphan*/);
int sock_peek(int fd, char *buf, int bufsize, void *arg) {
  int res;
  do
    res = recv(fd, buf, bufsize, MSG_PEEK);
  while (res == -1 && errno == EINTR);
  return res;
}


x86 assembly (clang, -O0):
<sock_peek>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    0x2eb1(%rip),%rax
mov    (%rax),%ecx
call   1030 <recv@plt>
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x24(%rbp)
mov    %al,-0x25(%rbp)
jne    116a <sock_peek+0x5a>
mov    0x2e81(%rip),%rax
mov    (%rax),%rax
mov    0x2e6f(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    1126 <sock_peek+0x16>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_peek>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2ebe(%rip),%r15
mov    0x2ea7(%rip),%r12
mov    0x2e98(%rip),%r13
nopl   0x0(%rax,%rax,1)
mov    (%r15),%ecx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
jne    115f <sock_peek+0x4f>
mov    0x0(%r13),%rcx
cmp    %rcx,(%r12)
je     1140 <sock_peek+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MSG_PEEK = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @sock_peek(i32 %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i8* %3, i8** %8
br label %10
10: ; preds = %23, %4
%11 = load i32, i32* %5
%12 = load i8*, i8** %6
%13 = load i32, i32* %7
%14 = load i32, i32* @MSG_PEEK
%15 = call i32 @recv(i32 %11, i8* %12, i32 %13, i32 %14)
store i32 %15, i32* %9
br label %16
16: ; preds = %10
%17 = load i32, i32* %9
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br label %23
23: ; preds = %19, %16
%24 = phi i1 [ false, %16 ], [ %22, %19 ]
br i1 %24, label %10, label %25
25: ; preds = %23
%26 = load i32, i32* %9
ret i32 %26
}

LLVM IR (-O2):
@MSG_PEEK = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @sock_peek(i32 %0, i8* %1, i32 %2, i8* nocapture readnone %3) {
br label %5
5: ; preds = %5, %4
%6 = load i32, i32* @MSG_PEEK
%7 = tail call i32 @recv(i32 %0, i8* %1, i32 %2, i32 %6)
%8 = icmp eq i32 %7, -1
%9 = load i64, i64* @errno
%10 = load i64, i64* @EINTR
%11 = icmp eq i64 %9, %10
%12 = select i1 %8, i1 %11, i1 false
br i1 %12, label %5, label %13
13: ; preds = %5
ret i32 %7
}