// File: kipa00[P]AplusB-mc/compiler/world.cpp
// Function: world::init(_IO_FILE*)
void world::init(FILE *fp) {
	int *arr = new int[1024];
	byte *temp = new byte[1048576];
	byte *inflated = new byte[1048576];
	int i;

	fseek(fp, 0, SEEK_SET);
	fread(temp, 1, 4096, fp);

	for (i=0; i<1024; ++i) {
		arr[i] = (temp[i << 2] << 16) | (temp[(i << 2) | 1] << 8) | temp[(i << 2) | 2];
	}

	// for performance issues
	for (i=0; i<1024; ++i) {
		if (arr[i]) {
			fseek(fp, arr[i] << 12, SEEK_SET);
			fread(temp, 1, 5, fp);
			int sz = (temp[0] << 24) | (temp[1] << 16) | (temp[2] << 8) | temp[3];
			if (temp[4] != 2) {
				throw UNSUPPORTED_COMPRESSION_ERROR;
			}
			fread(temp, 1, sz - 1, fp);

			{
				z_stream infstream;
				infstream.zalloc = Z_NULL;
				infstream.zfree = Z_NULL;
				infstream.opaque = Z_NULL;

				infstream.avail_in = (uInt)(sz - 1);
				infstream.next_in = (Bytef *)temp;
				infstream.avail_out = (uInt)1048576;
				infstream.next_out = (Bytef *)inflated;

				inflateInit(&infstream);
				if (inflate(&infstream, Z_NO_FLUSH) != Z_STREAM_END) {
					throw TOO_BIG_SECTION_ERROR;
				}
				inflateEnd(&infstream);
			}

			if (!this->world_data) {
				this->world_data = new chunk *[1024];
				memset(this->world_data, 0, sizeof(chunk *) * 1024);
			}
			this->world_data[i] = new chunk();
			try {
				this->world_data[i]->read(inflated);
			} catch (int reason) {
				if (reason == ATTR_VALUE_MISMATCH_ERROR) {
					fprintf(stderr, "Error: attribute does not have possible value; did you edit the world using hex editor?\n");
				}
				if (!this->world_data[i]->troubleshoot(i & 31, i >> 5)) {
					fprintf(stderr, "in chunk (%d, %d), exact position unknown.\n", i & 31, i >> 5);
				}
				throw reason;
			}
		}
	}
	delete []temp;
	delete []inflated;
	delete []arr;
}

