// File: /workspace/github_c_makefile_star2_O2/sisshiki1969[P]monocc/src/tokenize.c
// Function: tokenize
void tokenize(char *file, char *p, bool is_main) {
  Token head;
  head.next = NULL;
  Token *cur = &head;
  FileInfo *fi = new_file_info(file, p);
  TokContext *ctx = new_tok_context(fi, cur, p);

  while (*p) {
    skip_whitespace(&p);

    if (*p == '#') {
      char *line_end = strchr(p++, '\n');
      skip_whitespace(&p);
      int len = 0;
      while (is_ident_char(*(p + len))) len++;

      if (is_reserved(p, len, "include")) {
        p += len;
        skip_whitespace(&p);
        if (*p == '"') {
          // get directory name of the current file.
          char *cur_dir;
          char *f = strrchr(file, '/');
          if (!f)
            cur_dir = "./";
          else {
            cur_dir = calloc(1, f - file + 2);
            strncpy(cur_dir, file, f - file + 1);
          }
          // fprintf(stderr, "directory: %s\n", cur_dir);
          // concat the include file name to the directory name.
          char *end = strchr(p + 1, '"');
          int len = end - p - 1;
          char *file_name = calloc(1, strlen(cur_dir) + len + 1);
          strcpy(file_name, cur_dir);
          strncpy(file_name + strlen(cur_dir), p + 1, len);

          // fprintf(stderr, "include <%s>\n", file_name);
          char *text = read_file(file_name);
          tokenize(file_name, text, false);
        }
      } else if (is_reserved(p, len, "define")) {
        // fprintf(stderr, "macro define: ");
        p += len;
        skip_whitespace(&p);
        if (!isalpha(*p) && *p != '_')
          error_at_char(fi, p, "Expected identifier.");
        int len = 0;
        while (is_ident_char(p[len])) len++;

        Token *head = cur;
        Token *tok = new_token(TK_IDENT, cur, p, len);
        p += len;

        ctx->p = p;
        ctx->cur = head;
        // print_token(stderr, tok);
        head->next = NULL;

        if (read_if(ctx, '(')) {
          int i = 0;
          // read params of function-like macro
          // fprintf(stderr, " ( ");
          skip_whitespace(&ctx->p);
          // function macro
          while (*ctx->p != ')') {
            Token *param_token = read_token(ctx);
            param_token->num_val = i++;
            // print_token(stderr, param_token);

            skip_whitespace(&ctx->p);
            if (*ctx->p != ',') break;
            ctx->p++;
          }
          if (!read_if(ctx, ')')) error_at_char(fi, ctx->p, "Expected ')'");
          // fprintf(stderr, " ) ");
        }

        // fprintf(stderr, "=>");
        Token *params = head->next;
        head->next = NULL;
        ctx->cur = head;

        skip_whitespace(&ctx->p);
        int i = 0;
        while (*ctx->p != '\n' && i < 50) {
          read_token(ctx);
          // print_token(stderr, read_token(ctx));
          skip_whitespace(&ctx->p);
          i++;
        }
        new_macro(tok, params, head->next);
        head->next = NULL;

        // fprintf(stderr, "\n");
      }

      p = line_end + 1;
      continue;
    }

    // TokContext *ctx = new_tok_context(fi, cur, p);
    ctx->p = p;
    ctx->cur = cur;
    while (*ctx->p != '\n' && *ctx->p != 0) {
      read_token(ctx);
    }

    while (*ctx->p == '\n') ctx->p++;
    cur = ctx->cur;
    p = ctx->p;
  }

  if (!token) {
    token = head.next;
  } else {
    Token *cursor = token;
    while (cursor->next) cursor = cursor->next;
    cursor->next = head.next;
  }
  if (is_main) new_token(TK_EOF, ctx->cur, ctx->p, 1);
  fi->end = ctx->p;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %edx, 0x2c(%rsp)
movq %rsi, %rbp
movq %rsp, %r13
movq %rsi, (%r13)
leaq 0x38(%rsp), %rbx
andq $0x0, 0x8(%rbx)
movq %rdi, 0x30(%rsp)
callq 0x8105
movq %rax, 0x18(%rsp)
movq %rax, %rdi
movq %rbx, 0x8(%rsp)
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x8252
movq %rax, %r14
movq %rax, %r15
addq $0x10, %r15
cmpb $0x0, (%rbp)
je 0x8623
movq %r13, %rdi
callq 0x8294
movq (%rsp), %rbp
movb (%rbp), %al
cmpb $0x23, %al
jne 0x85d7
leaq 0x1(%rbp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
pushq $0xa
popq %rsi
callq 0x10a0
movq %rax, %rbp
movq %r13, %rdi
callq 0x8294
movq (%rsp), %r12
leaq -0x1(%r12), %rbx
pushq $-0x1
popq %r13
movsbl 0x1(%rbx), %edi
incq %rbx
callq 0x80ac
incl %r13d
testb %al, %al
jne 0x834c
movq %r12, %rdi
movl %r13d, %esi
leaq 0x35e0(%rip), %rdx      # 0xb94c
callq 0x80cd
testb %al, %al
je 0x8457
movq %rbx, (%rsp)
movq %rsp, %r13
movq %r13, %rdi
callq 0x8294
movq (%rsp), %r12
cmpb $0x22, (%r12)
jne 0x85cb
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
pushq $0x2f
popq %rsi
callq 0x10b0
movq %rax, %r13
leaq 0x35a3(%rip), %rax      # 0xb954
movq %rax, 0x10(%rsp)
testq %r13, %r13
je 0x83e0
subq %rbx, %r13
leaq 0x2(%r13), %rsi
pushq $0x1
popq %rdi
callq 0x10d0
incq %r13
movq %rax, 0x10(%rsp)
movq %rax, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x1040
leaq 0x1(%r12), %rdi
movq %rdi, 0x20(%rsp)
pushq $0x22
popq %rsi
callq 0x10a0
movq %rax, %rbx
notl %r12d
addl %r12d, %ebx
movq 0x10(%rsp), %rdi
callq 0x1090
movq %rax, %r13
movslq %ebx, %rbx
leaq (%rax,%rbx), %rsi
incq %rsi
pushq $0x1
popq %rdi
callq 0x10d0
movq %rax, %r12
movq %rax, %rdi
movq 0x10(%rsp), %rsi
callq 0x1060
addq %r12, %r13
movq %r13, %rdi
movq 0x20(%rsp), %rsi
movq %rbx, %rdx
callq 0x1040
movq %r12, %rdi
callq 0x4b78
movq %r12, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x82ae
jmp 0x85c8
movq %r12, %rdi
movl %r13d, %esi
leaq 0x34f3(%rip), %rdx      # 0xb957
callq 0x80cd
testb %al, %al
movq %rsp, %r13
je 0x85cb
movq %rbx, (%rsp)
movq %r13, %rdi
callq 0x8294
callq 0x11c0
movq (%rsp), %rbx
movsbq (%rbx), %rcx
cmpq $0x5f, %rcx
je 0x84b7
movq (%rax), %rax
movzwl (%rax,%rcx,2), %eax
andl $0x400, %eax            # imm = 0x400
jne 0x84b7
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
leaq 0x2eb9(%rip), %rdx      # 0xb369
xorl %eax, %eax
callq 0x47ee
pushq $-0x1
popq %r13
movq %rbx, 0x10(%rsp)
movsbl (%rbx), %edi
callq 0x80ac
incq %rbx
incl %r13d
testb %al, %al
jne 0x84c0
leaq -0x1(%rbx), %rax
movq %rax, 0x20(%rsp)
xorl %edi, %edi
movq 0x8(%rsp), %r12
movq %r12, %rsi
movq 0x10(%rsp), %rdx
movl %r13d, %ecx
callq 0x804c
movq 0x20(%rsp), %rcx
movq %rcx, (%rsp)
movq %rcx, 0x10(%r14)
movq %r12, 0x8(%r14)
andq $0x0, 0x8(%r12)
movq %rax, %r12
cmpb $0x28, -0x1(%rbx)
jne 0x8572
movq %rbx, (%r15)
movq %r15, %rdi
callq 0x8294
movq (%r15), %rsi
xorl %ebx, %ebx
cmpb $0x29, (%rsi)
je 0x8557
movq %r14, %rdi
callq 0x867e
movq %rbx, 0x10(%rax)
movq %r15, %rdi
callq 0x8294
movq 0x10(%r14), %rsi
movzbl (%rsi), %eax
cmpl $0x2c, %eax
jne 0x8552
incq %rbx
incq %rsi
movq %rsi, (%r15)
jmp 0x8522
cmpl $0x29, %eax
jne 0x855f
incq %rsi
movq %rsi, (%r15)
jmp 0x8572
movq 0x18(%rsp), %rdi
leaq 0x33f3(%rip), %rdx      # 0xb95e
xorl %eax, %eax
callq 0x47ee
movq 0x8(%rsp), %rax
movq 0x8(%rax), %r13
andq $0x0, 0x8(%rax)
movq %rax, 0x8(%r14)
movq %r15, %rdi
callq 0x8294
xorl %ebx, %ebx
movq (%r15), %rax
cmpb $0xa, (%rax)
je 0x85af
cmpl $0x31, %ebx
ja 0x85af
movq %r14, %rdi
callq 0x867e
movq %r15, %rdi
callq 0x8294
incl %ebx
jmp 0x858e
movq 0x8(%rsp), %rbx
movq 0x8(%rbx), %rdx
movq %r12, %rdi
movq %r13, %rsi
callq 0x814c
andq $0x0, 0x8(%rbx)
movq %rsp, %r13
incq %rbp
movq %rbp, (%rsp)
jmp 0x8300
movq %rbp, 0x10(%r14)
movq 0x8(%rsp), %rcx
movq %rcx, 0x8(%r14)
cmpb $0xa, %al
je 0x8600
movzbl %al, %eax
testl %eax, %eax
je 0x8600
movq %r14, %rdi
callq 0x867e
movq 0x10(%r14), %rbp
movb (%rbp), %al
jmp 0x85e4
decq %rbp
cmpb $0xa, 0x1(%rbp)
leaq 0x1(%rbp), %rbp
je 0x8603
movq %rbp, 0x10(%r14)
movq 0x8(%r14), %rax
movq %rax, 0x8(%rsp)
movq %rbp, (%rsp)
jmp 0x8300
leaq 0x8bfe(%rip), %rax      # 0x11228
movq (%rax), %rcx
testq %rcx, %rcx
je 0x8642
movq %rcx, %rax
movq 0x8(%rcx), %rcx
testq %rcx, %rcx
jne 0x8632
addq $0x8, %rax
movq 0x40(%rsp), %rcx
movq %rcx, (%rax)
movq (%r15), %rbx
cmpb $0x0, 0x2c(%rsp)
je 0x8666
movq 0x8(%r14), %rsi
pushq $0x41
popq %rdi
pushq $0x1
popq %rcx
movq %rbx, %rdx
callq 0x804c
movq 0x18(%rsp), %rax
movq %rbx, 0x18(%rax)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq