// File: /workspace/github_c_makefile_star2_O2/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 0x8d3d
cmpl $0x2, %edi
jne 0x8bfb
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %r15d, %r15d
jmp 0x8c76
movq %rdi, 0x8(%rsp)
leal -0x1(%rdi), %eax
movq %rax, 0x10(%rsp)
pushq $0x1
popq %rbx
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %r15d, %r15d
cmpq %rbx, 0x10(%rsp)
je 0x8c71
movq (%rsp), %rax
movq (%rax,%rbx,8), %rbp
movq %rbp, %rdi
leaq 0x2582(%rip), %rsi      # 0xb1af
callq 0x2110
testl %eax, %eax
sete %r14b
movq %rbp, %rdi
leaq 0x2573(%rip), %rsi      # 0xb1b5
callq 0x2110
testl %eax, %eax
sete %al
orb %al, %r13b
orb %al, %r14b
orb %r14b, %r15b
movq %rbp, %rdi
leaq 0x2561(%rip), %rsi      # 0xb1c0
callq 0x2110
testl %eax, %eax
sete %al
orb %al, %r12b
incq %rbx
jmp 0x8c14
movq 0x8(%rsp), %rdi
movl %edi, %eax
movq (%rsp), %rcx
movq -0x8(%rcx,%rax,8), %rdi
movq %rdi, 0x67e8(%rip)      # 0xf470
callq 0x8863
movq %rax, 0x67e4(%rip)      # 0xf478
movq %rax, %rdi
callq 0x8324
movq %rax, %rbx
movq %rax, 0x67da(%rip)      # 0xf480
testb $0x1, %r12b
je 0x8ccc
leaq 0x2520(%rip), %rdi      # 0xb1d3
callq 0x2070
leaq 0x250a(%rip), %rdi      # 0xb1c9
callq 0x2070
movq %rbx, %rdi
callq 0x46d4
xorl %eax, %eax
callq 0x4a06
movq %rax, %r12
testb $0x1, %r15b
je 0x8d16
leaq 0x24f0(%rip), %rdi      # 0xb1d3
callq 0x2070
leaq 0x2504(%rip), %rdi      # 0xb1f3
callq 0x2070
movq (%r12), %rdi
callq 0x4681
movq %r12, %r15
addq $0x8, %r15
movq (%r15), %r15
testq %r15, %r15
je 0x8d16
movq %r15, %rdi
callq 0x465e
jmp 0x8d04
testb $0x1, %r13b
jne 0x8d24
movq %r12, %rdi
callq 0x230c
movq %rbx, %rdi
callq 0x8201
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x62b4(%rip), %rax      # 0xeff8
movq (%rax), %rcx
leaq 0x2435(%rip), %rdi      # 0xb183
pushq $0x2b
popq %rsi
pushq $0x1
popq %rdx
callq 0x21d0
pushq $0x1
popq %rax
jmp 0x8d2e