// File: /workspace/github_c_makefile_star2_O2/salextpuru[P]speccy-toochain/src/mktrd/mktrd.cpp
// Function: extract(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void extract(const string fextra, const string aname) {
	if (fextra.size()>10) {cout<<"Filename is too long ('filename.C' is maximum)"<<endl; return;}
	if (fextra.size()<2) {cout<<"Filename is too short (must be '.C' at least)"<<endl; return;}
	if (fextra.at(fextra.size()-2)!='.') {cout<<"Filename must be in 'fileneme.e' format"<<endl; return;}
	string fname(fextra,0,fextra.size()-2); fname.resize(8,0x20);	// filename (8 chars)
	string fext(fextra,fextra.size()-1,1);				// extension (1 char)
	unsigned char *buf = new unsigned char[0xa0000];
	int len=readfile(aname,buf);
	if (len<1) {cout<<"Can't read file"<<endl; return;}
	int mode=testsig(buf,len);
	unsigned char* ptr = buf;
	unsigned char i=0;
	int fpos;
	switch (mode) {
		case 1:
			ptr+=9; fpos=9+*(buf+8)*14;		// begin of data
			for(;i<*(buf+8);i++) {
				if (strncmp(fname.c_str(),(const char*)ptr,8)==0) {
					extractfile(buf,ptr,fpos,fextra); i=128;
				}
				fpos+=(*(ptr+13)<<8); ptr+=14;
			}
			break;
		case 2:
			while(*ptr!=0 && i<128) {
				if (strncmp(fname.c_str(),(const char*)ptr,8)==0) {
					int fpos=(*(ptr+15)<<12)+(*(ptr+14)<<8);	// position of begin of file in buf
					extractfile(buf,ptr,fpos,fextra); i=128;
				}
				ptr+=16; i++;
			}
			break;
		default: cout<<"Unknown image format"<<endl; break;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rsi, %r15
movq 0x8(%rdi), %rsi
cmpq $0xb, %rsi
jb 0x40507a
movq $0x5ec460, %rdi         # imm = 0x5EC460
leaq 0x179066(%rip), %rsi    # 0x57e0db
jmp 0x4051df
cmpq $0x1, %rsi
ja 0x405093
movq $0x5ec460, %rdi         # imm = 0x5EC460
leaq 0x17907c(%rip), %rsi    # 0x57e10a
jmp 0x4051df
movq %rdi, %rbx
addq $-0x2, %rsi
callq 0x475470
cmpb $0x2e, (%rax)
jne 0x4051d1
movq 0x8(%rbx), %rcx
addq $-0x2, %rcx
leaq -0x58(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x477790
pushq $0x8
popq %rsi
pushq $0x20
popq %rdx
movq %r14, %rdi
callq 0x475b20
movq 0x8(%rbx), %rdx
decq %rdx
leaq -0x78(%rbp), %rdi
pushq $0x1
popq %rcx
movq %rbx, %rsi
callq 0x477790
movq %rbx, -0x38(%rbp)
movl $0xa0000, %edi          # imm = 0xA0000
callq 0x4068a0
movq %rax, -0x30(%rbp)
leaq -0xd8(%rbp), %rdi
movq %r15, %rsi
callq 0x4776b0
leaq -0xd8(%rbp), %r12
movq %r12, %rdi
movq -0x30(%rbp), %r13
movq %r13, %rsi
movl $0xa0000, %edx          # imm = 0xA0000
callq 0x404768
movl %eax, %r15d
movq %r12, %rdi
callq 0x474f20
testl %r15d, %r15d
jle 0x4051fd
movq %r13, %rdi
movl %r15d, %esi
callq 0x404848
cmpb $0x1, %al
je 0x40521d
movzbl %al, %eax
cmpl $0x2, %eax
jne 0x405297
xorl %ebx, %ebx
pushq $0x8
popq %r15
leaq -0x98(%rbp), %r12
cmpb $0x0, (%r13)
je 0x4052b2
testb %bl, %bl
js 0x4052b2
movq -0x58(%rbp), %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x401070
testl %eax, %eax
jne 0x4051c9
movq %r15, %r14
movzbl 0xf(%r13), %ebx
movzbl 0xe(%r13), %r15d
movq %r12, %rdi
movq -0x38(%rbp), %rsi
callq 0x4776b0
shll $0xc, %ebx
shll $0x8, %r15d
addl %ebx, %r15d
movq -0x30(%rbp), %rdi
movq %r13, %rsi
movl %r15d, %edx
movq %r12, %rcx
callq 0x40488a
movq %r12, %rdi
callq 0x474f20
movb $-0x80, %bl
movq %r14, %r15
addq $0x10, %r13
incb %bl
jmp 0x405161
movq $0x5ec460, %rdi         # imm = 0x5EC460
leaq 0x178f59(%rip), %rsi    # 0x57e138
callq 0x46eb50
movq %rax, %rdi
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x46e430
movq $0x5ec460, %rdi         # imm = 0x5EC460
leaq 0x178e1f(%rip), %rsi    # 0x57e02a
callq 0x46eb50
movq %rax, %rdi
callq 0x46e430
jmp 0x4052b2
movq %r13, %r14
movzbl 0x8(%r13), %r13d
imull $0xe, %r13d, %r15d
addl $0x9, %r15d
addq $0x16, %r14
xorl %ebx, %ebx
cmpb %r13b, %bl
jae 0x4052b2
leaq -0xd(%r14), %r12
movq -0x58(%rbp), %rdi
movq %r12, %rsi
pushq $0x8
popq %rdx
callq 0x401070
testl %eax, %eax
jne 0x405285
leaq -0xb8(%rbp), %r13
movq %r13, %rdi
movq -0x38(%rbp), %rsi
callq 0x4776b0
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movl %r15d, %edx
movq %r13, %rcx
callq 0x40488a
movq %r13, %rdi
callq 0x474f20
movb 0x8(%rbx), %r13b
movb $-0x80, %bl
movzbl (%r14), %eax
shll $0x8, %eax
addl %eax, %r15d
incb %bl
addq $0xe, %r14
jmp 0x405233
movq $0x5ec460, %rdi         # imm = 0x5EC460
leaq 0x178e21(%rip), %rsi    # 0x57e0c6
callq 0x46eb50
movq %rax, %rdi
callq 0x46e430
leaq -0x78(%rbp), %rdi
callq 0x474f20
leaq -0x58(%rbp), %rdi
callq 0x474f20
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4052e5
jmp 0x4052e5
jmp 0x4052dc
movq %rax, %rbx
jmp 0x40530e
jmp 0x4052e5
jmp 0x4052e5
movq %rax, %rbx
jmp 0x405305
movq %rax, %rbx
leaq -0xb8(%rbp), %rdi
jmp 0x405300
movq %rax, %rbx
leaq -0x98(%rbp), %rdi
callq 0x474f20
leaq -0x78(%rbp), %rdi
callq 0x474f20
leaq -0x58(%rbp), %rdi
callq 0x474f20
movq %rbx, %rdi
callq 0x4a7100