// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movl $0x0, -0x4(%rbp)
movb $0x0, %al
callq 0x2d10
xorps %xmm0, %xmm0
movsd %xmm0, -0x10(%rbp)
movl $0x164000, %edi         # imm = 0x164000
leaq -0x1a6d(%rip), %rsi     # 0x2fb0
callq 0x2fc0
movl $0x100, %edi            # imm = 0x100
callq 0x32d0
movq %rax, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x3200, %edi           # imm = 0x3200
callq 0x32d0
movq %rax, 0x3768(%rip)      # 0x81b0
movl $0x3200, %edi           # imm = 0x3200
callq 0x32d0
movq %rax, 0x3767(%rip)      # 0x81c0
movq 0x36c8(%rip), %rdi      # 0x8128
leaq 0x934(%rip), %rsi       # 0x539b
callq 0x2e80
movq %rax, -0x28(%rbp)
movq 0x36a1(%rip), %rdi      # 0x8118
leaq 0x91f(%rip), %rsi       # 0x539d
callq 0x2e80
movq %rax, -0x30(%rbp)
leaq 0x911(%rip), %rdi       # 0x539f
movb $0x0, %al
callq 0x10a0
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x2ed0
movl %eax, -0x20(%rbp)
cmpl $0x0, %eax
je 0x4ba4
movl -0x20(%rbp), %edi
addl $0x1, %edi
callq 0x32d0
movq %rax, %rdx
movq 0x36ed(%rip), %rax      # 0x81b0
movslq -0x1c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movslq -0x20(%rbp), %rax
shlq $0x2, %rax
movl %eax, %edi
callq 0x32d0
movq %rax, %rdx
movq 0x36dc(%rip), %rax      # 0x81c0
movslq -0x1c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x18(%rbp), %rdi
movq 0x36c9(%rip), %rax      # 0x81c0
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x3540
movq 0x36a5(%rip), %rax      # 0x81b0
movl -0x1c(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x1c(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
movq -0x18(%rbp), %rsi
callq 0x1030
movb $0x0, %al
callq 0x2d30
subsd -0x10(%rbp), %xmm0
movsd 0x7ee(%rip), %xmm1      # 0x5328
ucomisd %xmm1, %xmm0
jbe 0x4b9f
movb $0x0, %al
callq 0x2d30
movsd %xmm0, -0x10(%rbp)
movl -0x1c(%rbp), %esi
cvtsi2sdl -0x1c(%rbp), %xmm0
movsd 0x7dc(%rip), %xmm1      # 0x5338
divsd %xmm1, %xmm0
movsd 0x590(%rip), %xmm1      # 0x50f8
mulsd %xmm1, %xmm0
movsd -0x10(%rbp), %xmm1
movsd 0x7bf(%rip), %xmm2      # 0x5338
mulsd -0x10(%rbp), %xmm2
cvtsi2sdl -0x1c(%rbp), %xmm3
divsd %xmm3, %xmm2
subsd -0x10(%rbp), %xmm2
leaq 0x817(%rip), %rdi       # 0x53aa
movl $0x640, %edx            # imm = 0x640
movb $0x3, %al
callq 0x10a0
jmp 0x4a95
movq -0x30(%rbp), %rdi
callq 0x1070
leaq 0x816(%rip), %rdi       # 0x53ca
movb $0x0, %al
callq 0x10a0
movslq -0x1c(%rbp), %rax
shlq $0x2, %rax
movl %eax, %edi
callq 0x32d0
movq %rax, 0x35f7(%rip)      # 0x81c8
movslq -0x1c(%rbp), %rax
shlq $0x2, %rax
movl %eax, %edi
callq 0x32d0
movq %rax, 0x35d1(%rip)      # 0x81b8
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x4c25
movq 0x35bb(%rip), %rax      # 0x81b8
movslq -0x34(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movq 0x35b9(%rip), %rax      # 0x81c8
movslq -0x34(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x4bee
movb $0x0, %al
callq 0x2d60
movl $0x0, -0x38(%rbp)
movq 0x34e6(%rip), %rdi      # 0x8120
leaq 0x75c(%rip), %rsi       # 0x539d
callq 0x2e80
movq %rax, -0x30(%rbp)
movl $0x100000, -0x3c(%rbp)  # imm = 0x100000
movl -0x3c(%rbp), %edi
callq 0x32d0
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rdi
callq 0x1110
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x4c77
jmp 0x4d68
movq -0x48(%rbp), %rdi
movslq -0x3c(%rbp), %rsi
movq -0x30(%rbp), %rcx
movl $0x1, %edx
callq 0x1040
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x4d63
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x4cd0
movq -0x50(%rbp), %rax
movb (%rax), %al
movl -0x54(%rbp), %esi
movsbl %al, %edi
callq 0x4860
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x4cac
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movb $0x0, %al
callq 0x2d30
subsd -0x10(%rbp), %xmm0
movsd 0x62f(%rip), %xmm1      # 0x5328
ucomisd %xmm1, %xmm0
jbe 0x4d5e
movb $0x0, %al
callq 0x2d30
movsd %xmm0, -0x10(%rbp)
movl -0x38(%rbp), %esi
cvtsi2sdl -0x38(%rbp), %xmm0
movsd 0x615(%rip), %xmm1      # 0x5330
divsd %xmm1, %xmm0
movsd 0x3d1(%rip), %xmm1      # 0x50f8
mulsd %xmm1, %xmm0
movsd -0x10(%rbp), %xmm1
movsd 0x5f8(%rip), %xmm2      # 0x5330
mulsd -0x10(%rbp), %xmm2
cvtsi2sdl -0x38(%rbp), %xmm3
divsd %xmm3, %xmm2
subsd -0x10(%rbp), %xmm2
leaq 0x658(%rip), %rdi       # 0x53aa
movl $0x36d5393c, %edx       # imm = 0x36D5393C
movb $0x3, %al
callq 0x10a0
jmp 0x4c95
jmp 0x4c5d
movq -0x30(%rbp), %rdi
callq 0x1070
leaq 0x33f0(%rip), %rax      # 0x8168
movq (%rax), %rax
movq %rax, -0x60(%rbp)
movq 0x342a(%rip), %rax      # 0x81b0
movq %rax, -0x70(%rbp)
movq 0x3437(%rip), %rax      # 0x81c8
movq %rax, -0x68(%rbp)
movl -0x1c(%rbp), %esi
subl $0x1, %esi
leaq -0x70(%rbp), %rdx
xorl %edi, %edi
leaq -0x4d8(%rip), %rcx      # 0x48d0
leaq -0x45f(%rip), %r8       # 0x4950
callq 0x4400
movq -0x60(%rbp), %rcx
leaq 0x33a9(%rip), %rax      # 0x8168
movq %rcx, (%rax)
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x4e0b
movq -0x28(%rbp), %rdi
movq 0x33d4(%rip), %rax      # 0x81b0
movslq -0x74(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq 0x33dd(%rip), %rax      # 0x81c8
movslq -0x74(%rbp), %rcx
movl (%rax,%rcx,4), %ecx
leaq 0x5e4(%rip), %rsi       # 0x53dd
movb $0x0, %al
callq 0x1100
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x4dc9
movq -0x28(%rbp), %rax
movq %rax, -0x80(%rbp)
xorl %eax, %eax
callq 0x3230
movq -0x80(%rbp), %rdi
movq %rax, %xmm0
movaps 0x516(%rip), %xmm1      # 0x5340
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x51a(%rip), %xmm1      # 0x5350
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x4d3(%rip), %xmm1      # 0x5320
divsd %xmm1, %xmm0
leaq 0x3310(%rip), %rax      # 0x8168
movq (%rax), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
xorl %edx, %edx
divq %rcx
movq %rax, %rdx
leaq 0x575(%rip), %rsi       # 0x53e4
movb $0x1, %al
callq 0x1100
movb $0x0, %al
callq 0x2d30
leaq 0x56c(%rip), %rdi       # 0x53f0
movb $0x1, %al
callq 0x10a0
movb $0x0, %al
callq 0x3120
movq -0x28(%rbp), %rdi
callq 0x1070
xorl %eax, %eax
addq $0x80, %rsp
popq %rbp
retq