// File: /workspace/github_c_makefile_star2_O1/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, %r14d
movq %rdi, %rbx
cmpl $0x0, 0x444(%rdi)
je 0xf8e2
leaq 0x9f65(%rip), %rdi      # 0x19831
leaq 0x9576(%rip), %rsi      # 0x18e49
movl $0x4a5, %edx            # imm = 0x4A5
movl %r14d, %ecx
xorl %eax, %eax
callq 0xc960
testl %r14d, %r14d
js 0xfa2f
xorl %ebp, %ebp
cmpl $0x0, 0x444(%rbx)
je 0xf91f
leal (,%r14,4), %eax
movslq %eax, %rcx
addq 0x38(%rbx), %rcx
leaq 0x9f5d(%rip), %rdi      # 0x19869
leaq 0x9536(%rip), %rsi      # 0x18e49
movl $0x4ac, %edx            # imm = 0x4AC
xorl %eax, %eax
callq 0xc960
leal (,%r14,4), %eax
movslq %eax, %rdi
addq 0x38(%rbx), %rdi
cmpq 0x40(%rbx), %rdi
jbe 0xf969
movl $0x1, %r12d
movb $0x1, %r15b
cmpl $0x0, 0x444(%rbx)
je 0xf9ec
leaq 0x9f5a(%rip), %rdi      # 0x198ab
leaq 0x94f1(%rip), %rsi      # 0x18e49
movl $0x4ae, %edx            # imm = 0x4AE
xorl %eax, %eax
callq 0xc960
jmp 0xf9ec
callq 0x110fb
movl %eax, %r13d
cmpl $0x0, 0x444(%rbx)
je 0xf9a1
movl %r13d, (%rsp)
leaq 0x9f6d(%rip), %rdi      # 0x198f2
leaq 0x94bd(%rip), %rsi      # 0x18e49
movl $0x4b4, %edx            # imm = 0x4B4
movl %r14d, %ecx
movl %r14d, %r8d
movl %r13d, %r9d
xorl %eax, %eax
callq 0xc960
cmpl %r13d, %r14d
jne 0xf9b1
movl $0x2, %r12d
movb $0x1, %r15b
jmp 0xf9ec
incl %ebp
cmpl $-0x4, %r13d
setb %r15b
cmpl $0x0, 0x444(%rbx)
je 0xf9e6
xorl %r12d, %r12d
leaq 0x9f71(%rip), %rdi      # 0x1993f
leaq 0x9474(%rip), %rsi      # 0x18e49
movl $0x4ca, %edx            # imm = 0x4CA
movl %r13d, %ecx
xorl %eax, %eax
callq 0xc960
jmp 0xf9e9
xorl %r12d, %r12d
movl %r13d, %r14d
testl %r12d, %r12d
jne 0xfa02
testb %r15b, %r15b
je 0xfa08
cmpl 0x10(%rbx), %r14d
jle 0xf8ed
jmp 0xfa08
cmpl $0x2, %r12d
jne 0xfa2f
cmpl $0x0, 0x444(%rbx)
je 0xfa31
leaq 0x9f5d(%rip), %rdi      # 0x19975
leaq 0x942a(%rip), %rsi      # 0x18e49
movl $0x4cd, %edx            # imm = 0x4CD
movl %ebp, %ecx
xorl %eax, %eax
callq 0xc960
jmp 0xfa31
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq