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

/* Variables and functions */
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(size_t);
size_t ntohl(int /*<<< orphan*/);
int read(int, int /*<<< orphan*/ *, size_t);
int read_blob(int fd, uint8_t **buf, size_t *len) {
  uint32_t bloblen;
  if (read(fd, &bloblen, sizeof(bloblen)) != sizeof(bloblen))
    return (-1);
  *len = ntohl(bloblen);
  if ((*buf = malloc(*len)) == NULL)
    return (-1);
  if (read(fd, *buf, *len) != (ssize_t)*len) {
    free(*buf);
    *buf = NULL;
    *len = 0;
    return (-1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<read_blob>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
cltq
cmp    $0x4,%rax
je     117c <read_blob+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1211 <read_blob+0xd1>
mov    -0x1c(%rbp),%edi
call   1060 <ntohl@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1050 <malloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    11b7 <read_blob+0x77>
movl   $0xffffffff,-0x4(%rbp)
jmp    1211 <read_blob+0xd1>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
call   1040 <read@plt>
mov    -0x18(%rbp),%rcx
mov    (%rcx),%rcx
cmp    %ecx,%eax
je     120a <read_blob+0xca>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1211 <read_blob+0xd1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_blob>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %edi,%r12d
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
mov    $0xffffffff,%ebx
cmp    $0x4,%eax
jne    11bd <read_blob+0x7d>
mov    0xc(%rsp),%edi
call   1060 <ntohl@plt>
mov    %rax,%rbp
mov    %rax,(%r15)
mov    %rax,%rdi
call   1050 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     11bd <read_blob+0x7d>
mov    %r12d,%edi
mov    %rax,%rsi
mov    %rbp,%rdx
call   1040 <read@plt>
xor    %ebx,%ebx
cmp    (%r15),%eax
je     11bd <read_blob+0x7d>
mov    (%r14),%rdi
call   1030 <free@plt>
movq   $0x0,(%r14)
movq   $0x0,(%r15)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @read_blob(i32 %0, i32** nocapture %1, i64* nocapture %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = call i32 @read(i32 %0, i32* %4, i64 4)
%7 = icmp eq i32 %6, 4
br i1 %7, label %8, label %21
8: ; preds = %3
%9 = load i32, i32* %4
%10 = tail call i64 @ntohl(i32 %9)
store i64 %10, i64* %2
%11 = tail call i32* @malloc(i64 %10)
store i32* %11, i32** %1
%12 = icmp eq i32* %11, null
br i1 %12, label %21, label %13
13: ; preds = %8
%14 = tail call i32 @read(i32 %0, i32* %11, i64 %10)
%15 = load i64, i64* %2
%16 = trunc i64 %15 to i32
%17 = icmp eq i32 %14, %16
br i1 %17, label %21, label %18
18: ; preds = %13
%19 = load i32*, i32** %1
%20 = tail call i32 @free(i32* %19)
store i32* null, i32** %1
store i64 0, i64* %2
br label %21
21: ; preds = %13, %8, %3, %18
%22 = phi i32 [ -1, %18 ], [ -1, %3 ], [ -1, %8 ], [ 0, %13 ]
ret i32 %22
}