// File: /workspace/github_cpp_makefile_star2_O3/cphinton42[P]hinton-language/src/main.cpp
// Function: main
int main(int argc, char **argv)
{
    // Note: if this becomes multi-threaded, we can get rid of the globals (writes smaller than 4K are supposed to be atomic IIRC)
    // Alternatively, formatting could occur in thread-local buffers, and output is guarded by a global mutex
    init_std_print_buffers();
    init_primitive_types();
    
    String file_contents = read_entire_file("test.txt");
    if(!file_contents.data)
    {
        print_err("Unable to read test.txt\n");
    }
    
    Dynamic_Array<Token> tokens = lex_string(file_contents);
    if(!tokens.data)
    {
        return 1;
    }
    
    Pool_Allocator ast_pool;
    Parsing_Context ctx;
    
    pool_init(&ast_pool, 4096);
    init_parsing_context(&ctx, file_contents, tokens.array, &ast_pool);
    Dynamic_Array<Decl_AST*> decls = parse_tokens(&ctx);
    
    array_trim(&decls);
    
    Atom_Table atom_table;
    init_atom_table(&atom_table, 128, 4096);
    
    Scoping_Context scoping_ctx;
    scoping_ctx.atom_table = &atom_table;
    scoping_ctx.ast_pool = &ast_pool;
    bool success = create_scope_metadata(&scoping_ctx, decls.array);
    if(!success)
    {
        return 1;
    }
    success = typecheck_all(&ast_pool, decls.array);
    if(!success)
    {
        print_err("No success\n");
        return 1;
    }
    
    for(u64 i = 0; i < decls.count; ++i)
    {
        check_for_untyped(decls[i]);
    }
    
    return 0;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x100, %rsp            # imm = 0x100
leaq 0x30(%rsp), %rbx
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rdi
movl $0x1, %esi
callq 0x708d
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups %xmm1, 0xd043(%rip)     # 0x15f10
movups %xmm0, 0xd02c(%rip)     # 0x15f00
leaq 0x30(%rsp), %rbx
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rdi
movl $0x2, %esi
callq 0x708d
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups %xmm1, 0xd037(%rip)     # 0x15f30
movups %xmm0, 0xd020(%rip)     # 0x15f20
callq 0x331b
leaq 0x8318(%rip), %rdi      # 0x11224
callq 0x6faf
movq %rax, %r14
movq %rdx, %r15
testq %rdx, %rdx
je 0x8f29
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rdx
jmp 0x8f41
leaq 0x82fd(%rip), %rdi      # 0x1122d
xorl %eax, %eax
callq 0x416b
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x739c
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0x9049
xorps %xmm0, %xmm0
leaq 0xa0(%rsp), %r12
movaps %xmm0, (%r12)
movaps %xmm0, 0x10(%r12)
movaps %xmm0, 0x20(%r12)
movl $0x1000, %r13d          # imm = 0x1000
movq %r13, 0x30(%r12)
movq 0x18(%rsp), %rcx
leaq 0xd8(%rsp), %rsi
movq %r14, (%rsi)
movq %r15, 0x8(%rsi)
movq %rcx, 0x10(%rsi)
movq %rax, 0x18(%rsi)
movq %r12, 0x20(%rsi)
movq %rsp, %r14
movq %r14, %rdi
callq 0x909c
movq 0xcbbb(%rip), %rdx      # 0x15b68
movq 0xcbbc(%rip), %rcx      # 0x15b70
movq (%r14), %rsi
movq %r14, %rdi
callq 0xf10a
movl $0x80, %esi
movq %rbx, %rdi
callq 0xe9bb
xorps %xmm0, %xmm0
movups %xmm0, 0x70(%rsp)
movups %xmm0, 0x60(%rsp)
movups %xmm0, 0x50(%rsp)
movq %r13, 0x80(%rsp)
leaq 0x88(%rsp), %rdi
movq %rbx, (%rdi)
movq %r12, 0x8(%rdi)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
callq 0x9196
movl $0x1, %ebx
testb %al, %al
je 0x9062
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
leaq 0xa0(%rsp), %rdi
callq 0x6e61
testb %al, %al
je 0x9050
cmpq $0x0, (%rsp)
je 0x9060
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x8(%rsp), %rax
movq (%rax,%r14,8), %rdi
callq 0x8b4b
incq %r14
cmpq (%rsp), %r14
jb 0x9030
jmp 0x9062
movl $0x1, %ebx
jmp 0x9062
leaq 0x81ef(%rip), %rdi      # 0x11246
xorl %eax, %eax
callq 0x416b
jmp 0x9062
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq