// File: Merulo[P]CompilerJFTT/src/IRs/FourthIR.cpp
// Function: FourthIR::convertBlockToAssembler(Pair&, RegisterBlock&)
void FourthIR::convertBlockToAssembler(Pair& pair, RegisterBlock& registerBlock)
{
    if (std::find(_blocks.begin(), _blocks.end(), pair.block) != _blocks.end())
    {
        return;
    }
    pair.startRegisterBlock = registerBlock;
    pair.registerBlockIsSet = true;

    Block resultBlock;
    resultBlock.blockName = pair.block.blockName;
    for(auto& l : pair.block.lines)
    {
        if (l.operation == "CONST")
        {
            handleConst(registerBlock, resultBlock, l);
        }
        if (l.operation == "WRITE")
        {
            handleWrite(registerBlock, resultBlock, l);
        }
        if (l.operation == "READ")
        {
            handleRead(registerBlock, resultBlock, l);
        }
        if (l.operation == "COPY")
        {
            handleCopy(registerBlock, resultBlock, l);
        }
        if (l.operation == "ADD" || l.operation == "SUB")
        {
            handleDirectTranslation(registerBlock, resultBlock, l);
        }        
        if (l.operation == "HALT")
        {
            resultBlock.lines.push_back({"HALT"});
        }
        if (l.operation == "JUMP")
        {
            resultBlock.lines.push_back(l);
        }
        if (l.operation == "JZERO" || l.operation == "JODD" || l.operation == "INC" || l.operation == "DEC" || l.operation == "HALF")
        {
            handleSimpleOperation(registerBlock, resultBlock, l);
        }
        if (l.operation == "MUL")
        {
            handleMul(registerBlock, resultBlock, l);
        }
        if (l.operation == "DIV")
        {
            handleDiv(registerBlock, resultBlock, l);
        }
        if (l.operation == "MOD")
        {
            handleMod(registerBlock, resultBlock, l);
        }        
        if (l.operation == "#DO NOT REMOVE CONST")
        {
            _removeConsts = false;
        }    
    }
    pair.endRegisterBlock = registerBlock; 
    _blocks.push_back(resultBlock);
    continueConverting(pair, registerBlock);
}

