// File: /workspace/github_cpp_makefile_star2_O2/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 %rbx
subq $0x110, %rsp            # imm = 0x110
movl $0x400, %edi            # imm = 0x400
movl $0x400, %esi            # imm = 0x400
callq 0x6af9
callq 0x3319
leaq 0x78ea(%rip), %rdi      # 0xf1f4
callq 0x6961
movq %rax, %rbx
movq %rdx, %r14
testq %rdx, %rdx
jne 0x7928
leaq 0x78dc(%rip), %rdi      # 0xf1fd
xorl %eax, %eax
callq 0x3ec6
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x6d37
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x7a19
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %r15
movaps %xmm0, (%r15)
movaps %xmm0, 0x10(%r15)
movaps %xmm0, 0x20(%r15)
movq $0x1000, 0x30(%r15)     # imm = 0x1000
movq 0x20(%rsp), %rcx
leaq 0x90(%rsp), %rsi
movq %rbx, (%rsi)
movq %r14, 0x8(%rsi)
movq %rcx, 0x10(%rsi)
movq %rax, 0x18(%rsi)
movq %r15, 0x20(%rsi)
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x7a62
movq 0xd1ce(%rip), %rsi      # 0x14b68
movq 0xd1cf(%rip), %rdx      # 0x14b70
movq %rbx, %rdi
callq 0xcb5f
leaq 0xb8(%rsp), %r14
movl $0x80, %esi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0x7b19
leaq 0x38(%rsp), %rdi
movq %r14, (%rdi)
movq %r15, 0x8(%rdi)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
callq 0x7b46
pushq $0x1
popq %rbx
testb %al, %al
je 0x7a2c
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
leaq 0x50(%rsp), %rdi
callq 0x6822
testb %al, %al
je 0x7a1e
xorl %ebx, %ebx
xorl %r14d, %r14d
cmpq 0x8(%rsp), %r14
jae 0x7a2c
movq 0x10(%rsp), %rax
movq (%rax,%r14,8), %rdi
callq 0x76c6
incq %r14
jmp 0x79ff
pushq $0x1
popq %rbx
jmp 0x7a2c
leaq 0x77f1(%rip), %rdi      # 0xf216
xorl %eax, %eax
callq 0x3ec6
movl %ebx, %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbx
popq %r14
popq %r15
retq