#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*/ RIFF_ReadChunkInfo(int /*<<< orphan*/ *, int, int *, int *,
                                      int *);
int /*<<< orphan*/ fread(void *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
void *pdgl_avi_malloc(int);
int RIFF_ReadInChunkTBuf(FILE *fd, int ofs, void **rbuf, int *rbsz) {
  int cc1, cc2, sz, sz1;
  void *buf;
  RIFF_ReadChunkInfo(fd, ofs, &cc1, &sz, &cc2);
  buf = *rbuf;
  sz1 = *rbsz;
  if ((sz + 2048) > sz1) {
    free(buf);
    buf = NULL;
  }
  if (!buf) {
    sz1 = sz + 2048;
    buf = pdgl_avi_malloc(sz1);
    *rbuf = buf;
    *rbsz = sz1;
  }
  fseek(fd, ofs + 8, 0);
  fread(buf, 1, sz + 1024, fd);
  return (sz);
}


x86 assembly (clang, -O0):
<RIFF_ReadInChunkTBuf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
lea    -0x24(%rbp),%rdx
lea    -0x2c(%rbp),%rcx
lea    -0x28(%rbp),%r8
call   1060 <RIFF_ReadChunkInfo@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x800,%eax
cmp    -0x30(%rbp),%eax
jle    11b5 <RIFF_ReadInChunkTBuf+0x65>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11eb <RIFF_ReadInChunkTBuf+0x9b>
mov    -0x2c(%rbp),%eax
add    $0x800,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
call   1070 <pdgl_avi_malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
add    $0x8,%esi
xor    %edx,%edx
call   1050 <fseek@plt>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%edx
add    $0x400,%edx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x2c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<RIFF_ReadInChunkTBuf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %esi,%r13d
mov    %rdi,%r12
lea    0x24(%rsp),%rdx
lea    0xc(%rsp),%rcx
lea    0x20(%rsp),%r8
call   1060 <RIFF_ReadChunkInfo@plt>
mov    %rbx,0x10(%rsp)
mov    (%rbx),%rbx
mov    $0x800,%r14d
mov    0xc(%rsp),%ebp
add    %r14d,%ebp
mov    %r15,0x18(%rsp)
cmp    (%r15),%ebp
jle    11af <RIFF_ReadInChunkTBuf+0x5f>
mov    %rbx,%rdi
call   1030 <free@plt>
add    0xc(%rsp),%r14d
mov    %r14d,%ebp
jmp    11b4 <RIFF_ReadInChunkTBuf+0x64>
test   %rbx,%rbx
jne    11cd <RIFF_ReadInChunkTBuf+0x7d>
mov    %ebp,%edi
call   1070 <pdgl_avi_malloc@plt>
mov    %rax,%rbx
mov    0x10(%rsp),%rax
mov    %rbx,(%rax)
mov    0x18(%rsp),%rax
mov    %ebp,(%rax)
add    $0x8,%r13d
mov    %r12,%rdi
mov    %r13d,%esi
xor    %edx,%edx
call   1050 <fseek@plt>
mov    $0x400,%edx
add    0xc(%rsp),%edx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r12,%rcx
call   1040 <fread@plt>
mov    0xc(%rsp),%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @RIFF_ReadInChunkTBuf(i32* %0, i32 %1, i8** %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
store i32* %0, i32** %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
store i32* %3, i32** %8
%14 = load i32*, i32** %5
%15 = load i32, i32* %6
%16 = call i32 @RIFF_ReadChunkInfo(i32* %14, i32 %15, i32* %9, i32* %11, i32* %10)
%17 = load i8**, i8*** %7
%18 = load i8*, i8** %17
store i8* %18, i8** %13
%19 = load i32*, i32** %8
%20 = load i32, i32* %19
store i32 %20, i32* %12
%21 = load i32, i32* %11
%22 = add nsw i32 %21, 2048
%23 = load i32, i32* %12
%24 = icmp sgt i32 %22, %23
br i1 %24, label %25, label %28
25: ; preds = %4
%26 = load i8*, i8** %13
%27 = call i32 @free(i8* %26)
store i8* null, i8** %13
br label %28
28: ; preds = %25, %4
%29 = load i8*, i8** %13
%30 = icmp ne i8* %29, null
br i1 %30, label %40, label %31
31: ; preds = %28
%32 = load i32, i32* %11
%33 = add nsw i32 %32, 2048
store i32 %33, i32* %12
%34 = load i32, i32* %12
%35 = call i8* @pdgl_avi_malloc(i32 %34)
store i8* %35, i8** %13
%36 = load i8*, i8** %13
%37 = load i8**, i8*** %7
store i8* %36, i8** %37
%38 = load i32, i32* %12
%39 = load i32*, i32** %8
store i32 %38, i32* %39
br label %40
40: ; preds = %31, %28
%41 = load i32*, i32** %5
%42 = load i32, i32* %6
%43 = add nsw i32 %42, 8
%44 = call i32 @fseek(i32* %41, i32 %43, i32 0)
%45 = load i8*, i8** %13
%46 = load i32, i32* %11
%47 = add nsw i32 %46, 1024
%48 = load i32*, i32** %5
%49 = call i32 @fread(i8* %45, i32 1, i32 %47, i32* %48)
%50 = load i32, i32* %11
ret i32 %50
}

LLVM IR (-O2):
define i32 @RIFF_ReadInChunkTBuf(i32* %0, i32 %1, i8** nocapture %2, i32* nocapture %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = bitcast i32* %5 to i8*
%9 = bitcast i32* %6 to i8*
%10 = bitcast i32* %7 to i8*
%11 = call i32 @RIFF_ReadChunkInfo(i32* %0, i32 %1, i32* %5, i32* %7, i32* %6)
%12 = load i8*, i8** %2
%13 = load i32, i32* %3
%14 = load i32, i32* %7
%15 = add nsw i32 %14, 2048
%16 = icmp sgt i32 %15, %13
br i1 %16, label %17, label %21
17: ; preds = %4
%18 = call i32 @free(i8* %12)
%19 = load i32, i32* %7
%20 = add nsw i32 %19, 2048
br label %23
21: ; preds = %4
%22 = icmp eq i8* %12, null
br i1 %22, label %23, label %26
23: ; preds = %17, %21
%24 = phi i32 [ %20, %17 ], [ %15, %21 ]
%25 = call i8* @pdgl_avi_malloc(i32 %24)
store i8* %25, i8** %2
store i32 %24, i32* %3
br label %26
26: ; preds = %23, %21
%27 = phi i8* [ %12, %21 ], [ %25, %23 ]
%28 = add nsw i32 %1, 8
%29 = call i32 @fseek(i32* %0, i32 %28, i32 0)
%30 = load i32, i32* %7
%31 = add nsw i32 %30, 1024
%32 = call i32 @fread(i8* %27, i32 1, i32 %31, i32* %0)
%33 = load i32, i32* %7
ret i32 %33
}