#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 */
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
char *malloc(int);
int read(int, char **, int);
char *read_packet(int fd, int len) {
  char *packet;
  errno = 0;
  packet = malloc(len);
  if (packet == NULL)
    return NULL;
  if (read(fd, &packet, len) == -1) {
    free(packet);
    return NULL;
  }
  return packet;
}


x86 assembly (clang, -O0):
<read_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    0x2e9b(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1170 <read_packet+0x40>
movq   $0x0,-0x8(%rbp)
jmp    11a6 <read_packet+0x76>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%edx
lea    -0x18(%rbp),%rsi
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    119e <read_packet+0x6e>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11a6 <read_packet+0x76>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_packet>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
mov    0x2ea2(%rip),%rax
movq   $0x0,(%rax)
mov    %esi,%edi
call   1050 <malloc@plt>
mov    %rax,(%rsp)
test   %rax,%rax
je     1174 <read_packet+0x44>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    %ebx,%edx
call   1040 <read@plt>
mov    %eax,%ecx
mov    (%rsp),%rax
cmp    $0xffffffff,%ecx
jne    1176 <read_packet+0x46>
mov    %rax,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
define i8* @read_packet(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
store i64 0, i64* @errno
%7 = load i32, i32* %5
%8 = call i8* @malloc(i32 %7)
store i8* %8, i8** %6
%9 = load i8*, i8** %6
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %12
11: ; preds = %2
store i8* null, i8** %3
br label %22
12: ; preds = %2
%13 = load i32, i32* %4
%14 = load i32, i32* %5
%15 = call i32 @read(i32 %13, i8** %6, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i8*, i8** %6
%19 = call i32 @free(i8* %18)
store i8* null, i8** %3
br label %22
20: ; preds = %12
%21 = load i8*, i8** %6
store i8* %21, i8** %3
br label %22
22: ; preds = %20, %17, %11
%23 = load i8*, i8** %3
ret i8* %23
}

LLVM IR (-O2):
@errno = global i64 0
define i8* @read_packet(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
store i64 0, i64* @errno
%5 = tail call i8* @malloc(i32 %1)
store i8* %5, i8** %3
%6 = icmp eq i8* %5, null
br i1 %6, label %13, label %7
7: ; preds = %2
%8 = call i32 @read(i32 %0, i8** %3, i32 %1)
%9 = icmp eq i32 %8, -1
%10 = load i8*, i8** %3
br i1 %9, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @free(i8* %10)
br label %13
13: ; preds = %7, %2, %11
%14 = phi i8* [ null, %11 ], [ null, %2 ], [ %10, %7 ]
ret i8* %14
}