// File: /workspace/github_c_makefile_star2_O1/AMGarkin[P]BDOcrypt/BDO_decrypt.c
// Function: main
int main(int argc, char **argv)
{
	if (argc != 3){
		printf("BDO_decrypt <source file> <output file>");
		return 1;
	}

	char* srcFileName = argv[1];
	char* outFileName = argv[2];

	FILE* srcFile = fopen(srcFileName, "rb");
	if (srcFile == NULL){
		printf("File not found: %s", srcFileName);
		return -1;
	}
	FILE* tmpFile = tmpfile();
	FILE* outFile = fopen(outFileName, "wb");

	///Decompress source to temporary file
	unsigned long compressedSize = 0;
	unsigned long uncompressedSize = 0;

	fseek(srcFile, 0, SEEK_END);
	compressedSize = ftell(srcFile) - 4; // 1st 4 bytes holds information about uncompressed data size
	rewind(srcFile);

	fread(&uncompressedSize, 4, 1, srcFile);

	unsigned char *pCompressedData = (unsigned char *) calloc(compressedSize, sizeof(unsigned char));
	unsigned char *pUncompressedData = (unsigned char *) calloc(uncompressedSize, sizeof(unsigned char));

	fread(pCompressedData, 1, compressedSize, srcFile);

	int result = uncompress(pUncompressedData, &uncompressedSize, pCompressedData, compressedSize);

	if (result == Z_OK) {
		fwrite(pUncompressedData, 1, uncompressedSize, tmpFile);

		///Convert .bss data from temporary file to text file
		unsigned long strSize;
		unsigned long strType;
		unsigned long strID1;
		unsigned short strID2;
		unsigned char strID3;
		unsigned char strID4;
		int a, b;
		wchar_t tmp;
		wchar_t strBuff[MAX_BUFF_SIZE];

		rewind(tmpFile);

		//write BOM (byte order mark) 0xFEFF = UTF-16 little-endian
		fwrite(&BOM_UTF16LE, 2, 1, outFile);

		while (1){
			if (fread(&strSize, 4, 1, tmpFile) != 1) break;
			if (fread(&strType, 4, 1, tmpFile) != 1) break;
			if (fread(&strID1, 4, 1, tmpFile) != 1) break;
			if (fread(&strID2, 2, 1, tmpFile) != 1) break;
			if (fread(&strID3, 1, 1, tmpFile) != 1) break;
			if (fread(&strID4, 1, 1, tmpFile) != 1) break;
			b = 0;
			for (a = 0; a < strSize + 2; a++) {
				if (fread(&tmp, 2, 1, tmpFile) != 1) break;
				if (tmp == CHAR_LF) {
					strBuff[b] = L'\\';
					b++;
					strBuff[b] = L'n';
					b++;
				} else {
					strBuff[b] = tmp;
					b++;
				}
			}

			fwprintf(outFile, L"%u\t%u\t%u\t%u\t%u\t\"%s\"\r\n", strType, strID1, strID2, strID3, strID4, strBuff);
		}
	} else if (result == Z_MEM_ERROR) {
		printf("ERROR: Not enough memory.");
	} else if (result == Z_BUF_ERROR) {
		printf("ERROR: Output buffer is too small.");
	} else if (result == Z_DATA_ERROR) {
		printf("ERROR: Input data are corrupted or incomplete.");
	}

	fclose(srcFile);
	fclose(tmpFile);
	fclose(outFile);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4048, %rsp           # imm = 0x4048
cmpl $0x3, %edi
jne 0x4018ac
movq 0x8(%rsi), %r15
movq 0x10(%rsi), %r14
leaq 0x9e877(%rip), %rsi     # 0x4a0048
movq %r15, %rdi
callq 0x419760
testq %rax, %rax
je 0x4018c1
movq %rax, %rbx
callq 0x4120d0
movq %rax, %r15
leaq 0x9e86a(%rip), %rsi     # 0x4a005e
movq %r14, %rdi
callq 0x419760
movq %rax, 0x10(%rsp)
leaq 0x38(%rsp), %r13
movq $0x0, (%r13)
movq %rbx, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x41d510
movq %rbx, %rdi
callq 0x419960
leaq -0x4(%rax), %rbp
movq %rbx, %rdi
callq 0x41d5f0
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %rbx, %rcx
callq 0x419860
movl $0x1, %esi
movq %rbp, %rdi
callq 0x42a020
movq %rax, %r14
movq (%r13), %rdi
movl $0x1, %esi
callq 0x42a020
movq %rax, %r12
movl $0x1, %esi
movq %r14, %rdi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x419860
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %rbp, %rcx
callq 0x401ca0
addl $0x5, %eax
cmpl $0x5, %eax
ja 0x4018f0
leaq 0x9e76a(%rip), %rcx     # 0x4a0004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x9e7d1(%rip), %rdi     # 0x4a007b
jmp 0x4018e9
leaq 0x9e76d(%rip), %rdi     # 0x4a0020
xorl %eax, %eax
callq 0x411f40
movl $0x1, %eax
jmp 0x40190c
leaq 0x9e783(%rip), %rdi     # 0x4a004b
movq %r15, %rsi
xorl %eax, %eax
callq 0x411f40
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x40190c
leaq 0x9e7be(%rip), %rdi     # 0x4a009e
jmp 0x4018e9
leaq 0x9e778(%rip), %rdi     # 0x4a0061
xorl %eax, %eax
callq 0x411f40
movq %rbx, %rdi
callq 0x419030
movq %r15, %rdi
callq 0x419030
movq 0x10(%rsp), %rdi
callq 0x419030
xorl %eax, %eax
addq $0x4048, %rsp           # imm = 0x4048
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x38(%rsp), %rdx
movl $0x1, %esi
movq %r12, %rdi
movq %r15, %rcx
callq 0x419ab0
movq %r15, %rdi
callq 0x41d5f0
leaq 0x9e6da(%rip), %rdi     # 0x4a001c
movl $0x2, %esi
movl $0x1, %edx
movq 0x10(%rsp), %rcx
callq 0x419ab0
leaq 0x20(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x419860
cmpq $0x1, %rax
jne 0x4018f0
leaq 0x30(%rsp), %r13
leaq 0x1c(%rsp), %rbp
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r15, %rcx
callq 0x419860
cmpq $0x1, %rax
jne 0x4018f0
movl $0x4, %esi
movl $0x1, %edx
leaq 0x28(%rsp), %rdi
movq %r15, %rcx
callq 0x419860
cmpq $0x1, %rax
jne 0x4018f0
movl $0x2, %esi
movl $0x1, %edx
leaq 0xe(%rsp), %rdi
movq %r15, %rcx
callq 0x419860
cmpq $0x1, %rax
jne 0x4018f0
movl $0x1, %esi
movl $0x1, %edx
leaq 0xd(%rsp), %rdi
movq %r15, %rcx
callq 0x419860
cmpq $0x1, %rax
jne 0x4018f0
movl $0x1, %esi
movl $0x1, %edx
leaq 0xc(%rsp), %rdi
movq %r15, %rcx
callq 0x419860
cmpq $0x1, %rax
jne 0x4018f0
movq 0x20(%rsp), %r12
addq $0x2, %r12
je 0x401a82
xorl %r14d, %r14d
movl $0x2, %esi
movl $0x1, %edx
movq %rbp, %rdi
movq %r15, %rcx
callq 0x419860
cmpq $0x1, %rax
jne 0x401a82
movl 0x1c(%rsp), %edx
leal 0x1(%r14), %eax
movslq %r14d, %rcx
cmpl $0xa, %edx
jne 0x401a76
movl $0x5c, 0x40(%rsp,%rcx,4)
cltq
movl $0x6e, 0x40(%rsp,%rax,4)
addl $0x2, %ecx
movl %ecx, %eax
jmp 0x401a7a
movl %edx, 0x40(%rsp,%rcx,4)
movl %eax, %r14d
decq %r12
jne 0x401a32
movq 0x30(%rsp), %rdx
movq 0x28(%rsp), %rcx
movzwl 0xe(%rsp), %r8d
movzbl 0xd(%rsp), %r9d
movzbl 0xc(%rsp), %r10d
movq 0x10(%rsp), %rdi
leaq 0x9e626(%rip), %rsi     # 0x4a00d0
xorl %eax, %eax
leaq 0x40(%rsp), %r11
pushq %r11
pushq %r10
callq 0x41a0a0
addq $0x10, %rsp
movl $0x4, %esi
movl $0x1, %edx
leaq 0x20(%rsp), %rdi
movq %r15, %rcx
callq 0x419860
cmpq $0x1, %rax
je 0x401981
jmp 0x4018f0
nopw %cs:(%rax,%rax)
nop