// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %r12
movq %rdi, 0x8(%rsp)
movq $0x0, 0x18(%rsp)
movb (%rdi), %r15b
leaq 0x10(%rsp), %rax
testb %r15b, %r15b
je 0xa087
movq %rax, (%rsp)
callq 0x2210
movq %rax, %rbx
movq (%rbx), %r14
movsbq %r15b, %rax
movzwl (%r14,%rax,2), %ebp
btl $0xd, %ebp
jb 0x9c18
cmpw $0x2f2f, (%r12)         # imm = 0x2F2F
je 0x9c1d
cmpw $0x2a2f, (%r12)         # imm = 0x2A2F
jne 0x9c3e
leaq 0x2(%r12), %rdi
leaq 0x31f8(%rip), %rsi      # 0xcde9
callq 0x2200
movq %rax, %r13
testq %rax, %rax
jne 0x9c0f
movq %r12, %rdi
leaq 0x31e4(%rip), %rsi      # 0xcdec
xorl %eax, %eax
callq 0x487c
addq $0x2, %r13
movq %r13, %r12
jmp 0x9c30
incq %r12
jmp 0x9c30
incq %r12
movq %r12, %rax
leaq 0x1(%rax), %r12
cmpb $0xa, 0x1(%rax)
movq %r12, %rax
jne 0x9c23
movb (%r12), %r15b
testb %r15b, %r15b
jne 0x9bc1
jmp 0xa08b
movq %r12, 0x8(%rsp)
movq %r12, %rdi
callq 0x9ab1
testq %rax, %rax
je 0x9c88
movq %rax, %r13
movq (%rax), %rdi
callq 0x20b0
movq %rax, %rbp
movl 0x8(%r13), %r14d
movl $0x1, %edi
movl $0x38, %esi
callq 0x2100
movl %r14d, (%rax)
movq %r12, 0x18(%rax)
movl %ebp, 0x20(%rax)
movq (%rsp), %rcx
movq %rax, 0x8(%rcx)
movslq %ebp, %rcx
jmp 0x9cbf
movq %r12, %rdi
callq 0x9b37
testl %eax, %eax
je 0x9cd5
movl %eax, %r13d
movl $0x1, %edi
movl $0x38, %esi
callq 0x2100
movl $0x1, (%rax)
movq %r12, 0x18(%rax)
movl %r13d, 0x20(%rax)
movq (%rsp), %rcx
movq %rax, 0x8(%rcx)
movslq %r13d, %rcx
addq %r12, %rcx
movq %rcx, 0x8(%rsp)
movq %rax, (%rsp)
movq 0x8(%rsp), %r12
jmp 0x9c30
cmpb $0x5f, %r15b
je 0x9d47
movl %r15d, %eax
andb $-0x21, %al
addb $-0x41, %al
cmpb $0x19, %al
jbe 0x9d47
cmpb $0x27, %r15b
je 0x9da5
movzbl %r15b, %eax
cmpl $0x22, %eax
jne 0x9dc6
xorl %eax, %eax
callq 0x96e8
movq %rax, %rbp
movq %r12, %r15
movzbl 0x1(%r15), %eax
cmpl $0x5c, %eax
je 0x9d2d
testl %eax, %eax
je 0x9e60
cmpl $0x22, %eax
je 0x9e71
incq %r15
movsbl %al, %esi
jmp 0x9d3d
movsbl 0x2(%r15), %edi
addq $0x2, %r15
callq 0xa1c7
movl %eax, %esi
movq %rbp, %rdi
callq 0x9720
jmp 0x9d0a
movq 0x8(%rsp), %rcx
leaq -0x1(%rcx), %rax
movl %r12d, %ebp
notl %ebp
addl %ecx, %ebp
movb 0x1(%rax), %cl
incq %rax
incl %ebp
cmpb $0x5f, %cl
je 0x9d57
leal -0x30(%rcx), %edx
cmpb $0xa, %dl
jb 0x9d57
andb $-0x21, %cl
addb $-0x41, %cl
cmpb $0x1a, %cl
jb 0x9d57
movq %rax, 0x8(%rsp)
movl $0x1, %edi
movl $0x38, %esi
callq 0x2100
movl $0x2, (%rax)
movq %r12, 0x18(%rax)
movl %ebp, 0x20(%rax)
movq (%rsp), %rcx
movq %rax, 0x8(%rcx)
jmp 0x9cc7
movsbl 0x1(%r12), %r14d
cmpl $0x5c, %r14d
jne 0x9df0
leaq 0x2(%r12), %rbp
movsbl 0x2(%r12), %edi
callq 0xa1c7
movl %eax, %r14d
jmp 0x9df5
testb $0x4, %bpl
jne 0x9ef0
btl $0xb, %ebp
jb 0x9f1c
movq %r12, %rdi
leaq 0x303f(%rip), %rsi      # 0xce23
xorl %eax, %eax
callq 0x487c
jmp 0x9ccb
leaq 0x1(%r12), %rbp
cmpb $0x0, 0x1(%rbp)
jne 0x9e0c
movq %rbp, %rdi
leaq 0x3101(%rip), %rsi      # 0xcf06
xorl %eax, %eax
callq 0x487c
leaq 0x1(%rbp), %rdi
cmpb $0x27, (%rdi)
je 0x9e23
leaq 0x310f(%rip), %rsi      # 0xcf2b
xorl %eax, %eax
callq 0x487c
addq $0x2, %rbp
movq %rbp, 0x8(%rsp)
movl $0x1, %edi
movl $0x38, %esi
callq 0x2100
movl $0x3, (%rax)
movq %r12, 0x18(%rax)
movl $0x0, 0x20(%rax)
movq (%rsp), %rcx
movq %rax, 0x8(%rcx)
movslq %r14d, %rcx
movq %rcx, 0x10(%rax)
jmp 0x9cc7
movq %r12, %rdi
leaq 0x3074(%rip), %rsi      # 0xcede
xorl %eax, %eax
callq 0x487c
addq $0x2, %r15
movl %r15d, %r14d
subl %r12d, %r14d
movl $0x1, %edi
movl $0x38, %esi
callq 0x2100
movq %rax, %r13
movl $0x14, (%rax)
movq %r12, 0x18(%rax)
movl %r14d, 0x20(%rax)
movq (%rsp), %rax
movq %r13, 0x8(%rax)
movq %rbp, %rdi
xorl %esi, %esi
callq 0x9720
movq %rbp, %rdi
callq 0x97ba
movq %rax, %r12
movq %rbp, %rdi
callq 0x97be
movq %r12, %rdi
movl %eax, %esi
callq 0x4ae3
movq %rax, 0x28(%r13)
movq %rbp, %rdi
callq 0x97be
movl %eax, 0x30(%r13)
movq %rbp, %rdi
callq 0x9781
movq %r15, 0x8(%rsp)
movq %r13, (%rsp)
jmp 0x9ccb
leaq 0x1(%r12), %rax
movq %rax, 0x8(%rsp)
movl $0x1, %edi
movl $0x38, %esi
callq 0x2100
movl $0x1, %ecx
movl %ecx, (%rax)
movq %r12, 0x18(%rax)
movl %ecx, 0x20(%rax)
jmp 0x9d98
cmpw $0x7830, (%r12)         # imm = 0x7830
je 0x9f90
cmpw $0x6230, (%r12)         # imm = 0x6230
je 0xa030
cmpb $0x30, %r15b
jne 0x9f4a
movsbq 0x1(%r12), %rax
testb $0x8, 0x1(%r14,%rax,2)
jne 0xa05e
movl $0x1, %edi
movl $0x38, %esi
callq 0x2100
movq %rax, %r13
movl $0x3, (%rax)
movq %r12, 0x18(%rax)
movl $0x0, 0x20(%rax)
movq (%rsp), %rax
movq %r13, 0x8(%rax)
movq %r12, %rdi
leaq 0x8(%rsp), %rsi
movl $0xa, %edx
callq 0x2140
movq %rax, 0x10(%r13)
jmp 0x9ee7
leaq 0x2(%r12), %r14
xorl %r15d, %r15d
movzbl (%r14), %eax
movl %eax, %ecx
addl $-0x41, %ecx
cmpl $0x25, %ecx
ja 0x9ff2
movabsq $0x3f0000003f, %rdx     # imm = 0x3F0000003F
btq %rcx, %rdx
jae 0x9ff2
movsbq %al, %rax
shlq $0x4, %r15
leal -0x41(%rax), %ecx
cmpb $0x6, %cl
movq $-0x30, %rcx
movq $-0x37, %rdx
cmovbq %rdx, %rcx
leal -0x61(%rax), %edx
cmpb $0x6, %dl
movq $-0x57, %rdx
cmovbq %rdx, %rcx
addq %rax, %r15
addq %rcx, %r15
incq %r14
jmp 0x9f98
leal -0x30(%rax), %ecx
cmpb $0x9, %cl
jbe 0x9fb6
movl $0x1, %edi
movl $0x38, %esi
callq 0x2100
movl $0x3, (%rax)
movq %r12, 0x18(%rax)
movl $0x0, 0x20(%rax)
movq (%rsp), %rcx
movq %rax, 0x8(%rcx)
movq %r15, 0x10(%rax)
movq %r14, 0x8(%rsp)
jmp 0x9cc7
leaq 0x2(%r12), %r14
movb 0x2(%r12), %al
movl %eax, %ecx
andb $-0x2, %cl
xorl %r15d, %r15d
cmpb $0x30, %cl
jne 0x9ffa
andb $0x1, %al
movzbl %al, %eax
leaq (%rax,%r15,2), %r15
movb 0x1(%r14), %al
incq %r14
movl %eax, %ecx
andb $-0x2, %cl
jmp 0xa042
leaq 0x1(%r12), %r14
movl %eax, %ecx
andb $-0x8, %cl
xorl %r15d, %r15d
cmpb $0x30, %cl
jne 0x9ffa
andb $0x7, %al
movzbl %al, %eax
leaq (%rax,%r15,8), %r15
movb 0x1(%r14), %al
incq %r14
movl %eax, %ecx
andb $-0x8, %cl
jmp 0xa06b
movq %rax, (%rsp)
movq %r12, 0x8(%rsp)
movl $0x1, %edi
movl $0x38, %esi
callq 0x2100
movl $0x1e, (%rax)
movq %r12, 0x18(%rax)
movl $0x0, 0x20(%rax)
movq (%rsp), %rcx
movq %rax, 0x8(%rcx)
movq 0x18(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq