// File: OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsImageStd.cpp
// Function: CCapsImageStd::GenerateCLA2(DiskTrackInfo*, unsigned char*)
int CCapsImageStd::GenerateCLA2(PDISKTRACKINFO pti, PUBYTE buf)
{
	// set auto density
	GenerateAutoDensity(pti);

	// iterate block positions
	int lg = trackbuf.CalculateByteSize(di.block[di.blockcount - 1].gapbits);
	int pos=pti->sdpos, cp;

	for (unsigned blk=0; blk < pti->ci.blkcnt; blk++) {
		int bs = trackbuf.CalculateByteSize(di.block[blk].blockbits);
		int gs = trackbuf.CalculateByteSize(di.block[blk].gapbits);
		int xs=bs+gs;

		switch (blk) {
			case 0:
				for (cp=0-lg; cp < bs; cp++) {
					pti->timebuf[pos+cp]-=55;
				}
				break;

			case 1:
				for (cp=0-lg; cp < bs; cp++) {
					pti->timebuf[pos+cp]-=5;
				}
				break;

			case 2:
				for (cp=0-lg; cp < bs; cp++) {
					pti->timebuf[pos+cp]+=45;
				}
				break;
		}

		pos+=xs;
		if (pos >= pti->timecnt)
			pos-=pti->timecnt;
		lg=gs;
	}

	return imgeOk;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x40(%rbp)
movq -0x10(%rbp), %rsi
callq 0x100b0
movq -0x40(%rbp), %rcx
movq 0x1f8(%rcx), %rax
movl 0x204(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %edi
callq 0x5510
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movl 0xc0(%rax), %eax
movl %eax, -0x20(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x128(%rcx), %eax
jae 0x1054c
movq -0x40(%rbp), %rax
movq 0x1f8(%rax), %rax
movl -0x28(%rbp), %ecx
leaq (%rcx,%rcx,4), %rcx
movl (%rax,%rcx,8), %edi
callq 0x5510
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, -0x2c(%rbp)
movq 0x1f8(%rax), %rax
movl -0x28(%rbp), %ecx
leaq (%rcx,%rcx,4), %rcx
movl 0x4(%rax,%rcx,8), %edi
callq 0x5510
movl %eax, -0x30(%rbp)
movl -0x2c(%rbp), %eax
movl -0x30(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x34(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0x44(%rbp)
testl %eax, %eax
je 0x10460
jmp 0x10449
movl -0x44(%rbp), %eax
subl $0x1, %eax
je 0x1049a
jmp 0x10453
movl -0x44(%rbp), %eax
subl $0x2, %eax
je 0x104d4
jmp 0x1050e
xorl %eax, %eax
subl -0x1c(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x10498
movq -0x10(%rbp), %rax
movq 0xb0(%rax), %rax
movl -0x20(%rbp), %ecx
addl -0x24(%rbp), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
subl $0x37, %edx
movl %edx, (%rax,%rcx,4)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x10468
jmp 0x1050e
xorl %eax, %eax
subl -0x1c(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x104d2
movq -0x10(%rbp), %rax
movq 0xb0(%rax), %rax
movl -0x20(%rbp), %ecx
addl -0x24(%rbp), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
subl $0x5, %edx
movl %edx, (%rax,%rcx,4)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x104a2
jmp 0x1050e
xorl %eax, %eax
subl -0x1c(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x1050c
movq -0x10(%rbp), %rax
movq 0xb0(%rax), %rax
movl -0x20(%rbp), %ecx
addl -0x24(%rbp), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
addl $0x2d, %edx
movl %edx, (%rax,%rcx,4)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x104dc
jmp 0x1050e
movl -0x34(%rbp), %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0xa8(%rcx), %eax
jl 0x10538
movq -0x10(%rbp), %rax
movl 0xa8(%rax), %ecx
movl -0x20(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x20(%rbp)
movl -0x30(%rbp), %eax
movl %eax, -0x1c(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x103e2
xorl %eax, %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)