#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 */
int /*<<< orphan*/ free(void *);
void *malloc(unsigned char);
int /*<<< orphan*/ print_recved_message(void *, unsigned char);
unsigned char saferead(int, void *, unsigned char);
void *read_sf_packet(int fd, int *len) {
  unsigned char l;
  void *packet;
  if (saferead(fd, &l, 1) != 1)
    return NULL;
  packet = malloc(l);
  if (!packet)
    return NULL;
  if (saferead(fd, packet, l) != l) {
    free(packet);
    return NULL;
  }
  *len = l;
  print_recved_message(packet, l);
  return packet;
}


x86 assembly (clang, -O0):
<read_sf_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1050 <saferead@plt>
movzbl %al,%eax
cmp    $0x1,%eax
je     1179 <read_sf_packet+0x39>
movq   $0x0,-0x8(%rbp)
jmp    11f2 <read_sf_packet+0xb2>
movzbl -0x19(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119e <read_sf_packet+0x5e>
movq   $0x0,-0x8(%rbp)
jmp    11f2 <read_sf_packet+0xb2>
mov    -0xc(%rbp),%edi
mov    -0x28(%rbp),%rsi
movzbl -0x19(%rbp),%edx
call   1050 <saferead@plt>
movzbl %al,%eax
movzbl -0x19(%rbp),%ecx
cmp    %ecx,%eax
je     11d3 <read_sf_packet+0x93>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f2 <read_sf_packet+0xb2>
movzbl -0x19(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
movzbl -0x19(%rbp),%esi
call   1060 <print_recved_message@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_sf_packet>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1050 <saferead@plt>
cmp    $0x1,%al
jne    11a0 <read_sf_packet+0x60>
movzbl 0x7(%rsp),%r15d
mov    %r15d,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     11a0 <read_sf_packet+0x60>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r15d,%edx
call   1050 <saferead@plt>
cmp    0x7(%rsp),%al
jne    1198 <read_sf_packet+0x58>
movzbl %al,%esi
mov    %esi,(%r14)
mov    %rbx,%rdi
call   1060 <print_recved_message@plt>
jmp    11a2 <read_sf_packet+0x62>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @read_sf_packet(i32 %0, i32* nocapture writeonly %1) {
%3 = alloca i8
%4 = call zeroext i8 @saferead(i32 %0, i8* %3, i8 zeroext 1)
%5 = icmp eq i8 %4, 1
br i1 %5, label %6, label %19
6: ; preds = %2
%7 = load i8, i8* %3
%8 = call i8* @malloc(i8 zeroext %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %19, label %10
10: ; preds = %6
%11 = call zeroext i8 @saferead(i32 %0, i8* %8, i8 zeroext %7)
%12 = load i8, i8* %3
%13 = icmp eq i8 %11, %12
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = call i32 @free(i8* %8)
br label %19
16: ; preds = %10
%17 = zext i8 %11 to i32
store i32 %17, i32* %1
%18 = call i32 @print_recved_message(i8* %8, i8 zeroext %11)
br label %19
19: ; preds = %6, %2, %16, %14
%20 = phi i8* [ null, %14 ], [ %8, %16 ], [ null, %2 ], [ null, %6 ]
ret i8* %20
}