// Assembly:
subq $0x2c8, %rsp            # imm = 0x2C8
movq %rdi, 0x2c0(%rsp)
movq %rsi, 0x2b8(%rsp)
movq %rdx, 0x2b0(%rsp)
movq 0x2c0(%rsp), %rdi
movq %rdi, 0x50(%rsp)
addq $0x10, %rdi
callq 0x29fd0
movq 0x50(%rsp), %rdi
movq %rax, 0x2a0(%rsp)
addq $0x10, %rdi
callq 0x2a870
movq %rax, 0x298(%rsp)
movq 0x2b8(%rsp), %rdx
movq 0x2a0(%rsp), %rdi
movq 0x298(%rsp), %rsi
callq 0x390f0
movq 0x50(%rsp), %rdi
movq %rax, 0x2a8(%rsp)
addq $0x10, %rdi
callq 0x2a870
movq %rax, 0x290(%rsp)
leaq 0x2a8(%rsp), %rdi
leaq 0x290(%rsp), %rsi
callq 0x30be0
testb $0x1, %al
jne 0x326b9
jmp 0x326be
jmp 0x32e15
movq 0x2b0(%rsp), %rsi
movq 0x2b8(%rsp), %rdi
addq $0x128, %rdi            # imm = 0x128
callq 0x39150
movq 0x2b8(%rsp), %rax
movb $0x1, 0x1b0(%rax)
leaq 0x1f0(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x29950
movq 0x48(%rsp), %rdi
movq 0x2b8(%rsp), %rsi
callq 0x4050
jmp 0x3270f
movq 0x2b8(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x1d8(%rsp)
movq 0x1d8(%rsp), %rdi
callq 0xe3f0
movq %rax, 0x1d0(%rsp)
movq 0x1d8(%rsp), %rdi
callq 0xe380
movq %rax, 0x1c8(%rsp)
leaq 0x1d0(%rsp), %rdi
leaq 0x1c8(%rsp), %rsi
callq 0xec10
testb $0x1, %al
jne 0x3276b
jmp 0x32d91
leaq 0x1d0(%rsp), %rdi
callq 0xfb10
movq %rax, 0x1c0(%rsp)
movq 0x1c0(%rsp), %rdi
leaq 0x1edd3(%rip), %rsi     # 0x51562
callq 0xdd10
movb %al, 0x47(%rsp)
jmp 0x3279a
movb 0x47(%rsp), %al
testb $0x1, %al
jne 0x327a4
jmp 0x327e3
movq 0x50(%rsp), %rdi
movq 0x2b0(%rsp), %rsi
movq 0x1c0(%rsp), %rcx
leaq 0x1f0(%rsp), %rdx
callq 0x32e60
jmp 0x327c8
jmp 0x327e3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e8(%rsp)
movl %eax, 0x1e4(%rsp)
jmp 0x32e3b
movq 0x1c0(%rsp), %rdi
leaq 0x1ed0e(%rip), %rsi     # 0x51500
callq 0xdd10
movb %al, 0x46(%rsp)
jmp 0x327fd
movb 0x46(%rsp), %al
testb $0x1, %al
jne 0x32807
jmp 0x3282d
movq 0x50(%rsp), %rdi
movq 0x2b0(%rsp), %rsi
movq 0x1c0(%rsp), %rcx
leaq 0x1f0(%rsp), %rdx
callq 0x33570
jmp 0x3282b
jmp 0x3282d
movq 0x1c0(%rsp), %rdi
leaq 0x1ecfa(%rip), %rsi     # 0x51536
callq 0xdd10
movb %al, 0x45(%rsp)
jmp 0x32847
movb 0x45(%rsp), %al
testb $0x1, %al
jne 0x32851
jmp 0x32877
movq 0x50(%rsp), %rdi
movq 0x2b0(%rsp), %rsi
movq 0x1c0(%rsp), %rcx
leaq 0x1f0(%rsp), %rdx
callq 0x33a00
jmp 0x32875
jmp 0x32877
movq 0x1c0(%rsp), %rdi
leaq 0x1e877(%rip), %rsi     # 0x510fd
callq 0xdd10
movb %al, 0x44(%rsp)
jmp 0x32891
movb 0x44(%rsp), %al
testb $0x1, %al
jne 0x3289b
jmp 0x328c1
movq 0x50(%rsp), %rdi
movq 0x2b0(%rsp), %rsi
movq 0x1c0(%rsp), %rcx
leaq 0x1f0(%rsp), %rdx
callq 0x33db0
jmp 0x328bf
jmp 0x328c1
movq 0x1c0(%rsp), %rdi
leaq 0x1e80a(%rip), %rsi     # 0x510da
callq 0xdd10
movb %al, 0x43(%rsp)
jmp 0x328db
movb 0x43(%rsp), %al
testb $0x1, %al
jne 0x32909
jmp 0x328e5
movq 0x1c0(%rsp), %rdi
leaq 0x1e7da(%rip), %rsi     # 0x510ce
callq 0xdd10
movb %al, 0x42(%rsp)
jmp 0x328ff
movb 0x42(%rsp), %al
testb $0x1, %al
jne 0x32909
jmp 0x3292f
movq 0x50(%rsp), %rdi
movq 0x2b0(%rsp), %rsi
movq 0x1c0(%rsp), %rcx
leaq 0x1f0(%rsp), %rdx
callq 0x34480
jmp 0x3292d
jmp 0x3292f
movq 0x1c0(%rsp), %rdi
leaq 0x1eb71(%rip), %rsi     # 0x514af
callq 0xdd10
movb %al, 0x41(%rsp)
jmp 0x32949
movb 0x41(%rsp), %al
testb $0x1, %al
jne 0x32956
jmp 0x32b54
leaq 0x210(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x137(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x4600
movq 0x38(%rsp), %rdx
leaq 0x1eb2e(%rip), %rsi     # 0x514af
leaq 0x138(%rsp), %rdi
callq 0x4f10
jmp 0x32990
leaq 0x10f(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x4600
movq 0x28(%rsp), %rdx
leaq 0x1e76d(%rip), %rsi     # 0x5111b
leaq 0x110(%rsp), %rdi
callq 0x4f10
jmp 0x329bd
leaq 0xe7(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x4600
movq 0x20(%rsp), %rdx
leaq 0x1e740(%rip), %rsi     # 0x5111b
leaq 0xe8(%rsp), %rdi
callq 0x4f10
jmp 0x329ea
leaq 0x158(%rsp), %rdi
leaq 0x138(%rsp), %rsi
leaq 0x110(%rsp), %rdx
leaq 0xe8(%rsp), %rcx
callq 0xd3b0
jmp 0x32a11
movq 0x30(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0xd380
jmp 0x32a25
leaq 0x158(%rsp), %rdi
callq 0xd490
leaq 0xe8(%rsp), %rdi
callq 0x46e8
leaq 0xe7(%rsp), %rdi
callq 0x43b0
leaq 0x110(%rsp), %rdi
callq 0x46e8
leaq 0x10f(%rsp), %rdi
callq 0x43b0
leaq 0x138(%rsp), %rdi
callq 0x46e8
leaq 0x137(%rsp), %rdi
callq 0x43b0
jmp 0x32b54
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e8(%rsp)
movl %eax, 0x1e4(%rsp)
jmp 0x32b42
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e8(%rsp)
movl %eax, 0x1e4(%rsp)
jmp 0x32b28
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e8(%rsp)
movl %eax, 0x1e4(%rsp)
jmp 0x32b0e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e8(%rsp)
movl %eax, 0x1e4(%rsp)
jmp 0x32b01
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e8(%rsp)
movl %eax, 0x1e4(%rsp)
leaq 0x158(%rsp), %rdi
callq 0xd490
leaq 0xe8(%rsp), %rdi
callq 0x46e8
leaq 0xe7(%rsp), %rdi
callq 0x43b0
leaq 0x110(%rsp), %rdi
callq 0x46e8
leaq 0x10f(%rsp), %rdi
callq 0x43b0
leaq 0x138(%rsp), %rdi
callq 0x46e8
leaq 0x137(%rsp), %rdi
callq 0x43b0
jmp 0x32e3b
movq 0x1c0(%rsp), %rdi
leaq 0x1e661(%rip), %rsi     # 0x511c4
callq 0xdd10
movb %al, 0x1f(%rsp)
jmp 0x32b6e
movb 0x1f(%rsp), %al
testb $0x1, %al
jne 0x32b78
jmp 0x32b91
leaq 0x210(%rsp), %rdi
movq 0x1c0(%rsp), %rsi
callq 0x297e0
jmp 0x32b8f
jmp 0x32b91
movq 0x1c0(%rsp), %rdi
leaq 0x1e614(%rip), %rsi     # 0x511b4
callq 0xdd10
movb %al, 0x1e(%rsp)
jmp 0x32bab
movb 0x1e(%rsp), %al
testb $0x1, %al
jne 0x32c49
jmp 0x32bb9
movq 0x1c0(%rsp), %rdi
leaq 0x1e5f2(%rip), %rsi     # 0x511ba
callq 0xdd10
movb %al, 0x1d(%rsp)
jmp 0x32bd3
movb 0x1d(%rsp), %al
testb $0x1, %al
jne 0x32c49
jmp 0x32bdd
movq 0x1c0(%rsp), %rdi
leaq 0x1e4ea(%rip), %rsi     # 0x510d6
callq 0xdd10
movb %al, 0x1c(%rsp)
jmp 0x32bf7
movb 0x1c(%rsp), %al
testb $0x1, %al
jne 0x32c49
jmp 0x32c01
movq 0x1c0(%rsp), %rdi
leaq 0x1e4c2(%rip), %rsi     # 0x510d2
callq 0xdd10
movb %al, 0x1b(%rsp)
jmp 0x32c1b
movb 0x1b(%rsp), %al
testb $0x1, %al
jne 0x32c49
jmp 0x32c25
movq 0x1c0(%rsp), %rdi
leaq 0x1e58b(%rip), %rsi     # 0x511bf
callq 0xdd10
movb %al, 0x1a(%rsp)
jmp 0x32c3f
movb 0x1a(%rsp), %al
testb $0x1, %al
jne 0x32c49
jmp 0x32c6f
movq 0x50(%rsp), %rdi
movq 0x2b0(%rsp), %rsi
movq 0x1c0(%rsp), %rcx
leaq 0x1f0(%rsp), %rdx
callq 0x34c90
jmp 0x32c6d
jmp 0x32c6f
movq 0x1c0(%rsp), %rdi
leaq 0x1e83a(%rip), %rsi     # 0x514b8
callq 0xdd10
movb %al, 0x19(%rsp)
jmp 0x32c89
movb 0x19(%rsp), %al
testb $0x1, %al
jne 0x32c93
jmp 0x32cb9
movq 0x50(%rsp), %rdi
movq 0x2b0(%rsp), %rsi
movq 0x1c0(%rsp), %rcx
leaq 0x1f0(%rsp), %rdx
callq 0x35010
jmp 0x32cb7
jmp 0x32cb9
movq 0x1c0(%rsp), %rdi
leaq 0x1e7f4(%rip), %rsi     # 0x514bc
callq 0xdd10
movb %al, 0x18(%rsp)
jmp 0x32cd3
movb 0x18(%rsp), %al
testb $0x1, %al
jne 0x32cdd
jmp 0x32d03
movq 0x50(%rsp), %rdi
movq 0x2b0(%rsp), %rsi
movq 0x1c0(%rsp), %rcx
leaq 0x1f0(%rsp), %rdx
callq 0x35b00
jmp 0x32d01
jmp 0x32d03
movq 0x1c0(%rsp), %rdi
leaq 0x1e7ae(%rip), %rsi     # 0x514c0
callq 0xdd10
movb %al, 0x17(%rsp)
jmp 0x32d1d
movb 0x17(%rsp), %al
testb $0x1, %al
jne 0x32d27
jmp 0x32d4d
movq 0x50(%rsp), %rdi
movq 0x2b0(%rsp), %rsi
movq 0x1c0(%rsp), %rcx
leaq 0x1f0(%rsp), %rdx
callq 0x36910
jmp 0x32d4b
jmp 0x32d4d
movq 0x1c0(%rsp), %rdi
leaq 0x1e7f7(%rip), %rsi     # 0x51553
callq 0xdd10
movb %al, 0x16(%rsp)
jmp 0x32d67
movb 0x16(%rsp), %al
testb $0x1, %al
jne 0x32d71
jmp 0x32d7d
movq 0x50(%rsp), %rax
movb $0x0, 0xd0(%rax)
jmp 0x32d7f
leaq 0x1d0(%rsp), %rdi
callq 0xf950
jmp 0x3274d
movq 0x2b0(%rsp), %rsi
movq 0x2b8(%rsp), %rdi
addq $0xa0, %rdi
callq 0x39150
jmp 0x32daf
movq 0x50(%rsp), %rdi
addq $0x10, %rdi
leaq 0x1f0(%rsp), %rsi
callq 0x29850
jmp 0x32dc7
movq 0x2b8(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x2b0(%rsp), %rsi
leaq 0x58(%rsp), %rdi
callq 0x5560
jmp 0x32de8
movq 0x8(%rsp), %rsi
movq 0x50(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x37690
jmp 0x32dfe
leaq 0x58(%rsp), %rdi
callq 0x38fe0
leaq 0x1f0(%rsp), %rdi
callq 0x29a50
addq $0x2c8, %rsp            # imm = 0x2C8
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e8(%rsp)
movl %eax, 0x1e4(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x38fe0
leaq 0x1f0(%rsp), %rdi
callq 0x29a50
movq 0x1e8(%rsp), %rdi
callq 0x45f0
nopw %cs:(%rax,%rax)