#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 rdgd_check_message(int32_t sock) {
  int32_t len;
  unsigned char netbuf[300];
  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[1];
  len =
      recv(sock, netbuf, 2 + datasize, MSG_PEEK | MSG_NOSIGNAL | MSG_DONTWAIT);
  if (len != 2 + datasize)
    return -1;
  return len;
}


x86 assembly (clang, -O0):
<rdgd_check_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x140(%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 <rdgd_check_message+0x56>
movl   $0x0,-0x4(%rbp)
jmp    11e5 <rdgd_check_message+0xd5>
cmpl   $0x2,-0xc(%rbp)
je     117c <rdgd_check_message+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e5 <rdgd_check_message+0xd5>
movzbl -0x13f(%rbp),%eax
mov    %eax,-0x144(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    -0x144(%rbp),%edx
add    $0x2,%edx
mov    0x2e45(%rip),%rax
mov    (%rax),%ecx
mov    0x2e24(%rip),%rax
or     (%rax),%ecx
mov    0x2e23(%rip),%rax
or     (%rax),%ecx
call   1030 <recv@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x144(%rbp),%ecx
add    $0x2,%ecx
cmp    %ecx,%eax
je     11df <rdgd_check_message+0xcf>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e5 <rdgd_check_message+0xd5>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rdgd_check_message>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x130,%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     1187 <rdgd_check_message+0x77>
cmp    $0x2,%eax
jne    1182 <rdgd_check_message+0x72>
movzbl 0x1(%rsp),%ebp
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    1187 <rdgd_check_message+0x77>
mov    $0xffffffff,%eax
add    $0x130,%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 @rdgd_check_message(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [300 x i8]
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = getelementptr inbounds [300 x i8], [300 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 %43
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 %43
22: ; preds = %18
%23 = getelementptr inbounds [300 x i8], [300 x i8]* %5, i64 0, i64 1
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
store i32 %25, i32* %6
%26 = load i32, i32* %3
%27 = getelementptr inbounds [300 x i8], [300 x i8]* %5, i64 0, i64 0
%28 = load i32, i32* %6
%29 = add nsw i32 2, %28
%30 = load i32, i32* @MSG_PEEK
%31 = load i32, i32* @MSG_NOSIGNAL
%32 = or i32 %30, %31
%33 = load i32, i32* @MSG_DONTWAIT
%34 = or i32 %32, %33
%35 = call i32 @recv(i32 %26, i8* %27, i32 %29, i32 %34)
store i32 %35, i32* %4
%36 = load i32, i32* %4
%37 = load i32, i32* %6
%38 = add nsw i32 2, %37
%39 = icmp ne i32 %36, %38
br i1 %39, label %40, label %41
40: ; preds = %22
store i32 -1, i32* %2
br label %43
41: ; preds = %22
%42 = load i32, i32* %4
store i32 %42, i32* %2
br label %43
43: ; preds = %41, %40, %21, %17
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
@MSG_PEEK = global i32 0
@MSG_NOSIGNAL = global i32 0
@MSG_DONTWAIT = global i32 0
define i32 @rdgd_check_message(i32 %0) {
%2 = alloca [300 x i8]
%3 = getelementptr inbounds [300 x i8], [300 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 %24
i32 2, label %11
]
10: ; preds = %1
br label %24
11: ; preds = %1
%12 = getelementptr inbounds [300 x i8], [300 x i8]* %2, i64 0, i64 1
%13 = load i8, i8* %12
%14 = zext i8 %13 to i32
%15 = add nuw nsw i32 %14, 2
%16 = load i32, i32* @MSG_PEEK
%17 = load i32, i32* @MSG_NOSIGNAL
%18 = or i32 %17, %16
%19 = load i32, i32* @MSG_DONTWAIT
%20 = or i32 %18, %19
%21 = call i32 @recv(i32 %0, i8* %3, i32 %15, i32 %20)
%22 = icmp eq i32 %21, %15
%23 = select i1 %22, i32 %21, i32 -1
br label %24
24: ; preds = %1, %11, %10
%25 = phi i32 [ -1, %10 ], [ %23, %11 ], [ %9, %1 ]
ret i32 %25
}