// File: /workspace/github_c_makefile_star2_O3/adamjedlicka[P]blu/src/vm/compiler/compiler.c
// Function: ifStatement
static void ifStatement(bluCompiler* compiler) {
	beginScope(compiler);

	expression(compiler);
	int32_t ifJump = emitJump(compiler, OP_JUMP_IF_FALSE);
	emitByte(compiler, OP_POP); // Condition

	// One-line if notation
	if (match(compiler, TOKEN_COLON)) {
		statement(compiler);

		int32_t elseJump = emitJump(compiler, OP_JUMP);
		patchJump(compiler, ifJump);
		emitByte(compiler, OP_POP); // Condition
		patchJump(compiler, elseJump);
		endScope(compiler);
		return;
	}

	consume(compiler, TOKEN_LEFT_BRACE, "Expect '{' after if condition.");
	beginScope(compiler);
	block(compiler);
	endScope(compiler);

	int32_t elseJump = emitJump(compiler, OP_JUMP);
	patchJump(compiler, ifJump);
	emitByte(compiler, OP_POP); // Condition

	if (match(compiler, TOKEN_ELSE)) {
		if (match(compiler, TOKEN_LEFT_BRACE)) {
			beginScope(compiler);
			block(compiler);
			endScope(compiler);
		} else if (match(compiler, TOKEN_IF)) {
			ifStatement(compiler);
		} else {
			error(compiler, "Expect 'if' or '{' after 'else.'");
		}
	}

	patchJump(compiler, elseJump);

	endScope(compiler);
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
movq %rdi, %rbx
incb 0x58(%rdi)
callq 0x3d56
movq %rbx, %rdi
movl $0x16, %esi
callq 0x4438
movl %eax, %r14d
movq 0x8(%rbx), %rax
movq 0x28(%rbx), %rdi
movl 0x54(%rax), %edx
movl 0x58(%rax), %ecx
addq $0x28, %rdi
movl $0x5, %esi
callq 0x2a5f
movq 0x8(%rbx), %rax
cmpl $0x2, 0x40(%rax)
jne 0x49d6
movq %rbx, %rdi
callq 0x34a2
movq %rbx, %rdi
callq 0x44cf
movq %rbx, %rdi
movl $0x15, %esi
callq 0x4438
movl %eax, %ebp
movq 0x28(%rbx), %rcx
movl 0x40(%rcx), %eax
subl %r14d, %eax
addl $-0x2, %eax
movq 0x38(%rcx), %rdx
movslq %r14d, %rsi
movb %ah, (%rdx,%rsi)
movq 0x38(%rcx), %rcx
movb %al, 0x1(%rcx,%rsi)
movq 0x8(%rbx), %rax
movq 0x28(%rbx), %rdi
movl 0x54(%rax), %edx
movl 0x58(%rax), %ecx
addq $0x28, %rdi
movl $0x5, %esi
callq 0x2a5f
jmp 0x4aac
leaq 0x4bbf(%rip), %rdx      # 0x959c
movq %rbx, %rdi
movl $0x5, %esi
callq 0x3c36
incb 0x58(%rbx)
movq %rbx, %rdi
callq 0x42b4
movq %rbx, %rdi
callq 0x42e8
movq %rbx, %rdi
movl $0x15, %esi
callq 0x4438
movl %eax, %ebp
movq 0x28(%rbx), %rcx
movl 0x40(%rcx), %eax
subl %r14d, %eax
addl $-0x2, %eax
movq 0x38(%rcx), %rdx
movslq %r14d, %rsi
movb %ah, (%rdx,%rsi)
movq 0x38(%rcx), %rcx
movb %al, 0x1(%rcx,%rsi)
movq 0x8(%rbx), %rax
movq 0x28(%rbx), %rdi
movl 0x54(%rax), %edx
movl 0x58(%rax), %ecx
addq $0x28, %rdi
movl $0x5, %esi
callq 0x2a5f
movq 0x8(%rbx), %rax
cmpl $0x21, 0x40(%rax)
jne 0x4aac
movq %rbx, %rdi
callq 0x34a2
movq 0x8(%rbx), %rsi
movl 0x40(%rsi), %eax
cmpl $0x26, %eax
je 0x4a87
cmpl $0x5, %eax
jne 0x4a99
movq %rbx, %rdi
callq 0x34a2
incb 0x58(%rbx)
movq %rbx, %rdi
callq 0x42b4
movq %rbx, %rdi
callq 0x42e8
jmp 0x4aac
movq %rbx, %rdi
callq 0x34a2
movq %rbx, %rdi
callq 0x4932
jmp 0x4aac
addq $0x20, %rsi
leaq 0x4b17(%rip), %rdx      # 0x95bb
movq %rbx, %rdi
callq 0x3a3b
movq 0x28(%rbx), %rcx
movl 0x40(%rcx), %eax
subl %ebp, %eax
addl $-0x2, %eax
movq 0x38(%rcx), %rdx
movslq %ebp, %rsi
movb %ah, (%rdx,%rsi)
movq 0x38(%rcx), %rcx
movb %al, 0x1(%rcx,%rsi)
movq %rbx, %rdi
popq %rbx
popq %r14
popq %rbp
jmp 0x42e8