// File: /workspace/github_cpp_makefile_star2_O1/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, 0x20(%rsp)
leaq 0x43(%rsp), %r14
movl $0x4, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x22b0
xorl %r12d, %r12d
movb %r12b, 0x4(%r14)
leaq 0x13(%rsp), %r15
movl $0x4, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x22b0
movl (%r15), %ebp
leaq 0x13(%rsp), %r13
movl $0x4, %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x22b0
movb %r12b, 0x4(%r13)
movq 0x3c4a(%rip), %r12      # 0x7fb0
movq (%r12), %rdi
leaq 0x1449(%rip), %rsi      # 0x57ba
movq %r14, %rdx
movl %ebp, %ecx
movq %r13, %r8
movq 0x20(%rsp), %r9
xorl %eax, %eax
callq 0x2340
movq (%r12), %rcx
leaq 0x1494(%rip), %rdi      # 0x5824
movl $0x8, %esi
movl $0x1, %edx
callq 0x2410
movq %rbx, %rdi
callq 0x23e0
movl $0x4b4e554a, %ecx       # imm = 0x4B4E554A
xorl (%r14), %ecx
addl %ebp, %eax
addl $-0x4, %eax
movzbl 0x4(%r14), %edx
orl %ecx, %edx
movslq %eax, %r14
je 0x4608
movq 0x18(%rsp), %r15
leaq 0x2d0(%r15), %rax
movq %rax, 0x28(%rsp)
leaq 0x260(%r15), %rax
movq %rax, 0x30(%rsp)
leaq 0x13(%rsp), %rbp
leaq 0x7(%rsp), %r13
movl $0x0, 0xc(%rsp)
movq %r14, 0x38(%rsp)
movq %rbx, %rdi
callq 0x23e0
cmpq %r14, %rax
jge 0x4633
movq %rbx, %rdi
callq 0x23e0
movq %r12, %r14
movq %rax, %r12
movl $0x4, %edx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x22b0
movb $0x0, 0x17(%rsp)
movl $0x4, %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x22b0
movq %r13, %r15
movl 0x7(%rsp), %r13d
movq %rbx, %rdi
callq 0x23e0
movq %rax, 0x20(%rsp)
movq (%r14), %rdi
leaq 0x13a0(%rip), %rsi      # 0x57fb
movq %rbp, %rdx
movl %r13d, %ecx
movq %r12, %r8
movq %r14, %r12
xorl %eax, %eax
callq 0x2340
movq (%r14), %rcx
movl $0xb, %esi
movl $0x1, %edx
leaq 0x139f(%rip), %rdi      # 0x5821
callq 0x2410
leaq 0x139f(%rip), %rdi      # 0x582d
movq %rbp, %rsi
callq 0x2230
testl %eax, %eax
je 0x44e2
leaq 0x13b3(%rip), %rdi      # 0x5854
movq %rbp, %rsi
callq 0x2230
testl %eax, %eax
je 0x4552
leaq 0x13a1(%rip), %rdi      # 0x5859
movq %rbp, %rsi
callq 0x2230
testl %eax, %eax
je 0x45c1
movq (%r12), %rdi
leaq 0x138b(%rip), %rsi      # 0x585e
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2340
jmp 0x45c1
movq %rbx, %rdi
callq 0x23e0
movq %rax, %r12
movl $0x4, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x22b0
movb $0x0, 0xb(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x22f0
movl 0x7(%rsp), %eax
movl $0x73646976, %ecx       # imm = 0x73646976
xorl %ecx, %eax
movzbl 0xb(%rsp), %ecx
orl %eax, %ecx
je 0x456d
movl 0x7(%rsp), %eax
movl $0x73647561, %ecx       # imm = 0x73647561
xorl %ecx, %eax
movzbl 0xb(%rsp), %ecx
orl %eax, %ecx
je 0x4589
movq (%r14), %rdi
xorl %r12d, %r12d
leaq 0x12f8(%rip), %rsi      # 0x583c
leaq 0x7(%rsp), %rdx
xorl %eax, %eax
callq 0x2340
jmp 0x45a3
cmpl $0x0, 0xc(%rsp)
movq 0x18(%rsp), %rdi
je 0x45b4
callq 0x4130
movl $0x1, 0xc(%rsp)
jmp 0x45c1
movq 0x18(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0x3afa
movb $0x1, %r12b
movl $0x0, 0xc(%rsp)
jmp 0x45a3
movq 0x18(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x3afa
movb $0x1, %r12b
movl $0x1, 0xc(%rsp)
testb %r12b, %r12b
je 0x45ad
movq %r14, %r12
jmp 0x45c1
xorl %eax, %eax
movq %r14, %r12
jmp 0x45ef
callq 0x3de2
movl $0x0, 0xc(%rsp)
addl 0x20(%rsp), %r13d
movq (%r12), %rcx
movl $0xb, %esi
movl $0x1, %edx
leaq 0x12a7(%rip), %rdi      # 0x5882
callq 0x2410
movslq %r13d, %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x22f0
movb $0x1, %al
testb %al, %al
movq 0x38(%rsp), %r14
leaq 0x7(%rsp), %r13
jne 0x43f8
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x465c
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x22f0
movq (%r12), %rcx
leaq 0x1263(%rip), %rdi      # 0x5885
movl $0x8, %esi
movl $0x1, %edx
callq 0x2410
jmp 0x465c
movq (%r12), %rcx
leaq 0x1247(%rip), %rdi      # 0x5885
movl $0x8, %esi
movl $0x1, %edx
callq 0x2410
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x22f0
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop