// File: /workspace/github_c_makefile_star2_O3/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, %rbp
movq %rdi, 0x8(%rsp)
movq $0x0, 0x18(%rsp)
movb (%rdi), %r15b
leaq 0x10(%rsp), %r13
testb %r15b, %r15b
je 0x9c79
callq 0x2210
movq %rax, %rbx
movq %r13, (%rsp)
movq %rbp, %r12
movq (%rbx), %r14
movsbq %r15b, %rax
movzwl (%r14,%rax,2), %ebp
btl $0xd, %ebp
jb 0x97fb
cmpw $0x2f2f, (%r12)         # imm = 0x2F2F
je 0x980c
cmpw $0x2a2f, (%r12)         # imm = 0x2A2F
jne 0x9821
leaq 0x2(%r12), %rdi
leaq 0x3645(%rip), %rsi      # 0xce19
callq 0x2200
movq %rax, %r13
testq %rax, %rax
jne 0x97f2
movq %r12, %rdi
leaq 0x3631(%rip), %rsi      # 0xce1c
xorl %eax, %eax
callq 0x487c
addq $0x2, %r13
movq %r13, %r12
jmp 0x97fe
incq %r12
movb (%r12), %r15b
testb %r15b, %r15b
jne 0x97a4
jmp 0x9c72
incq %r12
cmpb $0xa, 0x1(%r12)
leaq 0x1(%r12), %r12
jne 0x980f
movb $0xa, %r15b
jmp 0x9802
movq %r12, 0x8(%rsp)
movq %r12, %rdi
callq 0x969d
testq %rax, %rax
je 0x9879
movq %rax, %r13
movq (%rax), %rdi
callq 0x20b0
movq %rax, %rbp
movl 0x8(%r13), %r14d
movl $0x1, %edi
movl $0x38, %esi
callq 0x2100
movq %rax, %r13
movl %r14d, (%rax)
movq %r12, 0x18(%rax)
movl %ebp, 0x20(%rax)
movq (%rsp), %rcx
movq %rax, 0x8(%rcx)
movslq %ebp, %rbp
addq %r12, %rbp
movq %rbp, 0x8(%rsp)
jmp 0x9ad9
movq %r12, %rdi
callq 0x9717
testl %eax, %eax
je 0x98c0
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, %rbp
addq %r12, %rbp
movq %rbp, 0x8(%rsp)
movq %rax, %r13
jmp 0x9ad9
cmpb $0x5f, %r15b
je 0x9930
movl %r15d, %eax
andb $-0x21, %al
addb $-0x41, %al
cmpb $0x19, %al
jbe 0x9930
cmpb $0x27, %r15b
je 0x998a
movzbl %r15b, %eax
cmpl $0x22, %eax
jne 0x99ab
xorl %eax, %eax
callq 0x9304
movq %rax, %r13
movq %r12, %rbp
movzbl 0x1(%rbp), %eax
cmpl $0x5c, %eax
je 0x9917
testl %eax, %eax
je 0x9a4f
cmpl $0x22, %eax
je 0x9a60
incq %rbp
movsbl %al, %esi
jmp 0x9926
movsbl 0x2(%rbp), %edi
addq $0x2, %rbp
callq 0x9dac
movl %eax, %esi
movq %r13, %rdi
callq 0x9335
jmp 0x98f5
leaq -0x1(%r12), %rbp
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movb 0x1(%rbp), %al
incq %rbp
incl %r14d
cmpb $0x5f, %al
je 0x993b
leal -0x30(%rax), %ecx
cmpb $0xa, %cl
jb 0x993b
andb $-0x21, %al
addb $-0x41, %al
cmpb $0x1a, %al
jb 0x993b
movq %rbp, 0x8(%rsp)
movl $0x1, %edi
movl $0x38, %esi
callq 0x2100
movq %rax, %r13
movl $0x2, (%rax)
movq %r12, 0x18(%rax)
movl %r14d, 0x20(%rax)
movq (%rsp), %rcx
movq %rax, 0x8(%rcx)
jmp 0x9ad9
movsbl 0x1(%r12), %r13d
cmpl $0x5c, %r13d
jne 0x99dc
leaq 0x2(%r12), %rbp
movsbl 0x2(%r12), %edi
callq 0x9dac
movl %eax, %r13d
jmp 0x99e1
testb $0x4, %bpl
jne 0x9aeb
btl $0xb, %ebp
jb 0x9b1a
movq %r12, %rdi
leaq 0x348a(%rip), %rsi      # 0xce53
xorl %eax, %eax
callq 0x487c
movq %r12, %rbp
movq (%rsp), %r13
jmp 0x9ad9
leaq 0x1(%r12), %rbp
movb 0x1(%rbp), %al
testb %al, %al
jne 0x99fc
movq %rbp, %rdi
leaq 0x3544(%rip), %rsi      # 0xcf36
xorl %eax, %eax
callq 0x487c
movb 0x1(%rbp), %al
cmpb $0x27, %al
je 0x9a12
leaq 0x1(%rbp), %rdi
leaq 0x3550(%rip), %rsi      # 0xcf5b
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 %r13d, %rcx
movq %rcx, 0x10(%rax)
jmp 0x98b8
movq %r12, %rdi
leaq 0x34b5(%rip), %rsi      # 0xcf0e
xorl %eax, %eax
callq 0x487c
addq $0x2, %rbp
movl %ebp, %r14d
subl %r12d, %r14d
movl $0x1, %edi
movl $0x38, %esi
callq 0x2100
movq %rax, %r15
movl $0x14, (%rax)
movq %r12, 0x18(%rax)
movl %r14d, 0x20(%rax)
movq (%rsp), %rax
movq %r15, 0x8(%rax)
movq %r13, %rdi
xorl %esi, %esi
callq 0x9335
movq %r13, %rdi
callq 0x93b0
movq %rax, %r12
movq %r13, %rdi
callq 0x93b4
movq %r12, %rdi
movl %eax, %esi
callq 0x4ae3
movq %rax, 0x28(%r15)
movq %r13, %rdi
callq 0x93b4
movl %eax, 0x30(%r15)
movq %r13, %rdi
callq 0x9377
movq %rbp, 0x8(%rsp)
movq %r15, %r13
movb (%rbp), %r15b
testb %r15b, %r15b
jne 0x979d
jmp 0x9c79
leaq 0x1(%r12), %rbp
movq %rbp, 0x8(%rsp)
movl $0x1, %edi
movl $0x38, %esi
callq 0x2100
movq %rax, %r13
movl $0x1, %ecx
movl %ecx, (%rax)
movq %r12, 0x18(%rax)
movl %ecx, 0x20(%rax)
jmp 0x997d
cmpw $0x7830, (%r12)         # imm = 0x7830
je 0x9b8c
cmpw $0x6230, (%r12)         # imm = 0x6230
je 0x9c1d
cmpb $0x30, %r15b
jne 0x9b48
movsbq 0x1(%r12), %rax
testb $0x8, 0x1(%r14,%rax,2)
jne 0x9c4a
movl $0x1, %edi
movl $0x38, %esi
callq 0x2100
movq %rax, %r13
movl $0x3, (%rax)
movq %r12, 0x18(%rax)
movq (%rsp), %rax
movq %r13, 0x8(%rax)
movq %r12, %rdi
leaq 0x8(%rsp), %rsi
movl $0xa, %edx
callq 0x2140
movq %rax, 0x10(%r13)
movq 0x8(%rsp), %rbp
jmp 0x9ad9
leaq 0x2(%r12), %rbp
xorl %r14d, %r14d
movq $-0x57, %rsi
movabsq $0x3f0000003f, %rdi     # imm = 0x3F0000003F
movzbl (%rbp), %eax
movl %eax, %ecx
addl $-0x41, %ecx
cmpl $0x25, %ecx
ja 0x9be8
btq %rcx, %rdi
jae 0x9be8
leal -0x41(%rax), %edx
movq $-0x37, %rcx
cmpb $0x6, %dl
jb 0x9bcf
movq $-0x30, %rcx
shlq $0x4, %r14
leal -0x61(%rax), %edx
cmpb $0x6, %dl
cmovbq %rsi, %rcx
addq %rax, %r14
addq %rcx, %r14
incq %rbp
jmp 0x9ba5
leal -0x30(%rax), %ecx
cmpb $0x9, %cl
jbe 0x9bb9
movl $0x1, %edi
movl $0x38, %esi
callq 0x2100
movq %rax, %r13
movl $0x3, (%rax)
movq %r12, 0x18(%rax)
movq (%rsp), %rcx
movq %rax, 0x8(%rcx)
movq %r14, 0x10(%rax)
jmp 0x986f
leaq 0x2(%r12), %rbp
movb 0x2(%r12), %al
movl %eax, %ecx
andb $-0x2, %cl
xorl %r14d, %r14d
cmpb $0x30, %cl
jne 0x9bf0
andb $0x1, %al
movzbl %al, %eax
leaq (%rax,%r14,2), %r14
movb 0x1(%rbp), %al
incq %rbp
movl %eax, %ecx
andb $-0x2, %cl
jmp 0x9c2f
leaq 0x1(%r12), %rbp
movl %eax, %ecx
andb $-0x8, %cl
xorl %r14d, %r14d
cmpb $0x30, %cl
jne 0x9bf0
andb $0x7, %al
movzbl %al, %eax
leaq (%rax,%r14,8), %r14
movb 0x1(%rbp), %al
incq %rbp
movl %eax, %ecx
andb $-0x8, %cl
jmp 0x9c57
movq %r12, %rbp
movq (%rsp), %r13
movl $0x1, %edi
movl $0x38, %esi
callq 0x2100
movl $0x1e, (%rax)
movq %rbp, 0x18(%rax)
movl $0x0, 0x20(%rax)
movq %rax, 0x8(%r13)
movq 0x18(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq