#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 uint32_t;
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ MSG_PEEK;
int /*<<< orphan*/ memcpy(char *, char *, int);
int ntohl(int);
int /*<<< orphan*/ printf(char *, int, size_t);
size_t socket_receive(int, char *, size_t, int /*<<< orphan*/);
ssize_t packet_receive(int sock, char *data) {
  char buf[200];
  size_t packet_size;
  uint32_t data_size;
  if (socket_receive(sock, buf, sizeof(uint32_t), MSG_PEEK) != sizeof(uint32_t))
    return -1;
  data_size = ntohl(*(uint32_t *)buf);
  packet_size = sizeof(uint32_t) + data_size;
  printf("packet_receive(): DATA = %u bytes / Packet = %lu bytes\n", data_size,
         packet_size);
  if (socket_receive(sock, buf, packet_size, 0) != packet_size)
    return -1;
  memcpy(data, (buf + sizeof(uint32_t)), data_size);
  return data_size;
}


x86 assembly (clang, -O0):
<packet_receive>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xe0(%rbp),%rsi
mov    0x2e85(%rip),%rax
mov    (%rax),%ecx
mov    $0x4,%edx
call   1050 <socket_receive@plt>
cmp    $0x4,%rax
je     1185 <packet_receive+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <packet_receive+0xd5>
mov    -0xe0(%rbp),%edi
call   1060 <ntohl@plt>
mov    %eax,-0xec(%rbp)
movslq -0xec(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0xe8(%rbp)
mov    -0xec(%rbp),%esi
mov    -0xe8(%rbp),%rdx
lea    0xe44(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0xe0(%rbp),%rsi
mov    -0xe8(%rbp),%rdx
xor    %ecx,%ecx
call   1050 <socket_receive@plt>
cmp    -0xe8(%rbp),%rax
je     11f2 <packet_receive+0xb2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <packet_receive+0xd5>
mov    -0x10(%rbp),%rdi
lea    -0xe0(%rbp),%rsi
add    $0x4,%rsi
mov    -0xec(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0xec(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<packet_receive>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0xd0,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x2e8d(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rsi
mov    $0x4,%edx
call   1050 <socket_receive@plt>
mov    $0xffffffff,%r12d
cmp    $0x4,%rax
jne    11c0 <packet_receive+0x80>
mov    (%rsp),%edi
call   1060 <ntohl@plt>
mov    %eax,%r15d
movslq %eax,%rbp
add    $0x4,%rbp
lea    0xe71(%rip),%rdi
mov    %eax,%esi
mov    %rbp,%rdx
call   1030 <printf@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
xor    %ecx,%ecx
call   1050 <socket_receive@plt>
cmp    %rbp,%rax
jne    11c0 <packet_receive+0x80>
lea    0x4(%rsp),%rsi
mov    %r14,%rdi
mov    %r15d,%edx
call   1040 <memcpy@plt>
mov    %r15d,%r12d
mov    %r12d,%eax
add    $0xd0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MSG_PEEK = global i32 0
@.str = [56 x i8] c"packet_receive(): DATA = %u bytes / Packet = %lu bytes\0A\00"
define i32 @packet_receive(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [200 x i8]
%7 = alloca i64
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = load i32, i32* %4
%10 = getelementptr inbounds [200 x i8], [200 x i8]* %6, i64 0, i64 0
%11 = load i32, i32* @MSG_PEEK
%12 = call i64 @socket_receive(i32 %9, i8* %10, i64 4, i32 %11)
%13 = icmp ne i64 %12, 4
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 -1, i32* %3
br label %40
15: ; preds = %2
%16 = getelementptr inbounds [200 x i8], [200 x i8]* %6, i64 0, i64 0
%17 = bitcast i8* %16 to i32*
%18 = load i32, i32* %17
%19 = call i32 @ntohl(i32 %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = sext i32 %20 to i64
%22 = add i64 4, %21
store i64 %22, i64* %7
%23 = load i32, i32* %8
%24 = load i64, i64* %7
%25 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32 %23, i64 %24)
%26 = load i32, i32* %4
%27 = getelementptr inbounds [200 x i8], [200 x i8]* %6, i64 0, i64 0
%28 = load i64, i64* %7
%29 = call i64 @socket_receive(i32 %26, i8* %27, i64 %28, i32 0)
%30 = load i64, i64* %7
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %15
store i32 -1, i32* %3
br label %40
33: ; preds = %15
%34 = load i8*, i8** %5
%35 = getelementptr inbounds [200 x i8], [200 x i8]* %6, i64 0, i64 0
%36 = getelementptr inbounds i8, i8* %35, i64 4
%37 = load i32, i32* %8
%38 = call i32 @memcpy(i8* %34, i8* %36, i32 %37)
%39 = load i32, i32* %8
store i32 %39, i32* %3
br label %40
40: ; preds = %33, %32, %14
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@MSG_PEEK = global i32 0
@.str = [56 x i8] c"packet_receive(): DATA = %u bytes / Packet = %lu bytes\0A\00"
define i32 @packet_receive(i32 %0, i8* %1) {
%3 = alloca [200 x i8]
%4 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @MSG_PEEK
%6 = call i64 @socket_receive(i32 %0, i8* %4, i64 4, i32 %5)
%7 = icmp eq i64 %6, 4
br i1 %7, label %8, label %20
8: ; preds = %2
%9 = bitcast [200 x i8]* %3 to i32*
%10 = load i32, i32* %9
%11 = call i32 @ntohl(i32 %10)
%12 = sext i32 %11 to i64
%13 = add nsw i64 %12, 4
%14 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32 %11, i64 %13)
%15 = call i64 @socket_receive(i32 %0, i8* %4, i64 %13, i32 0)
%16 = icmp eq i64 %15, %13
br i1 %16, label %17, label %20
17: ; preds = %8
%18 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 4
%19 = call i32 @memcpy(i8* %1, i8* %18, i32 %11)
br label %20
20: ; preds = %8, %2, %17
%21 = phi i32 [ %11, %17 ], [ -1, %2 ], [ -1, %8 ]
ret i32 %21
}