// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %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)
movl $0x8c, %edx
xorl %esi, %esi
callq 0x2170
movq %r14, %rdi
callq 0x2410
movq %rax, %r13
leaq 0x13(%rsp), %r12
pushq $0x4
popq %r15
movq %rbx, %rdi
movq %r12, %rsi
movl %r15d, %edx
callq 0x3542
movq %rbx, %rdi
callq 0x3566
leaq 0xe(%rsp), %rbp
movq %rbx, %rdi
movq %rbp, %rsi
movl %r15d, %edx
movq %rax, %r15
callq 0x3542
movq 0x523f(%rip), %rax      # 0x7fb0
movq (%rax), %rdi
leaq 0x2b48(%rip), %rsi      # 0x58c3
movq %r12, %rdx
movl %r15d, %ecx
movq %rbp, %r8
movq %r13, %r9
movq %rax, %rbp
xorl %eax, %eax
callq 0x2380
movq (%rbp), %rcx
leaq 0x2a8e(%rip), %rdi      # 0x582a
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
callq 0x2440
leaq 0x2b46(%rip), %rdi      # 0x58f4
movq %r12, %rsi
callq 0x2280
testl %eax, %eax
je 0x2dc7
movq (%rbp), %rcx
leaq 0x2b34(%rip), %rdi      # 0x58f9
jmp 0x2de7
leaq 0x2b3d(%rip), %rdi      # 0x590b
leaq 0xe(%rsp), %rsi
callq 0x2280
testl %eax, %eax
je 0x2e04
movq (%rbp), %rcx
leaq 0x2b29(%rip), %rdi      # 0x5910
pushq $0x11
popq %rsi
pushq $0x1
popq %rdx
callq 0x2440
pushq $0x1
popq %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x2410
addl %r15d, %eax
addl $-0x4, %eax
cltq
movq %rax, 0x28(%rsp)
leaq 0xe(%rsp), %r13
movq %r14, 0x18(%rsp)
movq %r14, %rdi
callq 0x2410
cmpq 0x28(%rsp), %rax
jge 0x309c
movq %r14, %rdi
callq 0x2410
movq %rax, 0x20(%rsp)
movq %rbx, %rdi
leaq 0x13(%rsp), %rbp
movq %rbp, %rsi
pushq $0x4
popq %rdx
callq 0x3542
movq %rbx, %rdi
callq 0x3566
movl %eax, %r12d
movq %r14, %rdi
callq 0x2410
movq %rax, 0x30(%rsp)
leaq 0x2981(%rip), %rdi      # 0x57f6
movq %rbp, %rsi
callq 0x2280
testl %eax, %eax
je 0x2ea7
leaq 0x2a9a(%rip), %rdi      # 0x5922
movq %rbp, %rsi
callq 0x2280
testl %eax, %eax
je 0x2ea7
movq %rbx, %rdi
movq %r13, %r15
movq %r13, %rsi
pushq $0x4
popq %rdx
callq 0x3542
jmp 0x2eaf
movb $0x0, 0xe(%rsp)
movq %r13, %r15
movq 0x50fa(%rip), %rbp      # 0x7fb0
movq (%rbp), %rdi
leaq 0x2a66(%rip), %rsi      # 0x5927
leaq 0x13(%rsp), %r14
movq %r14, %rdx
movl %r12d, %ecx
movq %r15, %r8
movq 0x20(%rsp), %r9
xorl %eax, %eax
callq 0x2380
movq (%rbp), %rcx
leaq 0x2941(%rip), %rdi      # 0x5827
pushq $0x5
popq %rsi
pushq $0x1
popq %rdx
callq 0x2440
movq (%rbp), %rdi
movq %r14, %rbp
callq 0x22b0
leaq 0x28f2(%rip), %rdi      # 0x57f6
movq %r14, %rsi
callq 0x2280
testl %eax, %eax
je 0x2fc9
leaq 0x2a07(%rip), %rdi      # 0x5922
movq %rbp, %rsi
callq 0x2280
testl %eax, %eax
je 0x2fc9
leaq 0x2a28(%rip), %rdi      # 0x595a
movq %r13, %r15
movq %r13, %rsi
callq 0x2280
testl %eax, %eax
movq 0x18(%rsp), %r14
je 0x2ff6
leaq 0x2a0e(%rip), %rdi      # 0x595f
movq %r15, %rsi
callq 0x2280
testl %eax, %eax
je 0x3017
leaq 0x29fc(%rip), %rdi      # 0x5964
movq %r15, %rsi
callq 0x2280
testl %eax, %eax
je 0x302e
leaq 0x29ea(%rip), %rdi      # 0x5969
movq %rbp, %rsi
callq 0x2280
testl %eax, %eax
je 0x3047
movq 0x501a(%rip), %r15      # 0x7fb0
movq (%r15), %rbp
movq %r14, %rdi
callq 0x2410
leal -0x8(%rax), %edx
movq %rbp, %rdi
movq %r15, %rbp
leaq 0x29bd(%rip), %rsi      # 0x596e
movq %r13, %rcx
xorl %eax, %eax
callq 0x2380
testl %r12d, %r12d
jne 0x306f
jmp 0x309c
movl %r12d, %eax
cltd
pushq $0x4
popq %rcx
idivl %ecx
movl %r12d, %esi
subl %edx, %esi
addl $0x4, %esi
testl %edx, %edx
cmovel %r12d, %esi
movq %rbx, %rdi
callq 0x32a6
movq 0x4fc1(%rip), %rbp      # 0x7fb0
movq 0x18(%rsp), %r14
jmp 0x306f
movl %r12d, %eax
cltd
pushq $0x4
popq %rcx
idivl %ecx
movl %r12d, %esi
subl %edx, %esi
addl $0x4, %esi
testl %edx, %edx
cmovel %r12d, %esi
movq %rbx, %rdi
callq 0x32a6
jmp 0x3022
movq %rbx, %rdi
movl %r12d, %esi
callq 0x41c4
movq 0x4f87(%rip), %rbp      # 0x7fb0
movq %r15, %r13
jmp 0x306f
movq 0x20(%rsp), %rax
movq %rax, 0x330(%rbx)
movq %rbx, %rdi
movl %r12d, %esi
callq 0x40ea
jmp 0x3068
movq %r14, %rdi
callq 0x2410
leaq -0x4(%rax), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x2330
movq %rbx, %rdi
movl %r12d, %esi
callq 0x340a
movq 0x4f41(%rip), %rbp      # 0x7fb0
addl 0x30(%rsp), %r12d
movslq %r12d, %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x2330
movq (%rbp), %rcx
leaq 0x27fc(%rip), %rdi      # 0x5888
pushq $0x5
popq %rsi
pushq $0x1
popq %rdx
callq 0x2440
jmp 0x2e23
movq 0x2c8(%rbx), %rdi
testq %rdi, %rdi
je 0x30b5
callq 0x2300
andq $0x0, 0x2c8(%rbx)
movq (%rbp), %rcx
leaq 0x27cb(%rip), %rdi      # 0x588b
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
callq 0x2440
xorl %eax, %eax
jmp 0x2df5