// File: /workspace/github_c_makefile_star2_O2/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, -0x40(%rbp)
movq %rdi, %rbx
leaq 0x9ca33(%rip), %rsi     # 0x49f051
callq 0x414760
movq %rax, -0x30(%rbp)
testq %rax, %rax
je 0x4027ee
movq %r12, -0x60(%rbp)
leaq -0x69(%rbp), %rsi
movq -0x40(%rbp), %rdi
callq 0x401c84
pushq $0x8
popq %rsi
xorl %r14d, %r14d
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x4189b0
movq %rbx, %rdi
callq 0x418a90
imull $0xe, %eax, %r12d
addl $0x9, %r12d
leaq -0x50(%rbp), %r13
pushq $0x1
popq %rbx
movl $0x0, -0x34(%rbp)
testl %eax, %eax
jle 0x4027bf
testl %r14d, %r14d
jne 0x4027bf
movl %eax, -0x58(%rbp)
movq %r13, %rdi
pushq $0xe
popq %rsi
movq %rbx, %rdx
movq -0x30(%rbp), %rcx
callq 0x414860
movq %rbx, %r14
movslq %r12d, %rbx
cmpq $0x0, -0x40(%rbp)
je 0x4026d2
movq %r13, %r15
movq %r13, %rdi
leaq -0x69(%rbp), %rsi
pushq $0x9
popq %rdx
callq 0x401028
testl %eax, %eax
je 0x402781
movzbl -0x43(%rbp), %eax
shll $0x8, %eax
addl %eax, %r12d
movq %r15, %r13
jmp 0x402779
movq -0x30(%rbp), %r15
movq %r15, %rdi
callq 0x414960
movl %r12d, -0x54(%rbp)
movq %rax, %r12
movq %r15, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x4189b0
movzbl -0x43(%rbp), %esi
shll $0x8, %esi
leaq -0x10170(%rbp), %rdi
movq %r14, %rdx
movq %r15, %rcx
callq 0x414860
movq %r15, %rdi
movq %r12, %rsi
movl -0x54(%rbp), %r12d
xorl %edx, %edx
callq 0x4189b0
movzbl -0x43(%rbp), %eax
shll $0x8, %eax
addl %eax, %r12d
movl $0x100, %edx            # imm = 0x100
leaq -0x170(%rbp), %r15
movq %r15, %rdi
movl $0xff, %esi
callq 0x4010a8
movsbl -0x48(%rbp), %r8d
movq %r15, %rdi
leaq 0x9ca17(%rip), %rsi     # 0x49f165
movl -0x34(%rbp), %ebx
movl %ebx, %edx
movq %r13, %rcx
xorl %eax, %eax
callq 0x40ddd0
movq -0x50(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq %r15, %rdi
leaq -0x10170(%rbp), %rsi
callq 0x401e9f
incl %ebx
movl %ebx, -0x34(%rbp)
movq %r14, %rbx
xorl %r14d, %r14d
jmp 0x4027b5
movq -0x30(%rbp), %r13
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x4189b0
movzbl -0x43(%rbp), %esi
shll $0x8, %esi
leaq -0x10170(%rbp), %rdi
movq %r14, %rbx
movq %r14, %rdx
movq %r13, %rcx
callq 0x414860
pushq $0x1
popq %r14
movq %r15, %r13
movl -0x58(%rbp), %eax
decl %eax
jmp 0x402673
movq -0x30(%rbp), %rdi
callq 0x414320
cmpq $0x0, -0x40(%rbp)
je 0x402822
testl %r14d, %r14d
je 0x402810
movq -0x50(%rbp), %rdx
movq -0x48(%rbp), %rcx
leaq -0x10170(%rbp), %rsi
movq -0x60(%rbp), %rdi
callq 0x401e9f
jmp 0x402822
leaq 0x9c85f(%rip), %rdi     # 0x49f054
movq %rbx, %rsi
xorl %eax, %eax
addq $0x10148, %rsp          # imm = 0x10148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x40dd00
leaq 0x9c95b(%rip), %rdi     # 0x49f172
movq -0x40(%rbp), %rsi
xorl %eax, %eax
callq 0x40dd00
addq $0x10148, %rsp          # imm = 0x10148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq