// File: /workspace/github_c_makefile_star2_O3/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 0x23ec
leaq -0x1061(%rip), %rsi     # 0x24fc
movl $0x426f3c00, %edi       # imm = 0x426F3C00
callq 0x2507
movl $0x100, %edi            # imm = 0x100
callq 0x2740
movq %rax, %r15
movl $0x11379e0, %edi        # imm = 0x11379E0
callq 0x2740
movq %rax, 0x3c43(%rip)      # 0x71c8
movq 0x3ba4(%rip), %rdi      # 0x7130
leaq 0xe18(%rip), %rsi       # 0x43ab
callq 0x2478
movq %rax, 0x10(%rsp)
movq 0x3b7c(%rip), %rdi      # 0x7120
leaq 0xe02(%rip), %rsi       # 0x43ad
callq 0x2478
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
callq 0x24a3
testl %eax, %eax
je 0x3617
leaq 0xde6(%rip), %r13       # 0x43af
xorl %r14d, %r14d
incl %eax
movl %eax, %edi
callq 0x2740
movq 0x3bec(%rip), %rcx      # 0x71c8
movq %rax, (%rcx,%r14,8)
movq 0x3be1(%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 0x24a3
testl %eax, %eax
jne 0x35cc
movq %r12, %rdi
callq 0x1090
leal (,%r14,4), %ebx
movl %ebx, %edi
callq 0x2740
movq %rax, 0x3b9b(%rip)      # 0x71d0
movl %ebx, %edi
callq 0x2740
movq %rax, 0x3b75(%rip)      # 0x71b8
movl $0x100, %edi            # imm = 0x100
callq 0x2740
movq %rax, 0x3b6c(%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 0x368b
movq 0x3b44(%rip), %rax      # 0x71b8
movq 0x3b55(%rip), %rcx      # 0x71d0
xorl %edx, %edx
movl %ebx, (%rax,%rdx,4)
movl %ebx, (%rcx,%rdx,4)
incq %rdx
cmpq %rdx, %rsi
jne 0x367d
movq %rsi, 0x8(%rsp)
leaq 0xd58(%rip), %rdi       # 0x43ef
callq 0x1050
xorl %eax, %eax
callq 0x2418
movq 0x3a7e(%rip), %rdi      # 0x7128
leaq 0xcfc(%rip), %rsi       # 0x43ad
callq 0x2478
movq %rax, %r12
movl $0x20000000, %edi       # imm = 0x20000000
callq 0x2740
movq %rax, %r15
movq %r12, %rdi
callq 0x1120
testl %eax, %eax
jne 0x37e2
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
leaq 0xcd0(%rip), %r13       # 0x43b6
xorl %ebp, %ebp
movq %r12, 0x18(%rsp)
movl $0x20000000, %esi       # imm = 0x20000000
movl $0x1, %edx
movq %r15, %rdi
movq %r12, %rcx
callq 0x1060
cmpb $0x0, (%r15)
je 0x37cd
movq %r15, %r12
movl $0x100, %eax            # imm = 0x100
movq 0x3aa5(%rip), %rcx      # 0x71c0
movb -0x2(%rcx,%rax), %dl
movb %dl, -0x1(%rcx,%rax)
decq %rax
cmpq $0x1, %rax
ja 0x3714
movb (%r12), %al
movq 0x3a89(%rip), %rcx      # 0x71c0
movb %al, (%rcx)
testl %r14d, %r14d
je 0x3753
xorl %ebx, %ebx
movsbl (%r12), %edi
movl %ebx, %esi
callq 0x3474
incl %ebx
cmpl %ebx, %r14d
jne 0x3740
incl %ebp
xorl %eax, %eax
callq 0x23fb
subsd (%rsp), %xmm0
ucomisd 0xbcf(%rip), %xmm0      # 0x4338
jbe 0x37bf
xorl %eax, %eax
callq 0x23fb
xorps %xmm1, %xmm1
cvtsi2sd %ebp, %xmm1
movapd %xmm0, %xmm4
movapd %xmm0, %xmm2
movsd 0xbb7(%rip), %xmm3      # 0x4340
mulsd %xmm3, %xmm2
divsd %xmm1, %xmm2
movapd %xmm1, %xmm0
divsd %xmm3, %xmm0
mulsd 0x96f(%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 0x370f
movq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0x1120
testl %eax, %eax
je 0x36ed
leal -0x1(%r14), %ebx
movq %r12, %rdi
callq 0x1090
leaq 0x397b(%rip), %r15      # 0x7170
movq (%r15), %r12
movq 0x39c9(%rip), %rax      # 0x71c8
leaq 0x20(%rsp), %rdx
movq %rax, (%rdx)
movq 0x39c2(%rip), %rax      # 0x71d0
movq %rax, 0x8(%rdx)
leaq -0x32a(%rip), %rcx      # 0x34ef
leaq -0x30c(%rip), %r8       # 0x3514
xorl %edi, %edi
movl %ebx, %esi
callq 0x311c
movq %r12, (%r15)
cmpl $0x1, %r14d
movq 0x10(%rsp), %r12
movq 0x8(%rsp), %r13
jb 0x386f
leaq 0xb93(%rip), %r14       # 0x43d6
xorl %ebx, %ebx
movq 0x397c(%rip), %rax      # 0x71c8
movq (%rax,%rbx,8), %rdx
movq 0x3979(%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 0x3845
xorl %eax, %eax
callq 0x26ec
movq %rax, %xmm1
punpckldq 0xacd(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xad5(%rip), %xmm1      # 0x4360
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
mulsd 0xaa9(%rip), %xmm0      # 0x4348
movq (%r15), %rax
shrq $0x3, %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
mulq %rcx
shrq $0x4, %rdx
leaq 0xb1f(%rip), %rsi       # 0x43dd
movq %r12, %rdi
movb $0x1, %al
callq 0x1110
xorl %eax, %eax
callq 0x23fb
leaq 0xb13(%rip), %rdi       # 0x43e9
movb $0x1, %al
callq 0x10b0
xorl %eax, %eax
callq 0x261e
movq %r12, %rdi
callq 0x1090
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq