// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %r14
leaq 0x20(%rdi), %rbx
movq %rbx, %rdi
callq 0x2410
movq %rax, %r13
leaq 0x13(%rsp), %rsi
pushq $0x4
popq %rbp
movq %r14, %rdi
movl %ebp, %edx
callq 0x3542
movq %r14, %rdi
callq 0x3566
movl %eax, %r12d
leaq 0xe(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x3542
movq 0x41b0(%rip), %rax      # 0x7fb0
movq (%rax), %rdi
leaq 0x19b0(%rip), %rsi      # 0x57ba
leaq 0x13(%rsp), %rdx
movl %r12d, %ecx
movq %r15, %r8
movq %r13, %r9
movq %rax, %r13
xorl %eax, %eax
callq 0x2380
movq (%r13), %rcx
leaq 0x19f7(%rip), %rdi      # 0x5824
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
callq 0x2440
movq %rbx, %rdi
callq 0x2410
leal (%r12,%rax), %ebp
addl $-0x4, %ebp
leaq 0x19a8(%rip), %rsi      # 0x57f6
pushq $0x5
popq %rdx
leaq 0x13(%rsp), %rdi
callq 0x2260
movslq %ebp, %r15
testl %eax, %eax
je 0x4092
leaq 0x2d0(%r14), %rax
movq %rax, 0x28(%rsp)
movq %r14, 0x18(%rsp)
leaq 0x260(%r14), %rax
movq %rax, 0x30(%rsp)
leaq 0xe(%rsp), %r12
pushq $0x4
popq %r14
movl $0x0, 0x8(%rsp)
movq %r15, 0x38(%rsp)
movq %rbx, %rdi
callq 0x2410
cmpq %r15, %rax
jge 0x4065
movq %rbx, %rdi
callq 0x2410
movq %rax, %rbp
movq 0x18(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
movl %r14d, %edx
callq 0x3542
movq %r15, %rdi
callq 0x3566
movl %eax, %r13d
movq %rbx, %rdi
callq 0x2410
movq %rax, 0x40(%rsp)
movq 0x40c9(%rip), %rax      # 0x7fb0
movq (%rax), %rdi
leaq 0x190a(%rip), %rsi      # 0x57fb
movq %r12, %rdx
movl %r13d, %ecx
movq %rbp, %r8
movq %rax, %rbp
xorl %eax, %eax
callq 0x2380
movq (%rbp), %rcx
leaq 0x1912(%rip), %rdi      # 0x5821
pushq $0xb
popq %rsi
pushq $0x1
popq %rdx
callq 0x2440
leaq 0x190c(%rip), %rdi      # 0x582d
movq %r12, %rsi
callq 0x2280
testl %eax, %eax
je 0x3f75
leaq 0x1920(%rip), %rdi      # 0x5854
movq %r12, %rsi
callq 0x2280
testl %eax, %eax
je 0x3ffe
leaq 0x190e(%rip), %rdi      # 0x5859
movq %r12, %rsi
callq 0x2280
testl %eax, %eax
je 0x402a
movq (%rbp), %rdi
leaq 0x18f8(%rip), %rsi      # 0x585e
movq %r12, %rdx
xorl %eax, %eax
callq 0x2380
jmp 0x402a
movq %rbx, %rdi
callq 0x2410
movq %rax, %rbp
movq %r15, %rdi
leaq 0x23(%rsp), %r12
movq %r12, %rsi
movl %r14d, %edx
callq 0x3542
movl $0x0, 0x8(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x2330
movq %r12, %rdi
leaq 0x1880(%rip), %rsi      # 0x5832
pushq $0x5
popq %r14
movq %r14, %rdx
callq 0x2260
movq 0x30(%rsp), %rsi
testl %eax, %eax
je 0x3fed
movq %r12, %rdi
leaq 0x1866(%rip), %rsi      # 0x5837
movq %r14, %rdx
callq 0x2260
movq 0x28(%rsp), %rsi
pushq $0x1
popq %rcx
movl %ecx, 0x8(%rsp)
testl %eax, %eax
jne 0x40b9
movq %r15, %rdi
callq 0x37c6
movq 0x3fb4(%rip), %rbp      # 0x7fb0
jmp 0x402a
cmpl $0x0, 0x8(%rsp)
je 0x4018
movq 0x18(%rsp), %rdi
callq 0x3c70
pushq $0x1
popq %rax
movl %eax, 0x8(%rsp)
jmp 0x402a
movq 0x18(%rsp), %rdi
callq 0x39ee
movl $0x0, 0x8(%rsp)
addl 0x40(%rsp), %r13d
movq (%rbp), %rcx
leaq 0x1848(%rip), %rdi      # 0x5882
pushq $0xb
popq %rsi
pushq $0x1
popq %rdx
callq 0x2440
movslq %r13d, %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x2330
movq 0x38(%rsp), %r15
leaq 0xe(%rsp), %r12
pushq $0x4
popq %r14
jmp 0x3e99
movq 0x3f44(%rip), %rax      # 0x7fb0
movq (%rax), %rcx
leaq 0x180f(%rip), %rdi      # 0x5885
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
callq 0x2440
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x2330
jmp 0x40d9
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x2330
movq (%r13), %rcx
leaq 0x17d9(%rip), %rdi      # 0x5885
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
callq 0x2440
jmp 0x40d9
movq 0x3ef0(%rip), %rax      # 0x7fb0
movq (%rax), %rdi
leaq 0x1772(%rip), %rsi      # 0x583c
leaq 0x23(%rsp), %rdx
xorl %eax, %eax
callq 0x2380
pushq $-0x1
popq %rbp
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq