// File: /workspace/github_c_makefile_star2_O3/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 $0x10138, %rsp          # imm = 0x10138
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %rbx
leaq 0x9c959(%rip), %rsi     # 0x49f051
callq 0x414900
movq %rax, -0x40(%rbp)
testq %rax, %rax
je 0x402888
movq %r15, -0x58(%rbp)
leaq -0x4d(%rbp), %rsi
movq %r13, %rdi
callq 0x401cb6
movl $0x8, %esi
movq -0x40(%rbp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x418b50
movq %rbx, %rdi
callq 0x418c30
testl %eax, %eax
jle 0x402863
movl %eax, %r12d
movl %r12d, %r15d
shll $0x4, %r15d
subl %r12d, %r15d
subl %r12d, %r15d
addl $0x9, %r15d
incl %r12d
leaq -0x38(%rbp), %rbx
movl $0x0, -0x44(%rbp)
movq %r13, -0x60(%rbp)
movl $0xe, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq -0x40(%rbp), %rcx
callq 0x414a00
testq %r13, %r13
je 0x4027a1
movq -0x38(%rbp), %rax
xorq -0x4d(%rbp), %rax
movb -0x30(%rbp), %cl
xorb -0x45(%rbp), %cl
movzbl %cl, %ecx
orq %rax, %rcx
je 0x4028aa
movb -0x2b(%rbp), %r13b
jmp 0x402844
movq -0x40(%rbp), %r14
movq %r14, %rdi
callq 0x414b00
movq %rax, %r13
movl %r15d, %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x418b50
movzbl -0x2b(%rbp), %esi
shll $0x8, %esi
movl $0x1, %edx
leaq -0x10160(%rbp), %rdi
movq %r14, %rcx
callq 0x414a00
movq %r14, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x418b50
movb -0x2b(%rbp), %r13b
movl $0x100, %edx            # imm = 0x100
leaq -0x160(%rbp), %r14
movq %r14, %rdi
movl $0xff, %esi
callq 0x4010a0
movsbl -0x30(%rbp), %r8d
movq %r14, %rdi
leaq 0x9c954(%rip), %rsi     # 0x49f165
movl -0x44(%rbp), %r14d
movl %r14d, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x40df70
movq -0x38(%rbp), %rdx
movq -0x30(%rbp), %rcx
leaq -0x160(%rbp), %rdi
leaq -0x10160(%rbp), %rsi
callq 0x401edf
incl %r14d
movl %r14d, -0x44(%rbp)
movzbl %r13b, %eax
shll $0x8, %eax
addl %eax, %r15d
decl %r12d
cmpl $0x1, %r12d
movq -0x60(%rbp), %r13
leaq -0x38(%rbp), %rbx
ja 0x402763
movq -0x40(%rbp), %rdi
callq 0x4144c0
testq %r13, %r13
je 0x4028f5
leaq 0x9c8f6(%rip), %rdi     # 0x49f172
movq %r13, %rsi
xorl %eax, %eax
callq 0x40dea0
jmp 0x4028f5
leaq 0x9c7c5(%rip), %rdi     # 0x49f054
movq %rbx, %rsi
xorl %eax, %eax
addq $0x10138, %rsp          # imm = 0x10138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x40dea0
movl %r15d, %esi
movq -0x40(%rbp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x418b50
movzbl -0x2b(%rbp), %esi
shll $0x8, %esi
leaq -0x10160(%rbp), %rbx
movl $0x1, %edx
movq %rbx, %rdi
movq %r14, %rcx
callq 0x414a00
movq %r14, %rdi
callq 0x4144c0
movq -0x38(%rbp), %rdx
movq -0x30(%rbp), %rcx
movq -0x58(%rbp), %rdi
movq %rbx, %rsi
callq 0x401edf
addq $0x10138, %rsp          # imm = 0x10138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq