// File: /workspace/github_c_makefile_star2_O1/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 $0x58, %rsp
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x2424
leaq -0xf28(%rip), %rsi      # 0x2548
movabsq $0x200000000, %rdi      # imm = 0x200000000
callq 0x2553
movl $0x100, %edi            # imm = 0x100
callq 0x2788
movq %rax, %r13
movl $0x11379e0, %edi        # imm = 0x11379E0
callq 0x2788
movq %rax, %r15
movl $0x18, %edi
xorl %eax, %eax
callq 0x1459
movq %rax, %r12
movl $0x11379e0, %edi        # imm = 0x11379E0
callq 0x2788
movq %rax, 0x18(%rsp)
movq 0x3c72(%rip), %rdi      # 0x7130
leaq 0xeef(%rip), %rsi       # 0x43b4
callq 0x24c0
movq %rax, 0x28(%rsp)
movq 0x3c4a(%rip), %rdi      # 0x7120
leaq 0xed9(%rip), %rsi       # 0x43b6
callq 0x24c0
movq %rax, %rbp
leaq 0xf05(%rip), %rdi       # 0x43f1
callq 0x1050
movq %rbp, %rdi
movq %r13, %rsi
callq 0x24eb
xorpd %xmm0, %xmm0
testl %eax, %eax
je 0x35cb
movsd %xmm0, 0x8(%rsp)
leaq 0xea3(%rip), %rbx       # 0x43b8
xorl %r14d, %r14d
incl %eax
movl %eax, %edi
callq 0x2788
movq %rax, (%r15,%r14,8)
movq %rax, %rdi
movq %r13, %rsi
callq 0x1040
movq (%r15,%r14,8), %rsi
movq %r12, %rdi
callq 0x150b
movq 0x18(%rsp), %rcx
movq %rax, (%rcx,%r14,8)
xorl %eax, %eax
callq 0x2433
subsd 0x8(%rsp), %xmm0
ucomisd 0xdde(%rip), %xmm0      # 0x4338
jbe 0x35b3
xorl %eax, %eax
callq 0x2433
leal 0x1(%r14), %esi
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
movapd %xmm0, %xmm3
movapd %xmm0, %xmm2
movsd 0xdc2(%rip), %xmm4      # 0x4340
mulsd %xmm4, %xmm2
divsd %xmm1, %xmm2
movapd %xmm1, %xmm0
divsd %xmm4, %xmm0
mulsd 0xb7a(%rip), %xmm0      # 0x4110
subsd %xmm3, %xmm2
movq %rbx, %rdi
movl $0x226f3c, %edx         # imm = 0x226F3C
movsd %xmm3, 0x8(%rsp)
movapd %xmm3, %xmm1
movb $0x3, %al
callq 0x10b0
movq %rbp, %rdi
movq %r13, %rsi
callq 0x24eb
incq %r14
testl %eax, %eax
jne 0x3518
jmp 0x35d1
movsd %xmm0, 0x8(%rsp)
movq %r15, 0x20(%rsp)
movq %rbp, %rdi
callq 0x1090
leaq 0xe16(%rip), %rdi       # 0x43fb
callq 0x1050
movq %r12, %rdi
callq 0x15e4
leaq 0xe14(%rip), %rdi       # 0x440d
callq 0x1050
xorl %eax, %eax
callq 0x2450
movq 0x3b1c(%rip), %rdi      # 0x7128
leaq 0xda3(%rip), %rsi       # 0x43b6
callq 0x24c0
movq %rax, %r13
movl $0x100000, %edi         # imm = 0x100000
callq 0x2788
movq %rax, 0x10(%rsp)
movq %r13, %rdi
callq 0x1120
testl %eax, %eax
jne 0x3720
movq 0x10(%rsp), %rax
incq %rax
movq %rax, 0x30(%rsp)
leaq 0xd6a(%rip), %rbx       # 0x43b8
xorl %r15d, %r15d
movq %r13, 0x38(%rsp)
movq 0x10(%rsp), %rbp
movl $0x100000, %esi         # imm = 0x100000
movl $0x1, %edx
movq %rbp, %rdi
movq %r13, %rcx
callq 0x1060
movb (%rbp), %al
testb %al, %al
je 0x370b
movq 0x30(%rsp), %r13
movsbl %al, %esi
movq %r12, %rdi
callq 0x16b9
incl %r15d
xorl %eax, %eax
callq 0x2433
subsd 0x8(%rsp), %xmm0
ucomisd 0xc95(%rip), %xmm0      # 0x4338
jbe 0x36fc
xorl %eax, %eax
callq 0x2433
xorps %xmm1, %xmm1
cvtsi2sd %r15d, %xmm1
movapd %xmm0, %xmm3
movapd %xmm0, %xmm2
movsd 0xc84(%rip), %xmm4      # 0x4348
mulsd %xmm4, %xmm2
divsd %xmm1, %xmm2
movapd %xmm1, %xmm0
divsd %xmm4, %xmm0
mulsd 0xa34(%rip), %xmm0      # 0x4110
subsd %xmm3, %xmm2
movq %rbx, %rdi
movl %r15d, %esi
movl $0x36d5393c, %edx       # imm = 0x36D5393C
movsd %xmm3, 0x8(%rsp)
movapd %xmm3, %xmm1
movb $0x3, %al
callq 0x10b0
movb (%r13), %al
incq %r13
testb %al, %al
jne 0x3680
movq 0x38(%rsp), %r13
movq %r13, %rdi
callq 0x1120
testl %eax, %eax
je 0x365b
movl $0xa, %edi
callq 0x1030
movq %r13, %rdi
callq 0x1090
leaq 0x3a37(%rip), %r12      # 0x7170
movq (%r12), %rbx
leaq 0x40(%rsp), %rdx
movq 0x20(%rsp), %rbp
movq %rbp, (%rdx)
movq 0x18(%rsp), %r15
movq %r15, 0x8(%rdx)
leal -0x1(%r14), %esi
leaq -0x366(%rip), %rcx      # 0x33f8
leaq -0x342(%rip), %r8       # 0x3423
xorl %edi, %edi
callq 0x3150
movq %rbx, (%r12)
cmpl $0x1, %r14d
movq 0x28(%rsp), %r13
jb 0x37a9
movl %r14d, %r14d
leaq 0xc53(%rip), %rbx       # 0x43d8
xorl %r12d, %r12d
movq (%rbp,%r12,8), %rdx
movq (%r15,%r12,8), %rax
movl 0x1c(%rax), %ecx
movq %r13, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1110
incq %r12
cmpq %r12, %r14
jne 0x3788
xorl %eax, %eax
callq 0x2734
movq %rax, %xmm1
punpckldq 0xba3(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xbab(%rip), %xmm1      # 0x4370
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
mulsd 0xb77(%rip), %xmm0      # 0x4350
leaq 0x3990(%rip), %rax      # 0x7170
movq (%rax), %rax
shrq $0x3, %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
mulq %rcx
shrq $0x4, %rdx
leaq 0xbe0(%rip), %rsi       # 0x43df
movq %r13, %rdi
movb $0x1, %al
callq 0x1110
xorl %eax, %eax
callq 0x2433
leaq 0xbd4(%rip), %rdi       # 0x43eb
movb $0x1, %al
callq 0x10b0
xorl %eax, %eax
callq 0x2667
movq %r13, %rdi
callq 0x1090
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq