// File: /workspace/github_c_makefile_star2_O2/salextpuru[P]speccy-toochain/src/mktrd/mktrd.cpp
// Function: pack(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void pack(const string fextra, const string aname) {
	//
	bool hobeta=false;	//
	unsigned char hhob[17]; // ��������� HOBETA
	string fname,fext;
	unsigned short start=0;
	//
	size_t dotpos = fextra.rfind('.');		// get ZX filename+ext (8+1)
	// HOBETA ��� ���?
	if(fextra[dotpos+1]=='$'){
		hobeta = true;
	}
	//
	if (dotpos==string::npos) {fname = fextra.substr(0,8); fext = "C";}
	else {fname = fextra.substr(0,min((int)dotpos,8));
		if (dotpos>=fextra.size()-1) fext=" "; else fext=fextra.substr(dotpos+1,1);
	}
	// ��� �����
	fname.resize(8,0x20);
	unsigned char *inbuf;
	int iflen;
	if(hobeta){
		inbuf = new unsigned char[0xff11]; // +17 ����
		iflen = readfile(fextra,inbuf,0xff11);
	}
	else{
		inbuf = new unsigned char[0xff00];
		iflen = readfile(fextra,inbuf,0xff00);
	}
	if (iflen==-1) {cout<<"Can't read file"<<endl; return;}
	if (iflen==0) {cout<<"Input file is too long"<<endl; return;}
	// ��� HOBETA 
	if(hobeta){
		// ��������� ��������� ��������
		memcpy(hhob,inbuf,17);
		// � inbuf - ������ ������
		memmove(inbuf,inbuf+17,iflen-17);
		iflen=iflen-17;
		// ���
		memcpy( (void*)(fname.c_str()), hhob, 8);
		// ����������
		fext = hhob[8];
	}
	// ������ ��������
	unsigned char *obuf = new unsigned char[0xa0000];
	int olen=readfile(aname,obuf);
	if (olen<1) {cout<<"Can't read output file"<<endl; return;}
	//
	int mode=testsig(obuf,olen);
	unsigned char seclen;
	if(hobeta){
		cout << "HOBETA" << endl;
		seclen = hhob[13]; // ������ � �������� �� hobeta
		start = hhob[9] + (hhob[10]<<8);
	}
	else{
		cout << "NON HOBETA" << endl;
		seclen = ((iflen & 0xff00)>>8); if ((iflen & 0xff) != 0) seclen++;	// sectors len
	}
	
	cout<<"file len: "<<(int)iflen << " " << (int)seclen << endl;
	
	unsigned char lastsec,lasttrk,files;
	unsigned int freesec,fpos,secnum;
	unsigned char* ptr = (unsigned char*)obuf;
	FILE *ofile;
	switch (mode) {
		case 1:
			files=*(obuf+8); if (files>127) {cout<<"Too many files in image"<<endl; break;}
			(*(obuf+8))++;
			ofile = fopen(aname.c_str(),"wb");
			if (!ofile) {cout<<"Can't write to file"<<endl; break;}
			freesec=9+14*files;				// old catalog len
			fwrite(obuf,1,freesec,ofile);			// save old catalog
			ptr = new unsigned char[14];
			makedsc(ptr,fname,fext,iflen,seclen,start);		// make 14bytes-len descriptor
			fwrite(ptr,1,14,ofile);				// write it
			fwrite(obuf+freesec,1,olen-freesec,ofile);	// write old data
			fwrite(inbuf,1,iflen,ofile);			// write new data
			if ((iflen&0xff)!=0) fwrite(inbuf,1,0x100-(iflen&0xff),ofile);	// write to end of sector
			fclose(ofile);
			break;
		case 2:
			files = *(obuf+0x8e4);
			if (files > 127) {
				cout << "Too many files in image" << endl;
				break;
			}
			freesec = *(obuf+0x8e5) + (*(obuf+0x8e6) << 8);
			if (freesec < seclen) {
				cout << "No room for file" << endl;
				break;
			}
	cout << "freesec = " << (int)freesec << endl;
			lastsec = *(obuf+0x8e1);
			lasttrk = *(obuf+0x8e2);
			files++;
			*(obuf+0x8e4) = files;
			freesec -= seclen;
			*(obuf+0x8e5) = freesec & 0xff;
			*(obuf+0x8e6) = ((freesec & 0xff00) >> 8);
			while (*ptr != 0) ptr += 16;
			makedsc(ptr,fname,fext,iflen,seclen,start);
			*(ptr+14) = lastsec;
			*(ptr+15) = lasttrk;
			secnum = ((lasttrk << 4) + lastsec);	// free sector abs num
			fpos = (secnum << 8); 			// (lasttrk<<12)+(lastsec<<8);
			secnum += seclen;
			lastsec = secnum & 15;
			lasttrk = ((secnum & 0xfff0) >> 4);
//			lastsec += (seclen & 15);
//			lasttrk+=((seclen&0xf0)>>4);
//			if (lastsec>15) {lastsec-=16; lasttrk++;}
			*(obuf+0x8e1) = lastsec;
			*(obuf+0x8e2) = lasttrk;
			memcpy(obuf+fpos,inbuf,iflen);
//	throw(0);
			savefile(aname,obuf,0xa0000);
			break;
		default: cout<<"Unknown image format"<<endl; break;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movq %rsi, -0x48(%rbp)
movq %rdi, %rbx
leaq -0x60(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
leaq -0x80(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
pushq $0x2e
popq %rsi
pushq $-0x1
popq %rdx
callq 0x476bc0
movq %rax, %r15
movq %rax, %r14
movq (%rbx), %rax
incq %r14
movb (%rax,%r14), %r13b
jne 0x404a2e
leaq -0xb0(%rbp), %rdi
pushq $0x8
popq %rcx
movq %rbx, %rsi
xorl %edx, %edx
callq 0x4778d0
leaq -0x70(%rbp), %rdi
leaq -0xb0(%rbp), %r14
movq %r14, %rsi
callq 0x474f70
movq %r14, %rdi
callq 0x474f20
leaq 0x18b037(%rip), %rsi    # 0x58fa60
jmp 0x404ab0
cmpl $0x8, %r15d
pushq $0x8
popq %rax
cmovll %r15d, %eax
movslq %eax, %rcx
leaq -0xb0(%rbp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x4778d0
leaq -0x70(%rbp), %rdi
leaq -0xb0(%rbp), %r12
movq %r12, %rsi
callq 0x474f70
movq %r12, %rdi
callq 0x474f20
movq 0x8(%rbx), %rax
decq %rax
cmpq %rax, %r15
jae 0x404aa9
leaq -0xb0(%rbp), %rdi
pushq $0x1
popq %rcx
movq %rbx, %rsi
movq %r14, %rdx
callq 0x4778d0
leaq -0x90(%rbp), %rdi
leaq -0xb0(%rbp), %r14
movq %r14, %rsi
callq 0x474f70
movq %r14, %rdi
callq 0x474f20
jmp 0x404abc
leaq 0x179903(%rip), %rsi    # 0x57e3b3
leaq -0x90(%rbp), %rdi
callq 0x475ed0
leaq -0x70(%rbp), %rdi
pushq $0x8
popq %rsi
pushq $0x20
popq %rdx
callq 0x475b20
cmpb $0x24, %r13b
jne 0x404afd
movl $0xff11, %edi           # imm = 0xFF11
callq 0x4068a0
movq %rax, %r15
leaq -0x1b0(%rbp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x4776b0
movq %r12, %rdi
movq %r15, %rsi
movl $0xff11, %edx           # imm = 0xFF11
jmp 0x404b27
movl $0xff00, %edi           # imm = 0xFF00
callq 0x4068a0
movq %rax, %r15
leaq -0x190(%rbp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x4776b0
movq %r12, %rdi
movq %r15, %rsi
movl $0xff00, %edx           # imm = 0xFF00
callq 0x404768
movl %eax, %ebx
movq %r12, %rdi
callq 0x474f20
cmpl $-0x1, %ebx
je 0x404b48
testl %ebx, %ebx
jne 0x404b8a
leaq 0x1794f4(%rip), %rsi    # 0x57e03a
jmp 0x404b4f
leaq 0x1794db(%rip), %rsi    # 0x57e02a
movq $0x5ec460, %rdi         # imm = 0x5EC460
callq 0x46eb50
movq %rax, %rdi
callq 0x46e430
leaq -0x90(%rbp), %rdi
callq 0x474f20
leaq -0x70(%rbp), %rdi
callq 0x474f20
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x24, %r13b
jne 0x404bdb
movq (%r15), %r12
movl %ebx, %eax
movsbl 0x8(%r15), %ebx
movb 0x9(%r15), %cl
movb %cl, -0x2a(%rbp)
movb 0xa(%r15), %cl
movb %cl, -0x29(%rbp)
movb 0xd(%r15), %cl
movl %ecx, -0x40(%rbp)
leaq 0x11(%r15), %rsi
addl $-0x11, %eax
movl %eax, -0x30(%rbp)
movslq %eax, %rdx
movq %r15, %rdi
callq 0x401068
movq -0x70(%rbp), %rax
movq %r12, (%rax)
leaq -0x90(%rbp), %rdi
movl %ebx, %esi
callq 0x475ab0
jmp 0x404be1
movl %ebx, -0x30(%rbp)
movl %eax, -0x40(%rbp)
movl $0xa0000, %edi          # imm = 0xA0000
callq 0x4068a0
movq %rax, -0x38(%rbp)
leaq -0x170(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x4776b0
leaq -0x170(%rbp), %r12
movq %r12, %rdi
movq -0x38(%rbp), %r14
movq %r14, %rsi
movl $0xa0000, %edx          # imm = 0xA0000
callq 0x404768
movl %eax, %ebx
movq %r12, %rdi
callq 0x474f20
testl %ebx, %ebx
jle 0x404c70
movq %r14, %rdi
movl %ebx, %esi
callq 0x404848
movl %eax, %r12d
cmpb $0x24, %r13b
movq %r15, -0x50(%rbp)
jne 0x404c90
movq $0x5ec460, %rdi         # imm = 0x5EC460
leaq 0x17941f(%rip), %rsi    # 0x57e06c
callq 0x46eb50
movl -0x40(%rbp), %r14d
movq %rax, %rdi
callq 0x46e430
movzbl -0x2a(%rbp), %eax
movzbl -0x29(%rbp), %r15d
shll $0x8, %r15d
orl %eax, %r15d
jmp 0x404cbe
movq $0x5ec460, %rdi         # imm = 0x5EC460
leaq 0x1793d3(%rip), %rsi    # 0x57e051
callq 0x46eb50
movq %rax, %rdi
callq 0x46e430
jmp 0x404b63
movq $0x5ec460, %rdi         # imm = 0x5EC460
leaq 0x1793ca(%rip), %rsi    # 0x57e068
callq 0x46eb50
movq %rax, %rdi
callq 0x46e430
movl -0x30(%rbp), %eax
movl %eax, %r14d
shrl $0x8, %r14d
cmpb $0x1, %al
sbbb $-0x1, %r14b
xorl %r15d, %r15d
movq $0x5ec460, %rdi         # imm = 0x5EC460
leaq 0x1793a7(%rip), %rsi    # 0x57e073
callq 0x46eb50
movq %rax, %rdi
movl -0x30(%rbp), %esi
callq 0x46eef0
leaq 0x1796d0(%rip), %rsi    # 0x57e3b3
movq %rax, %rdi
callq 0x46eb50
movzbl %r14b, %r13d
movq %rax, %rdi
movl %r13d, %esi
callq 0x46eef0
movq %rax, %rdi
callq 0x46e430
cmpb $0x2, %r12b
je 0x404e55
movzbl %r12b, %eax
cmpl $0x1, %eax
jne 0x404e7f
movq %rbx, -0x40(%rbp)
movl %r13d, %ebx
movq -0x38(%rbp), %r13
movzbl 0x8(%r13), %r12d
testb %r12b, %r12b
js 0x404e8b
leal 0x1(%r12), %eax
movb %al, 0x8(%r13)
movq -0x48(%rbp), %rax
movq (%rax), %rdi
leaq 0x1792be(%rip), %rsi    # 0x57e007
callq 0x4d48c0
testq %rax, %rax
je 0x404fd9
movq %rax, %r14
imulq $0xe, %r12, %rax
addq $0x9, %rax
pushq $0x1
popq %rsi
movq %r13, %rdi
movq %rax, %r13
movq %rax, %rdx
movq %r14, %rcx
callq 0x4d4d40
pushq $0xe
popq %rdi
callq 0x4068a0
movl %ebx, -0x48(%rbp)
movq %rax, %r12
leaq -0xf0(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x4776b0
leaq -0x110(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x4776b0
movq -0x40(%rbp), %rbx
movzwl %r15w, %r9d
leaq -0xf0(%rbp), %rsi
leaq -0x110(%rbp), %rdx
movq %r12, %rdi
movl -0x30(%rbp), %r15d
movl %r15d, %ecx
movl -0x48(%rbp), %r8d
callq 0x404946
leaq -0x110(%rbp), %rdi
callq 0x474f20
leaq -0xf0(%rbp), %rdi
callq 0x474f20
pushq $0x1
popq %rsi
pushq $0xe
popq %rdx
movq %r12, %rdi
movq %rsi, %r12
movq %r14, %rcx
callq 0x4d4d40
movq -0x38(%rbp), %rdi
addq %r13, %rdi
subl %r13d, %ebx
movq %r12, %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x4d4d40
movslq %r15d, %rdx
movq -0x50(%rbp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rcx
callq 0x4d4d40
andl $0xff, %r15d
je 0x404e48
movl $0x100, %edx            # imm = 0x100
subl %r15d, %edx
pushq $0x1
popq %rsi
movq %rbx, %rdi
movq %r14, %rcx
callq 0x4d4d40
movq %r14, %rdi
callq 0x4d4190
jmp 0x404b63
movq -0x38(%rbp), %rax
movb 0x8e4(%rax), %r12b
testb %r12b, %r12b
js 0x404e8b
movzwl 0x8e5(%rax), %ebx
movl %ebx, %eax
subl %r13d, %eax
jae 0x404e97
leaq 0x179230(%rip), %rsi    # 0x57e0aa
jmp 0x404fe0
leaq 0x179240(%rip), %rsi    # 0x57e0c6
jmp 0x404fe0
leaq 0x1791ec(%rip), %rsi    # 0x57e07e
jmp 0x404fe0
movl %eax, %r14d
movq $0x5ec460, %rdi         # imm = 0x5EC460
leaq 0x179213(%rip), %rsi    # 0x57e0bb
callq 0x46eb50
movq %rax, %rdi
movl %ebx, %esi
callq 0x46eef0
movq %rax, %rdi
callq 0x46e430
movq -0x38(%rbp), %rdx
movb 0x8e1(%rdx), %al
movb 0x8e2(%rdx), %cl
movzbl %al, %eax
movl %eax, -0x40(%rbp)
movzbl %cl, %ebx
incb %r12b
movb %r12b, 0x8e4(%rdx)
movl %r14d, %eax
movb %al, 0x8e5(%rdx)
movb %ah, 0x8e6(%rdx)
leaq -0x10(%rdx), %r12
cmpb $0x0, 0x10(%r12)
leaq 0x10(%r12), %r12
jne 0x404ef5
leaq -0xd0(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x4776b0
movl %r13d, %r14d
leaq -0x150(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x4776b0
movzwl %r15w, %r9d
leaq -0xd0(%rbp), %rsi
leaq -0x150(%rbp), %r13
movq %r12, %rdi
movq %r13, %rdx
movl -0x30(%rbp), %r15d
movl %r15d, %ecx
movl %r14d, %r8d
callq 0x404946
movq %r13, %rdi
callq 0x474f20
leaq -0xd0(%rbp), %rdi
callq 0x474f20
movl -0x40(%rbp), %eax
movb %al, 0xe(%r12)
movb %bl, 0xf(%r12)
shll $0x4, %ebx
addl %eax, %ebx
movl %ebx, %eax
shll $0x8, %eax
addl %r14d, %ebx
movl %ebx, %ecx
andb $0xf, %cl
shrl $0x4, %ebx
movq -0x38(%rbp), %rdi
movb %cl, 0x8e1(%rdi)
movb %bl, 0x8e2(%rdi)
addq %rax, %rdi
movslq %r15d, %rdx
movq -0x50(%rbp), %rsi
callq 0x401110
leaq -0x130(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x4776b0
leaq -0x130(%rbp), %r14
movq %r14, %rdi
movq -0x38(%rbp), %rsi
movl $0xa0000, %edx          # imm = 0xA0000
callq 0x4047f7
movq %r14, %rdi
callq 0x474f20
jmp 0x404b63
leaq 0x1790b6(%rip), %rsi    # 0x57e096
movq $0x5ec460, %rdi         # imm = 0x5EC460
callq 0x46eb50
movq %rax, %rdi
callq 0x46e430
jmp 0x404b63
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
jmp 0x40500f
movq %rax, %rbx
leaq -0xf0(%rbp), %rdi
callq 0x474f20
jmp 0x405029
jmp 0x405026
jmp 0x405026
jmp 0x405026
jmp 0x405026
jmp 0x405026
jmp 0x405026
jmp 0x405026
jmp 0x405026
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x474f20
leaq -0x70(%rbp), %rdi
callq 0x474f20
movq %rbx, %rdi
callq 0x4a7100