// File: olegk0[P]readAVI/ReadAVI.cpp
// Function: ReadAVI::parse_hdrl_list()
int ReadAVI::parse_hdrl_list()
{
    char chunk_id[5];
    int chunk_size;
    char chunk_type[5];
    int end_of_chunk;
    int next_chunk;
#ifdef VERBOSE
    long offset = inFile.tellg();
#endif
    int stream_type = 0;     // 0=video 1=sound

    read_chars(chunk_id, 4);
    chunk_size = read_int();
    read_chars(chunk_type, 4);

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

    end_of_chunk = chunk_size - 4 + inFile.tellg();
    if ((end_of_chunk % 4) != 0) {
//MSG_DBG(2, "Adjusting end of chunk %d\n", end_of_chunk);
	//end_of_chunk=end_of_chunk+(4-(end_of_chunk%4));
//MSG_DBG(2, "Adjusting end of chunk %d\n", end_of_chunk);
    }

    if (strcmp(chunk_id, "JUNK") == 0) {
	inFile.seekg(end_of_chunk, ios_base::beg);
	MSG_DBG(2, "      }\n");
	return 0;
    }

    while (inFile.tellg() < end_of_chunk) {
#ifdef VERBOSE
	long offset = inFile.tellg();
#endif
	read_chars(chunk_type, 4);
	chunk_size = read_int();
	next_chunk = chunk_size + inFile.tellg();
	if ((chunk_size % 4) != 0) {
//MSG_DBG(2, "Chunk size not a multiple of 4?\n");
	    //chunk_size=chunk_size+(4-(chunk_size%4));
	}

	MSG_DBG(2, "         %.4s (size=%d offset=0x%lx)\n", chunk_type, chunk_size, offset);
	MSG_DBG(2, "         {\n");

	if (strcasecmp("strh", chunk_type) == 0) {
	    long marker = inFile.tellg();
	    char buffer[5];
	    read_chars(buffer, 4);
	    inFile.seekg(marker, ios_base::beg);

	    if (strcmp(buffer, "vids") == 0) {
		stream_type = 0;
		read_stream_header(&stream_header_vid);
	    } else if (strcmp(buffer, "auds") == 0) {
		stream_type = 1;
		read_stream_header(&stream_header_auds);
	    } else {
		MSG_DBG(2, "Unknown stream type %s\n", buffer);
		return -1;
	    }
	} else if (strcasecmp("strf", chunk_type) == 0) {
	    if (stream_type == 0) {
		read_stream_format();
	    } else {
		read_stream_format_auds();
	    }
	} else if (strcasecmp("strd", chunk_type) == 0) {

	} else {
	    MSG_DBG(2, "            Unknown chunk type: %s\n", chunk_type);
	    // skip_chunk();
	}

	MSG_DBG(2, "         }\n");

	inFile.seekg(next_chunk, ios_base::beg);
    }

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

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
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)
movl $0x0, -0x4c(%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 0x4351(%rip), %rax      # 0x8fb0
movq (%rax), %rdi
leaq -0x15(%rbp), %rdx
movl -0x1c(%rbp), %ecx
leaq -0x21(%rbp), %r8
movq -0x38(%rbp), %r9
leaq 0x1b33(%rip), %rsi      # 0x67ab
movb $0x0, %al
callq 0x2360
movq 0x432a(%rip), %rax      # 0x8fb0
movq (%rax), %rdi
leaq 0x1b85(%rip), %rsi      # 0x6815
movb $0x0, %al
callq 0x2360
movq -0xc8(%rbp), %rdi
movl -0x1c(%rbp), %eax
subl $0x4, %eax
cltq
movq %rax, -0xc0(%rbp)
addq $0x20, %rdi
callq 0x23f0
movq %rax, -0x60(%rbp)
movq %rdx, -0x58(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x5350
movq %rax, %rcx
movq -0xc0(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x4ce9
jmp 0x4ce9
leaq -0x15(%rbp), %rdi
leaq 0x1af3(%rip), %rsi      # 0x67e7
callq 0x2320
cmpl $0x0, %eax
jne 0x4d38
movq -0xc8(%rbp), %rdi
addq $0x20, %rdi
movslq -0x28(%rbp), %rsi
xorl %edx, %edx
callq 0x2300
movq 0x4295(%rip), %rax      # 0x8fb0
movq (%rax), %rdi
leaq 0x1b51(%rip), %rsi      # 0x6876
movb $0x0, %al
callq 0x2360
movl $0x0, -0x4(%rbp)
jmp 0x5037
jmp 0x4d3a
movq -0xc8(%rbp), %rdi
addq $0x20, %rdi
callq 0x23f0
movq %rax, -0x70(%rbp)
movq %rdx, -0x68(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x5350
movslq -0x28(%rbp), %rcx
cmpq %rcx, %rax
jge 0x5002
movq -0xc8(%rbp), %rdi
addq $0x20, %rdi
callq 0x23f0
movq %rax, -0x88(%rbp)
movq %rdx, -0x80(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x5350
movq -0xc8(%rbp), %rdi
movq %rax, -0x78(%rbp)
leaq -0x21(%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, -0xd0(%rbp)
addq $0x20, %rdi
callq 0x23f0
movq %rax, -0x98(%rbp)
movq %rdx, -0x90(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x5350
movq %rax, %rcx
movq -0xd0(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x2c(%rbp)
movl -0x1c(%rbp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x4e0e
jmp 0x4e0e
movq 0x419b(%rip), %rax      # 0x8fb0
movq (%rax), %rdi
leaq -0x21(%rbp), %rdx
movl -0x1c(%rbp), %ecx
movq -0x78(%rbp), %r8
leaq 0x19c2(%rip), %rsi      # 0x67ec
movb $0x0, %al
callq 0x2360
movq 0x4178(%rip), %rax      # 0x8fb0
movq (%rax), %rdi
leaq 0x19d0(%rip), %rsi      # 0x6812
movb $0x0, %al
callq 0x2360
leaq -0x21(%rbp), %rsi
leaq 0x19ca(%rip), %rdi      # 0x681e
callq 0x2260
cmpl $0x0, %eax
jne 0x4f61
movq -0xc8(%rbp), %rdi
addq $0x20, %rdi
callq 0x23f0
movq %rax, -0xb0(%rbp)
movq %rdx, -0xa8(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x5350
movq -0xc8(%rbp), %rdi
movq %rax, -0xa0(%rbp)
leaq -0xb5(%rbp), %rsi
movl $0x4, %edx
callq 0x3f20
movq -0xc8(%rbp), %rdi
addq $0x20, %rdi
movq -0xa0(%rbp), %rsi
xorl %edx, %edx
callq 0x2300
leaq -0xb5(%rbp), %rdi
leaq 0x1951(%rip), %rsi      # 0x6823
callq 0x2320
cmpl $0x0, %eax
jne 0x4efb
movq -0xc8(%rbp), %rdi
movl $0x0, -0x4c(%rbp)
movq %rdi, %rsi
addq $0x260, %rsi            # imm = 0x260
callq 0x42d0
jmp 0x4f5f
leaq -0xb5(%rbp), %rdi
leaq 0x191f(%rip), %rsi      # 0x6828
callq 0x2320
cmpl $0x0, %eax
jne 0x4f32
movq -0xc8(%rbp), %rdi
movl $0x1, -0x4c(%rbp)
movq %rdi, %rsi
addq $0x2d0, %rsi            # imm = 0x2D0
callq 0x42d0
jmp 0x4f5d
movq 0x4077(%rip), %rax      # 0x8fb0
movq (%rax), %rdi
leaq -0xb5(%rbp), %rdx
leaq 0x18e3(%rip), %rsi      # 0x682d
movb $0x0, %al
callq 0x2360
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5037
jmp 0x4f5f
jmp 0x4fcf
leaq -0x21(%rbp), %rsi
leaq 0x18d9(%rip), %rdi      # 0x6845
callq 0x2260
cmpl $0x0, %eax
jne 0x4f98
cmpl $0x0, -0x4c(%rbp)
jne 0x4f8a
movq -0xc8(%rbp), %rdi
callq 0x4690
jmp 0x4f96
movq -0xc8(%rbp), %rdi
callq 0x4a30
jmp 0x4fcd
leaq -0x21(%rbp), %rsi
leaq 0x18a7(%rip), %rdi      # 0x684a
callq 0x2260
cmpl $0x0, %eax
jne 0x4faf
jmp 0x4fcb
movq 0x3ffa(%rip), %rax      # 0x8fb0
movq (%rax), %rdi
leaq -0x21(%rbp), %rdx
leaq 0x188b(%rip), %rsi      # 0x684f
movb $0x0, %al
callq 0x2360
jmp 0x4fcd
jmp 0x4fcf
movq 0x3fda(%rip), %rax      # 0x8fb0
movq (%rax), %rdi
leaq 0x1893(%rip), %rsi      # 0x6873
movb $0x0, %al
callq 0x2360
movq -0xc8(%rbp), %rdi
addq $0x20, %rdi
movslq -0x2c(%rbp), %rsi
xorl %edx, %edx
callq 0x2300
jmp 0x4d3a
movq 0x3fa7(%rip), %rax      # 0x8fb0
movq (%rax), %rdi
leaq 0x1863(%rip), %rsi      # 0x6876
movb $0x0, %al
callq 0x2360
movq -0xc8(%rbp), %rdi
addq $0x20, %rdi
movslq -0x28(%rbp), %rsi
xorl %edx, %edx
callq 0x2300
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)