// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x23ec
leaq -0xf3c(%rip), %rsi      # 0x24fc
movl $0x164000, %edi         # imm = 0x164000
callq 0x2507
movl $0x100, %edi            # imm = 0x100
callq 0x2740
movq %rax, %r14
movl $0x3200, %edi           # imm = 0x3200
callq 0x2740
movq %rax, 0x3d58(%rip)      # 0x71b8
movl $0x3200, %edi           # imm = 0x3200
callq 0x2740
movq %rax, 0x3d57(%rip)      # 0x71c8
movq 0x3cb8(%rip), %rdi      # 0x7130
leaq 0xf3c(%rip), %rsi       # 0x43bb
callq 0x2478
movq %rax, 0x18(%rsp)
movq 0x3c90(%rip), %rdi      # 0x7120
leaq 0xf26(%rip), %rsi       # 0x43bd
callq 0x2478
movq %rax, %r15
leaq 0xf52(%rip), %rdi       # 0x43f8
callq 0x1050
movq %r15, %rdi
movq %r14, %rsi
callq 0x24a3
testl %eax, %eax
je 0x35ad
movl %eax, %r13d
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0xeed(%rip), %r12       # 0x43bf
xorl %ebx, %ebx
leal 0x1(%r13), %edi
callq 0x2740
movq 0x3cd4(%rip), %rcx      # 0x71b8
movq %rax, (%rcx,%rbx,8)
shll $0x2, %r13d
movl %r13d, %edi
callq 0x2740
movq 0x3ccd(%rip), %rcx      # 0x71c8
movq %rax, (%rcx,%rbx,8)
movq 0x3cc2(%rip), %rax      # 0x71c8
movq (%rax,%rbx,8), %rsi
movq %r14, %rdi
callq 0x295c
movq 0x3c9f(%rip), %rax      # 0x71b8
movq (%rax,%rbx,8), %rdi
movq %r14, %rsi
callq 0x1040
xorl %eax, %eax
callq 0x23fb
subsd 0x8(%rsp), %xmm0
ucomisd 0xdfe(%rip), %xmm0      # 0x4338
jbe 0x3592
xorl %eax, %eax
callq 0x23fb
leal 0x1(%rbx), %esi
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
movapd %xmm0, %xmm3
movapd %xmm0, %xmm2
movsd 0xde3(%rip), %xmm4      # 0x4340
mulsd %xmm4, %xmm2
divsd %xmm1, %xmm2
movapd %xmm1, %xmm0
divsd %xmm4, %xmm0
mulsd 0xb9b(%rip), %xmm0      # 0x4110
subsd %xmm3, %xmm2
movq %r12, %rdi
movl $0x640, %edx            # imm = 0x640
movsd %xmm3, 0x8(%rsp)
movapd %xmm3, %xmm1
movb $0x3, %al
callq 0x10b0
movq %r15, %rdi
movq %r14, %rsi
callq 0x24a3
movl %eax, %r13d
incq %rbx
testl %eax, %eax
jne 0x34d4
jmp 0x35b7
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
callq 0x1090
leaq 0xe3c(%rip), %rdi       # 0x4402
callq 0x1050
leal (,%rbx,4), %ebp
movl %ebp, %edi
callq 0x2740
movq %rax, 0x3bf0(%rip)      # 0x71d0
movl %ebp, %edi
callq 0x2740
movq %rax, 0x3bd2(%rip)      # 0x71c0
movl %ebx, %edi
testl %ebx, %ebx
je 0x360d
movq 0x3bd5(%rip), %rcx      # 0x71d0
xorl %edx, %edx
xorl %esi, %esi
movl %edx, (%rax,%rsi,4)
movl %edx, (%rcx,%rsi,4)
incq %rsi
cmpq %rsi, %rdi
jne 0x35ff
movq %rdi, 0x10(%rsp)
xorl %eax, %eax
callq 0x2418
movq 0x3b08(%rip), %rdi      # 0x7128
leaq 0xd96(%rip), %rsi       # 0x43bd
callq 0x2478
movq %rax, %r14
movl $0x100000, %edi         # imm = 0x100000
callq 0x2740
movq %rax, %r15
movq %r14, %rdi
callq 0x1120
testl %eax, %eax
jne 0x375e
movq 0x10(%rsp), %rax
leaq (,%rax,4), %r13
leaq 0xd5f(%rip), %r12       # 0x43bf
xorl %ebp, %ebp
movq %r15, 0x20(%rsp)
movq %r14, 0x28(%rsp)
movl $0x100000, %esi         # imm = 0x100000
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x1060
cmpb $0x0, (%r15)
je 0x3744
testl %ebx, %ebx
je 0x36ce
xorl %r14d, %r14d
movsbl (%r15), %edi
movq 0x3b1b(%rip), %rax      # 0x71b8
movq (%rax,%r14,2), %rsi
movq 0x3b18(%rip), %rdx      # 0x71c0
addq %r14, %rdx
movq 0x3b16(%rip), %rax      # 0x71c8
movq (%rax,%r14,2), %rcx
movq 0x3b13(%rip), %r8       # 0x71d0
addq %r14, %r8
callq 0x29aa
addq $0x4, %r14
cmpq %r14, %r13
jne 0x3692
incl %ebp
xorl %eax, %eax
callq 0x23fb
subsd 0x8(%rsp), %xmm0
ucomisd 0xc53(%rip), %xmm0      # 0x4338
jbe 0x373c
xorl %eax, %eax
callq 0x23fb
xorps %xmm1, %xmm1
cvtsi2sd %ebp, %xmm1
movapd %xmm0, %xmm3
movapd %xmm0, %xmm2
movsd 0xc43(%rip), %xmm4      # 0x4348
mulsd %xmm4, %xmm2
divsd %xmm1, %xmm2
movapd %xmm1, %xmm0
divsd %xmm4, %xmm0
mulsd 0x9f3(%rip), %xmm0      # 0x4110
subsd %xmm3, %xmm2
movq %r12, %rdi
movl %ebp, %esi
movl $0x36d5393c, %edx       # imm = 0x36D5393C
movsd %xmm3, 0x8(%rsp)
movapd %xmm3, %xmm1
movb $0x3, %al
callq 0x10b0
incq %r15
jmp 0x3681
movq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x1120
testl %eax, %eax
movq 0x20(%rsp), %r15
je 0x366c
leal -0x1(%rbx), %ebp
movq %r14, %rdi
callq 0x1090
leaq 0x3a00(%rip), %r15      # 0x7170
movq (%r15), %r14
movq 0x3a3e(%rip), %rax      # 0x71b8
leaq 0x30(%rsp), %rdx
movq %rax, (%rdx)
movq 0x3a47(%rip), %rax      # 0x71d0
movq %rax, 0x8(%rdx)
leaq -0x3c9(%rip), %rcx      # 0x33cb
leaq -0x3ab(%rip), %r8       # 0x33f0
xorl %edi, %edi
movl %ebp, %esi
callq 0x311c
movq %r14, (%r15)
cmpl $0x1, %ebx
movq 0x18(%rsp), %r12
movq 0x10(%rsp), %r13
jb 0x37e9
leaq 0xc22(%rip), %r14       # 0x43df
xorl %ebx, %ebx
movq 0x39f2(%rip), %rax      # 0x71b8
movq (%rax,%rbx,8), %rdx
movq 0x39ff(%rip), %rax      # 0x71d0
movl (%rax,%rbx,4), %ecx
movq %r12, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x1110
incq %rbx
cmpq %rbx, %r13
jne 0x37bf
xorl %eax, %eax
callq 0x26ec
movq %rax, %xmm1
punpckldq 0xb63(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xb6b(%rip), %xmm1      # 0x4370
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
mulsd 0xb37(%rip), %xmm0      # 0x4350
movq (%r15), %rax
shrq $0x3, %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
mulq %rcx
shrq $0x4, %rdx
leaq 0xbae(%rip), %rsi       # 0x43e6
movq %r12, %rdi
movb $0x1, %al
callq 0x1110
xorl %eax, %eax
callq 0x23fb
leaq 0xba2(%rip), %rdi       # 0x43f2
movb $0x1, %al
callq 0x10b0
xorl %eax, %eax
callq 0x261e
movq %r12, %rdi
callq 0x1090
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq