// File: /workspace/github_c_makefile_star2_O1/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 0x2424
leaq -0xf54(%rip), %rsi      # 0x2548
movl $0x164000, %edi         # imm = 0x164000
callq 0x2553
movl $0x100, %edi            # imm = 0x100
callq 0x2788
movq %rax, %r14
movl $0x3200, %edi           # imm = 0x3200
callq 0x2788
movq %rax, 0x3cf4(%rip)      # 0x71b8
movl $0x3200, %edi           # imm = 0x3200
callq 0x2788
movq %rax, 0x3cf3(%rip)      # 0x71c8
movq 0x3c54(%rip), %rdi      # 0x7130
leaq 0xed8(%rip), %rsi       # 0x43bb
callq 0x24c0
movq %rax, 0x18(%rsp)
movq 0x3c2c(%rip), %rdi      # 0x7120
leaq 0xec2(%rip), %rsi       # 0x43bd
callq 0x24c0
movq %rax, %r15
leaq 0xeee(%rip), %rdi       # 0x43f8
callq 0x1050
movq %r15, %rdi
movq %r14, %rsi
callq 0x24eb
testl %eax, %eax
je 0x3611
movl %eax, %r13d
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0xe89(%rip), %r12       # 0x43bf
xorl %ebx, %ebx
leal 0x1(%r13), %edi
callq 0x2788
movq 0x3c70(%rip), %rcx      # 0x71b8
movq %rax, (%rcx,%rbx,8)
shll $0x2, %r13d
movl %r13d, %edi
callq 0x2788
movq 0x3c69(%rip), %rcx      # 0x71c8
movq %rax, (%rcx,%rbx,8)
movq 0x3c5e(%rip), %rax      # 0x71c8
movq (%rax,%rbx,8), %rsi
movq %r14, %rdi
callq 0x29ac
movq 0x3c3b(%rip), %rax      # 0x71b8
movq (%rax,%rbx,8), %rdi
movq %r14, %rsi
callq 0x1040
xorl %eax, %eax
callq 0x2433
subsd 0x8(%rsp), %xmm0
ucomisd 0xd9a(%rip), %xmm0      # 0x4338
jbe 0x35f6
xorl %eax, %eax
callq 0x2433
leal 0x1(%rbx), %esi
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
movapd %xmm0, %xmm3
movapd %xmm0, %xmm2
movsd 0xd7f(%rip), %xmm4      # 0x4340
mulsd %xmm4, %xmm2
divsd %xmm1, %xmm2
movapd %xmm1, %xmm0
divsd %xmm4, %xmm0
mulsd 0xb37(%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 0x24eb
movl %eax, %r13d
incq %rbx
testl %eax, %eax
jne 0x3538
jmp 0x361b
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
callq 0x1090
leaq 0xdd8(%rip), %rdi       # 0x4402
callq 0x1050
leal (,%rbx,4), %ebp
movl %ebp, %edi
callq 0x2788
movq %rax, 0x3b8c(%rip)      # 0x71d0
movl %ebp, %edi
callq 0x2788
movq %rax, 0x3b6e(%rip)      # 0x71c0
movl %ebx, %edi
testl %ebx, %ebx
je 0x3671
movq 0x3b71(%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 0x3663
movq %rdi, 0x10(%rsp)
xorl %eax, %eax
callq 0x2450
movq 0x3aa4(%rip), %rdi      # 0x7128
leaq 0xd32(%rip), %rsi       # 0x43bd
callq 0x24c0
movq %rax, %r14
movl $0x100000, %edi         # imm = 0x100000
callq 0x2788
movq %rax, %r15
movq %r14, %rdi
callq 0x1120
testl %eax, %eax
jne 0x37c2
movq 0x10(%rsp), %rax
leaq (,%rax,4), %r13
leaq 0xcfb(%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 0x37a8
testl %ebx, %ebx
je 0x3732
xorl %r14d, %r14d
movsbl (%r15), %edi
movq 0x3ab7(%rip), %rax      # 0x71b8
movq (%rax,%r14,2), %rsi
movq 0x3ab4(%rip), %rdx      # 0x71c0
addq %r14, %rdx
movq 0x3ab2(%rip), %rax      # 0x71c8
movq (%rax,%r14,2), %rcx
movq 0x3aaf(%rip), %r8       # 0x71d0
addq %r14, %r8
callq 0x29f8
addq $0x4, %r14
cmpq %r14, %r13
jne 0x36f6
incl %ebp
xorl %eax, %eax
callq 0x2433
subsd 0x8(%rsp), %xmm0
ucomisd 0xbef(%rip), %xmm0      # 0x4338
jbe 0x37a0
xorl %eax, %eax
callq 0x2433
xorps %xmm1, %xmm1
cvtsi2sd %ebp, %xmm1
movapd %xmm0, %xmm3
movapd %xmm0, %xmm2
movsd 0xbdf(%rip), %xmm4      # 0x4348
mulsd %xmm4, %xmm2
divsd %xmm1, %xmm2
movapd %xmm1, %xmm0
divsd %xmm4, %xmm0
mulsd 0x98f(%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 0x36e5
movq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x1120
testl %eax, %eax
movq 0x20(%rsp), %r15
je 0x36d0
movq %r14, %rdi
callq 0x1090
leaq 0x399f(%rip), %r15      # 0x7170
movq (%r15), %r14
movq 0x39dd(%rip), %rax      # 0x71b8
leaq 0x30(%rsp), %rdx
movq %rax, (%rdx)
movq 0x39e6(%rip), %rax      # 0x71d0
movq %rax, 0x8(%rdx)
leal -0x1(%rbx), %esi
leaq -0x3c9(%rip), %rcx      # 0x342f
leaq -0x3ab(%rip), %r8       # 0x3454
xorl %edi, %edi
callq 0x3150
movq %r14, (%r15)
cmpl $0x1, %ebx
movq 0x18(%rsp), %r12
movq 0x10(%rsp), %r13
jb 0x384b
leaq 0xbc0(%rip), %r14       # 0x43df
xorl %ebx, %ebx
movq 0x3990(%rip), %rax      # 0x71b8
movq (%rax,%rbx,8), %rdx
movq 0x399d(%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 0x3821
xorl %eax, %eax
callq 0x2734
movq %rax, %xmm1
punpckldq 0xb01(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xb09(%rip), %xmm1      # 0x4370
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
mulsd 0xad5(%rip), %xmm0      # 0x4350
movq (%r15), %rax
shrq $0x3, %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
mulq %rcx
shrq $0x4, %rdx
leaq 0xb4c(%rip), %rsi       # 0x43e6
movq %r12, %rdi
movb $0x1, %al
callq 0x1110
xorl %eax, %eax
callq 0x2433
leaq 0xb40(%rip), %rdi       # 0x43f2
movb $0x1, %al
callq 0x10b0
xorl %eax, %eax
callq 0x2667
movq %r12, %rdi
callq 0x1090
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq