// File: /workspace/github_c_makefile_star2_O2/inflex[P]ripMIME/ripOLE/ole.c
// Function: OLE_follow_minichain
int OLE_follow_minichain( struct OLE_object *ole, int miniFAT_sector_start )
{
	//unsigned int current_sector = miniFAT_sector_start;
	int current_sector = miniFAT_sector_start;
	int chain_length=0;
	int break_out = 0;

	DOLE LOGGER_log("%s:%d:OLE_follow_minichain:DEBUG: Starting at sector %d",FL, miniFAT_sector_start);

	if (miniFAT_sector_start < 0) return 0;

	do {
		unsigned int next_sector;

		DOLE LOGGER_log("%s:%d:OLE_follow_minichain:DEBUG: Requesting 4-byte value at '%d'",FL, ole->miniFAT +(LEN_ULONG *current_sector));
		if (ole->miniFAT +(LEN_ULONG *current_sector) > ole->miniFAT_limit) {
			DOLE LOGGER_log("%s:%d:OLE_follow_minichain:DEBUG: Requested location is out of bounds\n",FL);
			return 0;
		}

		next_sector = get_uint32((char*) ole->miniFAT +(LEN_ULONG *current_sector));

		DOLE LOGGER_log("%s:%d:OLE_follow_minichain:DEBUG: Current Msector(0x%0X:%d)->next(0x%0X:%d)\n", FL, current_sector, current_sector, next_sector, next_sector);

		/** Check for conditions that indicate we should stop traversing this chain **/

		/** 1. We cannot point to ourselves **/
		if (current_sector == next_sector) break;

		chain_length++;
		current_sector = next_sector;

		/** Test for non-positive type sector ID's **/
		switch (current_sector) {
			case OLE_SECTORID_MSAT:
			case OLE_SECTORID_SAT:
			case OLE_SECTORID_ENDOFCHAIN:
			case OLE_SECTORID_FREE:
				break_out=1;
				break;
			default:
				break_out=0;
		};

		DOLE LOGGER_log("%s:%d:OLE_follow_minichain:DEBUG: current sector = %d",FL,current_sector);
	} while ((break_out==0)&&(current_sector <= ole->last_sector ));

	DOLE LOGGER_log("%s:%d:OLE_follow_minichain:DEBUG: Done.  Chainlength=%d",FL, chain_length);

	return chain_length;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %r13d
movq %rdi, %r14
cmpl $0x0, 0x444(%rdi)
je 0xf0ef
leaq 0x9748(%rip), %rdi      # 0x18821
leaq 0x8d59(%rip), %rsi      # 0x17e39
movl $0x4a5, %edx            # imm = 0x4A5
movl %r13d, %ecx
xorl %eax, %eax
callq 0xc2a1
testl %r13d, %r13d
js 0xf217
leaq 0x8d3a(%rip), %r12      # 0x17e39
leaq 0x9753(%rip), %r15      # 0x18859
xorl %eax, %eax
movl %eax, %ebx
cmpl $0x0, 0x444(%r14)
je 0xf13a
leal (,%r13,4), %eax
movslq %eax, %rbp
movq 0x38(%r14), %rcx
addq %rbp, %rcx
movq %r15, %rdi
movq %r12, %rsi
movl $0x4ac, %edx            # imm = 0x4AC
xorl %eax, %eax
callq 0xc2a1
jmp 0xf145
leal (,%r13,4), %eax
movslq %eax, %rbp
addq 0x38(%r14), %rbp
cmpq 0x40(%r14), %rbp
ja 0xf1ef
movq %rbp, %rdi
callq 0x1082f
movl %eax, %ebp
cmpl $0x0, 0x444(%r14)
je 0xf189
movl %ebp, (%rsp)
leaq 0x9771(%rip), %rdi      # 0x188e2
movq %r12, %rsi
movl $0x4b4, %edx            # imm = 0x4B4
movl %r13d, %ecx
movl %r13d, %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0xc2a1
cmpl %ebp, %r13d
je 0xf1c7
cmpl $0x0, 0x444(%r14)
je 0xf1b0
leaq 0x9790(%rip), %rdi      # 0x1892f
movq %r12, %rsi
movl $0x4ca, %edx            # imm = 0x4CA
movl %ebp, %ecx
xorl %eax, %eax
callq 0xc2a1
cmpl $-0x5, %ebp
ja 0xf1c5
leal 0x1(%rbx), %eax
movl %ebp, %r13d
cmpl 0x10(%r14), %ebp
jle 0xf108
incl %ebx
cmpl $0x0, 0x444(%r14)
je 0xf219
leaq 0x978d(%rip), %rdi      # 0x18965
leaq 0x8c5a(%rip), %rsi      # 0x17e39
movl $0x4cd, %edx            # imm = 0x4CD
movl %ebx, %ecx
xorl %eax, %eax
callq 0xc2a1
jmp 0xf219
cmpl $0x0, 0x444(%r14)
je 0xf217
leaq 0x969b(%rip), %rdi      # 0x1889b
leaq 0x8c32(%rip), %rsi      # 0x17e39
xorl %ebx, %ebx
movl $0x4ae, %edx            # imm = 0x4AE
xorl %eax, %eax
callq 0xc2a1
jmp 0xf219
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq