// File: 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
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
addq $0x228, %rdi            # imm = 0x228
xorl %esi, %esi
movl $0x38, %edx
callq 0x2150
movq -0xc8(%rbp), %rdi
addq $0x260, %rdi            # imm = 0x260
xorl %esi, %esi
movl $0x34, %edx
callq 0x2150
movq -0xc8(%rbp), %rdi
addq $0x298, %rdi            # imm = 0x298
xorl %esi, %esi
movl $0x38, %edx
callq 0x2150
movq -0xc8(%rbp), %rdi
addq $0x2d0, %rdi            # imm = 0x2D0
xorl %esi, %esi
movl $0x34, %edx
callq 0x2150
movq -0xc8(%rbp), %rdi
addq $0x304, %rdi            # imm = 0x304
xorl %esi, %esi
movl $0x20, %edx
callq 0x2150
movq -0xc8(%rbp), %rdi
addq $0x20, %rdi
callq 0x23f0
movq %rax, -0x48(%rbp)
movq %rdx, -0x40(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x5350
movq -0xc8(%rbp), %rdi
movq %rax, -0x38(%rbp)
leaq -0x15(%rbp), %rsi
movl $0x4, %edx
callq 0x3f20
movq -0xc8(%rbp), %rdi
callq 0x3f60
movq -0xc8(%rbp), %rdi
movl %eax, -0x1c(%rbp)
leaq -0x21(%rbp), %rsi
movl $0x4, %edx
callq 0x3f20
movq 0x5cc6(%rip), %rax      # 0x8fb0
movq (%rax), %rdi
leaq -0x15(%rbp), %rdx
movl -0x1c(%rbp), %ecx
leaq -0x21(%rbp), %r8
movq -0x38(%rbp), %r9
leaq 0x35b1(%rip), %rsi      # 0x68b4
movb $0x0, %al
callq 0x2360
movq 0x5c9f(%rip), %rax      # 0x8fb0
movq (%rax), %rdi
leaq 0x3500(%rip), %rsi      # 0x681b
movb $0x0, %al
callq 0x2360
leaq -0x15(%rbp), %rsi
leaq 0x35b8(%rip), %rdi      # 0x68e5
callq 0x2260
cmpl $0x0, %eax
je 0x335b
movq 0x5c72(%rip), %rax      # 0x8fb0
movq (%rax), %rdi
leaq 0x35a2(%rip), %rsi      # 0x68ea
movb $0x0, %al
callq 0x2360
movl $0x1, -0x4(%rbp)
jmp 0x37ea
leaq -0x21(%rbp), %rsi
leaq 0x3596(%rip), %rdi      # 0x68fc
callq 0x2260
cmpl $0x0, %eax
je 0x3394
movq 0x5c39(%rip), %rax      # 0x8fb0
movq (%rax), %rdi
leaq 0x3580(%rip), %rsi      # 0x6901
movb $0x0, %al
callq 0x2360
movl $0x1, -0x4(%rbp)
jmp 0x37ea
jmp 0x3396
movq -0xc8(%rbp), %rdi
movl -0x1c(%rbp), %eax
subl $0x4, %eax
cltq
movq %rax, -0xd0(%rbp)
addq $0x20, %rdi
callq 0x23f0
movq %rax, -0x58(%rbp)
movq %rdx, -0x50(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x5350
movq %rax, %rcx
movq -0xd0(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x28(%rbp)
movq -0xc8(%rbp), %rdi
addq $0x20, %rdi
callq 0x23f0
movq %rax, -0x68(%rbp)
movq %rdx, -0x60(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x5350
movslq -0x28(%rbp), %rcx
cmpq %rcx, %rax
jge 0x3781
movq -0xc8(%rbp), %rdi
addq $0x20, %rdi
callq 0x23f0
movq %rax, -0x80(%rbp)
movq %rdx, -0x78(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x5350
movq -0xc8(%rbp), %rdi
movq %rax, -0x70(%rbp)
leaq -0x15(%rbp), %rsi
movl $0x4, %edx
callq 0x3f20
movq -0xc8(%rbp), %rdi
callq 0x3f60
movq -0xc8(%rbp), %rdi
movl %eax, -0x1c(%rbp)
movslq -0x1c(%rbp), %rax
movq %rax, -0xd8(%rbp)
addq $0x20, %rdi
callq 0x23f0
movq %rax, -0x90(%rbp)
movq %rdx, -0x88(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x5350
movq %rax, %rcx
movq -0xd8(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x2c(%rbp)
leaq -0x15(%rbp), %rsi
leaq 0x334a(%rip), %rdi      # 0x67e7
callq 0x2260
cmpl $0x0, %eax
je 0x34bc
leaq -0x15(%rbp), %rsi
leaq 0x3461(%rip), %rdi      # 0x6913
callq 0x2260
cmpl $0x0, %eax
jne 0x34c2
movb $0x0, -0x21(%rbp)
jmp 0x34d7
movq -0xc8(%rbp), %rdi
leaq -0x21(%rbp), %rsi
movl $0x4, %edx
callq 0x3f20
movq 0x5ad2(%rip), %rax      # 0x8fb0
movq (%rax), %rdi
leaq -0x15(%rbp), %rdx
movl -0x1c(%rbp), %ecx
leaq -0x21(%rbp), %r8
movq -0x70(%rbp), %r9
leaq 0x3421(%rip), %rsi      # 0x6918
movb $0x0, %al
callq 0x2360
movq 0x5aab(%rip), %rax      # 0x8fb0
movq (%rax), %rdi
leaq 0x3309(%rip), %rsi      # 0x6818
movb $0x0, %al
callq 0x2360
movq 0x5a93(%rip), %rax      # 0x8fb0
movq (%rax), %rdi
callq 0x2290
leaq -0x15(%rbp), %rsi
leaq 0x32b7(%rip), %rdi      # 0x67e7
callq 0x2260
cmpl $0x0, %eax
je 0x354f
leaq -0x15(%rbp), %rsi
leaq 0x33ce(%rip), %rdi      # 0x6913
callq 0x2260
cmpl $0x0, %eax
jne 0x3599
movl -0x1c(%rbp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x3585
movl -0x1c(%rbp), %eax
movl %eax, -0xdc(%rbp)
movl -0x1c(%rbp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl -0xdc(%rbp), %eax
movl $0x4, %ecx
subl %edx, %ecx
addl %ecx, %eax
movl %eax, -0x1c(%rbp)
movq -0xc8(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x3ba0
jmp 0x374e
leaq -0x21(%rbp), %rsi
leaq 0x33a7(%rip), %rdi      # 0x694b
callq 0x2260
cmpl $0x0, %eax
jne 0x35f8
movl -0x1c(%rbp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x35e4
movl -0x1c(%rbp), %eax
movl %eax, -0xe0(%rbp)
movl -0x1c(%rbp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl -0xe0(%rbp), %eax
movl $0x4, %ecx
subl %edx, %ecx
addl %ecx, %eax
movl %eax, -0x1c(%rbp)
movq -0xc8(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x3ba0
jmp 0x374c
leaq -0x21(%rbp), %rsi
leaq 0x334d(%rip), %rdi      # 0x6950
callq 0x2260
cmpl $0x0, %eax
jne 0x3621
movq -0xc8(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x5190
jmp 0x374a
leaq -0x21(%rbp), %rsi
leaq 0x3329(%rip), %rdi      # 0x6955
callq 0x2260
cmpl $0x0, %eax
jne 0x3655
movq -0xc8(%rbp), %rdi
movq -0x70(%rbp), %rax
movq %rax, 0x330(%rdi)
movl -0x1c(%rbp), %esi
callq 0x5050
jmp 0x3748
leaq -0x15(%rbp), %rsi
leaq 0x32fa(%rip), %rdi      # 0x695a
callq 0x2260
cmpl $0x0, %eax
jne 0x36e3
movq -0xc8(%rbp), %rdi
movq %rdi, %rax
addq $0x20, %rax
movq %rax, -0xe8(%rbp)
addq $0x20, %rdi
callq 0x23f0
movq %rax, -0xb0(%rbp)
movq %rdx, -0xa8(%rbp)
leaq -0xb0(%rbp), %rdi
movl $0x4, %esi
callq 0x54d0
movq %rax, -0xa0(%rbp)
movq %rdx, -0x98(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x5350
movq -0xe8(%rbp), %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x2300
movq -0xc8(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x3dd0
jmp 0x3746
movq -0xc8(%rbp), %rdi
movq 0x58bf(%rip), %rax      # 0x8fb0
movq (%rax), %rax
movq %rax, -0xf0(%rbp)
addq $0x20, %rdi
callq 0x23f0
movq %rax, -0xc0(%rbp)
movq %rdx, -0xb8(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x5350
movq -0xf0(%rbp), %rdi
movl %eax, %edx
subl $0x8, %edx
leaq -0x21(%rbp), %rcx
leaq 0x322a(%rip), %rsi      # 0x695f
movb $0x0, %al
callq 0x2360
cmpl $0x0, -0x1c(%rbp)
jne 0x3744
jmp 0x3781
jmp 0x3746
jmp 0x3748
jmp 0x374a
jmp 0x374c
jmp 0x374e
movq -0xc8(%rbp), %rdi
addq $0x20, %rdi
movslq -0x2c(%rbp), %rsi
xorl %edx, %edx
callq 0x2300
movq 0x5845(%rip), %rax      # 0x8fb0
movq (%rax), %rdi
leaq 0x3104(%rip), %rsi      # 0x6879
movb $0x0, %al
callq 0x2360
jmp 0x33d6
movq -0xc8(%rbp), %rax
cmpq $0x0, 0x2c8(%rax)
je 0x37cb
movq -0xc8(%rbp), %rax
movq 0x2c8(%rax), %rax
movq %rax, -0xf8(%rbp)
cmpq $0x0, %rax
je 0x37b9
movq -0xf8(%rbp), %rdi
callq 0x22d0
movq -0xc8(%rbp), %rax
movq $0x0, 0x2c8(%rax)
movq 0x57de(%rip), %rax      # 0x8fb0
movq (%rax), %rdi
leaq 0x30a0(%rip), %rsi      # 0x687c
movb $0x0, %al
callq 0x2360
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw %cs:(%rax,%rax)