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

	// iterate block positions
	int lg = trackbuf.CalculateByteSize(di.block[3].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 4:
				for (cp=0-lg; cp < bs; cp++) {
					pti->timebuf[pos+cp]-=55;
				}
				break;

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

			case 6:
				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), %rax
movq 0x1f8(%rax), %rax
movl 0x7c(%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 0x1036a
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)
subl $0x4, %eax
je 0x1027e
jmp 0x10267
movl -0x44(%rbp), %eax
subl $0x5, %eax
je 0x102b8
jmp 0x10271
movl -0x44(%rbp), %eax
subl $0x6, %eax
je 0x102f2
jmp 0x1032c
xorl %eax, %eax
subl -0x1c(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x102b6
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 0x10286
jmp 0x1032c
xorl %eax, %eax
subl -0x1c(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x102f0
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 0x102c0
jmp 0x1032c
xorl %eax, %eax
subl -0x1c(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x1032a
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 0x102fa
jmp 0x1032c
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 0x10356
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 0x101ff
xorl %eax, %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)