// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x444(%rax)
je 0x1602d
movl -0x14(%rbp), %ecx
leaq 0xa619(%rip), %rdi      # 0x20633
leaq 0x9c2a(%rip), %rsi      # 0x1fc4b
movl $0x4a5, %edx            # imm = 0x4A5
movb $0x0, %al
callq 0x117c0
cmpl $0x0, -0x14(%rbp)
jge 0x1603f
movl $0x0, -0x4(%rbp)
jmp 0x161d6
jmp 0x16041
movq -0x10(%rbp), %rax
cmpl $0x0, 0x444(%rax)
je 0x1607b
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rcx
movl -0x18(%rbp), %eax
shll $0x2, %eax
cltq
addq %rax, %rcx
leaq 0xa603(%rip), %rdi      # 0x2066b
leaq 0x9bdc(%rip), %rsi      # 0x1fc4b
movl $0x4ac, %edx            # imm = 0x4AC
movb $0x0, %al
callq 0x117c0
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movl -0x18(%rbp), %ecx
shll $0x2, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq -0x10(%rbp), %rcx
cmpq 0x40(%rcx), %rax
jbe 0x160cc
movq -0x10(%rbp), %rax
cmpl $0x0, 0x444(%rax)
je 0x160c0
leaq 0xa600(%rip), %rdi      # 0x206ad
leaq 0x9b97(%rip), %rsi      # 0x1fc4b
movl $0x4ae, %edx            # imm = 0x4AE
movb $0x0, %al
callq 0x117c0
movl $0x0, -0x4(%rbp)
jmp 0x161d6
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
movl -0x18(%rbp), %eax
shll $0x2, %eax
cltq
addq %rax, %rdi
callq 0x181f0
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x444(%rax)
je 0x1611f
movl -0x18(%rbp), %ecx
movl -0x18(%rbp), %r8d
movl -0x24(%rbp), %r9d
movl -0x24(%rbp), %eax
leaq 0xa5eb(%rip), %rdi      # 0x206f4
leaq 0x9b3b(%rip), %rsi      # 0x1fc4b
movl $0x4b4, %edx            # imm = 0x4B4
movl %eax, (%rsp)
movb $0x0, %al
callq 0x117c0
movl -0x18(%rbp), %eax
cmpl -0x24(%rbp), %eax
jne 0x16129
jmp 0x161a6
movl -0x1c(%rbp), %eax
incl %eax
movl %eax, -0x1c(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
addl $0x4, %eax
subl $0x3, %eax
ja 0x1614d
jmp 0x16144
movl $0x1, -0x20(%rbp)
jmp 0x16154
movl $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x444(%rax)
je 0x1617e
movl -0x18(%rbp), %ecx
leaq 0xa5d6(%rip), %rdi      # 0x20741
leaq 0x9ad9(%rip), %rsi      # 0x1fc4b
movl $0x4ca, %edx            # imm = 0x4CA
movb $0x0, %al
callq 0x117c0
jmp 0x16180
xorl %eax, %eax
cmpl $0x0, -0x20(%rbp)
movb %al, -0x25(%rbp)
jne 0x1619b
movl -0x18(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x10(%rcx), %eax
setle %al
movb %al, -0x25(%rbp)
movb -0x25(%rbp), %al
testb $0x1, %al
jne 0x16041
movq -0x10(%rbp), %rax
cmpl $0x0, 0x444(%rax)
je 0x161d0
movl -0x1c(%rbp), %ecx
leaq 0xa5ba(%rip), %rdi      # 0x20777
leaq 0x9a87(%rip), %rsi      # 0x1fc4b
movl $0x4cd, %edx            # imm = 0x4CD
movb $0x0, %al
callq 0x117c0
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nop