// File: /workspace/github_c_makefile_star2_O0/generot[P]VM-0/src/vm/entry.c
// Function: main
int main(int argc, char **argv) {
    if(argc < 2) ISSUE_ERROR("Usage: ./vm <bytecode_file>");

    bcode_file bcode = read_bytecode(argv[1]);

    bool stack_size_specified = bcode.metadata.stack_size_spec == STACK_SIZE_SPECIFIED;

    size_t mem_sz = stack_size_specified
                    ? bcode.metadata.code_seg_sz + bcode.metadata.data_seg_sz
                    : DEFAULT_STACK_SIZE; 

    memory = calloc(mem_sz, sizeof(double));

    stack_sz = stack_size_specified
               ? bcode.metadata.code_seg_sz
               : CODE_SEG_SZ;

    data_seg = memory;
    code_seg = &memory[stack_size_specified ? bcode.metadata.data_seg_sz : DATA_SEG_SZ];

    size_s entry_point_addr = (size_s)bcode.metadata.entry_point_addr;
    size_s prog_sz = (size_s)bcode.metadata.program_size;
    
    for(size_s i = entry_point_addr; i < prog_sz; i++) {
        handle_instr(bcode.prog[i], data_seg, &i);
    }

    free(memory);
    free(bcode.prog);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x1461
movq 0x2bb2(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc26(%rip), %rsi       # 0x2076
movb $0x0, %al
callq 0x1090
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x10c0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq -0x40(%rbp), %rdi
callq 0x1270
cmpl $0x0, -0x28(%rbp)
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x1495
movq -0x20(%rbp), %rax
addq -0x18(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x14a0
movl $0x800, %eax            # imm = 0x800
movq %rax, -0x70(%rbp)
jmp 0x14a0
movq -0x70(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
movl $0x8, %esi
callq 0x1080
movq %rax, 0x2bcb(%rip)      # 0x4088
cmpl $0x0, -0x44(%rbp)
je 0x14cd
movq -0x20(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0x14d8
movl $0x200, %eax            # imm = 0x200
movq %rax, -0x78(%rbp)
jmp 0x14d8
movq -0x78(%rbp), %rax
movq %rax, 0x2bbd(%rip)      # 0x40a0
movq 0x2b9e(%rip), %rax      # 0x4088
movq %rax, 0x2ba7(%rip)      # 0x4098
movq 0x2b90(%rip), %rax      # 0x4088
movq %rax, -0x80(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x150f
movq -0x18(%rbp), %rax
movq %rax, -0x88(%rbp)
jmp 0x151d
movl $0x600, %eax            # imm = 0x600
movq %rax, -0x88(%rbp)
jmp 0x151d
movq -0x80(%rbp), %rax
movq -0x88(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x2b5a(%rip)      # 0x4090
movq -0x30(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x60(%rbp), %rax
jge 0x159b
movq -0x40(%rbp), %rax
imulq $0x18, -0x68(%rbp), %rcx
addq %rcx, %rax
movq 0x2b2d(%rip), %rdi      # 0x4098
leaq -0x68(%rbp), %rsi
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x15c0
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x154e
movq 0x2ae6(%rip), %rdi      # 0x4088
callq 0x1030
movq -0x40(%rbp), %rdi
callq 0x1030
xorl %eax, %eax
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax,%rax)