// File: /workspace/github_cpp_makefile_star2_O3/olegk0[P]readAVI/ReadAVI.cpp
// Function: ReadAVI::parse_riff()
int ReadAVI::parse_riff()
{
    char chunk_id[5];
    int chunk_size;
    char chunk_type[5];
    int end_of_chunk, end_of_subchunk;

    ZEROIZE(avi_header);
    ZEROIZE(stream_header_vid);
    ZEROIZE(stream_format_vid);
    ZEROIZE(stream_header_auds);
    ZEROIZE(stream_format_auds);
#ifdef VERBOSE
    long offset = inFile.tellg();
#endif
    read_chars(chunk_id, 4);
    chunk_size = read_int();
    read_chars(chunk_type, 4);

    MSG_DBG(2, "RIFF Chunk (id=%s size=%d type=%s offset=0x%lx)\n", chunk_id, chunk_size, chunk_type, offset);
    MSG_DBG(2, "{\n");

    if (strcasecmp("RIFF", chunk_id) != 0) {
	MSG_DBG(2, "Not a RIFF file.\n");
	return 1;
    } else if (strcasecmp("AVI ", chunk_type) != 0) {
	MSG_DBG(2, "Not an AVI file.\n");
	return 1;
    }

    end_of_chunk = chunk_size - 4 + inFile.tellg();

    while (inFile.tellg() < end_of_chunk) {
	long offset = inFile.tellg();
	read_chars(chunk_id, 4);
	chunk_size = read_int();
	end_of_subchunk = chunk_size + inFile.tellg();

	if (strcasecmp("JUNK", chunk_id) == 0 || strcasecmp("PAD ", chunk_id) == 0) {
	    chunk_type[0] = 0;
	} else {
	    read_chars(chunk_type, 4);
	}

	MSG_DBG(2, "   New Chunk (id=%s size=%d type=%s offset=0x%lx)\n", chunk_id, chunk_size, chunk_type, offset);
	MSG_DBG(2, "   {\n");

	fflush(stdout);

	if (strcasecmp("JUNK", chunk_id) == 0 || strcasecmp("PAD ", chunk_id) == 0) {
	    if ((chunk_size % 4) != 0) {
		chunk_size = chunk_size + (4 - (chunk_size % 4));
	    }
#ifdef VERBOSE
	    hex_dump_chunk(chunk_size);
#endif
	} else if (strcasecmp("INFO", chunk_type) == 0) {
	    if ((chunk_size % 4) != 0) {
		chunk_size = chunk_size + (4 - (chunk_size % 4));
	    }
#ifdef VERBOSE
	    hex_dump_chunk(chunk_size);
#endif
	} else if (strcasecmp("hdrl", chunk_type) == 0) {
	    parse_hdrl(chunk_size);
	    /* skip_chunk(); */
	} else if (strcasecmp("movi", chunk_type) == 0) {
	    movi_offset = offset;
	    parse_movi(chunk_size);
	} else if (strcasecmp("idx1", chunk_id) == 0) {
	    inFile.seekg(inFile.tellg() - 4L, ios_base::beg);
	    parse_idx1(chunk_size);
	}/* else if (strcasecmp("indx", chunk_id) == 0) {
	 inFile.seekg(inFile.tellg() - 4L, ios_base::beg);
	 parse_indx(chunk_size);
	 }*/else {
	    MSG_DBG(2, "      Unknown chunk at %d (%4s)\n", (int ) inFile.tellg() - 8, chunk_type);
	    if (chunk_size == 0)
		break;
	}

	inFile.seekg(end_of_subchunk, ios_base::beg);
	MSG_DBG(2, "   }\n");

    }

    if (stream_format_vid.palette) {
	delete[] stream_format_vid.palette;
	stream_format_vid.palette = NULL;
    }

    MSG_DBG(2, "}\n");

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
addq $0x298, %rdi            # imm = 0x298
leaq 0x20(%rbx), %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x284(%rbx)
movups %xmm0, 0x278(%rbx)
movups %xmm0, 0x268(%rbx)
movups %xmm0, 0x258(%rbx)
movups %xmm0, 0x248(%rbx)
movups %xmm0, 0x238(%rbx)
movups %xmm0, 0x228(%rbx)
xorl %ebp, %ebp
movl $0x8c, %edx
xorl %esi, %esi
callq 0x2150
movq %r14, %rdi
callq 0x23e0
movq %rax, %r13
leaq 0xf(%rsp), %r12
movl $0x4, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x22b0
movb %bpl, 0x4(%r12)
leaq 0x8(%rsp), %r15
movl $0x4, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x22b0
movl (%r15), %r15d
leaq 0x8(%rsp), %rbp
movl $0x4, %edx
movq %r14, %rdi
movq %rbp, %rsi
callq 0x22b0
xorl %eax, %eax
movb %al, 0x4(%rbp)
movq 0x5156(%rip), %rax      # 0x7fb0
movq (%rax), %rdi
leaq 0x2a5f(%rip), %rsi      # 0x58c3
movq %r12, %rdx
movl %r15d, %ecx
movq %rbp, %r8
movq %r13, %r9
movq %rax, %r13
xorl %eax, %eax
callq 0x2340
movq (%r13), %rcx
leaq 0x29a5(%rip), %rdi      # 0x582a
movl $0x2, %esi
movl $0x1, %edx
callq 0x2410
leaq 0x2a59(%rip), %rdi      # 0x58f4
movq %r12, %rsi
callq 0x2230
testl %eax, %eax
je 0x2eb4
movq (%r13), %rcx
leaq 0x2a47(%rip), %rdi      # 0x58f9
jmp 0x2ed4
leaq 0x2a50(%rip), %rdi      # 0x590b
leaq 0x8(%rsp), %rsi
callq 0x2230
testl %eax, %eax
je 0x2ef7
movq (%r13), %rcx
leaq 0x2a3c(%rip), %rdi      # 0x5910
movl $0x11, %esi
movl $0x1, %edx
callq 0x2410
movl $0x1, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x23e0
leal (%r15,%rax), %ebp
addl $-0x4, %ebp
movq %r14, %rdi
callq 0x23e0
movslq %ebp, %rcx
movq %rcx, 0x18(%rsp)
cmpq %rcx, %rax
jge 0x3191
leaq 0xf(%rsp), %r15
leaq 0x28cb(%rip), %r13      # 0x57f6
movq %r14, %rdi
callq 0x23e0
movq %rax, %r12
movl $0x4, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x22b0
movb $0x0, 0x13(%rsp)
movl $0x4, %edx
movq %r14, %rdi
leaq 0x14(%rsp), %rsi
callq 0x22b0
movl 0x14(%rsp), %ebp
movq %r14, %rdi
callq 0x23e0
movq %rax, 0x20(%rsp)
movq %r13, %rdi
movq %r15, %rsi
callq 0x2230
testl %eax, %eax
je 0x2fac
leaq 0x299e(%rip), %rdi      # 0x5922
movq %r15, %rsi
callq 0x2230
testl %eax, %eax
je 0x2fac
movl $0x4, %edx
movq %r14, %rdi
leaq 0x8(%rsp), %r15
movq %r15, %rsi
callq 0x22b0
movb $0x0, 0xc(%rsp)
jmp 0x2fb6
movb $0x0, 0x8(%rsp)
leaq 0x8(%rsp), %r15
movq 0x4ff3(%rip), %r13      # 0x7fb0
movq (%r13), %rdi
leaq 0x295f(%rip), %rsi      # 0x5927
leaq 0xf(%rsp), %rdx
movl %ebp, %ecx
movq %r15, %r8
movq %rdx, %r15
movq %r12, %r9
xorl %eax, %eax
callq 0x2340
movq (%r13), %rcx
movl $0x5, %esi
movl $0x1, %edx
leaq 0x2833(%rip), %rdi      # 0x5827
callq 0x2410
movq (%r13), %rdi
callq 0x2270
leaq 0x27ed(%rip), %r13      # 0x57f6
movq %r13, %rdi
movq %r15, %rsi
callq 0x2230
testl %eax, %eax
je 0x30cd
leaq 0x28ff(%rip), %rdi      # 0x5922
movq %r15, %rsi
callq 0x2230
testl %eax, %eax
je 0x30cd
leaq 0x2920(%rip), %rdi      # 0x595a
leaq 0x8(%rsp), %r15
movq %r15, %rsi
callq 0x2230
testl %eax, %eax
je 0x30ea
leaq 0x2909(%rip), %rdi      # 0x595f
movq %r15, %rsi
callq 0x2230
testl %eax, %eax
je 0x3107
leaq 0x28f7(%rip), %rdi      # 0x5964
movq %r15, %rsi
callq 0x2230
testl %eax, %eax
je 0x3113
leaq 0x28e5(%rip), %rdi      # 0x5969
leaq 0xf(%rsp), %r15
movq %r15, %rsi
callq 0x2230
testl %eax, %eax
je 0x316f
movq 0x4f10(%rip), %rax      # 0x7fb0
movq (%rax), %r12
movq %r14, %rdi
callq 0x23e0
leal -0x8(%rax), %edx
movq %r12, %rdi
leaq 0x28b6(%rip), %rsi      # 0x596e
leaq 0x8(%rsp), %rcx
xorl %eax, %eax
callq 0x2340
testl %ebp, %ebp
jne 0x3129
jmp 0x3191
leal 0x3(%rbp), %eax
testl %ebp, %ebp
cmovnsl %ebp, %eax
andl $-0x4, %eax
leal 0x4(%rax), %esi
cmpl %eax, %ebp
cmovel %ebp, %esi
movq %rbx, %rdi
callq 0x33ca
jmp 0x3129
leal 0x3(%rbp), %eax
testl %ebp, %ebp
cmovnsl %ebp, %eax
andl $-0x4, %eax
leal 0x4(%rax), %esi
cmpl %eax, %ebp
cmovel %ebp, %esi
movq %rbx, %rdi
callq 0x33ca
jmp 0x3124
movq %rbx, %rdi
movl %ebp, %esi
callq 0x4786
jmp 0x3124
movq %r12, 0x330(%rbx)
movq %rbx, %rdi
movl %ebp, %esi
callq 0x4614
leaq 0xf(%rsp), %r15
addl 0x20(%rsp), %ebp
movslq %ebp, %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x22f0
movq 0x4e6f(%rip), %rax      # 0x7fb0
movq (%rax), %rcx
movl $0x5, %esi
movl $0x1, %edx
leaq 0x2733(%rip), %rdi      # 0x5888
callq 0x2410
movq %r14, %rdi
callq 0x23e0
cmpq 0x18(%rsp), %rax
jl 0x2f2b
jmp 0x3191
movq %r14, %rdi
callq 0x23e0
leaq -0x4(%rax), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x22f0
movq %rbx, %rdi
movl %ebp, %esi
callq 0x3542
jmp 0x3129
movq 0x2c8(%rbx), %rdi
testq %rdi, %rdi
je 0x31ad
callq 0x22c0
movq $0x0, 0x2c8(%rbx)
movq 0x4dfc(%rip), %rax      # 0x7fb0
movq (%rax), %rcx
leaq 0x26cd(%rip), %rdi      # 0x588b
movl $0x2, %esi
movl $0x1, %edx
callq 0x2410
xorl %eax, %eax
jmp 0x2ee8