#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 scalar_t__ int64_t;

/* Variables and functions */
scalar_t__ _recv_from_socket(int, ...);
int /*<<< orphan*/ assert(int /*<<< orphan*/);
void *socket_check_realloc(void *, scalar_t__, char *);
void *_recv_msg(int s, void *data, int64_t *length, int64_t offset) {
  int64_t incoming;
  if (_recv_from_socket(s, &incoming, sizeof(int64_t)) < 0)
    return data;
  if (*length < (offset + incoming)) {
    data = socket_check_realloc(data, offset + incoming, "receive buffer");
    assert(data != NULL);
    *length = offset + incoming;
  }
  _recv_from_socket(s, ((char *)data) + offset, incoming);
  return data;
}


x86 assembly (clang, -O0):
<_recv_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x8,%edx
mov    $0x0,%al
call   1040 <_recv_from_socket@plt>
cmp    $0x0,%rax
jge    1171 <_recv_msg+0x41>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e4 <_recv_msg+0xb4>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    -0x28(%rbp),%rcx
add    -0x30(%rbp),%rcx
cmp    %rcx,%rax
jge    11c6 <_recv_msg+0x96>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
add    -0x30(%rbp),%rsi
lea    0xe64(%rip),%rdx
call   1030 <socket_check_realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x28(%rbp),%rcx
add    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    $0x0,%al
call   1040 <_recv_from_socket@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_recv_msg>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %edi,%ebp
mov    %rsp,%rsi
mov    $0x8,%edx
xor    %eax,%eax
call   1040 <_recv_from_socket@plt>
test   %rax,%rax
js     119d <_recv_msg+0x6d>
mov    (%rsp),%rdx
lea    (%rdx,%rbx,1),%rsi
cmp    %rsi,(%r15)
jge    118e <_recv_msg+0x5e>
lea    0xe96(%rip),%rdx
mov    %r14,%rdi
call   1030 <socket_check_realloc@plt>
mov    %rax,%r14
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1050 <assert@plt>
mov    (%rsp),%rdx
lea    (%rdx,%rbx,1),%rax
mov    %rax,(%r15)
add    %r14,%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <_recv_from_socket@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"receive buffer\00"
define i8* @_recv_msg(i32 %0, i8* %1, i64* %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64*
%9 = alloca i64
%10 = alloca i64
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64* %2, i64** %8
store i64 %3, i64* %9
%11 = load i32, i32* %6
%12 = call i64 (i32, ...) @_recv_from_socket(i32 %11, i64* %10, i64 8)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %4
%15 = load i8*, i8** %7
store i8* %15, i8** %5
br label %45
16: ; preds = %4
%17 = load i64*, i64** %8
%18 = load i64, i64* %17
%19 = load i64, i64* %9
%20 = load i64, i64* %10
%21 = add nsw i64 %19, %20
%22 = icmp slt i64 %18, %21
br i1 %22, label %23, label %37
23: ; preds = %16
%24 = load i8*, i8** %7
%25 = load i64, i64* %9
%26 = load i64, i64* %10
%27 = add nsw i64 %25, %26
%28 = call i8* @socket_check_realloc(i8* %24, i64 %27, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i8* %28, i8** %7
%29 = load i8*, i8** %7
%30 = icmp ne i8* %29, null
%31 = zext i1 %30 to i32
%32 = call i32 @assert(i32 %31)
%33 = load i64, i64* %9
%34 = load i64, i64* %10
%35 = add nsw i64 %33, %34
%36 = load i64*, i64** %8
store i64 %35, i64* %36
br label %37
37: ; preds = %23, %16
%38 = load i32, i32* %6
%39 = load i8*, i8** %7
%40 = load i64, i64* %9
%41 = getelementptr inbounds i8, i8* %39, i64 %40
%42 = load i64, i64* %10
%43 = call i64 (i32, ...) @_recv_from_socket(i32 %38, i8* %41, i64 %42)
%44 = load i8*, i8** %7
store i8* %44, i8** %5
br label %45
45: ; preds = %37, %14
%46 = load i8*, i8** %5
ret i8* %46
}

LLVM IR (-O2):
@.str = [15 x i8] c"receive buffer\00"
define i8* @_recv_msg(i32 %0, i8* %1, i64* nocapture %2, i64 %3) {
%5 = alloca i64
%6 = bitcast i64* %5 to i8*
%7 = call i64 (i32, ...) @_recv_from_socket(i32 %0, i64* %5, i64 8)
%8 = icmp slt i64 %7, 0
br i1 %8, label %26, label %9
9: ; preds = %4
%10 = load i64, i64* %2
%11 = load i64, i64* %5
%12 = add nsw i64 %11, %3
%13 = icmp slt i64 %10, %12
br i1 %13, label %14, label %21
14: ; preds = %9
%15 = call i8* @socket_check_realloc(i8* %1, i64 %12, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%16 = icmp ne i8* %15, null
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i64, i64* %5
%20 = add nsw i64 %19, %3
store i64 %20, i64* %2
br label %21
21: ; preds = %14, %9
%22 = phi i64 [ %19, %14 ], [ %11, %9 ]
%23 = phi i8* [ %15, %14 ], [ %1, %9 ]
%24 = getelementptr inbounds i8, i8* %23, i64 %3
%25 = call i64 (i32, ...) @_recv_from_socket(i32 %0, i8* %24, i64 %22)
br label %26
26: ; preds = %4, %21
%27 = phi i8* [ %23, %21 ], [ %1, %4 ]
ret i8* %27
}