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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
unsigned int fread(unsigned char *, int, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, unsigned int,
                         int /*<<< orphan*/);
unsigned char *malloc(unsigned int);
unsigned int readLE32(unsigned char *);
int /*<<< orphan*/ stderr;
int lzpt_read_block(FILE *fh_in, int block_num, unsigned char *p_toc,
                    size_t toc_entries, unsigned char **pp_block_data,
                    size_t *psz_block) {
  unsigned int toc_offset, entry_offset, entry_size, nread;
  unsigned char *p_block;
  *pp_block_data = NULL;
  *psz_block = 0;
  if (block_num >= toc_entries) {
    return 1;
  }
  toc_offset = (block_num * (sizeof(unsigned int) * 2));
  entry_offset = readLE32(p_toc + toc_offset);
  entry_size = readLE32(p_toc + toc_offset + 4);
  if (!(p_block = malloc(entry_size))) {
    fprintf(stderr, "lzpt_read_block(): allocation error!\n");
    return 1;
  }
  fseek(fh_in, entry_offset, SEEK_SET);
  nread = fread(p_block, 1, entry_size, fh_in);
  if (nread != entry_size) {
    if ((block_num == (toc_entries - 1)) &&
        ((entry_size - nread) <= (sizeof(unsigned int) - 1))) {
    } else {
      fprintf(stderr, "lzpt_read_block(): block read error!\n");
      free((void *)p_block);
      return 1;
    }
  }
  *pp_block_data = p_block;
  *psz_block = nread;
  return 0;
}


