// File: /workspace/github_c_makefile_star2_O3/inflex[P]ripMIME/ripOLE/ole.c
// Function: OLE_load_chain
unsigned char *OLE_load_chain( struct OLE_object *ole, int FAT_sector_start )
{

	int chain_length = 0;
	int current_sector = FAT_sector_start;
	unsigned char *buffer = NULL;
	unsigned char *bp = NULL;


	ole->last_chain_size = 0;

	if (FAT_sector_start < 0) return NULL;

	DOLE LOGGER_log("%s:%d:OLE_load_chain:DEBUG: Loading chain, starting at sector %d",FL,FAT_sector_start);

	chain_length = OLE_follow_chain( ole, FAT_sector_start );
	DOLE LOGGER_log("%s:%d:OLE_load_chain:DEBUG: %d sectors need to be loaded",FL,chain_length);

	if (chain_length > 0)
	{
		size_t offset;

		offset = ole->last_chain_size = chain_length << ole->header.sector_shift;
		bp = buffer = malloc( offset *sizeof(unsigned char));
		if (buffer == NULL)
		{
			LOGGER_log("%s:%d:OLE_load_chain:ERROR: Cannot allocate %d bytes for OLE chain",FL,offset);
			return NULL;
		}

		if (buffer != NULL)
		{
			int tick = 0;
			unsigned char *bp_limit;
			bp_limit = bp +offset;

			do {
				int next_sector;

				DOLE LOGGER_log("%s:%d:OLE_load_chain:DEBUG: Loading sector[%d] %d",FL, tick, current_sector );

				ole->error = OLE_get_block( ole, current_sector, bp );
				if (ole->error != OLE_OK)
				{
					//FREE5					if (bp != NULL) free(bp);
					return NULL;
				}

				bp += ole->header.sector_size;
				if (bp > bp_limit) {  
					if (buffer != NULL) { free(buffer); bp = buffer = NULL; }
					VOLE LOGGER_log("%s:%d:OLE_load_chain:ERROR: Load-chain went over memory boundary",FL); 
					return NULL;
				};

				next_sector = get_uint32((char *)( ole->FAT +(LEN_ULONG *current_sector)));
				current_sector = next_sector;
				tick++;
			} while ((current_sector >= 0)&&(current_sector <= ole->last_sector));
		}
	}
	DOLE LOGGER_log("%s:%d:OLE_load_chain:DEBUG: Done loading chain",FL);

	return buffer;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq $0x0, 0x18(%rdi)
testl %esi, %esi
js 0xfbb3
movl %esi, %r15d
movq %rdi, %rbx
cmpl $0x0, 0x444(%rdi)
je 0xfa6d
leaq 0xa081(%rip), %rdi      # 0x19ad8
leaq 0x93db(%rip), %rsi      # 0x18e39
movl $0x52f, %edx            # imm = 0x52F
movl %r15d, %ecx
xorl %eax, %eax
callq 0xc84f
movq %rbx, %rdi
movl %r15d, %esi
callq 0xf606
movl %eax, %ebp
cmpl $0x0, 0x444(%rbx)
je 0xfa9f
leaq 0xa08f(%rip), %rdi      # 0x19b19
leaq 0x93a8(%rip), %rsi      # 0x18e39
movl $0x532, %edx            # imm = 0x532
movl %ebp, %ecx
xorl %eax, %eax
callq 0xc84f
testl %ebp, %ebp
jle 0xfb40
movb 0x264(%rbx), %cl
shll %cl, %ebp
movslq %ebp, %r12
movq %r12, 0x18(%rbx)
movq %r12, %rdi
callq 0x22a0
testq %rax, %rax
je 0xfb68
movq %rax, %r13
addq %rax, %r12
movq %rax, %r14
xorl %ebp, %ebp
cmpl $0x0, 0x444(%rbx)
je 0xfafa
leaq 0xa0b3(%rip), %rdi      # 0x19b95
leaq 0x9350(%rip), %rsi      # 0x18e39
movl $0x549, %edx            # imm = 0x549
movl %ebp, %ecx
movl %r15d, %r8d
xorl %eax, %eax
callq 0xc84f
movq %rbx, %rdi
movl %r15d, %esi
movq %r14, %rdx
callq 0xe895
movl %eax, (%rbx)
testl %eax, %eax
jne 0xfbb3
movl 0x268(%rbx), %eax
addq %rax, %r14
cmpq %r12, %r14
ja 0xfb8a
shll $0x2, %r15d
addq 0x28(%rbx), %r15
movq %r15, %rdi
callq 0x10eeb
testl %eax, %eax
js 0xfb43
movl %eax, %r15d
incl %ebp
cmpl 0x10(%rbx), %eax
jle 0xfad2
jmp 0xfb43
xorl %r13d, %r13d
cmpl $0x0, 0x444(%rbx)
je 0xfbb6
leaq 0xa0b5(%rip), %rdi      # 0x19c08
leaq 0x92df(%rip), %rsi      # 0x18e39
movl $0x55f, %edx            # imm = 0x55F
xorl %eax, %eax
callq 0xc84f
jmp 0xfbb6
leaq 0x9fe3(%rip), %rdi      # 0x19b52
leaq 0x92c3(%rip), %rsi      # 0x18e39
xorl %r13d, %r13d
movl $0x53c, %edx            # imm = 0x53C
movq %r12, %rcx
xorl %eax, %eax
callq 0xc84f
jmp 0xfbb6
movq %r13, %rdi
callq 0x2030
cmpl $0x0, 0x448(%rbx)
je 0xfbb3
leaq 0xa025(%rip), %rdi      # 0x19bc7
leaq 0x9290(%rip), %rsi      # 0x18e39
xorl %r13d, %r13d
movl $0x555, %edx            # imm = 0x555
jmp 0xfb5f
xorl %r13d, %r13d
movq %r13, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq