// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movl $0x0, -0x4(%rbp)
movb $0x0, %al
callq 0x2d10
movabsq $0x200000000, %rdi      # imm = 0x200000000
leaq -0x19fa(%rip), %rsi     # 0x2fb0
callq 0x2fc0
movl $0x100, %edi            # imm = 0x100
callq 0x32d0
movq %rax, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movl $0x11379e0, %edi        # imm = 0x11379E0
callq 0x32d0
movq %rax, -0x20(%rbp)
movl $0x18, %edi
movb $0x0, %al
callq 0x1340
movq %rax, -0x28(%rbp)
movl $0x11379e0, %edi        # imm = 0x11379E0
callq 0x32d0
movq %rax, -0x30(%rbp)
movq 0x3731(%rip), %rdi      # 0x8128
leaq 0x996(%rip), %rsi       # 0x5394
callq 0x2e80
movq %rax, -0x38(%rbp)
movq 0x370a(%rip), %rdi      # 0x8118
leaq 0x981(%rip), %rsi       # 0x5396
callq 0x2e80
movq %rax, -0x40(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x48(%rbp)
leaq 0x96b(%rip), %rdi       # 0x5398
movb $0x0, %al
callq 0x10a0
movq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x2ed0
movl %eax, -0x18(%rbp)
cmpl $0x0, %eax
je 0x4b27
movl -0x18(%rbp), %edi
addl $0x1, %edi
callq 0x32d0
movq %rax, %rdx
movq -0x20(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x20(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x10(%rbp), %rsi
callq 0x1030
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x1400
movq %rax, %rdx
movq -0x30(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movb $0x0, %al
callq 0x2d30
subsd -0x48(%rbp), %xmm0
movsd 0x86b(%rip), %xmm1      # 0x5328
ucomisd %xmm1, %xmm0
jbe 0x4b22
movb $0x0, %al
callq 0x2d30
movsd %xmm0, -0x48(%rbp)
movl -0x14(%rbp), %esi
cvtsi2sdl -0x14(%rbp), %xmm0
movsd 0x859(%rip), %xmm1      # 0x5338
divsd %xmm1, %xmm0
movsd 0x60d(%rip), %xmm1      # 0x50f8
mulsd %xmm1, %xmm0
movsd -0x48(%rbp), %xmm1
movsd 0x83c(%rip), %xmm2      # 0x5338
mulsd -0x48(%rbp), %xmm2
cvtsi2sdl -0x14(%rbp), %xmm3
divsd %xmm3, %xmm2
subsd -0x48(%rbp), %xmm2
leaq 0x88d(%rip), %rdi       # 0x53a3
movl $0x226f3c, %edx         # imm = 0x226F3C
movb $0x3, %al
callq 0x10a0
jmp 0x4a34
movq -0x40(%rbp), %rdi
callq 0x1070
leaq 0x88c(%rip), %rdi       # 0x53c3
movb $0x0, %al
callq 0x10a0
movq -0x28(%rbp), %rdi
callq 0x1510
leaq 0x888(%rip), %rdi       # 0x53d6
movb $0x0, %al
callq 0x10a0
movb $0x0, %al
callq 0x2d60
movl $0x0, -0x4c(%rbp)
movq 0x35b6(%rip), %rdi      # 0x8120
leaq 0x825(%rip), %rsi       # 0x5396
callq 0x2e80
movq %rax, -0x40(%rbp)
movl $0x100000, -0x50(%rbp)  # imm = 0x100000
movl -0x50(%rbp), %edi
callq 0x32d0
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rdi
callq 0x1110
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x4ba7
jmp 0x4c7d
movq -0x58(%rbp), %rdi
movslq -0x50(%rbp), %rsi
movq -0x40(%rbp), %rcx
movl $0x1, %edx
callq 0x1040
movq -0x58(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x4c78
movq -0x28(%rbp), %rdi
movq -0x60(%rbp), %rax
movsbl (%rax), %esi
callq 0x1700
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
movb $0x0, %al
callq 0x2d30
subsd -0x48(%rbp), %xmm0
movsd 0x71a(%rip), %xmm1      # 0x5328
ucomisd %xmm1, %xmm0
jbe 0x4c73
movb $0x0, %al
callq 0x2d30
movsd %xmm0, -0x48(%rbp)
movl -0x4c(%rbp), %esi
cvtsi2sdl -0x4c(%rbp), %xmm0
movsd 0x700(%rip), %xmm1      # 0x5330
divsd %xmm1, %xmm0
movsd 0x4bc(%rip), %xmm1      # 0x50f8
mulsd %xmm1, %xmm0
movsd -0x48(%rbp), %xmm1
movsd 0x6e3(%rip), %xmm2      # 0x5330
mulsd -0x48(%rbp), %xmm2
cvtsi2sdl -0x4c(%rbp), %xmm3
divsd %xmm3, %xmm2
subsd -0x48(%rbp), %xmm2
leaq 0x73c(%rip), %rdi       # 0x53a3
movl $0x36d5393c, %edx       # imm = 0x36D5393C
movb $0x3, %al
callq 0x10a0
jmp 0x4bc5
jmp 0x4b8d
leaq 0x413(%rip), %rdi       # 0x5097
movb $0x0, %al
callq 0x10a0
movq -0x40(%rbp), %rdi
callq 0x1070
leaq 0x34cd(%rip), %rax      # 0x8168
movq (%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x78(%rbp)
movl -0x14(%rbp), %esi
subl $0x1, %esi
leaq -0x80(%rbp), %rdx
xorl %edi, %edi
leaq -0x465(%rip), %rcx      # 0x4860
leaq -0x3ec(%rip), %r8       # 0x48e0
callq 0x4400
movq -0x68(%rbp), %rcx
leaq 0x348c(%rip), %rax      # 0x8168
movq %rcx, (%rax)
movl $0x0, -0x84(%rbp)
movl -0x84(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x4d38
movq -0x38(%rbp), %rdi
movq -0x20(%rbp), %rax
movslq -0x84(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x30(%rbp), %rax
movslq -0x84(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl 0x1c(%rax), %ecx
leaq 0x6c0(%rip), %rsi       # 0x53e0
movb $0x0, %al
callq 0x1100
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x4ce9
movq -0x38(%rbp), %rax
movq %rax, -0x90(%rbp)
xorl %eax, %eax
callq 0x3230
movq -0x90(%rbp), %rdi
movq %rax, %xmm0
movaps 0x5e3(%rip), %xmm1      # 0x5340
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x5e7(%rip), %xmm1      # 0x5350
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x5a0(%rip), %xmm1      # 0x5320
divsd %xmm1, %xmm0
leaq 0x33dd(%rip), %rax      # 0x8168
movq (%rax), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
xorl %edx, %edx
divq %rcx
movq %rax, %rdx
leaq 0x645(%rip), %rsi       # 0x53e7
movb $0x1, %al
callq 0x1100
movb $0x0, %al
callq 0x2d30
leaq 0x63c(%rip), %rdi       # 0x53f3
movb $0x1, %al
callq 0x10a0
movb $0x0, %al
callq 0x3120
movq -0x38(%rbp), %rdi
callq 0x1070
xorl %eax, %eax
addq $0x90, %rsp
popq %rbp
retq