// File: /workspace/github_c_makefile_star2_O1/salextpuru[P]speccy-toochain/src/mktrd/mctrd.c
// Function: sclPush
void sclPush(char* ipath, char* fpath) {
	char buf[0x10000];
	trdFile hd = prepareFile(fpath, buf);
	if (hd.name[0] == 0) return;
	FILE* file = fopen(ipath, "rb");
	if (!file) {
		printf("Can't open image '%s'\n", ipath);
		return;
	}
	char bufH[9];				// head
	fread(bufH, 9, 1, file);
	int cnt = bufH[8];
	int clen = cnt * 14;
	char* bufC = (char*)malloc(clen);	// catalog
	fread(bufC, clen, 1, file);
	int dsiz = 0;
	for (int i = 0; i < cnt; i++) {
		dsiz += bufC[14 * i + 13] & 0xff;
	}
	dsiz <<= 8;
	char* bufD = (char*)malloc(dsiz);	// data
	fread(bufD, dsiz, 1, file);
	fclose(file);
	if ((unsigned)bufH[8] > 127) {
		printf("Too much files in image\n");
	} else {
		bufH[8]++;
//		trdFile hd = makeDSC(fpath, ilen);
		file = fopen(ipath, "wb");
		if (!file) {
			printf("Can't open image '%s' for writing\n",ipath);
		} else {
			fwrite(bufH, 9, 1, file);		// header
			fwrite(bufC, clen, 1, file);		// old catalog
			fwrite((char*)&hd, 14, 1, file);	// new DSC
			fwrite(bufD, dsiz, 1, file);		// old data
			fwrite(buf, hd.slen << 8, 1, file);	// new data
			int crc = getSum(bufH, 9);
			crc += getSum(bufC, clen);
			crc += getSum((char*)&hd, 14);
			crc += getSum(bufD, dsiz);
			crc += getSum(buf, hd.slen << 8);
			fputi(file, crc);
			fclose(file);
		}
	}
	free(bufC);
	free(bufD);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10048, %rsp          # imm = 0x10048
movq %rsi, %rax
movq %rdi, %rbx
leaq -0x10070(%rbp), %rsi
movq %rax, %rdi
callq 0x401df9
movq %rax, -0x68(%rbp)
movq %rdx, -0x60(%rbp)
testb %al, %al
je 0x4026ca
leaq 0x9cbf3(%rip), %rsi     # 0x49f051
movq %rbx, %rdi
callq 0x4148a0
testq %rax, %rax
je 0x4024f9
movq %rax, %r15
movq %rbx, -0x30(%rbp)
leaq -0x39(%rbp), %rbx
movl $0x9, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %rax, %rcx
callq 0x4149a0
movsbl 0x8(%rbx), %r13d
movslq %r13d, %r14
movq %r14, %r12
shlq $0x4, %r12
subq %r14, %r12
subq %r14, %r12
movq %r12, %rdi
callq 0x4242d0
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x4149a0
testl %r14d, %r14d
movq %r13, -0x50(%rbp)
movq %r12, -0x48(%rbp)
jle 0x40251b
movl %r13d, %eax
leaq (%rax,%rax), %rcx
shlq $0x4, %rax
subq %rcx, %rax
xorl %ecx, %ecx
xorl %r13d, %r13d
movzbl 0xd(%rbx,%rcx), %edx
addl %edx, %r13d
addq $0xe, %rcx
cmpq %rcx, %rax
jne 0x4024e2
shll $0x8, %r13d
jmp 0x40251e
leaq 0x9cbcf(%rip), %rdi     # 0x49f0cf
movq %rbx, %rsi
xorl %eax, %eax
addq $0x10048, %rsp          # imm = 0x10048
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x40de40
xorl %r13d, %r13d
movslq %r13d, %r12
movq %r12, %rdi
callq 0x4242d0
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
movq %r12, -0x58(%rbp)
movq %r12, %rsi
movq %r15, %rcx
callq 0x4149a0
movq %r15, %rdi
callq 0x414460
movq -0x50(%rbp), %rax
testb %al, %al
js 0x40269b
incl %eax
movb %al, -0x31(%rbp)
leaq 0x9cb32(%rip), %rsi     # 0x49f095
movq -0x30(%rbp), %r15
movq %r15, %rdi
callq 0x4148a0
testq %rax, %rax
je 0x4026a9
leaq -0x39(%rbp), %rdi
movl $0x9, %esi
movl $0x1, %edx
movq %rax, %rcx
movq %rax, %r12
callq 0x414bf0
movl $0x1, %edx
movq %rbx, %rdi
movq -0x48(%rbp), %rsi
movq %r12, %rcx
callq 0x414bf0
leaq -0x68(%rbp), %r15
movl $0xe, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r12, %rcx
callq 0x414bf0
movl $0x1, %edx
movq %r14, %rdi
movq -0x58(%rbp), %rsi
movq %r12, %rcx
callq 0x414bf0
movzbl 0xd(%r15), %r15d
movl %r15d, %esi
shll $0x8, %esi
leaq -0x10070(%rbp), %rdi
movl $0x1, %edx
movq %r12, -0x30(%rbp)
movq %r12, %rcx
callq 0x414bf0
xorl %ecx, %ecx
xorl %eax, %eax
movzbl -0x39(%rbp,%rcx), %edx
addl %edx, %eax
incq %rcx
cmpl $0x9, %ecx
jne 0x4025f9
xorl %ecx, %ecx
movl $0x0, %esi
cmpb $0x0, -0x50(%rbp)
je 0x40262b
movl -0x48(%rbp), %edx
xorl %edi, %edi
xorl %esi, %esi
movzbl (%rbx,%rdi), %r8d
addl %r8d, %esi
incq %rdi
cmpl %edi, %edx
jne 0x40261c
xorl %edx, %edx
movzbl -0x68(%rbp,%rdx), %edi
addl %edi, %ecx
incq %rdx
cmpl $0xe, %edx
jne 0x40262d
addl %eax, %esi
addl %ecx, %esi
xorl %eax, %eax
movl $0x0, %ecx
testl %r13d, %r13d
je 0x402662
movl %r13d, %edx
xorl %edi, %edi
xorl %ecx, %ecx
movzbl (%r14,%rdi), %r8d
addl %r8d, %ecx
incq %rdi
cmpl %edi, %edx
jne 0x402653
addl %ecx, %esi
testq %r15, %r15
je 0x402683
shll $0x8, %r15d
xorl %ecx, %ecx
xorl %eax, %eax
movzbl -0x10070(%rbp,%rcx), %edx
addl %edx, %eax
incq %rcx
cmpl %ecx, %r15d
jne 0x402671
addl %eax, %esi
movq -0x30(%rbp), %r15
movq %r15, %rdi
callq 0x401c53
movq %r15, %rdi
callq 0x414460
jmp 0x4026ba
leaq 0x9ce5e(%rip), %rdi     # 0x49f500
callq 0x414ec0
jmp 0x4026ba
leaq 0x9ca92(%rip), %rdi     # 0x49f142
movq %r15, %rsi
xorl %eax, %eax
callq 0x40de40
movq %rbx, %rdi
callq 0x424610
movq %r14, %rdi
callq 0x424610
addq $0x10048, %rsp          # imm = 0x10048
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq