// File: /workspace/github_c_makefile_star2_O2/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 $0x10038, %rsp          # imm = 0x10038
movq %rsi, %rax
movq %rdi, %rbx
leaq -0x10060(%rbp), %rsi
movq %rax, %rdi
callq 0x401dc2
movq %rax, -0x60(%rbp)
movq %rdx, -0x58(%rbp)
testb %al, %al
je 0x4025e7
leaq 0x9cc68(%rip), %rsi     # 0x49f051
movq %rbx, %rdi
callq 0x414760
testq %rax, %rax
je 0x402596
movq %rax, %r14
movq %rbx, -0x50(%rbp)
leaq -0x31(%rbp), %rbx
pushq $0x9
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %rax, %rcx
callq 0x414860
movsbl 0x8(%rbx), %r13d
movslq %r13d, %r15
imulq $0xe, %r15, %r12
movq %r12, %rdi
callq 0x424190
movq %rax, %rbx
movq %rax, %rdi
movq %r12, -0x48(%rbp)
movq %r12, %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x414860
xorl %eax, %eax
testl %r15d, %r15d
cmovlel %eax, %r15d
imulq $0xe, %r15, %rdx
xorl %ecx, %ecx
cmpq %rax, %rdx
je 0x402463
movzbl 0xd(%rbx,%rax), %esi
addl %esi, %ecx
addq $0xe, %rax
jmp 0x402451
shll $0x8, %ecx
movslq %ecx, %r12
movq %r12, %rdi
callq 0x424190
movq %rax, %r15
pushq $0x1
popq %rdx
movq %rax, %rdi
movq %r12, -0x40(%rbp)
movq %r12, %rsi
movq %r14, %rcx
callq 0x414860
movq %r14, %rdi
callq 0x414320
testb %r13b, %r13b
js 0x4025b8
leal 0x1(%r13), %eax
movb %al, -0x29(%rbp)
leaq 0x9cbed(%rip), %rsi     # 0x49f095
movq -0x50(%rbp), %r12
movq %r12, %rdi
callq 0x414760
testq %rax, %rax
je 0x4025c6
movq %rax, %r14
leaq -0x31(%rbp), %rdi
pushq $0x9
popq %rsi
pushq $0x1
popq %r12
movq %r12, %rdx
movq %rax, %rcx
callq 0x414ab0
movq %rbx, %rdi
movq -0x48(%rbp), %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x414ab0
leaq -0x60(%rbp), %r13
pushq $0xe
popq %rsi
movq %r13, %rdi
movq %r12, %rdx
movq %r14, %rcx
callq 0x414ab0
movq %r15, %rdi
movq -0x40(%rbp), %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x414ab0
movzbl 0xd(%r13), %r12d
shll $0x8, %r12d
leaq -0x10060(%rbp), %rdi
movq %r12, %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x414ab0
pushq $0x9
popq %rsi
leaq -0x31(%rbp), %rdi
callq 0x401d11
movq %r15, %r13
movl %eax, %r15d
movq %rbx, %rdi
movq -0x48(%rbp), %rsi
callq 0x401d11
addl %eax, %r15d
pushq $0xe
popq %rsi
leaq -0x60(%rbp), %rdi
callq 0x401d11
addl %eax, %r15d
movq %r13, %rdi
movq -0x40(%rbp), %rsi
callq 0x401d11
addl %eax, %r15d
leaq -0x10060(%rbp), %rdi
movl %r12d, %esi
callq 0x401d11
addl %eax, %r15d
movq %r14, %rdi
movl %r15d, %esi
movq %r13, %r15
callq 0x401c26
movq %r14, %rdi
callq 0x414320
jmp 0x4025d7
leaq 0x9cb32(%rip), %rdi     # 0x49f0cf
movq %rbx, %rsi
xorl %eax, %eax
addq $0x10038, %rsp          # imm = 0x10038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x40dd00
leaq 0x9cf2f(%rip), %rdi     # 0x49f4ee
callq 0x414d80
jmp 0x4025d7
leaq 0x9cb75(%rip), %rdi     # 0x49f142
movq %r12, %rsi
xorl %eax, %eax
callq 0x40dd00
movq %rbx, %rdi
callq 0x4244d0
movq %r15, %rdi
callq 0x4244d0
addq $0x10038, %rsp          # imm = 0x10038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq