// File: /workspace/github_c_makefile_star2_O3/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 0x23ec
leaq -0xf10(%rip), %rsi      # 0x24fc
movabsq $0x200000000, %rdi      # imm = 0x200000000
callq 0x2507
movl $0x100, %edi            # imm = 0x100
callq 0x2740
movq %rax, %r13
movl $0x11379e0, %edi        # imm = 0x11379E0
callq 0x2740
movq %rax, %r15
movl $0x18, %edi
xorl %eax, %eax
callq 0x1459
movq %rax, %r12
movl $0x11379e0, %edi        # imm = 0x11379E0
callq 0x2740
movq %rax, 0x18(%rsp)
movq 0x3cd6(%rip), %rdi      # 0x7130
leaq 0xf53(%rip), %rsi       # 0x43b4
callq 0x2478
movq %rax, 0x28(%rsp)
movq 0x3cae(%rip), %rdi      # 0x7120
leaq 0xf3d(%rip), %rsi       # 0x43b6
callq 0x2478
movq %rax, %rbp
leaq 0xf69(%rip), %rdi       # 0x43f1
callq 0x1050
movq %rbp, %rdi
movq %r13, %rsi
callq 0x24a3
xorpd %xmm0, %xmm0
testl %eax, %eax
je 0x3567
movsd %xmm0, 0x8(%rsp)
leaq 0xf07(%rip), %rbx       # 0x43b8
xorl %r14d, %r14d
incl %eax
movl %eax, %edi
callq 0x2740
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 0x23fb
subsd 0x8(%rsp), %xmm0
ucomisd 0xe42(%rip), %xmm0      # 0x4338
jbe 0x354f
xorl %eax, %eax
callq 0x23fb
leal 0x1(%r14), %esi
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
movapd %xmm0, %xmm3
movapd %xmm0, %xmm2
movsd 0xe26(%rip), %xmm4      # 0x4340
mulsd %xmm4, %xmm2
divsd %xmm1, %xmm2
movapd %xmm1, %xmm0
divsd %xmm4, %xmm0
mulsd 0xbde(%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 0x24a3
incq %r14
testl %eax, %eax
jne 0x34b4
jmp 0x356d
movsd %xmm0, 0x8(%rsp)
movq %r15, 0x20(%rsp)
movq %rbp, %rdi
callq 0x1090
leaq 0xe7a(%rip), %rdi       # 0x43fb
callq 0x1050
movq %r12, %rdi
callq 0x15f3
leaq 0xe78(%rip), %rdi       # 0x440d
callq 0x1050
xorl %eax, %eax
callq 0x2418
movq 0x3b80(%rip), %rdi      # 0x7128
leaq 0xe07(%rip), %rsi       # 0x43b6
callq 0x2478
movq %rax, %r13
movl $0x100000, %edi         # imm = 0x100000
callq 0x2740
movq %rax, 0x10(%rsp)
movq %r13, %rdi
callq 0x1120
testl %eax, %eax
jne 0x36bc
movq 0x10(%rsp), %rax
incq %rax
movq %rax, 0x30(%rsp)
leaq 0xdce(%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 0x36a7
movq 0x30(%rsp), %r13
movsbl %al, %esi
movq %r12, %rdi
callq 0x16b2
incl %r15d
xorl %eax, %eax
callq 0x23fb
subsd 0x8(%rsp), %xmm0
ucomisd 0xcf9(%rip), %xmm0      # 0x4338
jbe 0x3698
xorl %eax, %eax
callq 0x23fb
xorps %xmm1, %xmm1
cvtsi2sd %r15d, %xmm1
movapd %xmm0, %xmm3
movapd %xmm0, %xmm2
movsd 0xce8(%rip), %xmm4      # 0x4348
mulsd %xmm4, %xmm2
divsd %xmm1, %xmm2
movapd %xmm1, %xmm0
divsd %xmm4, %xmm0
mulsd 0xa98(%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 0x361c
movq 0x38(%rsp), %r13
movq %r13, %rdi
callq 0x1120
testl %eax, %eax
je 0x35f7
movl $0xa, %edi
callq 0x1030
movq %r13, %rdi
callq 0x1090
leaq 0x3a9b(%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      # 0x3394
leaq -0x342(%rip), %r8       # 0x33bf
xorl %edi, %edi
callq 0x311c
movq %rbx, (%r12)
cmpl $0x1, %r14d
movq 0x28(%rsp), %r13
jb 0x3745
movl %r14d, %r14d
leaq 0xcb7(%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 0x3724
xorl %eax, %eax
callq 0x26ec
movq %rax, %xmm1
punpckldq 0xc07(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xc0f(%rip), %xmm1      # 0x4370
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
mulsd 0xbdb(%rip), %xmm0      # 0x4350
leaq 0x39f4(%rip), %rax      # 0x7170
movq (%rax), %rax
shrq $0x3, %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
mulq %rcx
shrq $0x4, %rdx
leaq 0xc44(%rip), %rsi       # 0x43df
movq %r13, %rdi
movb $0x1, %al
callq 0x1110
xorl %eax, %eax
callq 0x23fb
leaq 0xc38(%rip), %rdi       # 0x43eb
movb $0x1, %al
callq 0x10b0
xorl %eax, %eax
callq 0x261e
movq %r13, %rdi
callq 0x1090
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq