// File: /workspace/github_c_makefile_star2_O2/OhYee[P]computer_application_programming_experiment/pattern_match/ac_auto/ac_auto.c
// Function: main
int main() {
    clock_start();

    mp_init(memory_length, mp_exit);
    char *temp = mp_new(max_string_length * sizeof(char));

    int            pattern_number = 0;
    int            l;
    char **        patterns = mp_new(max_pattern_number * sizeof(char *));
    ac_automaton * ac = ac_init(sizeof(ac_automaton));
    ac_tree_node **nodes = mp_new(max_pattern_number * sizeof(ac_tree_node *));

    FILE *output = open_file(output_filename, "w");
    FILE *f = open_file(patterns_filename, "r");

    double cost_time = 0;

    printf("patterns:\n");
    while ((l = read_file(f, temp)) != 0) {
        patterns[pattern_number] = mp_new(l + 1);
        strcpy(patterns[pattern_number], temp);
        nodes[pattern_number] = ac_add(ac, patterns[pattern_number]);
        ++pattern_number;

        if (clock_duration() - cost_time > 0.01) {
            cost_time = clock_duration();
            printf("%d/%d %.2f%% %.2fs, %.2fs left\r", pattern_number,
                   max_pattern_number,
                   (double)pattern_number / max_pattern_number * 100, cost_time,
                   cost_time * max_pattern_number / pattern_number - cost_time);
        }
    }
    fclose(f);

    printf("\npatterns read ok\n");

    ac_build(ac);
    printf("build ok\n");

    compare_init();

    int file_pos = 0;
    f = open_file(string_filename, "r");

    int   buf_size = 1 << 20;
    char *buf = mp_new(buf_size);

    while (!feof(f)) {
        fread(buf, buf_size, 1, f);
        char *c = buf;
        while (*c != '\0') {
            ac_match_char(ac, *c);
            ++c;

            ++file_pos;
            if (clock_duration() - cost_time > 0.01) {
                cost_time = clock_duration();
                printf("%d/%d %.2f%% %.2fs, %.2fs left\r", file_pos,
                       string_file_byte,
                       (double)file_pos / string_file_byte * 100, cost_time,
                       cost_time * string_file_byte / file_pos - cost_time);
            }
        }
    }
    printf("\n");

    fclose(f);

    uint64_t compare_number_backup = compare_number;
    void *args[] = {patterns, nodes};
    sort(0, pattern_number - 1, args, compare, swap);
    compare_number = compare_number_backup;

    for (int i = 0; i < pattern_number; ++i) {
        fprintf(output, "%s\t%d\n", patterns[i], nodes[i]->match_number);
    }

    fprintf(output,
            "%10.2f "
            "%" PRIu64 "\n",
            (double)mp_get_length() / 1024, compare_number / 1000);
    printf("%f s\n", clock_duration());
    mp_info();
    fclose(output);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
xorl %eax, %eax
callq 0x202c
leaq -0xef9(%rip), %rsi      # 0x2138
movabsq $0x200000000, %rdi      # imm = 0x200000000
callq 0x2141
movl $0x100, %edi            # imm = 0x100
callq 0x232d
movq %rax, %r13
movl $0x11379e0, %edi        # imm = 0x11379E0
callq 0x232d
movq %rax, 0x10(%rsp)
pushq $0x18
popq %rdi
xorl %eax, %eax
callq 0x1329
movq %rax, %r12
movl $0x11379e0, %edi        # imm = 0x11379E0
callq 0x232d
movq %rax, 0x8(%rsp)
movq 0x40b1(%rip), %rdi      # 0x7130
leaq 0x130e(%rip), %rsi      # 0x4394
callq 0x20b8
movq %rax, 0x18(%rsp)
movq 0x4089(%rip), %rdi      # 0x7120
leaq 0x12f8(%rip), %rsi      # 0x4396
callq 0x20b8
movq %rax, %rbp
leaq 0x1324(%rip), %rdi      # 0x43d1
callq 0x1050
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
leaq 0x12d6(%rip), %rbx      # 0x4398
xorl %r15d, %r15d
movq 0x10(%rsp), %r14
movq %rbp, %rdi
movq %r13, %rsi
callq 0x20e1
testl %eax, %eax
je 0x317b
incl %eax
movl %eax, %edi
callq 0x232d
movq %rax, (%r14,%r15,8)
movq %rax, %rdi
movq %r13, %rsi
callq 0x1040
movq (%r14,%r15,8), %rsi
movq %r12, %rdi
callq 0x138c
movq 0x8(%rsp), %rcx
movq %rax, (%rcx,%r15,8)
xorl %eax, %eax
callq 0x203b
subsd (%rsp), %xmm0
ucomisd 0x1202(%rip), %xmm0     # 0x4320
jbe 0x3173
xorl %eax, %eax
callq 0x203b
leal 0x1(%r15), %esi
cvtsi2sd %esi, %xmm1
movapd %xmm0, %xmm3
movapd %xmm0, %xmm2
movsd 0x11e9(%rip), %xmm4     # 0x4328
mulsd %xmm4, %xmm2
divsd %xmm1, %xmm2
movapd %xmm1, %xmm0
divsd %xmm4, %xmm0
mulsd 0xfa1(%rip), %xmm0      # 0x40f8
subsd %xmm3, %xmm2
movq %rbx, %rdi
movl $0x226f3c, %edx         # imm = 0x226F3C
movsd %xmm3, (%rsp)
movapd %xmm3, %xmm1
movb $0x3, %al
callq 0x10b0
incq %r15
jmp 0x30ca
movq %rbp, %rdi
callq 0x1090
leaq 0x1251(%rip), %rdi      # 0x43db
callq 0x1050
movq %r12, %rdi
callq 0x1412
leaq 0x124f(%rip), %rdi      # 0x43ed
callq 0x1050
xorl %eax, %eax
callq 0x2058
movq 0x3f77(%rip), %rdi      # 0x7128
leaq 0x11de(%rip), %rsi      # 0x4396
callq 0x20b8
movq %rax, %r13
movl $0x100000, %edi         # imm = 0x100000
callq 0x232d
movq %rax, %r14
xorl %ebx, %ebx
movq %r13, %rdi
callq 0x1120
testl %eax, %eax
jne 0x327c
movl $0x100000, %esi         # imm = 0x100000
movq %r14, %rdi
pushq $0x1
popq %rdx
movq %r13, %rcx
callq 0x1060
movq %r14, %rbp
movb (%rbp), %al
testb %al, %al
je 0x31cf
movsbl %al, %esi
movq %r12, %rdi
callq 0x14c6
incq %rbp
incl %ebx
xorl %eax, %eax
callq 0x203b
subsd (%rsp), %xmm0
ucomisd 0x1100(%rip), %xmm0     # 0x4320
jbe 0x31f5
xorl %eax, %eax
callq 0x203b
cvtsi2sd %ebx, %xmm1
movapd %xmm0, %xmm3
movapd %xmm0, %xmm2
movsd 0x10fb(%rip), %xmm4     # 0x4338
mulsd %xmm4, %xmm2
divsd %xmm1, %xmm2
movapd %xmm1, %xmm0
divsd %xmm4, %xmm0
mulsd 0xea3(%rip), %xmm0      # 0x40f8
subsd %xmm3, %xmm2
leaq 0x1138(%rip), %rdi      # 0x4398
movl %ebx, %esi
movl $0x36d5393c, %edx       # imm = 0x36D5393C
movsd %xmm3, (%rsp)
movapd %xmm3, %xmm1
movb $0x3, %al
callq 0x10b0
jmp 0x31f5
pushq $0xa
popq %rdi
callq 0x1030
movq %r13, %rdi
callq 0x1090
leaq 0x3edd(%rip), %r14      # 0x7170
movq (%r14), %rbx
leaq 0x20(%rsp), %rdx
movq 0x10(%rsp), %rax
movq %rax, (%rdx)
movq 0x8(%rsp), %rax
movq %rax, 0x8(%rdx)
leal -0x1(%r15), %esi
leaq -0x2fb(%rip), %rcx      # 0x2fbc
leaq -0x2d7(%rip), %r8       # 0x2fe7
xorl %edi, %edi
callq 0x2d1c
movq %rbx, (%r14)
leaq 0x10e9(%rip), %rbx      # 0x43b8
xorl %r12d, %r12d
movq 0x18(%rsp), %r13
cmpq %r12, %r15
je 0x3303
movq 0x10(%rsp), %rax
movq (%rax,%r12,8), %rdx
movq 0x8(%rsp), %rax
movq (%rax,%r12,8), %rax
movl 0x1c(%rax), %ecx
movq %r13, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1110
incq %r12
jmp 0x32d7
xorl %eax, %eax
callq 0x22dd
movq %rax, %xmm1
punpckldq 0x1029(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x1031(%rip), %xmm1     # 0x4350
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
mulsd 0xffd(%rip), %xmm0      # 0x4330
movq (%r14), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
xorl %edx, %edx
divq %rcx
leaq 0x1078(%rip), %rsi      # 0x43bf
movq %r13, %rdi
movq %rax, %rdx
movb $0x1, %al
callq 0x1110
xorl %eax, %eax
callq 0x203b
leaq 0x1069(%rip), %rdi      # 0x43cb
movb $0x1, %al
callq 0x10b0
xorl %eax, %eax
callq 0x2251
movq %r13, %rdi
callq 0x1090
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq