// File: /workspace/github_c_makefile_star2_O2/hikalium[P]compilium/preprocessor.c
// Function: PreprocessBlock
static void PreprocessBlock(struct Node *replacement_list, int level) {
  struct Node *t;
  while (PeekToken()) {
    if ((t = ConsumeTokenStr("__LINE__"))) {
      char s[32];
      snprintf(s, sizeof(s), "%d", t->line);
      t->token_type = kTokenIntegerConstant;
      t->begin = t->src_str = strdup(s);
      t->length = strlen(t->begin);
      continue;
    }
    if ((t = ReadToken(kTokenLineComment))) {
      while (t && !IsEqualTokenWithCStr(t, "\n")) t = t->next_token;
      RemoveTokensTo(t);
      continue;
    }
    if ((t = ReadToken(kTokenBlockCommentBegin))) {
      while (t && !IsTokenWithType(t, kTokenBlockCommentEnd)) t = t->next_token;
      if (IsTokenWithType(t, kTokenBlockCommentEnd)) t = t->next_token;
      RemoveTokensTo(t);
      continue;
    }
    if (IsEqualTokenWithCStr((t = PeekToken()), "#")) {
      assert(t);
      t = SkipDelimiterTokensInLogicalLine(t->next_token);
      if (IsEqualTokenWithCStr(t, "define")) {
        assert(t);
        t = SkipDelimiterTokensInLogicalLine(t->next_token);
        struct Node *from = t;
        t = t->next_token;
        struct Node *ident_list = TryReadIdentListWrappedByParens(&t);
        t = SkipDelimiterTokensInLogicalLine(t);
        assert(t);
        struct Node *to_token_head = NULL;
        struct Node **to_token_last_holder = &to_token_head;
        while (t && !IsEqualTokenWithCStr(t, "\n")) {
          *to_token_last_holder = DuplicateToken(t);
          to_token_last_holder = &(*to_token_last_holder)->next_token;
          t = t->next_token;
        }
        assert(IsEqualTokenWithCStr(t, "\n"));
        RemoveTokensTo(t->next_token);
        PushKeyValueToList(replacement_list, CreateTokenStr(from),
                           CreateMacroReplacement(ident_list, to_token_head));
        continue;
      }
      if (IsEqualTokenWithCStr(t, "include")) {
        struct Node *token_include = t;
        const char *fname = NULL;
        const char *path = NULL;
        t = SkipDelimiterTokensInLogicalLine(t->next_token);
        if (IsTokenWithType(t, kTokenStringLiteral)) {
          char *tmp_fname = CreateTokenStr(t);
          tmp_fname++;  // Remove open "
          tmp_fname[strlen(tmp_fname) - 1] = 0;  // Remove close "
          fname = tmp_fname;
          RemoveTokensTo(t->next_token);
          path = CreateJoinedString(
              "./", fname);  // TODO: Make this relative to source, not cwd.
        } else if (IsEqualTokenWithCStr(t, "<")) {
          struct Node *markL = t;
          t = t->next_token;
          struct Node *begin = t;
          while (t && !IsEqualTokenWithCStr(t, ">")) {
            t = t->next_token;
          }
          if (!t) {
            ErrorWithToken(markL,
                           "Unexpected EOF. > is expected to match with this.");
          }
          struct Node *end = t;
          fname = CreateStrFromTokenRange(begin, end);
          RemoveTokensTo(end->next_token);
          if (!include_path) {
            ErrorWithToken(token_include,
                           "Include path is not provided in compiler args");
          }
          path = CreateJoinedString(include_path, fname);
        } else {
          ErrorWithToken(t, "Expected < or \" here");
        }
        assert(path);
        fprintf(stderr, "Include from: %s\n", path);
        FILE *fp = fopen(path, "rb");
        if (!fp) {
          ErrorWithToken(token_include, "File not found: %s", path);
        }
        const char *include_input = ReadFile(fp);
        InsertTokens(Tokenize(include_input));
        fclose(fp);
        continue;
      }
      if (IsEqualTokenWithCStr(t, "ifdef")) {
        struct Node *ifdef_token = t;
        t = SkipDelimiterTokensInLogicalLine(t->next_token);
        struct Node *e;
        bool cond = (e = GetNodeByTokenKey(replacement_list, t));
        // defined
        t = SkipDelimiterTokensInLogicalLine(t->next_token);
        RemoveTokensTo(t);
        if (cond) {
          PreprocessBlock(replacement_list, level + 1);
          if (IsEqualTokenWithCStr(PeekToken(), "else")) {
            RemoveCurrentToken();
            PreprocessRemoveBlock();
          }
        } else {
          PreprocessRemoveBlock();
          if (IsEqualTokenWithCStr(PeekToken(), "else")) {
            RemoveCurrentToken();
            PreprocessBlock(replacement_list, level + 1);
          }
        }
        t = PeekToken();
        if (!IsEqualTokenWithCStr(t, "endif")) {
          ErrorWithToken(ifdef_token,
                         "Unexpected eof. Expected #endif to match with this.");
        }
        t = SkipDelimiterTokensInLogicalLine(t->next_token);
        RemoveTokensTo(t);
        continue;
      }
      if (IsEqualTokenWithCStr(t, "endif")) {
        if (level == 0) {
          ErrorWithToken(t, "Unexpected endif here");
        }
        RemoveTokensTo(t);
        return;
      }
      if (IsEqualTokenWithCStr(t, "else")) {
        if (level == 0) {
          ErrorWithToken(t, "Unexpected else here");
        }
        RemoveTokensTo(t);
        return;
      }
      ErrorWithToken(NextToken(), "Not a valid macro");
    }
    struct Node *e;
    if ((e = GetNodeByTokenKey(replacement_list, (t = PeekToken())))) {
      assert(e->type == kNodeMacroReplacement);
      struct Node *rep = DuplicateTokenSequence(e->value);
      RemoveCurrentToken();
      if (!e->arg_expr_list) {
        // ident replace macro case
        InsertTokens(rep);
        continue;
      }
      // function-like macro case
      t = SkipDelimiterTokensInLogicalLine(t->next_token);
      if (!IsEqualTokenWithCStr(t, "(")) ErrorWithToken(t, "Expected ( here");
      t = t->next_token;
      struct Node *it;
      struct Node *arg_rep_list = AllocList();
      for (it = e->arg_expr_list; it; it = it->next_token) {
        if (IsEqualTokenWithCStr(it, ")")) break;
        struct Node *arg_token_head = NULL;
        struct Node **arg_token_last_holder = &arg_token_head;
        t = SkipDelimiterTokensInLogicalLine(t);
        for (; t; t = t->next_token) {
          if (IsEqualTokenWithCStr(t, ")") || IsEqualTokenWithCStr(t, ","))
            break;
          *arg_token_last_holder = DuplicateToken(t);
          arg_token_last_holder = &(*arg_token_last_holder)->next_token;
        }
        PushKeyValueToList(arg_rep_list, CreateTokenStr(it),
                           CreateMacroReplacement(NULL, arg_token_head));
        if (IsEqualTokenWithCStr(t, ")")) break;
        t = t->next_token;
      }
      if (!IsEqualTokenWithCStr(t, ")")) ErrorWithToken(t, "Expected ) here");
      RemoveTokensTo(t->next_token);
      // Insert & replace args
      InsertTokensWithIdentReplace(rep, arg_rep_list);
      continue;
    }
    NextToken();
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %r13
movq %rsi, 0x28(%rsp)
leal 0x1(%rsi), %eax
movl %eax, 0x24(%rsp)
leaq 0x4d85(%rip), %rbp      # 0xc2fe
leaq 0x30(%rsp), %rbx
leaq 0x3d2e(%rip), %r14      # 0xb2b3
pushq $0x1c
popq %r12
movq %rdi, 0x18(%rsp)
callq 0x89d5
testq %rax, %rax
je 0x7cbd
movq %rbp, %rdi
callq 0x8a4e
testq %rax, %rax
je 0x75f8
movq %rax, %r15
movl 0x130(%rax), %ecx
movq %rbx, %rdi
pushq $0x20
popq %rsi
leaq 0x4012(%rip), %rdx      # 0xb5d1
xorl %eax, %eax
callq 0x20a0
movl $0x3, 0x108(%r15)
movq %rbx, %rdi
callq 0x21c0
movq %rax, 0x128(%r15)
movq %rax, 0x118(%r15)
movq %rax, %rdi
callq 0x2080
movl %eax, 0x120(%r15)
jmp 0x758e
pushq $0x1a
popq %rdi
callq 0x89fc
testq %rax, %rax
je 0x7625
movq %rax, %r15
testq %r15, %r15
je 0x7668
movq %r15, %rdi
movq %r14, %rsi
callq 0x87de
testl %eax, %eax
jne 0x7668
movq 0x110(%r15), %r15
jmp 0x7608
pushq $0x1b
popq %rdi
callq 0x89fc
testq %rax, %rax
je 0x7675
movq %rax, %r15
testq %r15, %r15
je 0x7652
movq %r15, %rdi
movl %r12d, %esi
callq 0x86b7
testb %al, %al
jne 0x7652
movq 0x110(%r15), %r15
jmp 0x7635
movq %r15, %rdi
movl %r12d, %esi
callq 0x86b7
testb %al, %al
je 0x7668
movq 0x110(%r15), %r15
movq %r15, %rdi
callq 0x8ba6
jmp 0x758e
callq 0x89d5
movq %rax, %r15
movq %rax, %rdi
leaq 0x4c80(%rip), %rsi      # 0xc307
callq 0x87de
testl %eax, %eax
je 0x777b
testq %r15, %r15
je 0x7d38
movq 0x110(%r15), %rdi
callq 0x7e22
movq %rax, %r15
movq %rax, %rdi
leaq 0x4c62(%rip), %rsi      # 0xc318
callq 0x87de
testl %eax, %eax
je 0x78e0
testq %r15, %r15
je 0x7d71
movq 0x110(%r15), %rdi
callq 0x7e22
movq %rax, 0x8(%rsp)
movq 0x110(%rax), %r15
movq %r15, %rdi
leaq 0x3ed6(%rip), %rsi      # 0xb5c4
callq 0x87de
testl %eax, %eax
je 0x798a
andq $0x0, 0x30(%rsp)
movq %r15, 0x10(%rsp)
movq %r15, %r13
movq %rbx, %rbp
movq 0x110(%r13), %rdi
callq 0x7e22
testq %rax, %rax
je 0x79b2
movq %rax, %r13
movq %rax, %rdi
leaq 0x5147(%rip), %rsi      # 0xc875
callq 0x87de
testl %eax, %eax
jne 0x79b5
movq %r13, %rdi
callq 0x8716
movq %rax, %r15
movq %rax, (%rbp)
addq $0x110, %r15            # imm = 0x110
movq 0x110(%r13), %rdi
callq 0x7e22
movq %rax, %r13
movq %rax, %rdi
leaq 0x3ab0(%rip), %rsi      # 0xb21a
callq 0x87de
movq %r15, %rbp
testl %eax, %eax
jne 0x770c
jmp 0x79b8
callq 0x89d5
movq %rax, %r15
movq %r13, %rdi
movq %rax, %rsi
callq 0x4035
testq %rax, %rax
je 0x798f
movq %rax, %r13
cmpl $0x3, (%rax)
jne 0x7d83
movq 0xa0(%r13), %rdi
callq 0x876b
movq %rax, 0x10(%rsp)
callq 0x8b8c
cmpq $0x0, 0xb8(%r13)
je 0x799e
movq 0x110(%r15), %rdi
callq 0x7e22
movq %rax, %r15
movq %rax, %rdi
leaq 0x3de4(%rip), %rsi      # 0xb5c4
callq 0x87de
testl %eax, %eax
je 0x7dad
addq $0xb8, %r13
movq 0x110(%r15), %r15
xorl %eax, %eax
callq 0x3ea7
movq %rax, 0x8(%rsp)
movq (%r13), %r13
testq %r13, %r13
je 0x7b12
movq %r13, %rdi
leaq 0x5057(%rip), %rsi      # 0xc875
callq 0x87de
testl %eax, %eax
jne 0x7b12
andq $0x0, 0x30(%rsp)
movq %r15, %rdi
callq 0x7e22
movq %rax, %r15
movq %rbx, %rbp
testq %r15, %r15
je 0x7888
movq %r15, %rdi
leaq 0x5027(%rip), %rsi      # 0xc875
callq 0x87de
testl %eax, %eax
jne 0x7888
movq %r15, %rdi
leaq 0x39b9(%rip), %rsi      # 0xb21a
callq 0x87de
testl %eax, %eax
jne 0x7888
movq %r15, %rdi
callq 0x8716
movq %rax, (%rbp)
addq $0x110, %rax            # imm = 0x110
movq 0x110(%r15), %r15
movq %rax, %rbp
jmp 0x783f
movq %r13, %rdi
callq 0x87a7
movq %rax, %rbp
movq 0x30(%rsp), %rsi
xorl %edi, %edi
callq 0x3570
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x3f58
movq %r15, %rdi
leaq 0x4fbc(%rip), %rsi      # 0xc875
callq 0x87de
testl %eax, %eax
jne 0x7b0b
movq 0x110(%r15), %r15
addq $0x110, %r13            # imm = 0x110
leaq 0x4a23(%rip), %rbp      # 0xc2fe
jmp 0x7807
movq %r15, %rdi
leaq 0x4a53(%rip), %rsi      # 0xc33d
callq 0x87de
testl %eax, %eax
movq %r15, 0x8(%rsp)
jne 0x7b4e
movq %r15, %rdi
leaq 0x4adf(%rip), %rsi      # 0xc3e5
callq 0x87de
testl %eax, %eax
je 0x7ccc
movq 0x110(%r15), %rdi
callq 0x7e22
movq %rax, %r15
movq %r13, %rdi
movq %rax, %rsi
callq 0x4035
movq %r13, %rbp
movq %rax, %r13
movq 0x110(%r15), %rdi
callq 0x7e22
movq %rax, %rdi
callq 0x8ba6
testq %r13, %r13
je 0x7a9b
movq %rbp, %rdi
movl 0x24(%rsp), %esi
callq 0x7555
callq 0x89d5
movq %rax, %rdi
leaq 0x4a80(%rip), %rsi      # 0xc3eb
callq 0x87de
testl %eax, %eax
movq %rbp, %r13
je 0x7acc
callq 0x8b8c
callq 0x7edd
jmp 0x7acc
xorl %r13d, %r13d
jmp 0x79f4
callq 0x8b6e
movq 0x18(%rsp), %r13
jmp 0x758e
movq 0x10(%rsp), %rdi
callq 0x8bc7
movq 0x18(%rsp), %r13
jmp 0x758e
xorl %r13d, %r13d
movq %rbp, %r15
movq %r13, %rdi
leaq 0x4eb3(%rip), %rsi      # 0xc875
callq 0x87de
testl %eax, %eax
je 0x79ec
movq %r13, %rdi
callq 0x8716
movq %rax, (%r15)
movq 0x110(%r13), %rdi
callq 0x7e22
movq %rax, %r15
movq 0x30(%rsp), %r13
jmp 0x79f4
xorl %r13d, %r13d
movq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x7e22
testq %rax, %rax
je 0x7d5f
movq %rax, %r15
andq $0x0, 0x30(%rsp)
movq %rbx, %rbp
testq %r15, %r15
je 0x7a43
movq %r15, %rdi
movq %r14, %rsi
callq 0x87de
testl %eax, %eax
jne 0x7a43
movq %r15, %rdi
callq 0x8716
movq %rax, (%rbp)
addq $0x110, %rax            # imm = 0x110
movq 0x110(%r15), %r15
movq %rax, %rbp
jmp 0x7a11
movq %r15, %rdi
movq %r14, %rsi
callq 0x87de
testl %eax, %eax
je 0x7d4d
movq 0x110(%r15), %rdi
callq 0x8ba6
movq 0x8(%rsp), %rdi
callq 0x87a7
movq %rax, %r15
movq 0x30(%rsp), %rsi
movq %r13, %rdi
callq 0x3570
movq 0x18(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x3f58
leaq 0x4868(%rip), %rbp      # 0xc2fe
jmp 0x758e
callq 0x7edd
callq 0x89d5
movq %rax, %rdi
leaq 0x493c(%rip), %rsi      # 0xc3eb
callq 0x87de
testl %eax, %eax
movq %rbp, %r13
je 0x7acc
callq 0x8b8c
movq %r13, %rdi
movl 0x24(%rsp), %esi
callq 0x7555
callq 0x89d5
movq %rax, %r15
movq %rax, %rdi
leaq 0x4912(%rip), %rsi      # 0xc3f0
callq 0x87de
testl %eax, %eax
leaq 0x4812(%rip), %rbp      # 0xc2fe
je 0x7d9b
movq 0x110(%r15), %rdi
callq 0x7e22
movq %rax, %rdi
callq 0x8ba6
jmp 0x758e
leaq 0x47ec(%rip), %rbp      # 0xc2fe
movq %r15, %rdi
leaq 0x4d59(%rip), %rsi      # 0xc875
callq 0x87de
testl %eax, %eax
movq 0x18(%rsp), %r13
je 0x7da4
movq 0x110(%r15), %rdi
callq 0x8ba6
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x8c08
jmp 0x758e
movq 0x110(%r15), %rdi
callq 0x7e22
movq %rax, %r15
movq %rax, %rdi
pushq $0x18
popq %rsi
callq 0x86b7
movq %r15, %rdi
testb %al, %al
je 0x7bb0
callq 0x87a7
movq %rax, %r13
leaq 0x1(%rax), %rdi
movq %rdi, 0x10(%rsp)
callq 0x2080
movb $0x0, (%r13,%rax)
movq 0x110(%r15), %rdi
callq 0x8ba6
leaq 0x47a7(%rip), %rdi      # 0xc345
movq 0x10(%rsp), %rsi
callq 0x7e61
movq %rax, %r15
jmp 0x7c5d
leaq 0x41a5(%rip), %rsi      # 0xbd5c
callq 0x87de
testl %eax, %eax
je 0x7de0
movq 0x110(%r15), %rbp
movq %rbp, %r13
testq %r13, %r13
je 0x7d27
movq %r13, %rdi
leaq 0x34ad(%rip), %rsi      # 0xb08e
callq 0x87de
testl %eax, %eax
jne 0x7bf3
movq 0x110(%r13), %r13
jmp 0x7bce
testq %rbp, %rbp
je 0x7dca
xorl %eax, %eax
movq %rbp, %rcx
testq %rcx, %rcx
je 0x7c1a
cmpq %r13, %rcx
je 0x7c1a
addl 0x120(%rcx), %eax
movq 0x110(%rcx), %rcx
jmp 0x7c01
movq 0x118(%rbp), %rdi
movslq %eax, %rsi
callq 0x20e0
movq %rax, %r15
movq 0x110(%r13), %rdi
callq 0x8ba6
leaq 0x9721(%rip), %rax      # 0x11360
movq (%rax), %rdi
testq %rdi, %rdi
je 0x7db9
movq %r15, %rsi
callq 0x7e61
movq %rax, %r15
leaq 0x46a1(%rip), %rbp      # 0xc2fe
movq 0x9394(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
leaq 0x474f(%rip), %rsi      # 0xc3bd
movq %r15, %rdx
xorl %eax, %eax
callq 0x2110
movq %r15, %rdi
leaq 0x474d(%rip), %rsi      # 0xc3cf
callq 0x2170
testq %rax, %rax
je 0x7dec
movq %rax, %r13
movq %rax, %rdi
callq 0x43f2
movq %rax, %rdi
callq 0x93d0
movq %rax, %rdi
callq 0x8bc7
movq %r13, %rdi
callq 0x2070
movq 0x18(%rsp), %r13
jmp 0x758e
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x471d(%rip), %rsi      # 0xc3f0
movq %r15, %rdi
callq 0x87de
testl %eax, %eax
je 0x7cef
cmpl $0x0, 0x28(%rsp)
jne 0x7d11
leaq 0x473d(%rip), %rsi      # 0xc42a
jmp 0x7d2e
leaq 0x46f5(%rip), %rsi      # 0xc3eb
movq %r15, %rdi
callq 0x87de
testl %eax, %eax
je 0x7e02
cmpl $0x0, 0x28(%rsp)
je 0x7e16
movq %r15, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8ba6
leaq 0x461a(%rip), %rsi      # 0xc348
movq %r15, %rdi
xorl %eax, %eax
callq 0x3de9
leaq 0x4855(%rip), %rdi      # 0xc594
leaq 0x45c3(%rip), %rsi      # 0xc309
pushq $0x5f
jmp 0x7dda
leaq 0x45cb(%rip), %rdi      # 0xc31f
leaq 0x45ae(%rip), %rsi      # 0xc309
pushq $0x70
jmp 0x7dda
leaq 0x482e(%rip), %rdi      # 0xc594
leaq 0x459c(%rip), %rsi      # 0xc309
pushq $0x68
jmp 0x7dda
leaq 0x481c(%rip), %rdi      # 0xc594
leaq 0x458a(%rip), %rsi      # 0xc309
pushq $0x62
jmp 0x7dda
leaq 0x46dd(%rip), %rdi      # 0xc467
leaq 0x4578(%rip), %rsi      # 0xc309
movl $0xd4, %edx
callq 0x3cc4
leaq 0x4654(%rip), %rsi      # 0xc3f6
jmp 0x7dc0
leaq 0x46ed(%rip), %rsi      # 0xc498
jmp 0x7d2e
leaq 0x46d4(%rip), %rsi      # 0xc488
jmp 0x7d2e
leaq 0x45ba(%rip), %rsi      # 0xc37a
movq 0x8(%rsp), %rdi
jmp 0x7d31
leaq 0x46e0(%rip), %rdi      # 0xc4b1
leaq 0x4531(%rip), %rsi      # 0xc309
pushq $0xc
popq %rdx
callq 0x3cc4
leaq 0x45c1(%rip), %rsi      # 0xc3a8
jmp 0x7d2e
leaq 0x45df(%rip), %rsi      # 0xc3d2
movq 0x8(%rsp), %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x3de9
callq 0x8b6e
leaq 0x4647(%rip), %rsi      # 0xc455
movq %rax, %rdi
jmp 0x7d31
leaq 0x4623(%rip), %rsi      # 0xc440
jmp 0x7d2e