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

/* Variables and functions */
int fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int peek4(int /*<<< orphan*/ *);
uint8_t *pzx_load_block(FILE *fp) {
  uint8_t block_header[8];
  uint8_t *block_data;
  uint32_t block_len;
  if (fread(block_header, 1, 8, fp) < 8)
    return NULL;
  block_len = peek4(block_header + 4);
  block_data = malloc(8 + block_len);
  if (!block_data)
    return NULL;
  memcpy(block_data, block_header, 8);
  if (!block_len) {
    return block_data;
  }
  if (fread(block_data + 8, 1, block_len, fp) < block_len) {
    free(block_data);
    return NULL;
  }
  return block_data;
}


x86 assembly (clang, -O0):
<pzx_load_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x8,%edx
call   1040 <fread@plt>
cmp    $0x8,%eax
jge    1189 <pzx_load_block+0x39>
movq   $0x0,-0x8(%rbp)
jmp    1229 <pzx_load_block+0xd9>
lea    -0x30(%rbp),%rdi
add    $0x10,%rdi
call   1060 <peek4@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
add    $0x8,%edi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11c0 <pzx_load_block+0x70>
movq   $0x0,-0x8(%rbp)
jmp    1229 <pzx_load_block+0xd9>
mov    -0x38(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x8,%edx
call   1050 <memcpy@plt>
cmpl   $0x0,-0x3c(%rbp)
jne    11e9 <pzx_load_block+0x99>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1229 <pzx_load_block+0xd9>
mov    -0x38(%rbp),%rdi
add    $0x20,%rdi
mov    -0x3c(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x3c(%rbp),%eax
jge    1221 <pzx_load_block+0xd1>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1229 <pzx_load_block+0xd9>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pzx_load_block>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%r14
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x8,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
cmp    $0x8,%eax
jl     11cd <pzx_load_block+0x7d>
lea    0x10(%rsp),%rdi
call   1060 <peek4@plt>
mov    %eax,%r15d
lea    0x8(%r15),%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11cd <pzx_load_block+0x7d>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    %rax,%rdi
mov    $0x8,%edx
call   1050 <memcpy@plt>
test   %r15d,%r15d
je     11cf <pzx_load_block+0x7f>
mov    %rbx,%rdi
add    $0x20,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
cmp    %r15d,%eax
jge    11cf <pzx_load_block+0x7f>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32* @pzx_load_block(i32* %0) {
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca [8 x i32]
%5 = alloca i32*
%6 = alloca i32
store i32* %0, i32** %3
%7 = getelementptr inbounds [8 x i32], [8 x i32]* %4, i64 0, i64 0
%8 = load i32*, i32** %3
%9 = call i32 @fread(i32* %7, i32 1, i32 8, i32* %8)
%10 = icmp slt i32 %9, 8
br i1 %10, label %11, label %12
11: ; preds = %1
store i32* null, i32** %2
br label %43
12: ; preds = %1
%13 = getelementptr inbounds [8 x i32], [8 x i32]* %4, i64 0, i64 0
%14 = getelementptr inbounds i32, i32* %13, i64 4
%15 = call i32 @peek4(i32* %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = add nsw i32 8, %16
%18 = call i32* @malloc(i32 %17)
store i32* %18, i32** %5
%19 = load i32*, i32** %5
%20 = icmp ne i32* %19, null
br i1 %20, label %22, label %21
21: ; preds = %12
store i32* null, i32** %2
br label %43
22: ; preds = %12
%23 = load i32*, i32** %5
%24 = getelementptr inbounds [8 x i32], [8 x i32]* %4, i64 0, i64 0
%25 = call i32 @memcpy(i32* %23, i32* %24, i32 8)
%26 = load i32, i32* %6
%27 = icmp ne i32 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %22
%29 = load i32*, i32** %5
store i32* %29, i32** %2
br label %43
30: ; preds = %22
%31 = load i32*, i32** %5
%32 = getelementptr inbounds i32, i32* %31, i64 8
%33 = load i32, i32* %6
%34 = load i32*, i32** %3
%35 = call i32 @fread(i32* %32, i32 1, i32 %33, i32* %34)
%36 = load i32, i32* %6
%37 = icmp slt i32 %35, %36
br i1 %37, label %38, label %41
38: ; preds = %30
%39 = load i32*, i32** %5
%40 = call i32 @free(i32* %39)
store i32* null, i32** %2
br label %43
41: ; preds = %30
%42 = load i32*, i32** %5
store i32* %42, i32** %2
br label %43
43: ; preds = %41, %38, %28, %21, %11
%44 = load i32*, i32** %2
ret i32* %44
}

LLVM IR (-O2):
define i32* @pzx_load_block(i32* nocapture %0) {
%2 = alloca [8 x i32]
%3 = bitcast [8 x i32]* %2 to i8*
%4 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 0
%5 = call i32 @fread(i32* %4, i32 1, i32 8, i32* %0)
%6 = icmp slt i32 %5, 8
br i1 %6, label %22, label %7
7: ; preds = %1
%8 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 4
%9 = call i32 @peek4(i32* %8)
%10 = add nsw i32 %9, 8
%11 = call i32* @malloc(i32 %10)
%12 = icmp eq i32* %11, null
br i1 %12, label %22, label %13
13: ; preds = %7
%14 = call i32 @memcpy(i32* %11, i32* %4, i32 8)
%15 = icmp eq i32 %9, 0
br i1 %15, label %22, label %16
16: ; preds = %13
%17 = getelementptr inbounds i32, i32* %11, i64 8
%18 = call i32 @fread(i32* %17, i32 1, i32 %9, i32* %0)
%19 = icmp slt i32 %18, %9
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @free(i32* %11)
br label %22
22: ; preds = %16, %13, %7, %1, %20
%23 = phi i32* [ null, %20 ], [ null, %1 ], [ null, %7 ], [ %11, %13 ], [ %11, %16 ]
ret i32* %23
}