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

/* Variables and functions */
int /*<<< orphan*/ printf(char *);
int read_uint16(int /*<<< orphan*/ *);
int /*<<< orphan*/ *realloc(int /*<<< orphan*/ *, int);
int recv(int, int /*<<< orphan*/ *, int, int /*<<< orphan*/);
ssize_t recv_message(int s, uint8_t **msg) {
  ssize_t received = -1;
  uint8_t *new_msg = realloc(*msg, 4);
  if (!new_msg) {
    printf("Failed to allocate message buffer");
    return -1;
  }
  *msg = new_msg;
  received = recv(s, *msg, 4, 0);
  if (received == 4) {
    uint16_t reported_len = read_uint16((*msg) + 2);
    if (reported_len) {
      new_msg = realloc(*msg, reported_len + 4);
      if (!new_msg) {
        printf("Failed to allocate message buffer");
        return -1;
      }
      *msg = new_msg;
      received = recv(s, (*msg) + 4, reported_len, 0);
      if (received == -1)
        return -1;
      received += 4;
    }
  }
  return received;
}


x86 assembly (clang, -O0):
<recv_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    $0x4,%esi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118e <recv_message+0x4e>
lea    0xe83(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1262 <recv_message+0x122>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x4,-0x14(%rbp)
jne    125c <recv_message+0x11c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
add    $0x8,%rdi
call   1030 <read_uint16@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1257 <recv_message+0x117>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x24(%rbp),%esi
add    $0x4,%esi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1212 <recv_message+0xd2>
lea    0xdff(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1262 <recv_message+0x122>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
add    $0x10,%rsi
mov    -0x24(%rbp),%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    124e <recv_message+0x10e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1262 <recv_message+0x122>
mov    -0x14(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x14(%rbp)
jmp    125c <recv_message+0x11c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_message>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
mov    (%rsi),%rdi
mov    $0x4,%esi
call   1060 <realloc@plt>
test   %rax,%rax
je     11be <recv_message+0x7e>
mov    %rax,(%rbx)
mov    %ebp,%edi
mov    %rax,%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
cmp    $0x4,%eax
jne    11cf <recv_message+0x8f>
mov    (%rbx),%rdi
add    $0x8,%rdi
call   1030 <read_uint16@plt>
mov    %eax,%r14d
mov    $0x4,%eax
test   %r14d,%r14d
je     11cf <recv_message+0x8f>
mov    (%rbx),%rdi
lea    0x4(%r14),%esi
call   1060 <realloc@plt>
test   %rax,%rax
je     11be <recv_message+0x7e>
mov    %rax,(%rbx)
add    $0x10,%rax
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
cmp    $0xffffffff,%eax
je     11ca <recv_message+0x8a>
add    $0x4,%eax
jmp    11cf <recv_message+0x8f>
lea    0xe3b(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [34 x i8] c"Failed to allocate message buffer\00"
define i32 @recv_message(i32 %0, i32** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32**
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
store i32 %0, i32* %4
store i32** %1, i32*** %5
store i32 -1, i32* %6
%9 = load i32**, i32*** %5
%10 = load i32*, i32** %9
%11 = call i32* @realloc(i32* %10, i32 4)
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%13 = icmp ne i32* %12, null
br i1 %13, label %16, label %14
14: ; preds = %2
%15 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %60
16: ; preds = %2
%17 = load i32*, i32** %7
%18 = load i32**, i32*** %5
store i32* %17, i32** %18
%19 = load i32, i32* %4
%20 = load i32**, i32*** %5
%21 = load i32*, i32** %20
%22 = call i32 @recv(i32 %19, i32* %21, i32 4, i32 0)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp eq i32 %23, 4
br i1 %24, label %25, label %58
25: ; preds = %16
%26 = load i32**, i32*** %5
%27 = load i32*, i32** %26
%28 = getelementptr inbounds i32, i32* %27, i64 2
%29 = call i32 @read_uint16(i32* %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %57
32: ; preds = %25
%33 = load i32**, i32*** %5
%34 = load i32*, i32** %33
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 4
%37 = call i32* @realloc(i32* %34, i32 %36)
store i32* %37, i32** %7
%38 = load i32*, i32** %7
%39 = icmp ne i32* %38, null
br i1 %39, label %42, label %40
40: ; preds = %32
%41 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %60
42: ; preds = %32
%43 = load i32*, i32** %7
%44 = load i32**, i32*** %5
store i32* %43, i32** %44
%45 = load i32, i32* %4
%46 = load i32**, i32*** %5
%47 = load i32*, i32** %46
%48 = getelementptr inbounds i32, i32* %47, i64 4
%49 = load i32, i32* %8
%50 = call i32 @recv(i32 %45, i32* %48, i32 %49, i32 0)
store i32 %50, i32* %6
%51 = load i32, i32* %6
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %54
53: ; preds = %42
store i32 -1, i32* %3
br label %60
54: ; preds = %42
%55 = load i32, i32* %6
%56 = add nsw i32 %55, 4
store i32 %56, i32* %6
br label %57
57: ; preds = %54, %25
br label %58
58: ; preds = %57, %16
%59 = load i32, i32* %6
store i32 %59, i32* %3
br label %60
60: ; preds = %58, %53, %40, %14
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
@.str = [34 x i8] c"Failed to allocate message buffer\00"
define i32 @recv_message(i32 %0, i32** nocapture %1) {
%3 = load i32*, i32** %1
%4 = tail call i32* @realloc(i32* %3, i32 4)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %30
8: ; preds = %2
store i32* %4, i32** %1
%9 = tail call i32 @recv(i32 %0, i32* %4, i32 4, i32 0)
%10 = icmp eq i32 %9, 4
br i1 %10, label %11, label %28
11: ; preds = %8
%12 = load i32*, i32** %1
%13 = getelementptr inbounds i32, i32* %12, i64 2
%14 = tail call i32 @read_uint16(i32* %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %28, label %16
16: ; preds = %11
%17 = load i32*, i32** %1
%18 = add nsw i32 %14, 4
%19 = tail call i32* @realloc(i32* %17, i32 %18)
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %30
23: ; preds = %16
store i32* %19, i32** %1
%24 = getelementptr inbounds i32, i32* %19, i64 4
%25 = tail call i32 @recv(i32 %0, i32* %24, i32 %14, i32 0)
%26 = icmp eq i32 %25, -1
%27 = add nsw i32 %25, 4
br i1 %26, label %30, label %28
28: ; preds = %11, %23, %8
%29 = phi i32 [ %27, %23 ], [ %9, %8 ], [ 4, %11 ]
br label %30
30: ; preds = %21, %23, %28, %6
%31 = phi i32 [ %29, %28 ], [ -1, %23 ], [ -1, %6 ], [ -1, %21 ]
ret i32 %31
}