// File: /workspace/github_cpp_makefile_star2_O2/AgathokakologicalBit[P]wit/src/main.cpp
// Function: main
int main(int argc, char* argv[])
{
  if (argc != 2)
  {
    char const *name = (argc > 0 ? argv[0] : "witcc");
    std::cerr << "Usage: " << name << ' ' << "<filename>" << std::endl;
    return EXIT_FAILURE;
  }
  
  std::ifstream ifs(argv[1]);
  if (!ifs)
  {
    std::cerr << "File could not be opened!\n";
    std::cerr << "Reason: " << strerror(errno) << std::endl;
    return EXIT_FAILURE;
  }
  std::string source((std::istreambuf_iterator<char>(ifs)),
                     (std::istreambuf_iterator<char>()   ));
  
  auto tokens = akbit::system::parsing::tokenize(source);
  // for (auto t : tokens)
  //   std::cout << t << std::endl;

  auto ast = akbit::system::parsing::parse(tokens);
  if (!ast) return EXIT_FAILURE;
  
  akbit::system::annotation::preprocess_ast(ast);
  akbit::system::annotation::generate_context(ast);

  dump_ast(ast, -1u, 0ul);
  std::cout << "\n\x1b[39mResult: "
    << (std::get<akbit::system::Node::module_t>(ast->value).has_errors ? "\x1b[01;41mFAILURE" : "\x1b[01;44mSUCCESS")
    << "\x1b[49m\x1b[00;39m" << std::endl;
  
  std::fstream js_output_file;
	js_output_file.open("program.out.js", std::ios::out);
  if (js_output_file)
  {
    js_output_file << akbit::system::code_generation::generate(ast, akbit::system::code_generation::GenerationTarget::Javascript, nullptr);
    js_output_file.close();
  }

  return EXIT_SUCCESS;
}

// Assembly:
pushq %r14
pushq %rbx
subq $0x4c8, %rsp            # imm = 0x4C8
cmpl $0x2, %edi
jne 0x53bb
movq 0x8(%rsi), %rsi
leaq 0x2c0(%rsp), %rbx
pushq $0x8
popq %rdx
movq %rbx, %rdi
callq 0x43e0
movq (%rbx), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x2e0(%rsp,%rax)
je 0x53cb
movq 0x15c7e(%rip), %rdi     # 0x1aff0
leaq 0xbce2(%rip), %rsi      # 0x1105b
callq 0x4200
movq 0x15c6b(%rip), %rdi     # 0x1aff0
leaq 0xbcea(%rip), %rsi      # 0x11076
callq 0x4200
movq %rax, %rbx
callq 0x4030
movl (%rax), %edi
callq 0x40d0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x4200
movq %rax, %rdi
callq 0x4130
pushq $0x1
popq %rbx
jmp 0x55fc
testl %edi, %edi
jle 0x558f
movq (%rsi), %r14
jmp 0x5596
movq 0x3a8(%rsp,%rax), %rsi
leaq 0x80(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x70(%rsp), %rdi
pushq $-0x1
popq %rdx
xorl %ecx, %ecx
movl %edx, %r8d
callq 0x6d7c
leaq 0x58(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x7c9d
leaq 0x8(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x85eb
cmpq $0x0, 0x8(%rsp)
je 0x55db
leaq 0x48(%rsp), %rbx
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x6d30
movq %rbx, %rdi
callq 0xb5d4
leaq 0x50(%rsp), %rdi
callq 0x69e2
leaq 0x38(%rsp), %rbx
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x6d30
movq %rbx, %rdi
callq 0xd0f4
leaq 0x40(%rsp), %rdi
callq 0x69e2
leaq 0x28(%rsp), %rbx
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x6d30
pushq $-0x1
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x50bc
leaq 0x30(%rsp), %rdi
callq 0x69e2
movq 0x15b2d(%rip), %rdi     # 0x1afc0
leaq 0xbbe5(%rip), %rsi      # 0x1107f
callq 0x4200
movq %rax, %rbx
movq 0x8(%rsp), %rdi
addq $0x10, %rdi
callq 0x6d43
leaq 0xbbd7(%rip), %rcx      # 0x1108e
leaq 0xbbe0(%rip), %rsi      # 0x1109e
cmpb $0x0, 0x28(%rax)
cmovneq %rcx, %rsi
movq %rbx, %rdi
callq 0x4200
leaq 0xbbd9(%rip), %rsi      # 0x110ae
movq %rax, %rdi
callq 0x4200
movq %rax, %rdi
callq 0x4130
leaq 0xb0(%rsp), %rdi
callq 0x4290
leaq 0xbbc3(%rip), %rsi      # 0x110bc
leaq 0xb0(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x41f0
movq 0xb0(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0xd0(%rsp,%rax)
jne 0x557e
leaq 0x18(%rsp), %rbx
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x6d30
leaq 0x90(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x6629
leaq 0xc0(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x41e0
leaq 0x90(%rsp), %rdi
callq 0x4120
leaq 0x20(%rsp), %rdi
callq 0x69e2
leaq 0xb0(%rsp), %rdi
callq 0x4040
leaq 0xb0(%rsp), %rdi
callq 0x4080
xorl %ebx, %ebx
jmp 0x55de
leaq 0xbaac(%rip), %r14      # 0x11042
pushq $0x1
popq %rbx
movq 0x15a50(%rip), %rdi     # 0x1aff0
leaq 0xbaa1(%rip), %rsi      # 0x11048
callq 0x4200
movq %rax, %rdi
movq %r14, %rsi
callq 0x4200
pushq $0x20
popq %rsi
movq %rax, %rdi
callq 0x4280
leaq 0xba87(%rip), %rsi      # 0x11050
movq %rax, %rdi
callq 0x4200
movq %rax, %rdi
callq 0x4130
jmp 0x5609
pushq $0x1
popq %rbx
leaq 0x10(%rsp), %rdi
callq 0x69e2
leaq 0x58(%rsp), %rdi
callq 0x66ba
leaq 0x70(%rsp), %rdi
callq 0x4120
leaq 0x2c0(%rsp), %rdi
callq 0x40a0
movl %ebx, %eax
addq $0x4c8, %rsp            # imm = 0x4C8
popq %rbx
popq %r14
retq
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x4120
jmp 0x562b
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x69e2
jmp 0x565f
jmp 0x567a
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
jmp 0x5655
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
jmp 0x5655
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x69e2
jmp 0x567d
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x4080
jmp 0x567d
movq %rax, %rbx
jmp 0x5687
movq %rax, %rbx
jmp 0x5691
jmp 0x569d
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x69e2
leaq 0x58(%rsp), %rdi
callq 0x66ba
leaq 0x70(%rsp), %rdi
callq 0x4120
jmp 0x56a0
movq %rax, %rbx
leaq 0x2c0(%rsp), %rdi
callq 0x40a0
movq %rbx, %rdi
callq 0x43c0
nop