// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xf0, %rsp
movb %dl, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
andb $0x1, %al
movb %al, -0x11(%rbp)
movq $0x0, -0x40(%rbp)
leaq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0xb2f0
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x10(%rbp), %rdx
callq 0xb560
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, (%rax)
je 0xbbe7
leaq -0x10(%rbp), %rdi
callq 0xb600
movq -0x10(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0xbb48
movq -0x10(%rbp), %rdi
movq %rdi, %rax
addq $0x1, %rax
movq %rax, -0x10(%rbp)
movl $0xa, %esi
callq 0x10a0
movq %rax, -0x68(%rbp)
leaq -0x10(%rbp), %rdi
callq 0xb600
movl $0x0, -0x6c(%rbp)
movq -0x10(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movsbl (%rax,%rcx), %edi
callq 0xb240
testb $0x1, %al
jne 0xb70a
jmp 0xb715
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0xb6f3
movq -0x10(%rbp), %rdi
movl -0x6c(%rbp), %esi
leaq 0x54b5(%rip), %rdx      # 0x10bd8
callq 0xb290
testb $0x1, %al
jne 0xb731
jmp 0xb888
movl -0x6c(%rbp), %ecx
movq -0x10(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x10(%rbp)
leaq -0x10(%rbp), %rdi
callq 0xb600
movq -0x10(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x22, %eax
jne 0xb883
movq -0x8(%rbp), %rdi
movl $0x2f, %esi
callq 0x10b0
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
jne 0xb781
leaq 0x5465(%rip), %rax      # 0x10be0
movq %rax, -0x78(%rbp)
jmp 0xb7ba
movq -0x80(%rbp), %rsi
movq -0x8(%rbp), %rax
subq %rax, %rsi
addq $0x2, %rsi
movl $0x1, %edi
callq 0x10d0
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x80(%rbp), %rdx
movq -0x8(%rbp), %rax
subq %rax, %rdx
addq $0x1, %rdx
callq 0x1040
movq -0x10(%rbp), %rdi
addq $0x1, %rdi
movl $0x22, %esi
callq 0x10a0
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movq -0x10(%rbp), %rcx
subq %rcx, %rax
subq $0x1, %rax
movl %eax, -0x8c(%rbp)
movq -0x78(%rbp), %rdi
callq 0x1090
movq %rax, %rsi
movslq -0x8c(%rbp), %rax
addq %rax, %rsi
addq $0x1, %rsi
movl $0x1, %edi
callq 0x10d0
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x1060
movq -0x98(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x78(%rbp), %rdi
callq 0x1090
movq -0xe8(%rbp), %rdi
addq %rax, %rdi
movq -0x10(%rbp), %rsi
addq $0x1, %rsi
movslq -0x8c(%rbp), %rdx
callq 0x1040
movq -0x98(%rbp), %rdi
callq 0x6160
movq %rax, -0xa0(%rbp)
movq -0x98(%rbp), %rdi
movq -0xa0(%rbp), %rsi
xorl %edx, %edx
callq 0xb650
jmp 0xbb37
movq -0x10(%rbp), %rdi
movl -0x6c(%rbp), %esi
leaq 0x534d(%rip), %rdx      # 0x10be3
callq 0xb290
testb $0x1, %al
jne 0xb8a4
jmp 0xbb35
movl -0x6c(%rbp), %ecx
movq -0x10(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x10(%rbp)
leaq -0x10(%rbp), %rdi
callq 0xb600
callq 0x11b0
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movsbl (%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x400, %eax            # imm = 0x400
cmpl $0x0, %eax
jne 0xb900
movq -0x10(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x5f, %eax
je 0xb900
movq -0x58(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq 0x4aec(%rip), %rdx      # 0x103e5
movb $0x0, %al
callq 0x5c40
movl $0x0, -0xa4(%rbp)
movq -0x10(%rbp), %rax
movslq -0xa4(%rbp), %rcx
movsbl (%rax,%rcx), %edi
callq 0xb240
testb $0x1, %al
jne 0xb924
jmp 0xb935
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0xb90a
movq -0x50(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x50(%rbp), %rsi
movq -0x10(%rbp), %rdx
movl -0xa4(%rbp), %ecx
xorl %edi, %edi
callq 0xb190
movq %rax, -0xb8(%rbp)
movl -0xa4(%rbp), %ecx
movq -0x10(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0xb0(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0xb0(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x60(%rbp), %rdi
movl $0x28, %esi
callq 0xb5c0
testb $0x1, %al
jne 0xb9b1
jmp 0xba66
movl $0x0, -0xbc(%rbp)
movq -0x60(%rbp), %rdi
addq $0x10, %rdi
callq 0xb600
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movsbl (%rax), %eax
cmpl $0x29, %eax
je 0xba38
movq -0x60(%rbp), %rdi
callq 0xbc90
movq %rax, -0xc8(%rbp)
movl -0xbc(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0xbc(%rbp)
movslq %eax, %rcx
movq -0xc8(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x60(%rbp), %rdi
addq $0x10, %rdi
callq 0xb600
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movsbl (%rax), %eax
cmpl $0x2c, %eax
je 0xba26
jmp 0xba38
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x10(%rax)
jmp 0xb9c8
movq -0x60(%rbp), %rdi
movl $0x29, %esi
callq 0xb5c0
testb $0x1, %al
jne 0xba64
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rsi
leaq 0x518d(%rip), %rdx      # 0x10bea
movb $0x0, %al
callq 0x5c40
jmp 0xba66
movq -0xb0(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xd0(%rbp)
movq -0xb0(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0xb0(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x60(%rbp), %rdi
addq $0x10, %rdi
callq 0xb600
movl $0x0, -0xd4(%rbp)
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0xa, %ecx
movb %al, -0xe9(%rbp)
je 0xbad5
cmpl $0x32, -0xd4(%rbp)
setl %al
movb %al, -0xe9(%rbp)
movb -0xe9(%rbp), %al
testb $0x1, %al
jne 0xbae1
jmp 0xbb08
movq -0x60(%rbp), %rdi
callq 0xbc90
movq -0x60(%rbp), %rdi
addq $0x10, %rdi
callq 0xb600
movl -0xd4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd4(%rbp)
jmp 0xbaad
movq -0xb8(%rbp), %rdi
movq -0xd0(%rbp), %rsi
movq -0xb0(%rbp), %rax
movq 0x8(%rax), %rdx
callq 0xb390
movq -0xb0(%rbp), %rax
movq $0x0, 0x8(%rax)
jmp 0xbb37
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x10(%rbp)
jmp 0xb6a0
movq -0x10(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x50(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0xa, %ecx
movb %al, -0xea(%rbp)
je 0xbb8f
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
setne %al
movb %al, -0xea(%rbp)
movb -0xea(%rbp), %al
testb $0x1, %al
jne 0xbb9b
jmp 0xbba6
movq -0x60(%rbp), %rdi
callq 0xbc90
jmp 0xbb60
jmp 0xbba8
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
jne 0xbbca
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x10(%rax)
jmp 0xbba8
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x10(%rbp)
jmp 0xb6a0
leaq 0x962a(%rip), %rax      # 0x15218
cmpq $0x0, (%rax)
jne 0xbc04
movq -0x40(%rbp), %rcx
leaq 0x9619(%rip), %rax      # 0x15218
movq %rcx, (%rax)
jmp 0xbc46
leaq 0x960d(%rip), %rax      # 0x15218
movq (%rax), %rax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0xbc37
movq -0xe0(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xe0(%rbp)
jmp 0xbc15
movq -0x40(%rbp), %rcx
movq -0xe0(%rbp), %rax
movq %rcx, 0x8(%rax)
testb $0x1, -0x11(%rbp)
je 0xbc6b
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rdx
movl $0x41, %edi
movl $0x1, %ecx
callq 0xb190
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x58(%rbp), %rax
movq %rcx, 0x18(%rax)
addq $0xf0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)