// File: /workspace/github_c_makefile_star2_O1/salextpuru[P]speccy-toochain/src/mktrd/mctrd.c
// Function: sclPop
void sclPop(char* ipath, char* fname, char* oname) {
	FILE* file = fopen(ipath, "rb");
	if (!file) {
		printf("Can't open file '%s'\n",ipath);
		return;
	}
	char nbuf[9];
	char xbuf[256];
	char buf[0x10000];
	makeName(fname, nbuf);
	// printf("searching name '%.9s'\n",nbuf);
	fseek(file, 8, SEEK_SET);
	trdFile hd;
	int cnt = fgetc(file);
	int pos = 9 + cnt * 14;	// data pos
	int find = 0;
	int num = 0;
	size_t fpos;
	while ((cnt > 0) && (find == 0)) {
		fread((char*)&hd, 14, 1, file);
		if (fname == NULL) {				// all files
			fpos = ftell(file);
			fseek(file, pos, SEEK_SET);
			fread(buf, hd.slen << 8, 1, file);
			fseek(file, fpos, SEEK_SET);
			pos += (hd.slen << 8);
			memset(xbuf, 0xff, 256);
			sprintf(xbuf,"%.3i_%.8s.%c",num,hd.name,hd.ext);
			//printf("%s\n",xbuf);
			saveoutput(xbuf, buf, hd);
			num++;
		} else if (memcmp(hd.name, nbuf, 9) == 0) {	// compare name
			fseek(file, pos, SEEK_SET);
			fread(buf, hd.slen << 8, 1, file);
			find = 1;
		} else {					// don't save
			pos += (hd.slen << 8);
		}
		cnt--;
	}
	fclose(file);
	if (!fname) return;
	if (!find) {
		printf("File '%s' not found in image\n",fname);
	} else {
		saveoutput(oname, buf, hd);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10148, %rsp          # imm = 0x10148
movq %rdx, %r12
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x9c951(%rip), %rsi     # 0x49f051
callq 0x4148a0
testq %rax, %rax
je 0x40289d
movq %rax, %r15
movq %r12, -0x68(%rbp)
leaq -0x59(%rbp), %rsi
movq %rbx, -0x50(%rbp)
movq %rbx, %rdi
callq 0x401cb1
movl $0x8, %esi
movq %r15, %rdi
xorl %edx, %edx
callq 0x418af0
movq %r15, %rdi
callq 0x418bd0
testl %eax, %eax
jle 0x4028bf
movl %eax, %r12d
movl %r12d, %ecx
shll $0x4, %ecx
subl %r12d, %ecx
subl %r12d, %ecx
addl $0x9, %ecx
movl %ecx, -0x2c(%rbp)
leaq -0x40(%rbp), %r14
movl $0x0, -0x44(%rbp)
movl $0xe, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %r15, %rcx
callq 0x4149a0
cmpq $0x0, -0x50(%rbp)
je 0x4027a9
movq -0x40(%rbp), %rax
xorq -0x59(%rbp), %rax
movb -0x38(%rbp), %cl
xorb -0x51(%rbp), %cl
movzbl %cl, %ecx
orq %rax, %rcx
je 0x402859
movzbl -0x33(%rbp), %eax
shll $0x8, %eax
addl %eax, -0x2c(%rbp)
jmp 0x402854
movq %r15, %rdi
callq 0x414aa0
movq %rax, %rbx
movq %r15, %r13
movl -0x2c(%rbp), %r15d
movslq %r15d, %rsi
movq %r13, %rdi
xorl %edx, %edx
callq 0x418af0
movzbl -0x33(%rbp), %esi
shll $0x8, %esi
movl $0x1, %edx
leaq -0x10170(%rbp), %rdi
movq %r13, %rcx
callq 0x4149a0
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x418af0
movzbl -0x33(%rbp), %eax
shll $0x8, %eax
addl %eax, %r15d
movl %r15d, -0x2c(%rbp)
movq %r13, %r15
movl $0x100, %edx            # imm = 0x100
leaq -0x170(%rbp), %r13
movq %r13, %rdi
movl $0xff, %esi
callq 0x4010a0
movsbl -0x38(%rbp), %r8d
movq %r13, %rdi
leaq 0x9c93c(%rip), %rsi     # 0x49f165
movl -0x44(%rbp), %ebx
movl %ebx, %edx
movq %r14, %rcx
xorl %eax, %eax
callq 0x40df10
movq -0x40(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq %r13, %rdi
leaq -0x10170(%rbp), %rsi
callq 0x401ed6
incl %ebx
movl %ebx, -0x44(%rbp)
movb $0x1, %r13b
jmp 0x402885
movslq -0x2c(%rbp), %rsi
xorl %r13d, %r13d
movq %r15, %rdi
xorl %edx, %edx
callq 0x418af0
movzbl -0x33(%rbp), %esi
shll $0x8, %esi
movl $0x1, %edx
leaq -0x10170(%rbp), %rdi
movq %r15, %rcx
callq 0x4149a0
cmpl $0x2, %r12d
jl 0x4028c2
decl %r12d
testb %r13b, %r13b
leaq -0x40(%rbp), %r14
jne 0x402764
jmp 0x4028c2
leaq 0x9c7b0(%rip), %rdi     # 0x49f054
movq %r14, %rsi
xorl %eax, %eax
addq $0x10148, %rsp          # imm = 0x10148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x40de40
movb $0x1, %r13b
movq %r15, %rdi
callq 0x414460
movq -0x50(%rbp), %rsi
testq %rsi, %rsi
je 0x402900
testb %r13b, %r13b
je 0x4028e8
leaq 0x9c893(%rip), %rdi     # 0x49f172
xorl %eax, %eax
callq 0x40de40
jmp 0x402900
movq -0x40(%rbp), %rdx
movq -0x38(%rbp), %rcx
leaq -0x10170(%rbp), %rsi
movq -0x68(%rbp), %rdi
callq 0x401ed6
addq $0x10148, %rsp          # imm = 0x10148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq