// File: /workspace/github_cpp_makefile_star2_O3/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, 0x18(%rsp)
leaq 0x20(%rdi), %rbx
movq %rbx, %rdi
callq 0x23e0
movq %rax, %r15
leaq 0x43(%rsp), %r14
movl $0x4, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x22b0
xorl %ebp, %ebp
movb %bpl, 0x4(%r14)
leaq 0x10(%rsp), %r12
movl $0x4, %edx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x22b0
movl (%r12), %r12d
leaq 0x10(%rsp), %r13
movl $0x4, %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x22b0
movb %bpl, 0x4(%r13)
movq 0x3c96(%rip), %rax      # 0x7fb0
movq (%rax), %rdi
leaq 0x1496(%rip), %rsi      # 0x57ba
movq %r14, %rdx
movl %r12d, %ecx
movq %r13, %r8
movq %rax, %r13
movq %r15, %r9
xorl %eax, %eax
callq 0x2340
movq (%r13), %rcx
leaq 0x14df(%rip), %rdi      # 0x5824
movl $0x8, %esi
movl $0x1, %edx
callq 0x2410
movq %rbx, %rdi
callq 0x23e0
leal (%r12,%rax), %r15d
addl $-0x4, %r15d
movl $0x4b4e554a, %eax       # imm = 0x4B4E554A
xorl (%r14), %eax
movzbl 0x4(%r14), %ecx
orl %eax, %ecx
je 0x45bf
movq %rbx, %rdi
callq 0x23e0
movslq %r15d, %rcx
movq %rcx, 0x28(%rsp)
cmpq %rcx, %rax
jge 0x4592
movq 0x18(%rsp), %rax
leaq 0x2d0(%rax), %rcx
movq %rcx, 0x30(%rsp)
addq $0x260, %rax            # imm = 0x260
movq %rax, 0x38(%rsp)
leaq 0x4(%rsp), %r14
movl $0x0, 0xc(%rsp)
movq %rbx, %rdi
callq 0x23e0
movq %r13, %r15
movq %rax, %r13
movl $0x4, %edx
movq %rbx, %rdi
leaq 0x10(%rsp), %r12
movq %r12, %rsi
callq 0x22b0
movb $0x0, 0x14(%rsp)
movl $0x4, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x22b0
movl 0x4(%rsp), %ebp
movq %rbx, %rdi
callq 0x23e0
movq %rax, 0x20(%rsp)
movq (%r15), %rdi
leaq 0x13ed(%rip), %rsi      # 0x57fb
movq %r12, %rdx
movl %ebp, %ecx
movq %r13, %r8
movq %r15, %r13
xorl %eax, %eax
callq 0x2340
movq (%r15), %rcx
movl $0xb, %esi
movl $0x1, %edx
leaq 0x13ed(%rip), %rdi      # 0x5821
callq 0x2410
leaq 0x13ed(%rip), %rdi      # 0x582d
movq %r12, %rsi
callq 0x2230
testl %eax, %eax
je 0x4499
leaq 0x1401(%rip), %rdi      # 0x5854
movq %r12, %rsi
callq 0x2230
testl %eax, %eax
je 0x451e
leaq 0x13ef(%rip), %rdi      # 0x5859
movq %r12, %rsi
callq 0x2230
testl %eax, %eax
movq 0x20(%rsp), %r14
je 0x4550
movq (%r13), %rdi
leaq 0x13d4(%rip), %rsi      # 0x585e
movq %r12, %rdx
xorl %eax, %eax
callq 0x2340
jmp 0x4550
movq %rbx, %rdi
callq 0x23e0
movq %rax, %r13
movl $0x4, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x22b0
movb $0x0, 0x8(%rsp)
xorl %r14d, %r14d
movq %rbx, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x22f0
movl 0x4(%rsp), %eax
movl $0x73646976, %ecx       # imm = 0x73646976
xorl %ecx, %eax
movzbl 0x8(%rsp), %ecx
orl %eax, %ecx
movq 0x38(%rsp), %rsi
je 0x4505
movl 0x4(%rsp), %eax
movl $0x73647561, %ecx       # imm = 0x73647561
xorl %ecx, %eax
movzbl 0x8(%rsp), %ecx
orl %eax, %ecx
movl $0x1, %r14d
movq 0x30(%rsp), %rsi
jne 0x45f7
movl %r14d, 0xc(%rsp)
movq 0x18(%rsp), %rdi
callq 0x3ac4
movq %r15, %r13
movq 0x20(%rsp), %r14
jmp 0x4550
cmpl $0x0, 0xc(%rsp)
movq 0x20(%rsp), %r14
je 0x453e
movq 0x18(%rsp), %rdi
callq 0x40e6
movl $0x1, 0xc(%rsp)
jmp 0x4550
movq 0x18(%rsp), %rdi
callq 0x3dac
movl $0x0, 0xc(%rsp)
addl %r14d, %ebp
movq (%r13), %rcx
movl $0xb, %esi
movl $0x1, %edx
leaq 0x131a(%rip), %rdi      # 0x5882
callq 0x2410
movslq %ebp, %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x22f0
movq %rbx, %rdi
callq 0x23e0
cmpq 0x28(%rsp), %rax
leaq 0x4(%rsp), %r14
jl 0x43bb
movq (%r13), %rcx
leaq 0x12e8(%rip), %rdi      # 0x5885
movl $0x8, %esi
movl $0x1, %edx
callq 0x2410
xorl %ebp, %ebp
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
xorl %edx, %edx
callq 0x22f0
jmp 0x45e6
movslq %r15d, %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x22f0
movq (%r13), %rcx
leaq 0x12ae(%rip), %rdi      # 0x5885
movl $0x8, %esi
movl $0x1, %edx
callq 0x2410
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r15), %rdi
leaq 0x123b(%rip), %rsi      # 0x583c
leaq 0x4(%rsp), %rdx
xorl %eax, %eax
callq 0x2340
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x45e6