#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 *malloc(int);
int /*<<< orphan*/ printf(char *, void *, ...);
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 (buf && ((sz + 256) > sz1)) {
    printf("RIFF_ReadInChunkTBuf: Free %p %d\n", buf, sz1);
    free(buf);
    buf = NULL;
  }
  if (!buf) {
    sz1 = sz + 32768;
    if (sz1 < 65536)
      sz1 = 65536;
    printf("RIFF_ReadInChunkTBuf: Malloc %d\n", sz1);
    buf = malloc(sz1);
    *rbuf = buf;
    *rbsz = sz1;
    printf("RIFF_ReadInChunkTBuf: Malloc OK %p %d\n", buf, sz1);
  }
  fseek(fd, ofs + 8, 0);
  fread(buf, 1, sz, 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   1080 <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)
cmpq   $0x0,-0x38(%rbp)
je     11e5 <RIFF_ReadInChunkTBuf+0x85>
mov    -0x2c(%rbp),%eax
add    $0x100,%eax
cmp    -0x30(%rbp),%eax
jle    11e5 <RIFF_ReadInChunkTBuf+0x85>
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%edx
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1256 <RIFF_ReadInChunkTBuf+0xf6>
mov    -0x2c(%rbp),%eax
add    $0x8000,%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x10000,-0x30(%rbp)
jge    120f <RIFF_ReadInChunkTBuf+0xaf>
movl   $0x10000,-0x30(%rbp)
movslq -0x30(%rbp),%rsi
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x30(%rbp),%edi
call   1060 <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    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%edx
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
add    $0x8,%esi
xor    %edx,%edx
call   1070 <fseek@plt>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%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    $0x18,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %esi,%r13d
mov    %rdi,%r12
lea    0x14(%rsp),%rdx
lea    0xc(%rsp),%rcx
lea    0x10(%rsp),%r8
call   1080 <RIFF_ReadChunkInfo@plt>
mov    (%r15),%rbx
test   %rbx,%rbx
je     11bf <RIFF_ReadInChunkTBuf+0x5f>
mov    (%r14),%edx
mov    $0x100,%eax
add    0xc(%rsp),%eax
cmp    %edx,%eax
jle    120a <RIFF_ReadInChunkTBuf+0xaa>
lea    0xe53(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0xc(%rsp),%eax
cmp    $0x8001,%eax
mov    $0x8000,%ebp
cmovge %eax,%ebp
add    $0x8000,%ebp
lea    0xe45(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,(%r15)
mov    %ebp,(%r14)
lea    0xe45(%rip),%rdi
mov    %rax,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x8,%r13d
mov    %r12,%rdi
mov    %r13d,%esi
xor    %edx,%edx
call   1070 <fseek@plt>
mov    0xc(%rsp),%edx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r12,%rcx
call   1040 <fread@plt>
mov    0xc(%rsp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [34 x i8] c"RIFF_ReadInChunkTBuf: Free %p %d\0A\00"
@.str.1 = [33 x i8] c"RIFF_ReadInChunkTBuf: Malloc %d\0A\00"
@.str.2 = [39 x i8] c"RIFF_ReadInChunkTBuf: Malloc OK %p %d\0A\00"
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 i8*, i8** %13
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %34
23: ; preds = %4
%24 = load i32, i32* %11
%25 = add nsw i32 %24, 256
%26 = load i32, i32* %12
%27 = icmp sgt i32 %25, %26
br i1 %27, label %28, label %34
28: ; preds = %23
%29 = load i8*, i8** %13
%30 = load i32, i32* %12
%31 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %29, i32 %30)
%32 = load i8*, i8** %13
%33 = call i32 @free(i8* %32)
store i8* null, i8** %13
br label %34
34: ; preds = %28, %23, %4
%35 = load i8*, i8** %13
%36 = icmp ne i8* %35, null
br i1 %36, label %57, label %37
37: ; preds = %34
%38 = load i32, i32* %11
%39 = add nsw i32 %38, 32768
store i32 %39, i32* %12
%40 = load i32, i32* %12
%41 = icmp slt i32 %40, 65536
br i1 %41, label %42, label %43
42: ; preds = %37
store i32 65536, i32* %12
br label %43
43: ; preds = %42, %37
%44 = load i32, i32* %12
%45 = sext i32 %44 to i64
%46 = inttoptr i64 %45 to i8*
%47 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %46)
%48 = load i32, i32* %12
%49 = call i8* @malloc(i32 %48)
store i8* %49, i8** %13
%50 = load i8*, i8** %13
%51 = load i8**, i8*** %7
store i8* %50, i8** %51
%52 = load i32, i32* %12
%53 = load i32*, i32** %8
store i32 %52, i32* %53
%54 = load i8*, i8** %13
%55 = load i32, i32* %12
%56 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %54, i32 %55)
br label %57
57: ; preds = %43, %34
%58 = load i32*, i32** %5
%59 = load i32, i32* %6
%60 = add nsw i32 %59, 8
%61 = call i32 @fseek(i32* %58, i32 %60, i32 0)
%62 = load i8*, i8** %13
%63 = load i32, i32* %11
%64 = load i32*, i32** %5
%65 = call i32 @fread(i8* %62, i32 1, i32 %63, i32* %64)
%66 = load i32, i32* %11
ret i32 %66
}

LLVM IR (-O2):
@.str = [34 x i8] c"RIFF_ReadInChunkTBuf: Free %p %d\0A\00"
@.str.1 = [33 x i8] c"RIFF_ReadInChunkTBuf: Malloc %d\0A\00"
@.str.2 = [39 x i8] c"RIFF_ReadInChunkTBuf: Malloc OK %p %d\0A\00"
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 = icmp eq i8* %12, null
br i1 %14, label %22, label %15
15: ; preds = %4
%16 = load i32, i32* %7
%17 = add nsw i32 %16, 256
%18 = icmp sgt i32 %17, %13
br i1 %18, label %19, label %32
19: ; preds = %15
%20 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %12, i32 %13)
%21 = call i32 @free(i8* %12)
br label %22
22: ; preds = %19, %4
%23 = load i32, i32* %7
%24 = icmp sgt i32 %23, 32768
%25 = select i1 %24, i32 %23, i32 32768
%26 = add nuw nsw i32 %25, 32768
%27 = zext i32 %26 to i64
%28 = inttoptr i64 %27 to i8*
%29 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = call i8* @malloc(i32 %26)
store i8* %30, i8** %2
store i32 %26, i32* %3
%31 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %30, i32 %26)
br label %32
32: ; preds = %15, %22
%33 = phi i8* [ %30, %22 ], [ %12, %15 ]
%34 = add nsw i32 %1, 8
%35 = call i32 @fseek(i32* %0, i32 %34, i32 0)
%36 = load i32, i32* %7
%37 = call i32 @fread(i8* %33, i32 1, i32 %36, i32* %0)
%38 = load i32, i32* %7
ret i32 %38
}