// Assembly:
subq $0xe8, %rsp
movq %rdi, 0xe0(%rsp)
movq %rsi, 0xd8(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x30(%rsp)
movl $0x1000, %edi           # imm = 0x1000
callq 0x2030
movq %rax, 0xd0(%rsp)
movl $0x100000, %edi         # imm = 0x100000
callq 0x2030
movq %rax, 0xc8(%rsp)
movl $0x100000, %edi         # imm = 0x100000
callq 0x2030
movq %rax, 0xc0(%rsp)
movq 0xd8(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x2230
movq 0xc8(%rsp), %rdi
movq 0xd8(%rsp), %rcx
movl $0x1, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x22b0
movl $0x0, 0xbc(%rsp)
cmpl $0x400, 0xbc(%rsp) # imm = 0x400
jge 0x9d1c
movq 0xc8(%rsp), %rax
movl 0xbc(%rsp), %ecx
shll $0x2, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %edx
shll $0x10, %edx
movq 0xc8(%rsp), %rax
movl 0xbc(%rsp), %ecx
shll $0x2, %ecx
orl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
shll $0x8, %eax
orl %eax, %edx
movq 0xc8(%rsp), %rax
movl 0xbc(%rsp), %ecx
shll $0x2, %ecx
orl $0x2, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
orl %eax, %edx
movq 0xd0(%rsp), %rax
movslq 0xbc(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x9c87
movl $0x0, 0xbc(%rsp)
cmpl $0x400, 0xbc(%rsp) # imm = 0x400
jge 0xa0a9
movq 0xd0(%rsp), %rax
movslq 0xbc(%rsp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0xa091
movq 0xd8(%rsp), %rdi
movq 0xd0(%rsp), %rax
movslq 0xbc(%rsp), %rcx
movl (%rax,%rcx,4), %eax
shll $0xc, %eax
movslq %eax, %rsi
xorl %edx, %edx
callq 0x2230
movq 0xc8(%rsp), %rdi
movq 0xd8(%rsp), %rcx
movl $0x1, %esi
movl $0x5, %edx
callq 0x22b0
movq 0xc8(%rsp), %rax
movzbl (%rax), %eax
shll $0x18, %eax
movq 0xc8(%rsp), %rcx
movzbl 0x1(%rcx), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movq 0xc8(%rsp), %rcx
movzbl 0x2(%rcx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movq 0xc8(%rsp), %rcx
movzbl 0x3(%rcx), %ecx
orl %ecx, %eax
movl %eax, 0xb8(%rsp)
movq 0xc8(%rsp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x2, %eax
je 0x9e12
movl $0x4, %edi
callq 0x20d0
movq %rax, %rdi
movl $0xfffffffc, (%rdi)     # imm = 0xFFFFFFFC
movq 0x101bf(%rip), %rsi     # 0x19fc8
xorl %eax, %eax
movl %eax, %edx
callq 0x22f0
movq 0xc8(%rsp), %rdi
movl 0xb8(%rsp), %eax
subl $0x1, %eax
movslq %eax, %rdx
movq 0xd8(%rsp), %rcx
movl $0x1, %esi
callq 0x22b0
movq $0x0, 0x88(%rsp)
movq $0x0, 0x90(%rsp)
movq $0x0, 0x98(%rsp)
movl 0xb8(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x50(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x48(%rsp)
movl $0x100000, 0x68(%rsp)   # imm = 0x100000
movq 0xc0(%rsp), %rax
movq %rax, 0x60(%rsp)
leaq 0x48(%rsp), %rdi
leaq 0x977a(%rip), %rsi      # 0x13613
movl $0x70, %edx
callq 0x2180
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x2150
cmpl $0x1, %eax
je 0x9ed7
movl $0x4, %edi
callq 0x20d0
movq %rax, %rdi
movl $0xfffffffb, (%rdi)     # imm = 0xFFFFFFFB
movq 0x100fa(%rip), %rsi     # 0x19fc8
xorl %eax, %eax
movl %eax, %edx
callq 0x22f0
leaq 0x48(%rsp), %rdi
callq 0x21f0
movq 0x30(%rsp), %rax
cmpq $0x0, (%rax)
jne 0x9f10
movl $0x2000, %edi           # imm = 0x2000
callq 0x2030
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, (%rax)
movq (%rax), %rdi
xorl %esi, %esi
movl $0x2000, %edx           # imm = 0x2000
callq 0x20f0
movl $0x50, %edi
callq 0x21b0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x28(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rdi)
movaps %xmm0, 0x30(%rdi)
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
callq 0xa380
jmp 0x9f42
movq 0x30(%rsp), %rax
movq 0x28(%rsp), %rsi
movq (%rax), %rcx
movslq 0xbc(%rsp), %rdx
movq %rsi, (%rcx,%rdx,8)
movq (%rax), %rax
movslq 0xbc(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movq 0xc0(%rsp), %rsi
callq 0x9490
jmp 0x9f79
jmp 0xa08f
movq 0x28(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x40(%rsp)
movl %eax, 0x3c(%rsp)
movl $0x50, %esi
callq 0x21c0
jmp 0xa108
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x40(%rsp)
movl %eax, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0xa108
movq 0x40(%rsp), %rdi
callq 0x2090
movl (%rax), %eax
movl %eax, 0x38(%rsp)
cmpl $-0x2, 0x38(%rsp)
jne 0x9fee
movq 0x10003(%rip), %rax     # 0x19fe0
movq (%rax), %rdi
leaq 0x9633(%rip), %rsi      # 0x1361a
movb $0x0, %al
callq 0x2290
movq 0x30(%rsp), %rax
movq (%rax), %rax
movslq 0xbc(%rsp), %rcx
movl %ecx, %edx
movq (%rax,%rcx,8), %rdi
movl %edx, %esi
andl $0x1f, %esi
sarl $0x5, %edx
callq 0x96a0
movb %al, 0x27(%rsp)
jmp 0xa017
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0xa067
jmp 0xa021
movq 0xffb8(%rip), %rax      # 0x19fe0
movq (%rax), %rdi
movl 0xbc(%rsp), %edx
andl $0x1f, %edx
movl 0xbc(%rsp), %ecx
sarl $0x5, %ecx
leaq 0x962d(%rip), %rsi      # 0x13673
movb $0x0, %al
callq 0x2290
jmp 0xa067
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x40(%rsp)
movl %eax, 0x3c(%rsp)
callq 0x22e0
jmp 0xa108
movl $0x4, %edi
callq 0x20d0
movq %rax, %rdi
movl 0x38(%rsp), %eax
movl %eax, (%rdi)
movq 0xff47(%rip), %rsi      # 0x19fc8
xorl %eax, %eax
movl %eax, %edx
callq 0x22f0
jmp 0xa112
jmp 0xa091
jmp 0xa093
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x9d27
movq 0xc8(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0xa0c6
movq 0x18(%rsp), %rdi
callq 0x2270
movq 0xc0(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0xa0e3
movq 0x10(%rsp), %rdi
callq 0x2270
movq 0xd0(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0xa100
movq 0x8(%rsp), %rdi
callq 0x2270
addq $0xe8, %rsp
retq
movq 0x40(%rsp), %rdi
callq 0x2300
nopw %cs:(%rax,%rax)