// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x1c(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq $0x0, 0x18(%rax)
cmpl $0x0, -0x14(%rbp)
jge 0x1641b
movq $0x0, -0x8(%rbp)
jmp 0x1665e
movq -0x10(%rbp), %rax
cmpl $0x0, 0x444(%rax)
je 0x16445
movl -0x14(%rbp), %ecx
leaq 0xa4b8(%rip), %rdi      # 0x208ea
leaq 0x9812(%rip), %rsi      # 0x1fc4b
movl $0x52f, %edx            # imm = 0x52F
movb $0x0, %al
callq 0x117c0
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x15e00
movl %eax, -0x18(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x444(%rax)
je 0x1647e
movl -0x18(%rbp), %ecx
leaq 0xa4c0(%rip), %rdi      # 0x2092b
leaq 0x97d9(%rip), %rsi      # 0x1fc4b
movl $0x532, %edx            # imm = 0x532
movb $0x0, %al
callq 0x117c0
cmpl $0x0, -0x18(%rbp)
jle 0x1662f
movl -0x18(%rbp), %eax
movq -0x10(%rbp), %rcx
movl 0x264(%rcx), %ecx
shll %cl, %eax
cltq
movq -0x10(%rbp), %rcx
movq %rax, 0x18(%rcx)
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
shlq $0x0, %rdi
callq 0x2290
movq %rax, -0x28(%rbp)
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x164ec
movq -0x38(%rbp), %rcx
leaq 0xa498(%rip), %rdi      # 0x20964
leaq 0x9778(%rip), %rsi      # 0x1fc4b
movl $0x53c, %edx            # imm = 0x53C
movb $0x0, %al
callq 0x117c0
movq $0x0, -0x8(%rbp)
jmp 0x1665e
cmpq $0x0, -0x28(%rbp)
je 0x1662d
movl $0x0, -0x3c(%rbp)
movq -0x30(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x444(%rax)
je 0x16538
movl -0x3c(%rbp), %ecx
movl -0x1c(%rbp), %r8d
leaq 0xa482(%rip), %rdi      # 0x209a7
leaq 0x971f(%rip), %rsi      # 0x1fc4b
movl $0x549, %edx            # imm = 0x549
movb $0x0, %al
callq 0x117c0
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %esi
movq -0x30(%rbp), %rdx
callq 0x14ae0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, (%rax)
je 0x16566
movq $0x0, -0x8(%rbp)
jmp 0x1665e
movq -0x10(%rbp), %rax
movl 0x268(%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x48(%rbp), %rax
jbe 0x165db
cmpq $0x0, -0x28(%rbp)
je 0x165a7
movq -0x28(%rbp), %rdi
callq 0x2030
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x448(%rax)
je 0x165ce
leaq 0xa41e(%rip), %rdi      # 0x209d9
leaq 0x9689(%rip), %rsi      # 0x1fc4b
movl $0x555, %edx            # imm = 0x555
movb $0x0, %al
callq 0x117c0
movq $0x0, -0x8(%rbp)
jmp 0x1665e
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
movl -0x1c(%rbp), %eax
shll $0x2, %eax
cltq
addq %rax, %rdi
callq 0x181f0
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0x1c(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x1c(%rbp)
movb %al, -0x4d(%rbp)
jl 0x16620
movl -0x1c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x10(%rcx), %eax
setle %al
movb %al, -0x4d(%rbp)
movb -0x4d(%rbp), %al
testb $0x1, %al
jne 0x1650a
jmp 0x1662d
jmp 0x1662f
movq -0x10(%rbp), %rax
cmpl $0x0, 0x444(%rax)
je 0x16656
leaq 0xa3d7(%rip), %rdi      # 0x20a1a
leaq 0x9601(%rip), %rsi      # 0x1fc4b
movl $0x55f, %edx            # imm = 0x55F
movb $0x0, %al
callq 0x117c0
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)