// File: /workspace/github_c_makefile_star2_O0/salextpuru[P]speccy-toochain/src/mktrd/mctrd.c
// Function: trdPush
void trdPush(char* ipath, char* fpath) {
	char buf[0x10000];
	trdFile hd = prepareFile(fpath, buf);
	if (hd.name[0] == 0) return;
	char img[0xa0000];
	memset(img, 0, 0xa0000);
	if (readfile(ipath, img, 0xa0000) < 0) {
		printf("Can't read image '%s'\n", ipath);
		return;
	}
	int files = img[0x8e4];			// test files count
	if (files > 127) {
		printf("Too many files in image\n");
		return;
	}
	int freesec = (img[0x8e5] & 0xff) | ((img[0x8e6] & 0xff) << 8);
	
	if (freesec < hd.slen) {		// test free sectors
		printf("No room for file\n");
		return;
	}
	int lastsec = img[0x8e1] & 0xff;
	int lasttrk = img[0x8e2] & 0xff;
	hd.sec = lastsec;
	hd.trk = lasttrk;
	files++;
	img[0x8e4] = files;
	freesec -= hd.slen;
	img[0x8e5] = freesec & 0xff;
	img[0x8e6] = ((freesec & 0xff00) >> 8);
	int diskType = img[0x8e3] & 0xff;
	char* ptr = img;
	while (*ptr)				// find 1st free descriptor
		ptr += 16;
	memcpy(ptr, (char*)&hd, 16);		// make new descriptor
	int secnum = ((hd.trk << 4) + hd.sec);	// free sector num
	// int fpos = (secnum << 8); 		// position in image
	secnum += hd.slen;
	img[0x8e1] = secnum & 0x0f;
	img[0x8e2] = ((secnum & 0xfff0) >> 4);
	trd_put_data(img, buf, hd.trk, hd.sec, hd.slen, diskType);
	// memcpy(img + fpos, buf, hd.slen << 8);	// copy file data
	savefile(ipath, img, 0xa0000);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0050, %rsp          # imm = 0xB0050
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x10010(%rbp), %rsi
callq 0x402020
movq %rax, -0x10020(%rbp)
movq %rdx, -0x10018(%rbp)
movsbl -0x10020(%rbp), %eax
cmpl $0x0, %eax
jne 0x4033c2
jmp 0x4035f8
leaq -0xb0020(%rbp), %rdi
xorl %esi, %esi
movl $0xa0000, %edx          # imm = 0xA0000
callq 0x4010a0
movq -0x8(%rbp), %rdi
leaq -0xb0020(%rbp), %rsi
movl $0xa0000, %edx          # imm = 0xA0000
callq 0x401810
cmpl $0x0, %eax
jge 0x403406
movq -0x8(%rbp), %rsi
leaq 0x9be5d(%rip), %rdi     # 0x49f257
movb $0x0, %al
callq 0x40eab0
jmp 0x4035f8
movsbl -0xaf73c(%rbp), %eax
movl %eax, -0xb0024(%rbp)
cmpl $0x7f, -0xb0024(%rbp)
jle 0x40342f
leaq 0x9be4b(%rip), %rdi     # 0x49f26e
movb $0x0, %al
callq 0x40eab0
jmp 0x4035f8
movsbl -0xaf73b(%rbp), %eax
andl $0xff, %eax
movsbl -0xaf73a(%rbp), %ecx
andl $0xff, %ecx
shll $0x8, %ecx
orl %ecx, %eax
movl %eax, -0xb0028(%rbp)
movl -0xb0028(%rbp), %eax
movzbl -0x10013(%rbp), %ecx
cmpl %ecx, %eax
jge 0x403477
leaq 0x9be1c(%rip), %rdi     # 0x49f287
movb $0x0, %al
callq 0x40eab0
jmp 0x4035f8
movsbl -0xaf73f(%rbp), %eax
andl $0xff, %eax
movl %eax, -0xb002c(%rbp)
movsbl -0xaf73e(%rbp), %eax
andl $0xff, %eax
movl %eax, -0xb0030(%rbp)
movl -0xb002c(%rbp), %eax
movb %al, -0x10012(%rbp)
movl -0xb0030(%rbp), %eax
movb %al, -0x10011(%rbp)
movl -0xb0024(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb0024(%rbp)
movl -0xb0024(%rbp), %eax
movb %al, -0xaf73c(%rbp)
movzbl -0x10013(%rbp), %ecx
movl -0xb0028(%rbp), %eax
subl %ecx, %eax
movl %eax, -0xb0028(%rbp)
movl -0xb0028(%rbp), %eax
andl $0xff, %eax
movb %al, -0xaf73b(%rbp)
movl -0xb0028(%rbp), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
movb %al, -0xaf73a(%rbp)
movsbl -0xaf73d(%rbp), %eax
andl $0xff, %eax
movl %eax, -0xb0034(%rbp)
leaq -0xb0020(%rbp), %rax
movq %rax, -0xb0040(%rbp)
movq -0xb0040(%rbp), %rax
cmpb $0x0, (%rax)
je 0x403548
movq -0xb0040(%rbp), %rax
addq $0x10, %rax
movq %rax, -0xb0040(%rbp)
jmp 0x403528
movq -0xb0040(%rbp), %rax
movq -0x10020(%rbp), %rcx
movq %rcx, (%rax)
movq -0x10018(%rbp), %rcx
movq %rcx, 0x8(%rax)
movzbl -0x10011(%rbp), %eax
shll $0x4, %eax
movzbl -0x10012(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0xb0044(%rbp)
movzbl -0x10013(%rbp), %eax
addl -0xb0044(%rbp), %eax
movl %eax, -0xb0044(%rbp)
movl -0xb0044(%rbp), %eax
andl $0xf, %eax
movb %al, -0xaf73f(%rbp)
movl -0xb0044(%rbp), %eax
andl $0xfff0, %eax           # imm = 0xFFF0
sarl $0x4, %eax
movb %al, -0xaf73e(%rbp)
leaq -0xb0020(%rbp), %rdi
leaq -0x10010(%rbp), %rsi
movzbl -0x10011(%rbp), %edx
movzbl -0x10012(%rbp), %ecx
movzbl -0x10013(%rbp), %r8d
movl -0xb0034(%rbp), %r9d
callq 0x4032a0
movq -0x8(%rbp), %rdi
leaq -0xb0020(%rbp), %rsi
movl $0xa0000, %edx          # imm = 0xA0000
callq 0x4018e0
addq $0xb0050, %rsp          # imm = 0xB0050
popq %rbp
retq
nopw %cs:(%rax,%rax)