// File: /workspace/github_c_makefile_star2_O0/calmopyrin[P]tedplay/tedplay.cpp
// Function: tedplayMain(char*, Audio*)
int tedplayMain(char *fileName, Audio *player_)
{
	size_t bufLength;
	unsigned char *buf = 0;

	if (!player) {
		player = player_;
		if (!player)
			return 1;
	}

	if (!readFile(fileName, &buf, &bufLength)) {

		tedplayPause();
		machineReset();
		tedplayPlay();
		// if we do not flush the buffer, the reset may not get executed
		player->flush();
		// let it run so that the reset sequence can finish
		player->sleep(150);
		tedplayStop();

		psidHdr.fileName = fileName;

		if (!strncmp((const char *) buf + 1, "SID", 3) || !strncmp((const char *) buf + 1, "TED", 3)) {
			psidHdr.loadAddress = buf[PSID_START + 1] + (buf[PSID_START] << 8);
			unsigned int corr = 0;
			// zero load address means PRG module
			if (!psidHdr.loadAddress) {
				psidHdr.loadAddress = buf[PSID_MAX_HEADER_LENGTH] + (buf[PSID_MAX_HEADER_LENGTH + 1] << 8);
				corr = 2;
			}
			unsigned short initAddr = buf[PSID_INIT + 1] + (buf[PSID_INIT] << 8);
			unsigned short replayAddr = buf[PSID_MAIN + 1] + (buf[PSID_MAIN] << 8);
			// zero init address means equal to load address
			psidHdr.initAddress = initAddr ? initAddr : psidHdr.loadAddress;
			// 0 means an IRQ handler will be installed in the init routine
			// (most likely nothing happens)
			psidHdr.replayAddress = replayAddr ? replayAddr : 0xfe33;
			psidHdr.defaultTune = readPsid16(buf, PSID_DEFSONG);
			psidHdr.current = psidHdr.defaultTune;
			psidHdr.version = readPsid16(buf, PSID_VERSION);
			psidHdr.tracks = readPsid16(buf, PSID_NUMBER);
			psidHdr.speed = readPsid32(buf, PSID_SPEED);
			parsePsid(buf, psidHdr);

			// setup the SID card
			SIDsound *sid = ted->getSidCard();
			if (sid) {
				// Disable the ROM & IRQ's, so almost the entire memory is RAM
				// more SID's are likely to play
				ted->cpuptr->setST(ted->cpuptr->getST() | 4);
				ted->ChangeMemBankSetup(true);
				// v2 SID files have flags
				if (psidHdr.version == 2 && (readPsid16(buf, PSID_FLAGS) & 0x20)) {
					if (sidModelSelect != SID6581 + 1)
						sid->setModel(SID8580);
					else
						sid->setModel(SID6581);
					strcpy(psidHdr.model, "SID8580");
				} else {
					if (sidModelSelect != SID8580 + 1)
						sid->setModel(SID6581);
					else
						sid->setModel(SID8580);
					strcpy(psidHdr.model, "SID6581");
				}
			} else {
				strcpy(psidHdr.model, "TED8360?");
			}
			if (buf[0] == 'P') { // PSID/PTED
				psidHdr.type = 0;
#ifdef NEWPLAYER
				psidPlayer[0x22] = psidHdr.defaultTune - 1;
				psidPlayer[0x5b] = psidHdr.initAddress & 0xff;
				psidPlayer[0x5c] = psidHdr.initAddress >> 8;
				if (psidHdr.replayAddress) {
					psidPlayer[0xb1] = psidHdr.replayAddress & 0xff;
					psidPlayer[0xb2] = psidHdr.replayAddress >> 8;
				}
				psidPlayer[0xA3] = psidHdr.speed & 0xFF;
				psidPlayer[0xA4] = (psidHdr.speed >> 8) & 0xFF;
				psidPlayer[0xA5] = (psidHdr.speed >> 16) & 0xFF;
				psidPlayer[0xA6] = (psidHdr.speed >> 24) & 0xFF;
#else
				ted->Write(0xFF0A, 0x00); // no IRQ allowed
				ted->Write(0xFF09, ted->Read(0xFF09)); // ack IRQ
#if 1
				tmfPlayer[1] = psidHdr.defaultTune - 1;
				tmfPlayer[3] = psidHdr.initAddress & 0xff;
				tmfPlayer[4] = psidHdr.initAddress >> 8;
				tmfPlayer[6] = 0x00;
				// 60 Hz or Vblank based - we use timers anyway
				unsigned short timer = (TED_SOUND_CLOCK * 4) / (psidHdr.speed & (1 << tmfPlayer[1]) ? 60 : 50);
				tmfPlayer[11] = timer & 0xFF;
				tmfPlayer[16] = timer >> 8;
				tmfPlayer[32] = psidHdr.replayAddress & 0xff;
				tmfPlayer[33] = psidHdr.replayAddress >> 8;

				ted->writeProtectedPlayerMemory(playerStartAddress, tmfPlayer, sizeof(tmfPlayer));
#else
				psidPlayer[1] = psidHdr.defaultTune - 1;
				psidPlayer[3] = psidHdr.initAddress & 0xff;
				psidPlayer[4] = psidHdr.initAddress >> 8;
				psidPlayer[22] = psidHdr.replayAddress & 0xff;
				psidPlayer[23] = psidHdr.replayAddress >> 8;
				ted->writeProtectedPlayerMemory(playerStartAddress, psidPlayer, sizeof(psidPlayer));
#endif
#endif
			} else if (buf[0] == 'R') { // RSID/RTED
				ted->Write(0xFF13, 0xD3); // slow mode to match C64 frequency better
				psidHdr.type = 1;
				rsidPlayer[1] = psidHdr.defaultTune - 1;
				rsidPlayer[3] = psidHdr.initAddress & 0xff;
				rsidPlayer[4] = psidHdr.initAddress >> 8;
				if (replayAddr != 0) {
					rsidPlayer[5] = 0x4C; // JMP
					rsidPlayer[6] = psidHdr.replayAddress & 0xff;
					rsidPlayer[7] = psidHdr.replayAddress >> 8;
				} else {
					rsidPlayer[5] = 0x18; // CLC
					rsidPlayer[6] = 0x90; // BCC *-2
					rsidPlayer[7] = 0xfe;
				}
				ted->writeProtectedPlayerMemory(playerStartAddress, rsidPlayer, sizeof(rsidPlayer));
			}
			ted->injectCodeToRAM(psidHdr.loadAddress, buf + PSID_MAX_HEADER_LENGTH + corr,
				bufLength - PSID_MAX_HEADER_LENGTH - corr);
		}
		else if (!strncmp((const char*)buf + 17, "TEDMUSIC", 8)) {
			// specs http://siz.hu/en/tedmusiccollection
			psidHdr.type = 3;
			psidHdr.version = buf[25];
			psidHdr.tracks = buf[34];
			psidHdr.defaultTune = psidHdr.current = 1;
			psidHdr.replayAddress = playerStartAddress;
			memset(psidHdr.title, 0, sizeof(psidHdr.title));
			memset(psidHdr.author, 0, sizeof(psidHdr.author));
			memset(psidHdr.copyright, 0, sizeof(psidHdr.copyright));
			copyPetsciiToAsc(psidHdr.title, (char*)buf + 65, 32);
			trimTrailingSpace(psidHdr.title, 32);
			copyPetsciiToAsc(psidHdr.author, (char*)buf + 97, 32);
			trimTrailingSpace(psidHdr.author, 32);
			copyPetsciiToAsc(psidHdr.copyright, (char*)buf + 129, 32);
			trimTrailingSpace(psidHdr.copyright, 32);
			psidHdr.loadAddress = buf[0] + (buf[1] << 8);
			psidHdr.initAddress = buf[30] + (buf[31] << 8);
			psidHdr.replayAddress = buf[32] + (buf[33] << 8);
			// The offset where song data starts from the beginning of the file (load address included)
			unsigned int musicDataOffset = buf[26] + (buf[27] << 8);
			// Memory address where the real music data should be loaded (in case of BASIC load this is the address where the music should be relocated to)
			unsigned int relocatedAddress = buf[28] + (buf[29] << 8);
			// Music data size
			size_t musicDataSize = bufLength - musicDataOffset;
			// Timing
			unsigned char timingLo = buf[36];
			unsigned char timingHi = buf[37];

			tmfPlayer[1] = psidHdr.defaultTune - 1;
			tmfPlayer[3] = psidHdr.initAddress & 0xff;
			tmfPlayer[4] = psidHdr.initAddress >> 8;
			tmfPlayer[6] = (buf[38] & 1) ? 0x00 : 0x1B;
			// timer based timing? FIXME: PAL only
			if (buf[35] >= 2 && buf[35] < 6) {
				tmfPlayer[11] = timingLo;
				tmfPlayer[16] = timingHi;
			}
			// Vblank based - we use timers anyway
			else {
				unsigned int frameRate = (buf[35] == 1 || buf[35] == 7) ? 60 : 50;
				unsigned short timer = (TED_SOUND_CLOCK * 4) / frameRate;
				// doubleSpeed?
				if (buf[35] >= 6)
					timer /= 2;
				tmfPlayer[11] = timer & 0xFF;
				tmfPlayer[16] = timer >> 8;
			}
			tmfPlayer[32] = psidHdr.replayAddress & 0xff;
			tmfPlayer[33] = psidHdr.replayAddress >> 8;

			// if offset is illegal fall back to normal load
			if (int(musicDataSize) - 257 < 0) {
				ted->injectCodeToRAM(psidHdr.loadAddress, buf + 2, bufLength - 2);
			}
			else {
				psidHdr.loadAddress = relocatedAddress;
				ted->injectCodeToRAM(relocatedAddress, buf + musicDataOffset, musicDataSize);
			}
			ted->writeProtectedPlayerMemory(playerStartAddress, tmfPlayer, sizeof(tmfPlayer));

			strcpy(psidHdr.model, "TED8360");
			if (buf[38] & 2) {
				SIDsound* sid = ted->getSidCard();
				if (sid) {
					if (sidModelSelect != SID6581 + 1)
						sid->setModel(SID8580);
					else
						sid->setModel(SID6581);
				}
			}

		} else if (!strncmp((const char *) buf, "CBM8M", 5)) {
			CbmTune tune;
			tune.parse(fileName);
			psidHdr.type = 2;
			strcpy(psidHdr.title, tune.getName());
			strcpy(psidHdr.author, tune.getAuthor());
			strcpy(psidHdr.copyright, tune.getReleaseDate());
			unsigned int dataLen = tune.getDumpLength(0);
			psidHdr.loadAddress = tune.getLoadAddress(0);
			unsigned short initAddr = tune.getInitAddress(0);
			unsigned short replayAddr = tune.getPlayAddress(0);
			unsigned char *playerData;
			unsigned int playerLength;
			psidHdr.version = 1;
			psidHdr.tracks = tune.getNrOfSubtunes() + 1;
			psidHdr.defaultTune = tune.getDefaultSubtune() + 1;
			psidHdr.current = psidHdr.defaultTune;
			psidHdr.initAddress = initAddr;
			psidHdr.replayAddress = replayAddr;
			if (replayAddr) {
				psidPlayer[1] = psidHdr.defaultTune;
				psidPlayer[3] = psidHdr.loadAddress & 0xff;
				psidPlayer[4] = psidHdr.loadAddress >> 8;
				psidPlayer[22] = psidHdr.replayAddress & 0xff;
				psidPlayer[23] = psidHdr.replayAddress >> 8;
				playerLength = 33;
				playerData = psidPlayer;
			} else {
				rsidPlayer[1] = psidHdr.defaultTune;
				rsidPlayer[2] = 0x4C; // JMP
				rsidPlayer[3] = psidHdr.loadAddress & 0xff;
				rsidPlayer[4] = psidHdr.loadAddress >> 8;
				playerLength = 8;
				playerData = rsidPlayer;
			}
			strcpy(psidHdr.model, "TED8360");

			ted->injectCodeToRAM(psidHdr.loadAddress, tune.getBinaryData(0), dataLen - 2);
			ted->writeProtectedPlayerMemory(playerStartAddress, playerData, playerLength);
		} else { // PRG
			psidHdr.type = -1;
			psidHdr.version = 0;
			psidHdr.tracks = psidHdr.defaultTune = 1;
			psidHdr.current = 1;
			psidHdr.replayAddress = playerStartAddress;
			strcpy(psidHdr.title, fileName);
			strcpy(psidHdr.author, "Unknown");
			strcpy(psidHdr.copyright, "Unknown");
			strcpy(psidHdr.model, "Unknown");
			psidHdr.loadAddress = buf[0] + (buf[1] << 8);

			ted->injectCodeToRAM(psidHdr.loadAddress, buf + 2, bufLength - 2);
			ted->writeProtectedPlayerMemory(playerStartAddress, prgPlayer, sizeof(prgPlayer));
			SIDsound *sid = ted->getSidCard();
			if (sid) {
				if (sidModelSelect != SID6581 + 1)
					sid->setModel(SID8580);
				else
					sid->setModel(SID6581);
			}
		}
#if 1
		cpu->setPC(playerStartAddress);
#else
		char start[64];
		ted->copyToKbBuffer("M\317:\r");
		player->sleep(200);
		sprintf(start, "G%04X\r", playerStartAddress);
		ted->copyToKbBuffer(start);
#endif
		try {
			tedplayPlay();
		} catch (char *txt) {
			std::cerr << "Exception: " << txt << std::endl;
			exit(2);
		}
#if 0 //_DEBUG
		//Sleep(3000);
		if (fileName) {
			std::string fn = fileName;
			fn += ".bin";
			dumpMem(fn);
		}
#endif
		return 0;
	}
	if (fileName) {
		std::cerr << "Error opening: " << fileName << std::endl;
		return 1;
	}
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq $0x0, -0x28(%rbp)
cmpq $0x0, 0x29f15(%rip)     # 0x462b8
jne 0x1c3c8
movq -0x18(%rbp), %rax
movq %rax, 0x29f08(%rip)     # 0x462b8
cmpq $0x0, 0x29f00(%rip)     # 0x462b8
jne 0x1c3c6
movl $0x1, -0x4(%rbp)
jmp 0x1d270
jmp 0x1c3c8
movq -0x10(%rbp), %rdi
leaq -0x28(%rbp), %rsi
leaq -0x20(%rbp), %rdx
callq 0x1b630
cmpl $0x0, %eax
jne 0x1d22b
callq 0x1bd10
callq 0x1ba90
callq 0x1bd50
movq 0x29ec0(%rip), %rdi     # 0x462b8
movq (%rdi), %rax
callq *0x30(%rax)
movq 0x29eb3(%rip), %rdi     # 0x462b8
movq (%rdi), %rax
movl $0x96, %esi
callq *0x28(%rax)
callq 0x1bdb0
movq -0x10(%rbp), %rsi
leaq 0x197e8(%rip), %rdi     # 0x35c08
callq 0x3340
movq -0x28(%rbp), %rdi
addq $0x1, %rdi
leaq 0x37d2(%rip), %rsi      # 0x1fc06
movl $0x3, %edx
callq 0x3110
cmpl $0x0, %eax
je 0x1c465
movq -0x28(%rbp), %rdi
addq $0x1, %rdi
leaq 0x38ba(%rip), %rsi      # 0x1fd0c
movl $0x3, %edx
callq 0x3110
cmpl $0x0, %eax
jne 0x1c8e2
movq -0x28(%rbp), %rax
movzbl 0x9(%rax), %eax
movq -0x28(%rbp), %rcx
movzbl 0x8(%rcx), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movl %eax, 0x197b4(%rip)     # 0x35c34
movl $0x0, -0x2c(%rbp)
cmpl $0x0, 0x197a6(%rip)     # 0x35c34
jne 0x1c4b2
movq -0x28(%rbp), %rax
movzbl 0x7c(%rax), %eax
movq -0x28(%rbp), %rcx
movzbl 0x7d(%rcx), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movl %eax, 0x19789(%rip)     # 0x35c34
movl $0x2, -0x2c(%rbp)
movq -0x28(%rbp), %rax
movzbl 0xb(%rax), %eax
movq -0x28(%rbp), %rcx
movzbl 0xa(%rcx), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movw %ax, -0x2e(%rbp)
movq -0x28(%rbp), %rax
movzbl 0xd(%rax), %eax
movq -0x28(%rbp), %rcx
movzbl 0xc(%rcx), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movw %ax, -0x30(%rbp)
cmpw $0x0, -0x2e(%rbp)
je 0x1c4f7
movzwl -0x2e(%rbp), %eax
movl %eax, -0xe4(%rbp)
jmp 0x1c503
movl 0x19737(%rip), %eax     # 0x35c34
movl %eax, -0xe4(%rbp)
movl -0xe4(%rbp), %eax
movl %eax, 0x1971d(%rip)     # 0x35c2c
cmpw $0x0, -0x30(%rbp)
je 0x1c522
movzwl -0x30(%rbp), %eax
movl %eax, -0xe8(%rbp)
jmp 0x1c52f
movl $0xfe33, %eax           # imm = 0xFE33
movl %eax, -0xe8(%rbp)
jmp 0x1c52f
movl -0xe8(%rbp), %eax
movl %eax, 0x196f5(%rip)     # 0x35c30
movq -0x28(%rbp), %rdi
movl $0x10, %esi
callq 0x1d600
movzwl %ax, %eax
movl %eax, 0x196e6(%rip)     # 0x35c38
movl 0x196e0(%rip), %eax     # 0x35c38
movl %eax, 0x19d26(%rip)     # 0x36284
movq -0x28(%rbp), %rdi
movl $0x4, %esi
callq 0x1d600
movzwl %ax, %eax
movl %eax, 0x19d0b(%rip)     # 0x36280
movq -0x28(%rbp), %rdi
movl $0xe, %esi
callq 0x1d600
movzwl %ax, %eax
movl %eax, 0x1969c(%rip)     # 0x35c28
movq -0x28(%rbp), %rdi
movl $0x12, %esi
callq 0x1d630
movl %eax, 0x19cf8(%rip)     # 0x36298
movq -0x28(%rbp), %rdi
leaq 0x1965d(%rip), %rsi     # 0x35c08
callq 0x1b020
movq 0x19ce9(%rip), %rdi     # 0x362a0
callq 0x1d480
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x1c6ae
movq 0x19cce(%rip), %rax     # 0x362a0
movq 0x45fb8(%rax), %rax
movq %rax, -0xf0(%rbp)
movq 0x19cb9(%rip), %rax     # 0x362a0
movq 0x45fb8(%rax), %rdi
callq 0x1d6b0
movq -0xf0(%rbp), %rdi
orl $0x4, %eax
movzbl %al, %esi
callq 0x1d690
movq 0x19c94(%rip), %rdi     # 0x362a0
movl $0x1, %esi
callq 0x16460
cmpl $0x2, 0x19c63(%rip)     # 0x36280
jne 0x1c673
movq -0x28(%rbp), %rdi
movl $0x76, %esi
callq 0x1d600
movzwl %ax, %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x1c673
cmpl $0x1, 0x19c69(%rip)     # 0x362a8
je 0x1c652
movq -0x38(%rbp), %rdi
movq (%rdi), %rax
movl $0x1, %esi
callq *0x20(%rax)
jmp 0x1c65e
movq -0x38(%rbp), %rdi
movq (%rdi), %rax
xorl %esi, %esi
callq *0x20(%rax)
leaq 0x19bd7(%rip), %rdi     # 0x3623c
leaq 0x35e8(%rip), %rsi      # 0x1fc54
callq 0x3230
jmp 0x1c6ac
cmpl $0x2, 0x19c2e(%rip)     # 0x362a8
je 0x1c68a
movq -0x38(%rbp), %rdi
movq (%rdi), %rax
xorl %esi, %esi
callq *0x20(%rax)
jmp 0x1c699
movq -0x38(%rbp), %rdi
movq (%rdi), %rax
movl $0x1, %esi
callq *0x20(%rax)
leaq 0x19b9c(%rip), %rdi     # 0x3623c
leaq 0x35a5(%rip), %rsi      # 0x1fc4c
callq 0x3230
jmp 0x1c6c1
leaq 0x19b87(%rip), %rdi     # 0x3623c
leaq 0x3654(%rip), %rsi      # 0x1fd10
callq 0x3230
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x50, %eax
jne 0x1c7ea
movl $0x0, 0x19b81(%rip)     # 0x3625c
movq 0x19bbe(%rip), %rdi     # 0x362a0
movq (%rdi), %rax
movl $0xff0a, %esi           # imm = 0xFF0A
xorl %edx, %edx
callq *(%rax)
movq 0x19bab(%rip), %rax     # 0x362a0
movq %rax, -0xf8(%rbp)
movq 0x19b9d(%rip), %rdi     # 0x362a0
movq (%rdi), %rax
movl $0xff09, %esi           # imm = 0xFF09
callq *0x8(%rax)
movq -0xf8(%rbp), %rdi
movb %al, %cl
movq (%rdi), %rax
movl $0xff09, %esi           # imm = 0xFF09
movzbl %cl, %edx
callq *(%rax)
movl 0x1950e(%rip), %eax     # 0x35c38
subl $0x1, %eax
movb %al, 0x17b8e(%rip)      # 0x342c1
movl 0x194f3(%rip), %eax     # 0x35c2c
andl $0xff, %eax
movb %al, 0x17b7f(%rip)      # 0x342c3
movl 0x194e2(%rip), %eax     # 0x35c2c
shrl $0x8, %eax
movb %al, 0x17b71(%rip)      # 0x342c4
movb $0x0, 0x17b6c(%rip)     # 0x342c6
movl 0x19b38(%rip), %edx     # 0x36298
movzbl 0x17b5a(%rip), %ecx     # 0x342c1
movl $0x1, %eax
shll %cl, %eax
andl %eax, %edx
movl $0x32, %ecx
movl $0x3c, %eax
cmpl $0x0, %edx
cmovnel %eax, %ecx
movl $0xd87c0, %eax          # imm = 0xD87C0
cltd
idivl %ecx
movw %ax, -0x3a(%rbp)
movzwl -0x3a(%rbp), %eax
andl $0xff, %eax
movb %al, 0x17b30(%rip)      # 0x342cb
movzwl -0x3a(%rbp), %eax
sarl $0x8, %eax
movb %al, 0x17b28(%rip)      # 0x342d0
movl 0x19482(%rip), %eax     # 0x35c30
andl $0xff, %eax
movb %al, 0x17b27(%rip)      # 0x342e0
movl 0x19471(%rip), %eax     # 0x35c30
shrl $0x8, %eax
movb %al, 0x17b19(%rip)      # 0x342e1
movq 0x19ad1(%rip), %rdi     # 0x362a0
movl $0xfe00, %esi           # imm = 0xFE00
leaq 0x17ae5(%rip), %rdx     # 0x342c0
movl $0x25, %ecx
callq 0x1d550
jmp 0x1c8af
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x52, %eax
jne 0x1c8ad
movq 0x19a9f(%rip), %rdi     # 0x362a0
movq (%rdi), %rax
movl $0xff13, %esi           # imm = 0xFF13
movl $0xd3, %edx
callq *(%rax)
movl $0x1, 0x19a42(%rip)     # 0x3625c
movl 0x19418(%rip), %eax     # 0x35c38
subl $0x1, %eax
movb %al, 0x17a88(%rip)      # 0x342b1
movl 0x193fd(%rip), %eax     # 0x35c2c
andl $0xff, %eax
movb %al, 0x17a79(%rip)      # 0x342b3
movl 0x193ec(%rip), %eax     # 0x35c2c
shrl $0x8, %eax
movb %al, 0x17a6b(%rip)      # 0x342b4
movzwl -0x30(%rbp), %eax
cmpl $0x0, %eax
je 0x1c87b
movb $0x4c, 0x17a5c(%rip)    # 0x342b5
movl 0x193d1(%rip), %eax     # 0x35c30
andl $0xff, %eax
movb %al, 0x17a4c(%rip)      # 0x342b6
movl 0x193c0(%rip), %eax     # 0x35c30
shrl $0x8, %eax
movb %al, 0x17a3e(%rip)      # 0x342b7
jmp 0x1c890
movb $0x18, 0x17a33(%rip)    # 0x342b5
movb $-0x70, 0x17a2d(%rip)   # 0x342b6
movb $-0x2, 0x17a27(%rip)    # 0x342b7
movq 0x19a09(%rip), %rdi     # 0x362a0
movl $0xfe00, %esi           # imm = 0xFE00
leaq 0x17a0d(%rip), %rdx     # 0x342b0
movl $0x8, %ecx
callq 0x1d550
jmp 0x1c8af
movq 0x199ea(%rip), %rdi     # 0x362a0
movl 0x19378(%rip), %esi     # 0x35c34
movq -0x28(%rbp), %rdx
addq $0x7c, %rdx
movl -0x2c(%rbp), %eax
addq %rax, %rdx
movq -0x20(%rbp), %rcx
subq $0x7c, %rcx
movl -0x2c(%rbp), %eax
subq %rax, %rcx
callq 0x16b00
jmp 0x1d15d
movq -0x28(%rbp), %rdi
addq $0x11, %rdi
leaq 0x340e(%rip), %rsi      # 0x1fcff
movl $0x8, %edx
callq 0x3110
cmpl $0x0, %eax
jne 0x1ccdf
movl $0x3, 0x1994e(%rip)     # 0x3625c
movq -0x28(%rbp), %rax
movzbl 0x19(%rax), %eax
movl %eax, 0x19964(%rip)     # 0x36280
movq -0x28(%rbp), %rax
movzbl 0x22(%rax), %eax
movl %eax, 0x192fe(%rip)     # 0x35c28
movl $0x1, 0x19950(%rip)     # 0x36284
movl $0x1, 0x192fa(%rip)     # 0x35c38
movl $0xfe00, 0x192e8(%rip)  # imm = 0xFE00
leaq 0x192b9(%rip), %rdi     # 0x35c08
addq $0x34, %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x3130
leaq 0x192a2(%rip), %rdi     # 0x35c08
addq $0x234, %rdi            # imm = 0x234
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x3130
leaq 0x19288(%rip), %rdi     # 0x35c08
addq $0x434, %rdi            # imm = 0x434
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x3130
movq -0x28(%rbp), %rsi
addq $0x41, %rsi
leaq 0x19266(%rip), %rdi     # 0x35c08
addq $0x34, %rdi
movl $0x20, %edx
callq 0x1c320
leaq 0x19251(%rip), %rdi     # 0x35c08
addq $0x34, %rdi
movl $0x20, %esi
callq 0x1af80
movq -0x28(%rbp), %rsi
addq $0x61, %rsi
leaq 0x19234(%rip), %rdi     # 0x35c08
addq $0x234, %rdi            # imm = 0x234
movl $0x20, %edx
callq 0x1c320
leaq 0x1921c(%rip), %rdi     # 0x35c08
addq $0x234, %rdi            # imm = 0x234
movl $0x20, %esi
callq 0x1af80
movq -0x28(%rbp), %rsi
addq $0x81, %rsi
leaq 0x191f9(%rip), %rdi     # 0x35c08
addq $0x434, %rdi            # imm = 0x434
movl $0x20, %edx
callq 0x1c320
leaq 0x191e1(%rip), %rdi     # 0x35c08
addq $0x434, %rdi            # imm = 0x434
movl $0x20, %esi
callq 0x1af80
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
movq -0x28(%rbp), %rcx
movzbl 0x1(%rcx), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movl %eax, 0x191e2(%rip)     # 0x35c34
movq -0x28(%rbp), %rax
movzbl 0x1e(%rax), %eax
movq -0x28(%rbp), %rcx
movzbl 0x1f(%rcx), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movl %eax, 0x191bf(%rip)     # 0x35c2c
movq -0x28(%rbp), %rax
movzbl 0x20(%rax), %eax
movq -0x28(%rbp), %rcx
movzbl 0x21(%rcx), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movl %eax, 0x191a8(%rip)     # 0x35c30
movq -0x28(%rbp), %rax
movzbl 0x1a(%rax), %eax
movq -0x28(%rbp), %rcx
movzbl 0x1b(%rcx), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movl %eax, -0x40(%rbp)
movq -0x28(%rbp), %rax
movzbl 0x1c(%rax), %eax
movq -0x28(%rbp), %rcx
movzbl 0x1d(%rcx), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movl %eax, -0x44(%rbp)
movq -0x20(%rbp), %rax
movl -0x40(%rbp), %ecx
subq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x28(%rbp), %rax
movb 0x24(%rax), %al
movb %al, -0x51(%rbp)
movq -0x28(%rbp), %rax
movb 0x25(%rax), %al
movb %al, -0x52(%rbp)
movl 0x19158(%rip), %eax     # 0x35c38
subl $0x1, %eax
movb %al, 0x177d8(%rip)      # 0x342c1
movl 0x1913d(%rip), %eax     # 0x35c2c
andl $0xff, %eax
movb %al, 0x177c9(%rip)      # 0x342c3
movl 0x1912c(%rip), %eax     # 0x35c2c
shrl $0x8, %eax
movb %al, 0x177bb(%rip)      # 0x342c4
movq -0x28(%rbp), %rax
movzbl 0x26(%rax), %edx
andl $0x1, %edx
movl $0x1b, %eax
xorl %ecx, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movb %al, 0x1779f(%rip)      # 0x342c6
movq -0x28(%rbp), %rax
movzbl 0x23(%rax), %eax
cmpl $0x2, %eax
jl 0x1cb58
movq -0x28(%rbp), %rax
movzbl 0x23(%rax), %eax
cmpl $0x6, %eax
jge 0x1cb58
movb -0x51(%rbp), %al
movb %al, 0x17781(%rip)      # 0x342cb
movb -0x52(%rbp), %al
movb %al, 0x1777d(%rip)      # 0x342d0
jmp 0x1cbe1
movq -0x28(%rbp), %rax
movzbl 0x23(%rax), %ecx
movb $0x1, %al
cmpl $0x1, %ecx
movb %al, -0xf9(%rbp)
je 0x1cb81
movq -0x28(%rbp), %rax
movzbl 0x23(%rax), %eax
cmpl $0x7, %eax
sete %al
movb %al, -0xf9(%rbp)
movb -0xf9(%rbp), %dl
movl $0x32, %eax
movl $0x3c, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, -0x58(%rbp)
movl $0xd87c0, %eax          # imm = 0xD87C0
xorl %edx, %edx
divl -0x58(%rbp)
movw %ax, -0x5a(%rbp)
movq -0x28(%rbp), %rax
movzbl 0x23(%rax), %eax
cmpl $0x6, %eax
jl 0x1cbc5
movzwl -0x5a(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movw %ax, -0x5a(%rbp)
movzwl -0x5a(%rbp), %eax
andl $0xff, %eax
movb %al, 0x176f7(%rip)      # 0x342cb
movzwl -0x5a(%rbp), %eax
sarl $0x8, %eax
movb %al, 0x176ef(%rip)      # 0x342d0
movl 0x19049(%rip), %eax     # 0x35c30
andl $0xff, %eax
movb %al, 0x176ee(%rip)      # 0x342e0
movl 0x19038(%rip), %eax     # 0x35c30
shrl $0x8, %eax
movb %al, 0x176e0(%rip)      # 0x342e1
movq -0x50(%rbp), %rax
subl $0x101, %eax            # imm = 0x101
cmpl $0x0, %eax
jge 0x1cc33
movq 0x1968a(%rip), %rdi     # 0x362a0
movl 0x19018(%rip), %esi     # 0x35c34
movq -0x28(%rbp), %rdx
addq $0x2, %rdx
movq -0x20(%rbp), %rcx
subq $0x2, %rcx
callq 0x16b00
jmp 0x1cc59
movl -0x44(%rbp), %eax
movl %eax, 0x18ff8(%rip)     # 0x35c34
movq 0x1965d(%rip), %rdi     # 0x362a0
movl -0x44(%rbp), %esi
movq -0x28(%rbp), %rdx
movl -0x40(%rbp), %eax
addq %rax, %rdx
movq -0x50(%rbp), %rcx
callq 0x16b00
movq 0x19640(%rip), %rdi     # 0x362a0
movl $0xfe00, %esi           # imm = 0xFE00
leaq 0x17654(%rip), %rdx     # 0x342c0
movl $0x25, %ecx
callq 0x1d550
leaq 0x195bf(%rip), %rdi     # 0x3623c
leaq 0x3095(%rip), %rsi      # 0x1fd19
callq 0x3230
movq -0x28(%rbp), %rax
movzbl 0x26(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x1ccda
movq 0x19600(%rip), %rdi     # 0x362a0
callq 0x1d480
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x1ccd8
cmpl $0x1, 0x195f1(%rip)     # 0x362a8
je 0x1ccca
movq -0x68(%rbp), %rdi
movq (%rdi), %rax
movl $0x1, %esi
callq *0x20(%rax)
jmp 0x1ccd6
movq -0x68(%rbp), %rdi
movq (%rdi), %rax
xorl %esi, %esi
callq *0x20(%rax)
jmp 0x1ccd8
jmp 0x1ccda
jmp 0x1d15b
movq -0x28(%rbp), %rdi
leaq 0x1444(%rip), %rsi      # 0x1e12e
movl $0x5, %edx
callq 0x3110
cmpl $0x0, %eax
jne 0x1d02e
leaq -0xa8(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0x4450
movq -0x108(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x44f0
jmp 0x1cd22
movl $0x2, 0x19530(%rip)     # 0x3625c
leaq -0xa8(%rbp), %rdi
callq 0x4900
movq %rax, -0x110(%rbp)
jmp 0x1cd41
movq -0x110(%rbp), %rsi
leaq 0x18eed(%rip), %rdi     # 0x35c3c
callq 0x3230
leaq -0xa8(%rbp), %rdi
callq 0x4920
movq %rax, -0x118(%rbp)
jmp 0x1cd69
movq -0x118(%rbp), %rsi
leaq 0x190c5(%rip), %rdi     # 0x35e3c
callq 0x3230
leaq -0xa8(%rbp), %rdi
callq 0x4960
movq %rax, -0x120(%rbp)
jmp 0x1cd91
movq -0x120(%rbp), %rsi
leaq 0x1929d(%rip), %rdi     # 0x3603c
callq 0x3230
leaq -0xa8(%rbp), %rdi
xorl %esi, %esi
callq 0x4b50
movw %ax, -0x122(%rbp)
jmp 0x1cdbb
movw -0x122(%rbp), %ax
movzwl %ax, %eax
movl %eax, -0xb8(%rbp)
leaq -0xa8(%rbp), %rdi
xorl %esi, %esi
callq 0x4b70
movw %ax, -0x124(%rbp)
jmp 0x1cde2
movw -0x124(%rbp), %ax
movzwl %ax, %eax
movl %eax, 0x18e42(%rip)     # 0x35c34
leaq -0xa8(%rbp), %rdi
xorl %esi, %esi
callq 0x4b10
movw %ax, -0x126(%rbp)
jmp 0x1ce09
movw -0x126(%rbp), %ax
movw %ax, -0xba(%rbp)
leaq -0xa8(%rbp), %rdi
xorl %esi, %esi
callq 0x4b30
movw %ax, -0x128(%rbp)
jmp 0x1ce2e
movw -0x128(%rbp), %ax
movw %ax, -0xbc(%rbp)
movl $0x1, 0x1943a(%rip)     # 0x36280
leaq -0xa8(%rbp), %rdi
callq 0x49d0
movl %eax, -0x12c(%rbp)
jmp 0x1ce5a
movl -0x12c(%rbp), %eax
incl %eax
movl %eax, 0x18dc0(%rip)     # 0x35c28
leaq -0xa8(%rbp), %rdi
callq 0x4a70
movl %eax, -0x130(%rbp)
jmp 0x1ce7c
movl -0x130(%rbp), %eax
addl $0x1, %eax
movl %eax, 0x18dad(%rip)     # 0x35c38
movl 0x18da7(%rip), %eax     # 0x35c38
movl %eax, 0x193ed(%rip)     # 0x36284
movzwl -0xba(%rbp), %eax
movl %eax, 0x18d88(%rip)     # 0x35c2c
movzwl -0xbc(%rbp), %eax
movl %eax, 0x18d7f(%rip)     # 0x35c30
cmpw $0x0, -0xbc(%rbp)
je 0x1cf48
movl 0x18d73(%rip), %eax     # 0x35c38
movb %al, 0x173c6(%rip)      # 0x34291
movl 0x18d63(%rip), %eax     # 0x35c34
andl $0xff, %eax
movb %al, 0x173b7(%rip)      # 0x34293
movl 0x18d52(%rip), %eax     # 0x35c34
shrl $0x8, %eax
movb %al, 0x173a9(%rip)      # 0x34294
movl 0x18d3f(%rip), %eax     # 0x35c30
andl $0xff, %eax
movb %al, 0x173aa(%rip)      # 0x342a6
movl 0x18d2e(%rip), %eax     # 0x35c30
shrl $0x8, %eax
movb %al, 0x1739c(%rip)      # 0x342a7
movl $0x21, -0xcc(%rbp)
leaq 0x17374(%rip), %rax     # 0x34290
movq %rax, -0xc8(%rbp)
jmp 0x1cf93
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x4490
jmp 0x1d27c
movl 0x18cea(%rip), %eax     # 0x35c38
movb %al, 0x1735d(%rip)      # 0x342b1
movb $0x4c, 0x17357(%rip)    # 0x342b2
movl 0x18cd3(%rip), %eax     # 0x35c34
andl $0xff, %eax
movb %al, 0x17347(%rip)      # 0x342b3
movl 0x18cc2(%rip), %eax     # 0x35c34
shrl $0x8, %eax
movb %al, 0x17339(%rip)      # 0x342b4
movl $0x8, -0xcc(%rbp)
leaq 0x17324(%rip), %rax     # 0x342b0
movq %rax, -0xc8(%rbp)
leaq 0x192a2(%rip), %rdi     # 0x3623c
leaq 0x2d78(%rip), %rsi      # 0x1fd19
callq 0x3230
movq 0x192f3(%rip), %rax     # 0x362a0
movq %rax, -0x148(%rbp)
movl 0x18c7a(%rip), %eax     # 0x35c34
movl %eax, -0x13c(%rbp)
leaq -0xa8(%rbp), %rdi
xorl %esi, %esi
callq 0x4b90
movq %rax, -0x138(%rbp)
jmp 0x1cfd7
movq -0x138(%rbp), %rdx
movl -0x13c(%rbp), %esi
movq -0x148(%rbp), %rdi
movl -0xb8(%rbp), %eax
addl $-0x2, %eax
movl %eax, %ecx
callq 0x16b00
jmp 0x1cffd
movq 0x1929c(%rip), %rdi     # 0x362a0
movq -0xc8(%rbp), %rdx
movl -0xcc(%rbp), %ecx
movl $0xfe00, %esi           # imm = 0xFE00
callq 0x1d550
jmp 0x1d01d
leaq -0xa8(%rbp), %rdi
callq 0x4490
jmp 0x1d159
movl $0xffffffff, 0x19224(%rip) # imm = 0xFFFFFFFF
movl $0x0, 0x1923e(%rip)     # 0x36280
movl $0x1, 0x18bec(%rip)     # 0x35c38
movl $0x1, 0x18bd2(%rip)     # 0x35c28
movl $0x1, 0x19224(%rip)     # 0x36284
movl $0xfe00, 0x18bc6(%rip)  # imm = 0xFE00
movq -0x10(%rbp), %rsi
leaq 0x18bc7(%rip), %rdi     # 0x35c3c
callq 0x3230
leaq 0x18dbb(%rip), %rdi     # 0x35e3c
leaq 0x2c6f(%rip), %rsi      # 0x1fcf7
callq 0x3230
leaq 0x18fa8(%rip), %rdi     # 0x3603c
leaq 0x2c5c(%rip), %rsi      # 0x1fcf7
callq 0x3230
leaq 0x19195(%rip), %rdi     # 0x3623c
leaq 0x2c49(%rip), %rsi      # 0x1fcf7
callq 0x3230
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
movq -0x28(%rbp), %rcx
movzbl 0x1(%rcx), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movl %eax, 0x18b67(%rip)     # 0x35c34
movq 0x191cc(%rip), %rdi     # 0x362a0
movl 0x18b5a(%rip), %esi     # 0x35c34
movq -0x28(%rbp), %rdx
addq $0x2, %rdx
movq -0x20(%rbp), %rcx
subq $0x2, %rcx
callq 0x16b00
movq 0x191aa(%rip), %rdi     # 0x362a0
movl $0xfe00, %esi           # imm = 0xFE00
leaq 0x171e3(%rip), %rdx     # 0x342e5
movl $0x6, %ecx
callq 0x1d550
movq 0x1918d(%rip), %rdi     # 0x362a0
callq 0x1d480
movq %rax, -0xd8(%rbp)
cmpq $0x0, -0xd8(%rbp)
je 0x1d157
cmpl $0x1, 0x19178(%rip)     # 0x362a8
je 0x1d146
movq -0xd8(%rbp), %rdi
movq (%rdi), %rax
movl $0x1, %esi
callq *0x20(%rax)
jmp 0x1d155
movq -0xd8(%rbp), %rdi
movq (%rdi), %rax
xorl %esi, %esi
callq *0x20(%rax)
jmp 0x1d157
jmp 0x1d159
jmp 0x1d15b
jmp 0x1d15d
movq 0x2914c(%rip), %rdi     # 0x462b0
movl $0xfe00, %esi           # imm = 0xFE00
callq 0x4ff0
callq 0x1bd50
jmp 0x1d175
jmp 0x1d222
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x1d27c
movq -0xb0(%rbp), %rdi
callq 0x30e0
movq %rax, -0xe0(%rbp)
movq 0x6e37(%rip), %rdi      # 0x23ff0
leaq 0x2b61(%rip), %rsi      # 0x1fd21
callq 0x3250
movq %rax, -0x150(%rbp)
jmp 0x1d1ce
movq -0x150(%rbp), %rdi
movq -0xe0(%rbp), %rsi
callq 0x3250
movq %rax, -0x158(%rbp)
jmp 0x1d1ea
movq -0x158(%rbp), %rdi
movq 0x6dc0(%rip), %rsi      # 0x23fb8
callq 0x3290
jmp 0x1d1ff
movl $0x2, %edi
callq 0x3300
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
callq 0x33e0
jmp 0x1d27c
movl $0x0, -0x4(%rbp)
jmp 0x1d270
cmpq $0x0, -0x10(%rbp)
je 0x1d269
movq 0x6db7(%rip), %rdi      # 0x23ff0
leaq 0x2aed(%rip), %rsi      # 0x1fd2d
callq 0x3250
movq %rax, %rdi
movq -0x10(%rbp), %rsi
callq 0x3250
movq %rax, %rdi
movq 0x6d5d(%rip), %rsi      # 0x23fb8
callq 0x3290
movl $0x1, -0x4(%rbp)
jmp 0x1d270
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
movq -0xb0(%rbp), %rdi
callq 0x3410
nopl (%rax,%rax)