// File: OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsImage.cpp
// Function: CCapsImage::UpdateDump()
int CCapsImage::UpdateDump()
{
	PDISKTRACKINFO pti = di.pdt;

	// get actual revolution number, restart after the last revolution
	int rev = dii.nextrev % pti->rawtrackcnt;

	// true if all the available revolutions should be returned
	int allrev = (pti->trackcnt == pti->rawtrackcnt);

	// if all revolutions used the used data must be revolution#0
	if (allrev)
		rev = 0;

	// save the real revolution number
	dii.realrev = rev;

	// set data buffer
	pti->trackbuf = pti->trackdata[rev];
	pti->tracklen = allrev ? pti->rawlen : pti->tracksize[rev];
	pti->trackbc = pti->tracklen << 3;
	pti->singletrackbc = pti->trackbc;

	// timing information is the same size as the current data, rev#0 for all revolutions
	pti->timecnt = pti->tracksize[rev];

	// the real size of the sampled timings
	int rawsize = pti->rawtimecnt;

	// the amount of timing to be copied track size or real size - whichever is shorter
	int tsize = std::min(rawsize, pti->timecnt);

	// copy the timing
	memcpy(pti->timebuf, pti->rawtimebuf, tsize*sizeof(UDWORD));

	// If sampled timing is shorter, fill the remaining area with 1000, default value
	// This does not change the average, since the previous data adds up to an average of 1000
	int pos;
	for (pos = tsize; pos < pti->timecnt; pos++) {
			pti->timebuf[pos] = 1000;
	}

	// reset alternate density
	pti->timebuf[pos] = 0;

	// density map conversion
	if (di.flag & DI_LOCK_DENALT)
		ConvertDensity(pti);

	UpdateImage(rev);

	return imgeOk;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x30(%rbp)
movq 0x210(%rax), %rcx
movq %rcx, -0x10(%rbp)
movl 0x3c(%rax), %eax
movq -0x10(%rbp), %rcx
cltd
idivl 0x2c(%rcx)
movl %edx, -0x14(%rbp)
movq -0x10(%rbp), %rax
movl 0x28(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x2c(%rcx), %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0xdef3
movl $0x0, -0x14(%rbp)
movq -0x30(%rbp), %rax
movl -0x14(%rbp), %ecx
movl %ecx, 0x44(%rax)
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq 0x58(%rax,%rcx,8), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x48(%rax)
cmpl $0x0, -0x18(%rbp)
je 0xdf24
movq -0x10(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, -0x34(%rbp)
jmp 0xdf36
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movl 0x80(%rax,%rcx,4), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x50(%rax)
movq -0x10(%rbp), %rax
movl 0x50(%rax), %ecx
shll $0x3, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xe8(%rax)
movq -0x10(%rbp), %rax
movl 0xe8(%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xec(%rax)
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movl 0x80(%rax,%rcx,4), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xa8(%rax)
movq -0x10(%rbp), %rax
movl 0x34(%rax), %eax
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rsi
addq $0xa8, %rsi
leaq -0x1c(%rbp), %rdi
callq 0xe0c0
movl (%rax), %eax
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0xb0(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rsi
movslq -0x20(%rbp), %rdx
shlq $0x2, %rdx
callq 0x41a0
movl -0x20(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0xa8(%rcx), %eax
jge 0xdffa
movq -0x10(%rbp), %rax
movq 0xb0(%rax), %rax
movslq -0x24(%rbp), %rcx
movl $0x3e8, (%rax,%rcx,4)   # imm = 0x3E8
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xdfca
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0xb0(%rcx), %rcx
movslq -0x24(%rbp), %rdx
movl $0x0, (%rcx,%rdx,4)
movl 0x208(%rax), %eax
andq $0x400, %rax            # imm = 0x400
cmpq $0x0, %rax
je 0xe033
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x10cc0
movq -0x30(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0xee20
xorl %eax, %eax
addq $0x40, %rsp
popq %rbp
retq
nopw (%rax,%rax)