// File: OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/DiskImage.cpp
// Function: CDiskImage::MapTrack(int, int)
PDISKTRACKINFO CDiskImage::MapTrack(int cylinder, int head)
{
	// ignore wrong cylinder
	if (cylinder<0 || cylinder>=MAX_CYLINDER)
		return NULL;

	// ignore wrong head
	if (head<0 || head>=MAX_HEAD)
		return NULL;

	// return if available
	PDISKTRACKINFO pt=GetTrack(cylinder, head);
	if (pt)
		return pt;

	if (!dti) {
		// first allocation
		dtihed=MAX_HEAD;
		dticnt=((cylinder*dtihed)/DEF_TRACKINFO+1)*DEF_TRACKINFO;
		dticyl=dticnt/MAX_HEAD;
		dti=new DiskTrackInfo[dticnt];
		memset(dti, 0, sizeof(DiskTrackInfo)*dticnt);
	} else {
		// reallocation
		int scnt=dticnt;
		PDISKTRACKINFO sti=dti;
		dticnt=((cylinder*dtihed)/DEF_TRACKINFO+1)*DEF_TRACKINFO;
		dticyl=dticnt/MAX_HEAD;
		dti=new DiskTrackInfo[dticnt];
		memcpy(dti, sti, sizeof(DiskTrackInfo)*scnt);
		memset(dti+scnt, 0, sizeof(DiskTrackInfo)*(dticnt-scnt));
		delete [] sti;
	}

	return &dti[cylinder*dtihed+head];
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpl $0x0, -0x14(%rbp)
jl 0x17fb9
cmpl $0x10000, -0x14(%rbp)   # imm = 0x10000
jl 0x17fc6
movq $0x0, -0x8(%rbp)
jmp 0x181d6
cmpl $0x0, -0x18(%rbp)
jl 0x17fd2
cmpl $0x2, -0x18(%rbp)
jl 0x17fdf
movq $0x0, -0x8(%rbp)
jmp 0x181d6
movq -0x38(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
callq 0x182b0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x18006
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x181d6
movq -0x38(%rbp), %rax
cmpq $0x0, 0xb0(%rax)
jne 0x180bb
movq -0x38(%rbp), %rcx
movl $0x2, 0xa8(%rcx)
movl -0x14(%rbp), %eax
imull 0xa8(%rcx), %eax
movl $0x800, %ecx            # imm = 0x800
cltd
idivl %ecx
movl %eax, %ecx
movq -0x38(%rbp), %rax
addl $0x1, %ecx
shll $0xb, %ecx
movl %ecx, 0xa0(%rax)
movl 0xa0(%rax), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0xa4(%rax)
movslq 0xa0(%rax), %rax
movl $0x148, %ecx            # imm = 0x148
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x4040
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0xb0(%rax)
movq 0xb0(%rax), %rdi
movslq 0xa0(%rax), %rax
imulq $0x148, %rax, %rdx      # imm = 0x148
xorl %esi, %esi
callq 0x4140
jmp 0x181ad
movq -0x38(%rbp), %rcx
movl 0xa0(%rcx), %eax
movl %eax, -0x24(%rbp)
movq 0xb0(%rcx), %rax
movq %rax, -0x30(%rbp)
movl -0x14(%rbp), %eax
imull 0xa8(%rcx), %eax
movl $0x800, %ecx            # imm = 0x800
cltd
idivl %ecx
movl %eax, %ecx
movq -0x38(%rbp), %rax
addl $0x1, %ecx
shll $0xb, %ecx
movl %ecx, 0xa0(%rax)
movl 0xa0(%rax), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0xa4(%rax)
movslq 0xa0(%rax), %rax
movl $0x148, %ecx            # imm = 0x148
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x4040
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0xb0(%rax)
movq 0xb0(%rax), %rdi
movq -0x30(%rbp), %rsi
movslq -0x24(%rbp), %rax
imulq $0x148, %rax, %rdx      # imm = 0x148
callq 0x41a0
movq -0x38(%rbp), %rax
movq 0xb0(%rax), %rdi
movslq -0x24(%rbp), %rcx
imulq $0x148, %rcx, %rcx      # imm = 0x148
addq %rcx, %rdi
movl 0xa0(%rax), %eax
subl -0x24(%rbp), %eax
cltq
imulq $0x148, %rax, %rdx      # imm = 0x148
xorl %esi, %esi
callq 0x4140
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
je 0x181ab
movq -0x40(%rbp), %rdi
callq 0x42e0
jmp 0x181ad
movq -0x38(%rbp), %rdx
movq 0xb0(%rdx), %rax
movl -0x14(%rbp), %ecx
imull 0xa8(%rdx), %ecx
addl -0x18(%rbp), %ecx
movslq %ecx, %rcx
imulq $0x148, %rcx, %rcx      # imm = 0x148
addq %rcx, %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq