#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 */

/* Variables and functions */
size_t read_all(int, ...);
int /*<<< orphan*/ xfree(char *);
char *zalloc(size_t);
int recv_packet(int fd, char **buf, size_t *len) {
  if (read_all(fd, len, sizeof(*len)) != sizeof(*len))
    return -1;
  (*buf) = (zalloc(*len));
  if (!*buf)
    return -1;
  if (read_all(fd, *buf, *len) != *len) {
    (xfree(*buf));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<recv_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x8,%edx
mov    $0x0,%al
call   1050 <read_all@plt>
cmp    $0x8,%rax
je     116c <recv_packet+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e0 <recv_packet+0xb0>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1040 <zalloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    119c <recv_packet+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e0 <recv_packet+0xb0>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
mov    $0x0,%al
call   1050 <read_all@plt>
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
je     11d9 <recv_packet+0xa9>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <xfree@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e0 <recv_packet+0xb0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_packet>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
mov    $0x8,%edx
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <read_all@plt>
mov    $0xffffffff,%ebp
cmp    $0x8,%rax
jne    118e <recv_packet+0x5e>
mov    (%rbx),%rdi
call   1040 <zalloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     118e <recv_packet+0x5e>
mov    (%rbx),%rdx
xor    %ebp,%ebp
mov    %r15d,%edi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <read_all@plt>
cmp    (%rbx),%rax
je     118e <recv_packet+0x5e>
mov    (%r14),%rdi
call   1030 <xfree@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @recv_packet(i32 %0, i8** %1, i64* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i64*
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i64* %2, i64** %7
%8 = load i32, i32* %5
%9 = load i64*, i64** %7
%10 = call i64 (i32, ...) @read_all(i32 %8, i64* %9, i64 8)
%11 = icmp ne i64 %10, 8
br i1 %11, label %12, label %13
12: ; preds = %3
store i32 -1, i32* %4
br label %37
13: ; preds = %3
%14 = load i64*, i64** %7
%15 = load i64, i64* %14
%16 = call i8* @zalloc(i64 %15)
%17 = load i8**, i8*** %6
store i8* %16, i8** %17
%18 = load i8**, i8*** %6
%19 = load i8*, i8** %18
%20 = icmp ne i8* %19, null
br i1 %20, label %22, label %21
21: ; preds = %13
store i32 -1, i32* %4
br label %37
22: ; preds = %13
%23 = load i32, i32* %5
%24 = load i8**, i8*** %6
%25 = load i8*, i8** %24
%26 = load i64*, i64** %7
%27 = load i64, i64* %26
%28 = call i64 (i32, ...) @read_all(i32 %23, i8* %25, i64 %27)
%29 = load i64*, i64** %7
%30 = load i64, i64* %29
%31 = icmp ne i64 %28, %30
br i1 %31, label %32, label %36
32: ; preds = %22
%33 = load i8**, i8*** %6
%34 = load i8*, i8** %33
%35 = call i32 @xfree(i8* %34)
store i32 -1, i32* %4
br label %37
36: ; preds = %22
store i32 0, i32* %4
br label %37
37: ; preds = %36, %32, %21, %12
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
define i32 @recv_packet(i32 %0, i8** nocapture %1, i64* %2) {
%4 = tail call i64 (i32, ...) @read_all(i32 %0, i64* %2, i64 8)
%5 = icmp eq i64 %4, 8
br i1 %5, label %6, label %18
6: ; preds = %3
%7 = load i64, i64* %2
%8 = tail call i8* @zalloc(i64 %7)
store i8* %8, i8** %1
%9 = icmp eq i8* %8, null
br i1 %9, label %18, label %10
10: ; preds = %6
%11 = load i64, i64* %2
%12 = tail call i64 (i32, ...) @read_all(i32 %0, i8* %8, i64 %11)
%13 = load i64, i64* %2
%14 = icmp eq i64 %12, %13
br i1 %14, label %18, label %15
15: ; preds = %10
%16 = load i8*, i8** %1
%17 = tail call i32 @xfree(i8* %16)
br label %18
18: ; preds = %10, %6, %3, %15
%19 = phi i32 [ -1, %15 ], [ -1, %3 ], [ -1, %6 ], [ 0, %10 ]
ret i32 %19
}