// File: /workspace/github_cpp_makefile_star2_O3/pixlark[P]Badge/src/main.cc
// Function: work_from_source(char const*)
void work_from_source(const char * path)
{
	/*
	if (strcmp(path, "-") == 0) {
		// Read from stdin
		String_Builder builder;
		const int chunk_size = 64;
		char buf[chunk_size];
		
		size_t read;
		while ((read = fread(buf, sizeof(char), chunk_size, stdin)) == chunk_size) {
			builder.append_size(buf, chunk_size);
		}
		if (ferror(stdin)) {
			fatal("Error reading from stdin");
		}
		assert(read <= chunk_size);
		builder.append_size(buf, read);
		source = builder.final_string();
		}*/

	Blocks blocks;
	blocks.init();
	const char * source = load_and_compile_file(&blocks, path);
	if (!source) {
		fatal("File '%s' does not exist!", path);
	}

	#if OUTPUT_BYTECODE
	for (int i = 0; i < blocks.blocks.size; i++) {
		auto block = blocks.blocks[i];
		auto size = blocks.sizes[i];
		printf("Block %d:\n", i);
		for (int j = 0; j < size; j++) {
			char * s = block[j].to_string();
			printf("%02d %s\n", j, s);
			free(s);
		}
	}
	#endif
	
	auto export_scope = Environment::alloc();
	
	// Finally, just run our bytecode through the VM, starting with
	// `block_reference` 0
	List<VM> vm_stack;
	vm_stack.alloc();

	{
		VM vm;
		vm.init(&blocks, export_scope, 0);
		vm_stack.push(vm);
	}

	while (vm_stack.size > 0) {
		auto vm = &vm_stack[vm_stack.size - 1];
		auto response = vm->step();
		switch (response) {
		case VM_OK:
			break;
		case VM_HALTED: {
			vm->destroy();
			vm_stack.pop();
		} break;
		case VM_SWITCH: {
			VM new_vm;
			new_vm.init(&blocks, export_scope, vm->block_reference_to_push);
			vm_stack.push(new_vm);
		} break;
		default:
			assert(false);
		}

		#if DEBUG_OUTPUT
		vm->print_debug_info();
		#endif
		
		// This should ALWAYS run before the loop breaks, that way
		// anything that needs to get cleaned up, will be.
		#if COLLECTION
		do {
			#if RELEASE
			if (!GC::should_collect()) {
				break;
			}
			GC::reset_heuristics();
			#endif
			GC::unmark_all();
			for (int i = 0; i < vm_stack.size; i++) {
				vm_stack[i].mark_reachable();
			}
			GC::free_unmarked();
		} while(0);
		#endif
	}
	
	assert(vm_stack.size == 0);
	vm_stack.dealloc();
	
	blocks.destroy();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
movq %rdi, %rbx
xorl %eax, %eax
leaq -0xa8(%rbp), %r14
movq %rax, 0x8(%r14)
movl $0x4, %r13d
movq %r13, 0x10(%r14)
movq 0xb28e(%rip), %r15      # 0xffd0
movq %r15, 0x18(%r14)
movq 0xb27b(%rip), %r12      # 0xffc8
movq %r12, 0x20(%r14)
movl $0x20, %edi
callq 0x1200
movq %rax, (%r14)
xorl %eax, %eax
movq %rax, 0x30(%r14)
movq %r13, 0x38(%r14)
movq %r15, 0x40(%r14)
movq %r12, 0x48(%r14)
movl $0x20, %edi
callq 0x1200
movq %rax, 0x28(%r14)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4b3d
testq %rax, %rax
je 0x4fb6
callq 0x5e44
movq %rax, %r13
leaq -0x48(%rbp), %rbx
movq $0x4, 0x8(%rbx)
movq %r15, 0x10(%rbx)
movq %r12, 0x18(%rbx)
movl $0x260, %edi            # imm = 0x260
callq 0x1200
movq %rax, %r14
movq %rax, -0x8(%rbx)
leaq -0x1d8(%rbp), %r15
leaq -0xa8(%rbp), %rsi
movq %r15, %rdi
movq %r13, -0x58(%rbp)
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x5ee2
movq $0x1, (%rbx)
movl $0x98, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x10e0
movl $0x1, %eax
leaq -0x140(%rbp), %r15
leaq -0x270(%rbp), %r12
movq -0x50(%rbp), %r13
imulq $0x98, %rax, %rbx
leaq (%rbx,%r13), %r14
addq $-0x98, %r14
movq %r14, %rdi
callq 0x5fb0
testl %eax, %eax
je 0x4f07
cmpl $0x2, %eax
je 0x4e55
cmpl $0x1, %eax
jne 0x4f97
movq %r14, %rdi
callq 0x6a24
movq %r12, %rdi
leaq -0x50(%rbp), %rsi
callq 0x6a8a
jmp 0x4f07
addq %rbx, %r13
movq -0x8(%r13), %rcx
movq %r15, %rdi
leaq -0xa8(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0x5ee2
movl $0x98, %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x10e0
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rax
leaq 0x1(%rcx), %rdx
cmpq %rdx, %rax
jae 0x4eeb
testq %rax, %rax
js 0x4e9f
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x4eb7
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
addss %xmm0, %xmm0
cvttss2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subss 0x6135(%rip), %xmm0     # 0xb004
cvttss2si %xmm0, %rsi
andq %rcx, %rsi
orq %rax, %rsi
leaq -0x50(%rbp), %rdi
callq 0xa7ba
movq -0x48(%rbp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, -0x48(%rbp)
imulq $0x98, %rcx, %rdi
addq -0x50(%rbp), %rdi
movl $0x98, %edx
movq %r12, %rsi
callq 0x10e0
cmpq $0x0, 0xb301(%rip)      # 0x10210
je 0x4f2d
xorl %eax, %eax
movq 0xb2ee(%rip), %rcx      # 0x10208
movq (%rcx,%rax,8), %rcx
movb $0x0, (%rcx)
incq %rax
cmpq %rax, 0xb2e5(%rip)      # 0x10210
ja 0x4f13
cmpq $0x0, -0x48(%rbp)
je 0x4f56
xorl %r14d, %r14d
xorl %r13d, %r13d
movq -0x50(%rbp), %rdi
addq %r14, %rdi
callq 0x6af4
incq %r13
addq $0x98, %r14
cmpq %r13, -0x48(%rbp)
ja 0x4f3a
callq 0x443c
movq -0x48(%rbp), %rax
testq %rax, %rax
jne 0x4e08
movq -0x50(%rbp), %rdi
callq *-0x30(%rbp)
xorps %xmm0, %xmm0
leaq -0x48(%rbp), %rax
movups %xmm0, (%rax)
leaq -0xa8(%rbp), %rdi
callq 0x6bae
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x64d0(%rip), %rdi      # 0xb46e
leaq 0x6711(%rip), %rsi      # 0xb6b6
leaq 0x6716(%rip), %rcx      # 0xb6c2
movl $0x8d, %edx
callq 0x10d0
leaq 0x66df(%rip), %rdi      # 0xb69c
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1a00