// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, %r15
movq %rdi, 0x8(%rsp)
movl $0x1000, %edi           # imm = 0x1000
callq 0x2030
movq %rax, %r14
movl $0x100000, %edi         # imm = 0x100000
callq 0x2030
movq %rax, %r13
movl $0x100000, %edi         # imm = 0x100000
callq 0x2030
movq %rax, 0x10(%rsp)
xorl %r12d, %r12d
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2190
pushq $0x1
popq %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %r13, %rdi
movq %r15, %rcx
callq 0x2200
cmpq $0x400, %r12            # imm = 0x400
je 0x4b64
movzbl (%r13,%r12,4), %eax
shll $0x10, %eax
movzbl 0x1(%r13,%r12,4), %ecx
shll $0x8, %ecx
orl %eax, %ecx
movzbl 0x2(%r13,%r12,4), %eax
orl %ecx, %eax
movl %eax, (%r14,%r12,4)
incq %r12
jmp 0x4b36
xorl %r12d, %r12d
pushq $0x1
popq %rbx
cmpq $0x400, %r12            # imm = 0x400
je 0x4cc3
movl (%r14,%r12,4), %eax
testl %eax, %eax
je 0x4cbb
shll $0xc, %eax
movslq %eax, %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x2190
movq %r13, %rdi
movq %rbx, %rsi
pushq $0x5
popq %rdx
movq %r15, %rcx
callq 0x2200
cmpb $0x2, 0x4(%r13)
jne 0x4cff
movq %rbx, %rsi
movzbl (%r13), %eax
shll $0x18, %eax
movzbl 0x1(%r13), %ecx
shll $0x10, %ecx
cltq
orq %rcx, %rax
movzbl 0x2(%r13), %ecx
shll $0x8, %ecx
orq %rax, %rcx
movzbl 0x3(%r13), %eax
leaq (%rax,%rcx), %rbp
decq %rbp
movq %r13, %rdi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x2200
leaq 0x58(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movl %ebp, 0x20(%rsp)
movq %r13, 0x18(%rsp)
movl $0x100000, 0x38(%rsp)   # imm = 0x100000
movq 0x10(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x18(%rsp), %rbp
movq %rbp, %rdi
leaq 0x3a39(%rip), %rsi      # 0x865f
pushq $0x70
popq %rdx
callq 0x2120
movq %rbp, %rdi
xorl %esi, %esi
callq 0x2100
cmpl $0x1, %eax
jne 0x4cef
movq %rbp, %rdi
callq 0x2180
movq 0x8(%rsp), %rax
cmpq $0x0, (%rax)
jne 0x4c75
movl $0x2000, %edi           # imm = 0x2000
callq 0x2030
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movl $0x2000, %edx           # imm = 0x2000
movq %rax, %rdi
xorl %esi, %esi
callq 0x20d0
pushq $0x50
popq %rdi
callq 0x2150
movq %rax, %rbp
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x40(%rax)
movq %rax, %rdi
callq 0x4eb6
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movq %rbp, (%rax,%r12,8)
movq (%rcx), %rax
movq (%rax,%r12,8), %rdi
movq 0x10(%rsp), %rsi
callq 0x4814
incq %r12
jmp 0x4b6a
movq %r13, %rdi
callq 0x21d0
movq 0x10(%rsp), %rdi
callq 0x21d0
movq %r14, %rdi
callq 0x21d0
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x4
popq %rdi
callq 0x20b0
movl $0xfffffffb, (%rax)     # imm = 0xFFFFFFFB
jmp 0x4d0d
pushq $0x4
popq %rdi
callq 0x20b0
movl $0xfffffffc, (%rax)     # imm = 0xFFFFFFFC
movq 0x72b4(%rip), %rsi      # 0xbfc8
movq %rax, %rdi
xorl %edx, %edx
callq 0x2230
movq %rax, %r14
cmpl $0x1, %edx
jne 0x4dc9
movq %r14, %rdi
callq 0x2080
movl (%rax), %ebp
cmpl $-0x2, %ebp
jne 0x4d55
movq 0x72a0(%rip), %rax      # 0xbfe0
movq (%rax), %rcx
leaq 0x391c(%rip), %rdi      # 0x8666
pushq $0x58
popq %rsi
pushq $0x1
popq %rdx
callq 0x2260
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r12,8), %rdi
movl %r12d, %ebx
andl $0x1f, %ebx
shrl $0x5, %r12d
movl %ebx, %esi
movl %r12d, %edx
callq 0x48f0
testb %al, %al
jne 0x4d96
movq 0x7260(%rip), %rax      # 0xbfe0
movq (%rax), %rdi
leaq 0x3935(%rip), %rsi      # 0x86bf
movl %ebx, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x21f0
pushq $0x4
popq %rdi
callq 0x20b0
movl %ebp, (%rax)
movq 0x7221(%rip), %rsi      # 0xbfc8
movq %rax, %rdi
xorl %edx, %edx
callq 0x2230
movq %rax, %r14
callq 0x2220
jmp 0x4dc9
movq %rax, %r14
pushq $0x50
popq %rsi
movq %rbp, %rdi
callq 0x2160
movq %r14, %rdi
callq 0x2240
nop