// File: /workspace/github_c_makefile_star2_O2/OhYee[P]computer_application_programming_experiment/pattern_match/multikmp/multikmp.c
// Function: main
int main() {
    clock_start();
    double cost_time = 0;

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

    int pattern_number = 0;
    int l;
    patterns = mp_new(max_pattern_number * sizeof(char *));
    next = mp_new(max_pattern_number * sizeof(int *));

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

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

        next[pattern_number] = mp_new(l * sizeof(int));
        kmp_get_next(temp, next[pattern_number]);

        strcpy(patterns[pattern_number++], temp);

        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");

    count = mp_new(pattern_number * sizeof(int));
    pos = mp_new(pattern_number * sizeof(int));
    for (int i = 0; i < pattern_number; ++i) {
        count[i] = pos[i] = 0;
    }

    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') {
            for (int i = 0; i < pattern_number; ++i) {
                match(*c, i);
            }

            ++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);
            }
        }
    }
    fclose(f);

    uint64_t compare_number_backup = compare_number;
    void *   args[] = {patterns, count};
    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], count[i]);
    }

    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 $0x28, %rsp
xorl %eax, %eax
callq 0x202c
leaq -0xf26(%rip), %rsi      # 0x2138
movl $0x164000, %edi         # imm = 0x164000
callq 0x2141
movl $0x100, %edi            # imm = 0x100
callq 0x232d
movq %rax, %r14
movl $0x3200, %edi           # imm = 0x3200
callq 0x232d
movq %rax, 0x4132(%rip)      # 0x71b8
movl $0x3200, %edi           # imm = 0x3200
callq 0x232d
movq %rax, 0x4131(%rip)      # 0x71c8
movq 0x4092(%rip), %rdi      # 0x7130
leaq 0x12f6(%rip), %rsi      # 0x439b
callq 0x20b8
movq %rax, 0x8(%rsp)
movq 0x406a(%rip), %rdi      # 0x7120
leaq 0x12e0(%rip), %rsi      # 0x439d
callq 0x20b8
movq %rax, %r15
leaq 0x130c(%rip), %rdi      # 0x43d8
callq 0x1050
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
leaq 0x12be(%rip), %r12      # 0x439f
xorl %ebp, %ebp
xorl %r13d, %r13d
movq %r15, %rdi
movq %r14, %rsi
callq 0x20e1
testl %eax, %eax
je 0x31bd
movl %eax, %ebx
leal 0x1(%rbx), %edi
callq 0x232d
movq 0x40ae(%rip), %rcx      # 0x71b8
movq %rax, (%rcx,%r13,8)
shll $0x2, %ebx
movl %ebx, %edi
callq 0x232d
movq 0x40a9(%rip), %rcx      # 0x71c8
movq %rax, (%rcx,%r13,8)
movq 0x409e(%rip), %rax      # 0x71c8
movq (%rax,%r13,8), %rsi
movq %r14, %rdi
callq 0x2528
movq 0x407b(%rip), %rax      # 0x71b8
movq (%rax,%r13,8), %rdi
movq %r14, %rsi
callq 0x1040
xorl %eax, %eax
callq 0x203b
subsd (%rsp), %xmm0
ucomisd 0x11c3(%rip), %xmm0     # 0x4320
jbe 0x31b2
xorl %eax, %eax
callq 0x203b
leal 0x1(%r13), %esi
cvtsi2sd %esi, %xmm1
movapd %xmm0, %xmm3
movapd %xmm0, %xmm2
movsd 0x11aa(%rip), %xmm4     # 0x4328
mulsd %xmm4, %xmm2
divsd %xmm1, %xmm2
movapd %xmm1, %xmm0
divsd %xmm4, %xmm0
mulsd 0xf62(%rip), %xmm0      # 0x40f8
subsd %xmm3, %xmm2
movq %r12, %rdi
movl $0x640, %edx            # imm = 0x640
movsd %xmm3, (%rsp)
movapd %xmm3, %xmm1
movb $0x3, %al
callq 0x10b0
addl $0x4, %ebp
incq %r13
jmp 0x30e6
movq %r15, %rdi
callq 0x1090
leaq 0x1216(%rip), %rdi      # 0x43e2
callq 0x1050
movl %ebp, %edi
callq 0x232d
movq %rax, 0x3ff1(%rip)      # 0x71d0
movl %ebp, %edi
callq 0x232d
movq %rax, 0x3fd3(%rip)      # 0x71c0
xorl %ecx, %ecx
movq 0x3fda(%rip), %rdx      # 0x71d0
cmpq %rcx, %r13
je 0x3208
andl $0x0, (%rax,%rcx,4)
andl $0x0, (%rdx,%rcx,4)
incq %rcx
jmp 0x31f6
xorl %eax, %eax
callq 0x2058
movq 0x3f12(%rip), %rdi      # 0x7128
leaq 0x1180(%rip), %rsi      # 0x439d
callq 0x20b8
movq %rax, %r14
movl $0x100000, %edi         # imm = 0x100000
callq 0x232d
movq %rax, %r12
xorl %ebp, %ebp
movq %r14, %rdi
callq 0x1120
testl %eax, %eax
jne 0x32eb
movl $0x100000, %esi         # imm = 0x100000
movq %r12, %rdi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x1060
movq %r12, %r15
cmpb $0x0, (%r15)
je 0x3234
xorl %ebx, %ebx
cmpl %ebx, %r13d
je 0x3276
movsbl (%r15), %edi
movl %ebx, %esi
callq 0x2fbc
incl %ebx
jmp 0x3262
incq %r15
incl %ebp
xorl %eax, %eax
callq 0x203b
subsd (%rsp), %xmm0
ucomisd 0x1091(%rip), %xmm0     # 0x4320
jbe 0x325a
xorl %eax, %eax
callq 0x203b
cvtsi2sd %ebp, %xmm1
movapd %xmm0, %xmm3
movapd %xmm0, %xmm2
movsd 0x108c(%rip), %xmm4     # 0x4338
mulsd %xmm4, %xmm2
divsd %xmm1, %xmm2
movapd %xmm1, %xmm0
divsd %xmm4, %xmm0
mulsd 0xe34(%rip), %xmm0      # 0x40f8
subsd %xmm3, %xmm2
leaq 0x10d0(%rip), %rdi      # 0x439f
movl %ebp, %esi
movl $0x36d5393c, %edx       # imm = 0x36D5393C
movsd %xmm3, (%rsp)
movapd %xmm3, %xmm1
movb $0x3, %al
callq 0x10b0
jmp 0x325a
movq %r14, %rdi
callq 0x1090
leaq 0x3e76(%rip), %rbx      # 0x7170
movq (%rbx), %r14
movq 0x3eb4(%rip), %rax      # 0x71b8
leaq 0x10(%rsp), %rdx
movq %rax, (%rdx)
movq 0x3ebd(%rip), %rax      # 0x71d0
movq %rax, 0x8(%rdx)
leal -0x1(%r13), %esi
leaq -0x32f(%rip), %rcx      # 0x2ff3
leaq -0x311(%rip), %r8       # 0x3018
xorl %edi, %edi
callq 0x2d1c
movq %r14, (%rbx)
leaq 0x1085(%rip), %r14      # 0x43bf
xorl %r15d, %r15d
movq 0x8(%rsp), %r12
cmpq %r15, %r13
je 0x336f
movq 0x3e6a(%rip), %rax      # 0x71b8
movq (%rax,%r15,8), %rdx
movq 0x3e77(%rip), %rax      # 0x71d0
movl (%rax,%r15,4), %ecx
movq %r12, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x1110
incq %r15
jmp 0x3342
xorl %eax, %eax
callq 0x22dd
movq %rax, %xmm1
punpckldq 0xfbd(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xfc5(%rip), %xmm1      # 0x4350
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
mulsd 0xf91(%rip), %xmm0      # 0x4330
movq (%rbx), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
xorl %edx, %edx
divq %rcx
leaq 0x1013(%rip), %rsi      # 0x43c6
movq %r12, %rdi
movq %rax, %rdx
movb $0x1, %al
callq 0x1110
xorl %eax, %eax
callq 0x203b
leaq 0x1004(%rip), %rdi      # 0x43d2
movb $0x1, %al
callq 0x10b0
xorl %eax, %eax
callq 0x2251
movq %r12, %rdi
callq 0x1090
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq