// File: /workspace/github_c_makefile_star2_O1/OhYee[P]computer_application_programming_experiment/pattern_match/strstr/strstr.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(int *));

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

    while ((l = read_file(f, temp)) != 0) {
        patterns[pattern_number] = mp_new(l + 1);
        strcpy(patterns[pattern_number++], temp);
        printf("%d/%d\r", pattern_number, 1500);
    }
    fclose(f);

    count = mp_new(pattern_number * sizeof(int));
    pos = mp_new(pattern_number * sizeof(int));
    string = mp_new(max_string_length);
    memset(string, 0, max_string_length);
    for (int i = 0; i < pattern_number; ++i) {
        count[i] = pos[i] = 0;
    }
    printf("\npatterns read ok\n");

    compare_init();

    int file_pos = 0;
    f = open_file(string_filename, "r");

    int   buf_size = 1 << 29;
    char *buf = mp_new(buf_size);

    while (!feof(f)) {
        fread(buf, buf_size, 1, f);
        char *c = buf;
        while (*c != '\0') {
            for (int i = max_string_length - 1; i > 0; --i) {
                string[i] = string[i - 1];
            }
            string[0] = *c;

            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 $0x38, %rsp
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x2424
leaq -0x106f(%rip), %rsi     # 0x2548
movl $0x426f3c00, %edi       # imm = 0x426F3C00
callq 0x2553
movl $0x100, %edi            # imm = 0x100
callq 0x2788
movq %rax, %r15
movl $0x11379e0, %edi        # imm = 0x11379E0
callq 0x2788
movq %rax, 0x3be9(%rip)      # 0x71c8
movq 0x3b4a(%rip), %rdi      # 0x7130
leaq 0xdbe(%rip), %rsi       # 0x43ab
callq 0x24c0
movq %rax, 0x10(%rsp)
movq 0x3b22(%rip), %rdi      # 0x7120
leaq 0xda8(%rip), %rsi       # 0x43ad
callq 0x24c0
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
callq 0x24eb
testl %eax, %eax
je 0x3671
leaq 0xd8c(%rip), %r13       # 0x43af
xorl %r14d, %r14d
incl %eax
movl %eax, %edi
callq 0x2788
movq 0x3b92(%rip), %rcx      # 0x71c8
movq %rax, (%rcx,%r14,8)
movq 0x3b87(%rip), %rax      # 0x71c8
movq (%rax,%r14,8), %rdi
incq %r14
movq %r15, %rsi
callq 0x1040
movq %r13, %rdi
movl %r14d, %esi
movl $0x5dc, %edx            # imm = 0x5DC
xorl %eax, %eax
callq 0x10b0
movq %r12, %rdi
movq %r15, %rsi
callq 0x24eb
testl %eax, %eax
jne 0x3626
movq %r12, %rdi
callq 0x1090
leal (,%r14,4), %ebx
movl %ebx, %edi
callq 0x2788
movq %rax, 0x3b41(%rip)      # 0x71d0
movl %ebx, %edi
callq 0x2788
movq %rax, 0x3b1b(%rip)      # 0x71b8
movl $0x100, %edi            # imm = 0x100
callq 0x2788
movq %rax, 0x3b12(%rip)      # 0x71c0
xorl %ebx, %ebx
movl $0x100, %edx            # imm = 0x100
movq %rax, %rdi
xorl %esi, %esi
callq 0x10c0
movl %r14d, %esi
testl %r14d, %r14d
je 0x36e5
movq 0x3aea(%rip), %rax      # 0x71b8
movq 0x3afb(%rip), %rcx      # 0x71d0
xorl %edx, %edx
movl %ebx, (%rax,%rdx,4)
movl %ebx, (%rcx,%rdx,4)
incq %rdx
cmpq %rdx, %rsi
jne 0x36d7
movq %rsi, 0x8(%rsp)
leaq 0xcfe(%rip), %rdi       # 0x43ef
callq 0x1050
xorl %eax, %eax
callq 0x2450
movq 0x3a24(%rip), %rdi      # 0x7128
leaq 0xca2(%rip), %rsi       # 0x43ad
callq 0x24c0
movq %rax, %rbx
movl $0x20000000, %edi       # imm = 0x20000000
callq 0x2788
movq %rax, %r15
movq %rbx, %rdi
callq 0x1120
testl %eax, %eax
jne 0x383c
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
leaq 0xc76(%rip), %r13       # 0x43b6
xorl %ebp, %ebp
movq %rbx, 0x18(%rsp)
movl $0x20000000, %esi       # imm = 0x20000000
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x1060
cmpb $0x0, (%r15)
je 0x3827
movq %r15, %r12
movl $0x100, %eax            # imm = 0x100
movq 0x3a4b(%rip), %rcx      # 0x71c0
movb -0x2(%rcx,%rax), %dl
movb %dl, -0x1(%rcx,%rax)
decq %rax
cmpq $0x1, %rax
ja 0x376e
movb (%r12), %al
movq 0x3a2f(%rip), %rcx      # 0x71c0
movb %al, (%rcx)
testl %r14d, %r14d
je 0x37ad
xorl %ebx, %ebx
movsbl (%r12), %edi
movl %ebx, %esi
callq 0x34ce
incl %ebx
cmpl %ebx, %r14d
jne 0x379a
incl %ebp
xorl %eax, %eax
callq 0x2433
subsd (%rsp), %xmm0
ucomisd 0xb75(%rip), %xmm0      # 0x4338
jbe 0x3819
xorl %eax, %eax
callq 0x2433
xorps %xmm1, %xmm1
cvtsi2sd %ebp, %xmm1
movapd %xmm0, %xmm4
movapd %xmm0, %xmm2
movsd 0xb5d(%rip), %xmm3      # 0x4340
mulsd %xmm3, %xmm2
divsd %xmm1, %xmm2
movapd %xmm1, %xmm0
divsd %xmm3, %xmm0
mulsd 0x915(%rip), %xmm0      # 0x4110
subsd %xmm4, %xmm2
movq %r13, %rdi
movl %ebp, %esi
movl $0x36d5393c, %edx       # imm = 0x36D5393C
movsd %xmm4, (%rsp)
movapd %xmm4, %xmm1
movb $0x3, %al
callq 0x10b0
incq %r12
cmpb $0x0, (%r12)
jne 0x3769
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x1120
testl %eax, %eax
je 0x3747
movq %rbx, %rdi
callq 0x1090
leaq 0x3925(%rip), %rbx      # 0x7170
movq (%rbx), %r15
movq 0x3973(%rip), %rax      # 0x71c8
leaq 0x20(%rsp), %rdx
movq %rax, (%rdx)
movq 0x396c(%rip), %rax      # 0x71d0
movq %rax, 0x8(%rdx)
leal -0x1(%r14), %esi
leaq -0x32a(%rip), %rcx      # 0x3549
leaq -0x30c(%rip), %r8       # 0x356e
xorl %edi, %edi
callq 0x3150
movq %r15, (%rbx)
cmpl $0x1, %r14d
movq 0x10(%rsp), %r12
movq 0x8(%rsp), %r13
jb 0x38c9
leaq 0xb3b(%rip), %r14       # 0x43d6
xorl %r15d, %r15d
movq 0x3923(%rip), %rax      # 0x71c8
movq (%rax,%r15,8), %rdx
movq 0x3920(%rip), %rax      # 0x71d0
movl (%rax,%r15,4), %ecx
movq %r12, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x1110
incq %r15
cmpq %r15, %r13
jne 0x389e
xorl %eax, %eax
callq 0x2734
movq %rax, %xmm1
punpckldq 0xa73(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xa7b(%rip), %xmm1      # 0x4360
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
mulsd 0xa4f(%rip), %xmm0      # 0x4348
movq (%rbx), %rax
shrq $0x3, %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
mulq %rcx
shrq $0x4, %rdx
leaq 0xac5(%rip), %rsi       # 0x43dd
movq %r12, %rdi
movb $0x1, %al
callq 0x1110
xorl %eax, %eax
callq 0x2433
leaq 0xab9(%rip), %rdi       # 0x43e9
movb $0x1, %al
callq 0x10b0
xorl %eax, %eax
callq 0x2667
movq %r12, %rdi
callq 0x1090
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq