#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 */
typedef int int32_t;

/* Variables and functions */
int MSG_DONTWAIT;
int MSG_NOSIGNAL;
int MSG_PEEK;
int recv(int, unsigned char *, int, int);
int32_t cs_msg_chkrecv(int32_t sock) {
  int32_t len;
  unsigned char netbuf[600];
  len = recv(sock, netbuf, 2, MSG_PEEK | MSG_NOSIGNAL | MSG_DONTWAIT);
  if (len == 0)
    return 0;
  if (len != 2)
    return -1;
  int32_t datasize = (netbuf[0] << 8) | netbuf[1];
  if (datasize > 600 - 2)
    return 0;
  len =
      recv(sock, netbuf, 2 + datasize, MSG_PEEK | MSG_NOSIGNAL | MSG_DONTWAIT);
  if (len != 2 + datasize)
    return -1;
  return len;
}


x86 assembly (clang, -O0):
<cs_msg_chkrecv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x280,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x270(%rbp),%rsi
mov    0x2eb9(%rip),%rax
mov    (%rax),%ecx
mov    0x2e98(%rip),%rax
or     (%rax),%ecx
mov    0x2e97(%rip),%rax
or     (%rax),%ecx
mov    $0x2,%edx
call   1030 <recv@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1166 <cs_msg_chkrecv+0x56>
movl   $0x0,-0x4(%rbp)
jmp    120d <cs_msg_chkrecv+0xfd>
cmpl   $0x2,-0xc(%rbp)
je     117c <cs_msg_chkrecv+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <cs_msg_chkrecv+0xfd>
movzbl -0x270(%rbp),%eax
shl    $0x8,%eax
movzbl -0x26f(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0x274(%rbp)
cmpl   $0x256,-0x274(%rbp)
jle    11b1 <cs_msg_chkrecv+0xa1>
movl   $0x0,-0x4(%rbp)
jmp    120d <cs_msg_chkrecv+0xfd>
mov    -0x8(%rbp),%edi
lea    -0x270(%rbp),%rsi
mov    -0x274(%rbp),%edx
add    $0x2,%edx
mov    0x2e1d(%rip),%rax
mov    (%rax),%ecx
mov    0x2dfc(%rip),%rax
or     (%rax),%ecx
mov    0x2dfb(%rip),%rax
or     (%rax),%ecx
call   1030 <recv@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x274(%rbp),%ecx
add    $0x2,%ecx
cmp    %ecx,%eax
je     1207 <cs_msg_chkrecv+0xf7>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <cs_msg_chkrecv+0xfd>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x280,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cs_msg_chkrecv>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x260,%rsp
mov    0x2ec2(%rip),%r14
mov    0x2ea3(%rip),%r15
mov    (%r15),%ecx
or     (%r14),%ecx
mov    %edi,%ebx
mov    0x2e9c(%rip),%r12
or     (%r12),%ecx
mov    %rsp,%rsi
mov    $0x2,%edx
call   1030 <recv@plt>
test   %eax,%eax
je     1195 <cs_msg_chkrecv+0x85>
cmp    $0x2,%eax
jne    1190 <cs_msg_chkrecv+0x80>
movzwl (%rsp),%ebp
shl    $0x10,%ebp
bswap  %ebp
xor    %eax,%eax
cmp    $0x256,%ebp
ja     1195 <cs_msg_chkrecv+0x85>
add    $0x2,%ebp
mov    (%r15),%ecx
or     (%r14),%ecx
or     (%r12),%ecx
mov    %rsp,%rsi
mov    %ebx,%edi
mov    %ebp,%edx
call   1030 <recv@plt>
mov    %eax,%ecx
cmp    %ebp,%eax
mov    $0xffffffff,%eax
cmove  %ecx,%eax
jmp    1195 <cs_msg_chkrecv+0x85>
mov    $0xffffffff,%eax
add    $0x260,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MSG_PEEK = global i32 0
@MSG_NOSIGNAL = global i32 0
@MSG_DONTWAIT = global i32 0
define i32 @cs_msg_chkrecv(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [600 x i8]
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = getelementptr inbounds [600 x i8], [600 x i8]* %5, i64 0, i64 0
%9 = load i32, i32* @MSG_PEEK
%10 = load i32, i32* @MSG_NOSIGNAL
%11 = or i32 %9, %10
%12 = load i32, i32* @MSG_DONTWAIT
%13 = or i32 %11, %12
%14 = call i32 @recv(i32 %7, i8* %8, i32 2, i32 %13)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 0, i32* %2
br label %52
18: ; preds = %1
%19 = load i32, i32* %4
%20 = icmp ne i32 %19, 2
br i1 %20, label %21, label %22
21: ; preds = %18
store i32 -1, i32* %2
br label %52
22: ; preds = %18
%23 = getelementptr inbounds [600 x i8], [600 x i8]* %5, i64 0, i64 0
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = shl i32 %25, 8
%27 = getelementptr inbounds [600 x i8], [600 x i8]* %5, i64 0, i64 1
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = or i32 %26, %29
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp sgt i32 %31, 598
br i1 %32, label %33, label %34
33: ; preds = %22
store i32 0, i32* %2
br label %52
34: ; preds = %22
%35 = load i32, i32* %3
%36 = getelementptr inbounds [600 x i8], [600 x i8]* %5, i64 0, i64 0
%37 = load i32, i32* %6
%38 = add nsw i32 2, %37
%39 = load i32, i32* @MSG_PEEK
%40 = load i32, i32* @MSG_NOSIGNAL
%41 = or i32 %39, %40
%42 = load i32, i32* @MSG_DONTWAIT
%43 = or i32 %41, %42
%44 = call i32 @recv(i32 %35, i8* %36, i32 %38, i32 %43)
store i32 %44, i32* %4
%45 = load i32, i32* %4
%46 = load i32, i32* %6
%47 = add nsw i32 2, %46
%48 = icmp ne i32 %45, %47
br i1 %48, label %49, label %50
49: ; preds = %34
store i32 -1, i32* %2
br label %52
50: ; preds = %34
%51 = load i32, i32* %4
store i32 %51, i32* %2
br label %52
52: ; preds = %50, %49, %33, %21, %17
%53 = load i32, i32* %2
ret i32 %53
}

LLVM IR (-O2):
@MSG_PEEK = global i32 0
@MSG_NOSIGNAL = global i32 0
@MSG_DONTWAIT = global i32 0
define i32 @cs_msg_chkrecv(i32 %0) {
%2 = alloca [600 x i8]
%3 = getelementptr inbounds [600 x i8], [600 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @MSG_PEEK
%5 = load i32, i32* @MSG_NOSIGNAL
%6 = or i32 %5, %4
%7 = load i32, i32* @MSG_DONTWAIT
%8 = or i32 %6, %7
%9 = call i32 @recv(i32 %0, i8* %3, i32 2, i32 %8)
switch i32 %9, label %10 [
i32 0, label %30
i32 2, label %11
]
10: ; preds = %1
br label %30
11: ; preds = %1
%12 = load i8, i8* %3
%13 = zext i8 %12 to i32
%14 = shl nuw nsw i32 %13, 8
%15 = getelementptr inbounds [600 x i8], [600 x i8]* %2, i64 0, i64 1
%16 = load i8, i8* %15
%17 = zext i8 %16 to i32
%18 = or i32 %14, %17
%19 = icmp ugt i32 %18, 598
br i1 %19, label %30, label %20
20: ; preds = %11
%21 = add nuw nsw i32 %18, 2
%22 = load i32, i32* @MSG_PEEK
%23 = load i32, i32* @MSG_NOSIGNAL
%24 = or i32 %23, %22
%25 = load i32, i32* @MSG_DONTWAIT
%26 = or i32 %24, %25
%27 = call i32 @recv(i32 %0, i8* %3, i32 %21, i32 %26)
%28 = icmp eq i32 %27, %21
%29 = select i1 %28, i32 %27, i32 -1
br label %30
30: ; preds = %11, %20, %1, %10
%31 = phi i32 [ -1, %10 ], [ %9, %1 ], [ 0, %11 ], [ %29, %20 ]
ret i32 %31
}