// File: /workspace/github_c_makefile_star2_O0/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
subq $0x10160, %rsp          # imm = 0x10160
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x9c3b2(%rip), %rsi     # 0x49f004
callq 0x415510
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x402c79
movq -0x8(%rbp), %rsi
leaq 0x9c39a(%rip), %rdi     # 0x49f007
movb $0x0, %al
callq 0x40eab0
jmp 0x402ecf
movq -0x10(%rbp), %rdi
leaq -0x29(%rbp), %rsi
callq 0x401e00
movq -0x20(%rbp), %rdi
movl $0x8, %esi
xorl %edx, %edx
callq 0x4195c0
movq -0x20(%rbp), %rdi
callq 0x4196a0
movl %eax, -0x10144(%rbp)
imull $0xe, -0x10144(%rbp), %eax
addl $0x9, %eax
movl %eax, -0x10148(%rbp)
movl $0x0, -0x1014c(%rbp)
movl $0x0, -0x10150(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x10144(%rbp)
movb %al, -0x10159(%rbp)
jle 0x402cea
cmpl $0x0, -0x1014c(%rbp)
sete %al
movb %al, -0x10159(%rbp)
movb -0x10159(%rbp), %al
testb $0x1, %al
jne 0x402cf9
jmp 0x402e82
movq -0x20(%rbp), %rcx
leaq -0x10140(%rbp), %rdi
movl $0xe, %esi
movl $0x1, %edx
callq 0x415610
cmpq $0x0, -0x10(%rbp)
jne 0x402dfc
movq -0x20(%rbp), %rdi
callq 0x415710
movq %rax, -0x10158(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x10148(%rbp), %rsi
xorl %edx, %edx
callq 0x4195c0
leaq -0x10130(%rbp), %rdi
movzbl -0x10133(%rbp), %eax
shll $0x8, %eax
movslq %eax, %rsi
movq -0x20(%rbp), %rcx
movl $0x1, %edx
callq 0x415610
movq -0x20(%rbp), %rdi
movq -0x10158(%rbp), %rsi
xorl %edx, %edx
callq 0x4195c0
movzbl -0x10133(%rbp), %eax
shll $0x8, %eax
addl -0x10148(%rbp), %eax
movl %eax, -0x10148(%rbp)
leaq -0x130(%rbp), %rdi
movl $0xff, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x4010a0
leaq -0x130(%rbp), %rdi
movl -0x10150(%rbp), %edx
leaq -0x10140(%rbp), %rcx
movsbl -0x10138(%rbp), %r8d
leaq 0x9c3f5(%rip), %rsi     # 0x49f1b8
movb $0x0, %al
callq 0x40eb80
leaq -0x130(%rbp), %rdi
leaq -0x10130(%rbp), %rsi
movq -0x10140(%rbp), %rdx
movq -0x10138(%rbp), %rcx
callq 0x4021a0
movl -0x10150(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10150(%rbp)
jmp 0x402e6e
leaq -0x10140(%rbp), %rdi
leaq -0x29(%rbp), %rsi
movl $0x9, %edx
callq 0x401088
cmpl $0x0, %eax
jne 0x402e56
movq -0x20(%rbp), %rdi
movslq -0x10148(%rbp), %rsi
xorl %edx, %edx
callq 0x4195c0
leaq -0x10130(%rbp), %rdi
movzbl -0x10133(%rbp), %eax
shll $0x8, %eax
movslq %eax, %rsi
movq -0x20(%rbp), %rcx
movl $0x1, %edx
callq 0x415610
movl $0x1, -0x1014c(%rbp)
jmp 0x402e6c
movzbl -0x10133(%rbp), %eax
shll $0x8, %eax
addl -0x10148(%rbp), %eax
movl %eax, -0x10148(%rbp)
jmp 0x402e6e
movl -0x10144(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x10144(%rbp)
jmp 0x402cc9
movq -0x20(%rbp), %rdi
callq 0x4150d0
cmpq $0x0, -0x10(%rbp)
jne 0x402e94
jmp 0x402ecf
cmpl $0x0, -0x1014c(%rbp)
jne 0x402eb1
movq -0x10(%rbp), %rsi
leaq 0x9c31d(%rip), %rdi     # 0x49f1c5
movb $0x0, %al
callq 0x40eab0
jmp 0x402ecf
movq -0x18(%rbp), %rdi
leaq -0x10130(%rbp), %rsi
movq -0x10140(%rbp), %rdx
movq -0x10138(%rbp), %rcx
callq 0x4021a0
addq $0x10160, %rsp          # imm = 0x10160
popq %rbp
retq
nopl (%rax,%rax)