x86 assembly (clang, -O0):
<lzpt_read_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    -0x30(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x38(%rbp),%rax
movq   $0x0,(%rax)
movslq -0x14(%rbp),%rax
cmp    -0x28(%rbp),%rax
jb     11af <lzpt_read_block+0x4f>
movl   $0x1,-0x4(%rbp)
jmp    12cb <lzpt_read_block+0x16b>
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x3c(%rbp),%eax
add    %rax,%rdi
call   1060 <readLE32@plt>
mov    %eax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x3c(%rbp),%eax
add    %rax,%rdi
add    $0x4,%rdi
call   1060 <readLE32@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x50(%rbp)
cmp    $0x0,%rax
jne    1219 <lzpt_read_block+0xb9>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
lea    0xdf8(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12cb <lzpt_read_block+0x16b>
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%esi
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
call   1080 <fseek@plt>
mov    -0x50(%rbp),%rdi
mov    -0x44(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x44(%rbp),%eax
je     12ad <lzpt_read_block+0x14d>
movslq -0x14(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jne    127e <lzpt_read_block+0x11e>
mov    -0x44(%rbp),%eax
sub    -0x48(%rbp),%eax
mov    %eax,%eax
cmp    $0x3,%rax
ja     127e <lzpt_read_block+0x11e>
jmp    12a8 <lzpt_read_block+0x148>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
lea    0xd98(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    12cb <lzpt_read_block+0x16b>
jmp    12ad <lzpt_read_block+0x14d>
mov    -0x50(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x48(%rbp),%eax
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lzpt_read_block>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movq   $0x0,(%r8)
movq   $0x0,(%r9)
movslq %esi,%r15
mov    $0x1,%r14d
cmp    %rcx,%r15
jae    1266 <lzpt_read_block+0x106>
mov    %rcx,%r13
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %rdi,0x20(%rsp)
mov    %r8,0x10(%rsp)
mov    %r9,0x18(%rsp)
shl    $0x3,%ebp
lea    (%rdx,%rbp,1),%rdi
call   1060 <readLE32@plt>
mov    %eax,0xc(%rsp)
lea    (%rbx,%rbp,1),%rdi
add    $0x4,%rdi
call   1060 <readLE32@plt>
mov    %eax,%r12d
mov    %eax,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     122c <lzpt_read_block+0xcc>
mov    %rax,%rbx
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
mov    0x20(%rsp),%rbp
mov    %rbp,%rdi
mov    0xc(%rsp),%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %rbp,%rcx
call   1040 <fread@plt>
sub    %eax,%r12d
je     1215 <lzpt_read_block+0xb5>
add    $0xffffffffffffffff,%r13
cmp    %r15,%r13
jne    1243 <lzpt_read_block+0xe3>
cmp    $0x4,%r12d
jae    1243 <lzpt_read_block+0xe3>
mov    0x10(%rsp),%rcx
mov    %rbx,(%rcx)
mov    %eax,%eax
mov    0x18(%rsp),%rcx
mov    %rax,(%rcx)
xor    %r14d,%r14d
jmp    1266 <lzpt_read_block+0x106>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
lea    0xdc4(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1266 <lzpt_read_block+0x106>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
lea    0xdd3(%rip),%rsi
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [38 x i8] c"lzpt_read_block(): allocation error!\0A\00"
@SEEK_SET = global i32 0
@.str.1 = [38 x i8] c"lzpt_read_block(): block read error!\0A\00"
define i32 @lzpt_read_block(i32* %0, i32 %1, i8* %2, i64 %3, i8** %4, i64* %5) {
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8**
%13 = alloca i64*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i8*
store i32* %0, i32** %8
store i32 %1, i32* %9
store i8* %2, i8** %10
store i64 %3, i64* %11
store i8** %4, i8*** %12
store i64* %5, i64** %13
%19 = load i8**, i8*** %12
store i8* null, i8** %19
%20 = load i64*, i64** %13
store i64 0, i64* %20
%21 = load i32, i32* %9
%22 = sext i32 %21 to i64
%23 = load i64, i64* %11
%24 = icmp uge i64 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %6
store i32 1, i32* %7
br label %85
26: ; preds = %6
%27 = load i32, i32* %9
%28 = sext i32 %27 to i64
%29 = mul i64 %28, 8
%30 = trunc i64 %29 to i32
store i32 %30, i32* %14
%31 = load i8*, i8** %10
%32 = load i32, i32* %14
%33 = zext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = call i32 @readLE32(i8* %34)
store i32 %35, i32* %15
%36 = load i8*, i8** %10
%37 = load i32, i32* %14
%38 = zext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = getelementptr inbounds i8, i8* %39, i64 4
%41 = call i32 @readLE32(i8* %40)
store i32 %41, i32* %16
%42 = load i32, i32* %16
%43 = call i8* @malloc(i32 %42)
store i8* %43, i8** %18
%44 = icmp ne i8* %43, null
br i1 %44, label %48, label %45
45: ; preds = %26
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %7
br label %85
48: ; preds = %26
%49 = load i32*, i32** %8
%50 = load i32, i32* %15
%51 = load i32, i32* @SEEK_SET
%52 = call i32 @fseek(i32* %49, i32 %50, i32 %51)
%53 = load i8*, i8** %18
%54 = load i32, i32* %16
%55 = load i32*, i32** %8
%56 = call i32 @fread(i8* %53, i32 1, i32 %54, i32* %55)
store i32 %56, i32* %17
%57 = load i32, i32* %17
%58 = load i32, i32* %16
%59 = icmp ne i32 %57, %58
br i1 %59, label %60, label %79
60: ; preds = %48
%61 = load i32, i32* %9
%62 = sext i32 %61 to i64
%63 = load i64, i64* %11
%64 = sub i64 %63, 1
%65 = icmp eq i64 %62, %64
br i1 %65, label %66, label %73
66: ; preds = %60
%67 = load i32, i32* %16
%68 = load i32, i32* %17
%69 = sub i32 %67, %68
%70 = zext i32 %69 to i64
%71 = icmp ule i64 %70, 3
br i1 %71, label %72, label %73
72: ; preds = %66
br label %78
73: ; preds = %66, %60
%74 = load i32, i32* @stderr
%75 = call i32 @fprintf(i32 %74, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%76 = load i8*, i8** %18
%77 = call i32 @free(i8* %76)
store i32 1, i32* %7
br label %85
78: ; preds = %72
br label %79
79: ; preds = %78, %48
%80 = load i8*, i8** %18
%81 = load i8**, i8*** %12
store i8* %80, i8** %81
%82 = load i32, i32* %17
%83 = zext i32 %82 to i64
%84 = load i64*, i64** %13
store i64 %83, i64* %84
store i32 0, i32* %7
br label %85
85: ; preds = %79, %73, %45, %25
%86 = load i32, i32* %7
ret i32 %86
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [38 x i8] c"lzpt_read_block(): allocation error!\0A\00"
@SEEK_SET = global i32 0
@.str.1 = [38 x i8] c"lzpt_read_block(): block read error!\0A\00"
define i32 @lzpt_read_block(i32* nocapture %0, i32 %1, i8* %2, i64 %3, i8** nocapture writeonly %4, i64* nocapture writeonly %5) {
store i8* null, i8** %4
store i64 0, i64* %5
%7 = sext i32 %1 to i64
%8 = icmp ult i64 %7, %3
br i1 %8, label %9, label %38
9: ; preds = %6
%10 = shl i32 %1, 3
%11 = zext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %2, i64 %11
%13 = tail call i32 @readLE32(i8* %12)
%14 = getelementptr inbounds i8, i8* %12, i64 4
%15 = tail call i32 @readLE32(i8* %14)
%16 = tail call i8* @malloc(i32 %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %21
18: ; preds = %9
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
br label %38
21: ; preds = %9
%22 = load i32, i32* @SEEK_SET
%23 = tail call i32 @fseek(i32* %0, i32 %13, i32 %22)
%24 = tail call i32 @fread(i8* %16, i32 1, i32 %15, i32* %0)
%25 = icmp eq i32 %15, %24
br i1 %25, label %36, label %26
26: ; preds = %21
%27 = add i64 %3, -1
%28 = icmp eq i64 %27, %7
%29 = sub i32 %15, %24
%30 = icmp ult i32 %29, 4
%31 = and i1 %28, %30
br i1 %31, label %36, label %32
32: ; preds = %26
%33 = load i32, i32* @stderr
%34 = tail call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%35 = tail call i32 @free(i8* %16)
br label %38
36: ; preds = %26, %21
store i8* %16, i8** %4
%37 = zext i32 %24 to i64
store i64 %37, i64* %5
br label %38
38: ; preds = %6, %36, %32, %18
%39 = phi i32 [ 0, %36 ], [ 1, %32 ], [ 1, %18 ], [ 1, %6 ]
ret i32 %39
}