#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 ssize_t;
typedef int /*<<< orphan*/ msgLen;

/* Variables and functions */
int /*<<< orphan*/ MSG_WAITALL;
unsigned short ntohs(unsigned short);
int recv(int, ...);
int read_packet(int sock, unsigned char *buf, int buflen) {
  int result = -1;
  ssize_t nread;
  unsigned short msgLen;
  nread = recv(sock, &msgLen, sizeof(msgLen), MSG_WAITALL);
  if (nread == sizeof(msgLen)) {
    msgLen = ntohs(msgLen);
    if (msgLen <= buflen) {
      nread = recv(sock, buf, msgLen, MSG_WAITALL);
      if (nread == msgLen) {
        result = nread;
      }
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<read_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e95(%rip),%rax
mov    (%rax),%ecx
lea    -0x1e(%rbp),%rsi
mov    $0x2,%edx
mov    $0x0,%al
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    $0x2,%rax
jne    11bd <read_packet+0x9d>
movzwl -0x1e(%rbp),%edi
call   1040 <ntohs@plt>
mov    %ax,-0x1e(%rbp)
movzwl -0x1e(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     11b8 <read_packet+0x98>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movzwl -0x1e(%rbp),%edx
mov    0x2e46(%rip),%rax
mov    (%rax),%ecx
mov    $0x0,%al
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
movzwl -0x1e(%rbp),%ecx
cmp    %ecx,%eax
jne    11b3 <read_packet+0x93>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11b8 <read_packet+0x98>
jmp    11bd <read_packet+0x9d>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_packet>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r15d
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2e9d(%rip),%r12
mov    (%r12),%ecx
lea    0xe(%rsp),%rsi
mov    $0x2,%edx
xor    %eax,%eax
call   1030 <recv@plt>
mov    $0xffffffff,%ebx
cmp    $0x2,%eax
jne    1190 <read_packet+0x70>
movzwl 0xe(%rsp),%edi
call   1040 <ntohs@plt>
mov    %ax,0xe(%rsp)
movzwl %ax,%edx
cmp    %r15d,%edx
jg     1190 <read_packet+0x70>
mov    (%r12),%ecx
mov    %ebp,%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <recv@plt>
movzwl 0xe(%rsp),%ecx
cmp    %ecx,%eax
mov    $0xffffffff,%ebx
cmove  %eax,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MSG_WAITALL = global i32 0
define i32 @read_packet(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i16
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 -1, i32* %7
%10 = load i32, i32* %4
%11 = load i32, i32* @MSG_WAITALL
%12 = call i32 (i32, ...) @recv(i32 %10, i16* %9, i64 2, i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = sext i32 %13 to i64
%15 = icmp eq i64 %14, 2
br i1 %15, label %16, label %38
16: ; preds = %3
%17 = load i16, i16* %9
%18 = call zeroext i16 @ntohs(i16 zeroext %17)
store i16 %18, i16* %9
%19 = load i16, i16* %9
%20 = zext i16 %19 to i32
%21 = load i32, i32* %6
%22 = icmp sle i32 %20, %21
br i1 %22, label %23, label %37
23: ; preds = %16
%24 = load i32, i32* %4
%25 = load i8*, i8** %5
%26 = load i16, i16* %9
%27 = zext i16 %26 to i32
%28 = load i32, i32* @MSG_WAITALL
%29 = call i32 (i32, ...) @recv(i32 %24, i8* %25, i32 %27, i32 %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = load i16, i16* %9
%32 = zext i16 %31 to i32
%33 = icmp eq i32 %30, %32
br i1 %33, label %34, label %36
34: ; preds = %23
%35 = load i32, i32* %8
store i32 %35, i32* %7
br label %36
36: ; preds = %34, %23
br label %37
37: ; preds = %36, %16
br label %38
38: ; preds = %37, %3
%39 = load i32, i32* %7
ret i32 %39
}

LLVM IR (-O2):
@MSG_WAITALL = global i32 0
define i32 @read_packet(i32 %0, i8* %1, i32 %2) {
%4 = alloca i16
%5 = bitcast i16* %4 to i8*
%6 = load i32, i32* @MSG_WAITALL
%7 = call i32 (i32, ...) @recv(i32 %0, i16* %4, i64 2, i32 %6)
%8 = icmp eq i32 %7, 2
br i1 %8, label %9, label %21
9: ; preds = %3
%10 = load i16, i16* %4
%11 = call zeroext i16 @ntohs(i16 zeroext %10)
store i16 %11, i16* %4
%12 = zext i16 %11 to i32
%13 = icmp sgt i32 %12, %2
br i1 %13, label %21, label %14
14: ; preds = %9
%15 = load i32, i32* @MSG_WAITALL
%16 = call i32 (i32, ...) @recv(i32 %0, i8* %1, i32 %12, i32 %15)
%17 = load i16, i16* %4
%18 = zext i16 %17 to i32
%19 = icmp eq i32 %16, %18
%20 = select i1 %19, i32 %16, i32 -1
br label %21
21: ; preds = %14, %9, %3
%22 = phi i32 [ -1, %9 ], [ -1, %3 ], [ %20, %14 ]
ret i32 %22
}