// File: /workspace/github_c_makefile_star2_O0/pocari[P]compilerbook-9cc/tokenize.c
// Function: tokenize
Token *tokenize(char *p) {
  Token head;
  head.next = NULL;
  Token *cur = &head;

  while (*p) {
    // fprintf(stderr, "*p ... %c\n", *p);
    if (isspace(*p)) {
      p++;
      continue;
    }

    if (starts_with(p, "//")) {
      p += 2;
      while (*p != '\n') {
        p++;
      }
      continue;
    }

    if (starts_with(p, "/*")) {
      char *q = strstr(p + 2, "*/");
      if (!q) {
        error_at(p, "ブロックコメントが閉じられていません");
      }
      p = q + 2;
      continue;
    }

    // fprintf(stderr, "c ... %c\n", *p);
    Keyword *key = tokenize_keyword(p);
    if (key) {
      int keyword_len = strlen(key->keyword);
      cur = new_token(key->kind, cur, p, keyword_len);
      assert(cur);
      p += keyword_len;
      continue;
    }

    int len = is_multi_char_operator(p);
    if (len) {
      cur = new_token(TK_RESERVED, cur, p, len);
      assert(cur);
      p += len;
      continue;
    }

    if (is_ident_first_char(*p)) {
      // fprintf(stderr, "tokenize ident(%c)\n", *p);
      char *s = p; // 識別子の先頭
      while (is_alnum(*p)) {
        p++;
      }

      cur = new_token(TK_IDENT, cur, s, p - s); //p - s で文字列長さになる
      assert(cur);
      continue;
    }

    if (*p == '"') {
      cur = tokenize_string_literal(cur, &p);
      assert(cur);
      continue;
    }
    if (*p == '\'') {
      cur = tokenize_char_literal(cur, &p);
      assert(cur);
      continue;
    }


    if (ispunct(*p)) {
      cur = new_token(TK_RESERVED, cur, p++, 1);
      assert(cur);
      continue;
    }

    if (isdigit(*p)) {
      cur = tokenize_number(cur, &p);
      assert(cur);
      continue;
    }


    error_at(p, "トークナイズできません");
  }
  new_token(TK_EOF, cur, p, 0);

  return head.next;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq $0x0, -0x38(%rbp)
leaq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
cmpb $0x0, (%rax)
je 0xcf93
callq 0x21d0
movq (%rax), %rax
movq -0x8(%rbp), %rcx
movsbl (%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
je 0xcc17
movq -0x8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x8(%rbp)
jmp 0xcbdc
movq -0x8(%rbp), %rdi
leaq 0x41ee(%rip), %rsi      # 0x10e10
callq 0xca60
testb $0x1, %al
jne 0xcc2d
jmp 0xcc55
movq -0x8(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
je 0xcc53
movq -0x8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x8(%rbp)
jmp 0xcc39
jmp 0xcbdc
movq -0x8(%rbp), %rdi
leaq 0x41b3(%rip), %rsi      # 0x10e13
callq 0xca60
testb $0x1, %al
jne 0xcc6b
jmp 0xccad
movq -0x8(%rbp), %rdi
addq $0x2, %rdi
leaq 0x419c(%rip), %rsi      # 0x10e16
callq 0x21c0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0xcc9c
movq -0x8(%rbp), %rdi
leaq 0x4184(%rip), %rsi      # 0x10e19
movb $0x0, %al
callq 0x6370
movq -0x50(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x8(%rbp)
jmp 0xcbdc
movq -0x8(%rbp), %rdi
callq 0xcab0
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
je 0xcd29
movq -0x58(%rbp), %rax
movq (%rax), %rdi
callq 0x2090
movl %eax, -0x5c(%rbp)
movq -0x58(%rbp), %rax
movl 0x8(%rax), %edi
movq -0x48(%rbp), %rsi
movq -0x8(%rbp), %rdx
movl -0x5c(%rbp), %ecx
callq 0xc5e0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0xccf4
jmp 0xcd13
leaq 0x4155(%rip), %rdi      # 0x10e50
leaq 0x4152(%rip), %rsi      # 0x10e54
movl $0x224, %edx            # imm = 0x224
leaq 0x4151(%rip), %rcx      # 0x10e5f
callq 0x20b0
movl -0x5c(%rbp), %ecx
movq -0x8(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x8(%rbp)
jmp 0xcbdc
movq -0x8(%rbp), %rdi
callq 0xcb50
movl %eax, -0x60(%rbp)
cmpl $0x0, -0x60(%rbp)
je 0xcd92
movq -0x48(%rbp), %rsi
movq -0x8(%rbp), %rdx
movl -0x60(%rbp), %ecx
movl $0x1, %edi
callq 0xc5e0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0xcd5d
jmp 0xcd7c
leaq 0x40ec(%rip), %rdi      # 0x10e50
leaq 0x40e9(%rip), %rsi      # 0x10e54
movl $0x22c, %edx            # imm = 0x22C
leaq 0x40e8(%rip), %rcx      # 0x10e5f
callq 0x20b0
movl -0x60(%rbp), %ecx
movq -0x8(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x8(%rbp)
jmp 0xcbdc
movq -0x8(%rbp), %rax
movsbl (%rax), %edi
callq 0xca00
testb $0x1, %al
jne 0xcda4
jmp 0xce1c
movq -0x8(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rax
movsbl (%rax), %edi
callq 0xc990
testb $0x1, %al
jne 0xcdbe
jmp 0xcdcc
movq -0x8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x8(%rbp)
jmp 0xcdac
movq -0x48(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq -0x8(%rbp), %rax
movq -0x68(%rbp), %rcx
subq %rcx, %rax
movl %eax, %ecx
movl $0x2, %edi
callq 0xc5e0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0xcdf8
jmp 0xce17
leaq 0x4051(%rip), %rdi      # 0x10e50
leaq 0x404e(%rip), %rsi      # 0x10e54
movl $0x239, %edx            # imm = 0x239
leaq 0x404d(%rip), %rcx      # 0x10e5f
callq 0x20b0
jmp 0xcbdc
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x22, %eax
jne 0xce66
movq -0x48(%rbp), %rdi
leaq -0x8(%rbp), %rsi
callq 0xcfc0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0xce42
jmp 0xce61
leaq 0x4007(%rip), %rdi      # 0x10e50
leaq 0x4004(%rip), %rsi      # 0x10e54
movl $0x23f, %edx            # imm = 0x23F
leaq 0x4003(%rip), %rcx      # 0x10e5f
callq 0x20b0
jmp 0xcbdc
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x27, %eax
jne 0xceb0
movq -0x48(%rbp), %rdi
leaq -0x8(%rbp), %rsi
callq 0xd130
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0xce8c
jmp 0xceab
leaq 0x3fbd(%rip), %rdi      # 0x10e50
leaq 0x3fba(%rip), %rsi      # 0x10e54
movl $0x244, %edx            # imm = 0x244
leaq 0x3fb9(%rip), %rcx      # 0x10e5f
callq 0x20b0
jmp 0xcbdc
callq 0x21d0
movq (%rax), %rax
movq -0x8(%rbp), %rcx
movsbl (%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0xcf1e
movq -0x48(%rbp), %rsi
movq -0x8(%rbp), %rdx
movq %rdx, %rax
addq $0x1, %rax
movq %rax, -0x8(%rbp)
movl $0x1, %ecx
movl %ecx, %edi
callq 0xc5e0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0xcefa
jmp 0xcf19
leaq 0x3f4f(%rip), %rdi      # 0x10e50
leaq 0x3f4c(%rip), %rsi      # 0x10e54
movl $0x24b, %edx            # imm = 0x24B
leaq 0x3f4b(%rip), %rcx      # 0x10e5f
callq 0x20b0
jmp 0xcbdc
callq 0x21d0
movq (%rax), %rax
movq -0x8(%rbp), %rcx
movsbl (%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x800, %eax            # imm = 0x800
cmpl $0x0, %eax
je 0xcf7c
movq -0x48(%rbp), %rdi
leaq -0x8(%rbp), %rsi
callq 0xd230
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0xcf58
jmp 0xcf77
leaq 0x3ef1(%rip), %rdi      # 0x10e50
leaq 0x3eee(%rip), %rsi      # 0x10e54
movl $0x251, %edx            # imm = 0x251
leaq 0x3eed(%rip), %rcx      # 0x10e5f
callq 0x20b0
jmp 0xcbdc
movq -0x8(%rbp), %rdi
leaq 0x3ef0(%rip), %rsi      # 0x10e77
movb $0x0, %al
callq 0x6370
jmp 0xcbdc
movq -0x48(%rbp), %rsi
movq -0x8(%rbp), %rdx
movl $0x1e, %edi
xorl %ecx, %ecx
callq 0xc5e0
movq -0x38(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)