// File: /workspace/github_c_makefile_star2_O3/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 0x401e02
movq %rax, -0x68(%rbp)
movq %rdx, -0x60(%rbp)
testb %al, %al
je 0x4026c2
leaq 0x9cbfb(%rip), %rsi     # 0x49f051
movq %rbx, %rdi
callq 0x414900
testq %rax, %rax
je 0x4024f1
movq %rax, %r15
movq %rbx, -0x30(%rbp)
leaq -0x39(%rbp), %rbx
movl $0x9, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %rax, %rcx
callq 0x414a00
movsbl 0x8(%rbx), %r13d
movslq %r13d, %r14
movq %r14, %r12
shlq $0x4, %r12
subq %r14, %r12
subq %r14, %r12
movq %r12, %rdi
callq 0x424330
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x414a00
testl %r14d, %r14d
movq %r13, -0x50(%rbp)
movq %r12, -0x48(%rbp)
jle 0x402513
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 0x4024da
shll $0x8, %r13d
jmp 0x402516
leaq 0x9cbd7(%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 0x40dea0
xorl %r13d, %r13d
movslq %r13d, %r12
movq %r12, %rdi
callq 0x424330
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
movq %r12, -0x58(%rbp)
movq %r12, %rsi
movq %r15, %rcx
callq 0x414a00
movq %r15, %rdi
callq 0x4144c0
movq -0x50(%rbp), %rax
testb %al, %al
js 0x402693
incl %eax
movb %al, -0x31(%rbp)
leaq 0x9cb3a(%rip), %rsi     # 0x49f095
movq -0x30(%rbp), %r15
movq %r15, %rdi
callq 0x414900
testq %rax, %rax
je 0x4026a1
leaq -0x39(%rbp), %rdi
movl $0x9, %esi
movl $0x1, %edx
movq %rax, %rcx
movq %rax, %r12
callq 0x414c50
movl $0x1, %edx
movq %rbx, %rdi
movq -0x48(%rbp), %rsi
movq %r12, %rcx
callq 0x414c50
leaq -0x68(%rbp), %r15
movl $0xe, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r12, %rcx
callq 0x414c50
movl $0x1, %edx
movq %r14, %rdi
movq -0x58(%rbp), %rsi
movq %r12, %rcx
callq 0x414c50
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 0x414c50
xorl %ecx, %ecx
xorl %eax, %eax
movzbl -0x39(%rbp,%rcx), %edx
addl %edx, %eax
incq %rcx
cmpl $0x9, %ecx
jne 0x4025f1
xorl %ecx, %ecx
movl $0x0, %esi
cmpb $0x0, -0x50(%rbp)
je 0x402623
movl -0x48(%rbp), %edx
xorl %edi, %edi
xorl %esi, %esi
movzbl (%rbx,%rdi), %r8d
addl %r8d, %esi
incq %rdi
cmpl %edi, %edx
jne 0x402614
xorl %edx, %edx
movzbl -0x68(%rbp,%rdx), %edi
addl %edi, %ecx
incq %rdx
cmpl $0xe, %edx
jne 0x402625
addl %eax, %esi
addl %ecx, %esi
xorl %eax, %eax
movl $0x0, %ecx
testl %r13d, %r13d
je 0x40265a
movl %r13d, %edx
xorl %edi, %edi
xorl %ecx, %ecx
movzbl (%r14,%rdi), %r8d
addl %r8d, %ecx
incq %rdi
cmpl %edi, %edx
jne 0x40264b
addl %ecx, %esi
testq %r15, %r15
je 0x40267b
shll $0x8, %r15d
xorl %ecx, %ecx
xorl %eax, %eax
movzbl -0x10070(%rbp,%rcx), %edx
addl %edx, %eax
incq %rcx
cmpl %ecx, %r15d
jne 0x402669
addl %eax, %esi
movq -0x30(%rbp), %r15
movq %r15, %rdi
callq 0x401c58
movq %r15, %rdi
callq 0x4144c0
jmp 0x4026b2
leaq 0x9ce54(%rip), %rdi     # 0x49f4ee
callq 0x414f20
jmp 0x4026b2
leaq 0x9ca9a(%rip), %rdi     # 0x49f142
movq %r15, %rsi
xorl %eax, %eax
callq 0x40dea0
movq %rbx, %rdi
callq 0x424670
movq %r14, %rdi
callq 0x424670
addq $0x10048, %rsp          # imm = 0x10048
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq