// File: /workspace/github_c_makefile_star2_O1/pocari[P]compilerbook-9cc/ynicc.c
// Function: main
int main(int argc, char **argv) {
  bool f_dump_ast = false;
  bool f_dump_ast_only = false;
  bool f_dump_tokens = false;

  if (argc < 2) {
    fprintf(stderr, "引数の個数が正しくありません\n");
    return 1;
  }

  if (argc > 2) {
    for (int i = 1; i < argc - 1; i++) {
      if (strcmp(argv[i], "--ast") == 0) {
        f_dump_ast = true;
      }
      if (strcmp(argv[i], "--ast-only") == 0) {
        f_dump_ast = true;
        f_dump_ast_only = true;
      }
      if (strcmp(argv[i], "--tokens") == 0) {
        f_dump_tokens = true;
      }
    }
  }

  // プログラム全体を保存
  filename = argv[argc - 1];
  user_input = read_file(filename);

  // head はfree用
  Token *head = token = tokenize(user_input);
  // fprintf(stderr, "-------------------------------- tokenized\n");
  if (f_dump_tokens) {
    printf("##-----------------------------\n");
    printf("## tokens\n");
    dump_tokens(head);
  }

  // パースする(結果は グローバル変数のfunctionsに入る)
  Program *pgm = program();


  if (f_dump_ast) {
    printf("##-----------------------------\n");
    printf("## ast\n");
    dump_globals(pgm->global_var);
    for (Function *f = pgm->functions; f; f = f->next) {
      if (f_dump_ast) {
        dump_function(f);
      }
    }
  }

  if (!f_dump_ast_only) {
    codegen(pgm);
  }

  free_tokens(head);

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, (%rsp)
cmpl $0x1, %edi
jle 0xa61d
cmpl $0x2, %edi
jne 0xa4ce
xorl %r12d, %r12d
xorl %r13d, %r13d
xorl %r15d, %r15d
jmp 0xa55a
movq %rdi, 0x8(%rsp)
leal -0x1(%rdi), %eax
movq %rax, 0x10(%rsp)
movl $0x1, %ebx
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %r12d, %r12d
movq (%rsp), %rax
movq (%rax,%rbx,8), %rbp
movq %rbp, %rdi
leaq 0x2c5c(%rip), %rsi      # 0xd157
callq 0x2110
movl %eax, %r14d
movq %rbp, %rdi
leaq 0x2c50(%rip), %rsi      # 0xd15d
callq 0x2110
testl %r14d, %r14d
movzbl %r15b, %r15d
movl $0x1, %r14d
cmovel %r14d, %r15d
testl %eax, %eax
movzbl %r12b, %r12d
cmovel %r14d, %r12d
cmovel %r14d, %r15d
movq %rbp, %rdi
leaq 0x2c2d(%rip), %rsi      # 0xd168
callq 0x2110
testl %eax, %eax
je 0xa548
movzbl %r13b, %r14d
movl %r14d, %r13d
incq %rbx
cmpq %rbx, 0x10(%rsp)
jne 0xa4e9
movq 0x8(%rsp), %rdi
movl %edi, %eax
movq (%rsp), %rcx
movq -0x8(%rcx,%rax,8), %rdi
movq %rdi, 0x5f04(%rip)      # 0x10470
callq 0xa0cc
movq %rax, 0x5f00(%rip)      # 0x10478
movq %rax, %rdi
callq 0x9b85
movq %rax, %rbx
movq %rax, 0x5ef6(%rip)      # 0x10480
testb $0x1, %r13b
je 0xa5b0
leaq 0x2be4(%rip), %rdi      # 0xd17b
callq 0x2070
leaq 0x2bce(%rip), %rdi      # 0xd171
callq 0x2070
movq %rbx, %rdi
callq 0x485d
xorl %eax, %eax
callq 0x4c25
movq %rax, %r14
testb $0x1, %r15b
je 0xa5f6
leaq 0x2bb4(%rip), %rdi      # 0xd17b
callq 0x2070
leaq 0x2bc8(%rip), %rdi      # 0xd19b
callq 0x2070
movq (%r14), %rdi
callq 0x4808
movq 0x8(%r14), %r15
testq %r15, %r15
je 0xa5f6
movq %r15, %rdi
callq 0x47e5
movq (%r15), %r15
jmp 0xa5e4
testb $0x1, %r12b
jne 0xa604
movq %r14, %rdi
callq 0x23dc
movq %rbx, %rdi
callq 0x9a37
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x22c5
movl $0x1, %eax
jmp 0xa60e