// File: /workspace/github_cpp_makefile_star2_O1/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
leaq 0x30(%rsp), %r14
movl $0x1, %ebx
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
movl $0x1, %esi
callq 0x7461
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups %xmm1, 0xd07f(%rip)     # 0x15f10
movups %xmm0, 0xd068(%rip)     # 0x15f00
leaq 0x30(%rsp), %r14
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
movl $0x2, %esi
callq 0x7461
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups %xmm1, 0xd071(%rip)     # 0x15f30
movups %xmm0, 0xd05a(%rip)     # 0x15f20
callq 0x331b
leaq 0x8352(%rip), %rdi      # 0x11224
callq 0x7361
movq %rax, %r15
movq %rdx, %r12
testq %rdx, %rdx
jne 0x8ef0
leaq 0x8344(%rip), %rdi      # 0x1122d
xorl %eax, %eax
callq 0x4261
leaq 0x18(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x7791
movq 0x8(%r13), %rax
testq %rax, %rax
je 0x9013
xorps %xmm0, %xmm0
leaq 0xa0(%rsp), %r13
movaps %xmm0, (%r13)
movaps %xmm0, 0x10(%r13)
movaps %xmm0, 0x20(%r13)
movl $0x1000, %ebp           # imm = 0x1000
movq %rbp, 0x30(%r13)
movq 0x18(%rsp), %rcx
leaq 0xe0(%rsp), %rsi
movq %r15, (%rsi)
movq %r12, 0x8(%rsi)
movq %rcx, 0x10(%rsi)
movq %rax, 0x18(%rsi)
movq %r13, 0x20(%rsi)
movq %rsp, %rbx
movq %rbx, %rdi
callq 0x904e
movq 0xcc03(%rip), %rdx      # 0x15b68
movq 0xcc04(%rip), %rcx      # 0x15b70
movq (%rbx), %rsi
movq %rbx, %rdi
callq 0xf540
movl $0x80, %esi
movq %r14, %rdi
callq 0xedc8
xorps %xmm0, %xmm0
movups %xmm0, 0x70(%rsp)
movups %xmm0, 0x60(%rsp)
movups %xmm0, 0x50(%rsp)
movq %rbp, 0x80(%rsp)
leaq 0x88(%rsp), %rdi
movq %r14, (%rdi)
movq %r13, 0x8(%rdi)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
callq 0x9145
movl $0x1, %ebx
testb %al, %al
je 0x9013
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
leaq 0xa0(%rsp), %rdi
callq 0x71e5
testb %al, %al
je 0x9001
cmpq $0x0, (%rsp)
je 0x9011
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x8(%rsp), %rax
movq (%rax,%r14,8), %rdi
callq 0x8b22
incq %r14
cmpq (%rsp), %r14
jb 0x8fe8
jmp 0x9013
leaq 0x823e(%rip), %rdi      # 0x11246
xorl %eax, %eax
callq 0x4261
jmp 0x9013
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq