// File: /workspace/github_c_makefile_star2_O2/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 %eax, %eax
callq 0x202c
leaq -0x104a(%rip), %rsi     # 0x2138
movl $0x426f3c00, %edi       # imm = 0x426F3C00
callq 0x2141
movl $0x100, %edi            # imm = 0x100
callq 0x232d
movq %rax, %rbx
movl $0x11379e0, %edi        # imm = 0x11379E0
callq 0x232d
movq %rax, 0x401e(%rip)      # 0x71c8
movq 0x3f7f(%rip), %rdi      # 0x7130
leaq 0x11db(%rip), %rsi      # 0x4393
callq 0x20b8
movq %rax, %r13
movq 0x3f59(%rip), %rdi      # 0x7120
leaq 0x11c7(%rip), %rsi      # 0x4395
callq 0x20b8
movq %rax, %r12
pushq $0x1
popq %rbp
leaq 0x11b7(%rip), %r14      # 0x4397
xorl %r15d, %r15d
movq $0x0, 0x8(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x20e1
testl %eax, %eax
je 0x3240
incl %eax
movl %eax, %edi
callq 0x232d
movq 0x3fbd(%rip), %rcx      # 0x71c8
movq %rax, (%rcx,%r15,2)
movq 0x3fb2(%rip), %rax      # 0x71c8
movq (%rax,%r15,2), %rdi
movq %rbx, %rsi
callq 0x1040
movq %r14, %rdi
movl %ebp, %esi
movl $0x5dc, %edx            # imm = 0x5DC
xorl %eax, %eax
callq 0x10b0
decq 0x8(%rsp)
addq $0x4, %r15
incl %ebp
jmp 0x31ec
movq %r12, %rdi
callq 0x1090
movl %r15d, %edi
callq 0x232d
movq %rax, 0x3f79(%rip)      # 0x71d0
movl %r15d, %edi
callq 0x232d
movq %rax, 0x3f52(%rip)      # 0x71b8
movl $0x100, %edi            # imm = 0x100
callq 0x232d
movq %rax, 0x3f49(%rip)      # 0x71c0
xorl %ebx, %ebx
movl $0x100, %edx            # imm = 0x100
movq %rax, %rdi
xorl %esi, %esi
callq 0x10c0
movq 0x3f29(%rip), %rax      # 0x71b8
movq 0x3f3a(%rip), %rcx      # 0x71d0
movq 0x8(%rsp), %r14
negq %r14
cmpq %rbx, %r14
je 0x32b0
andl $0x0, (%rax,%rbx,4)
andl $0x0, (%rcx,%rbx,4)
incq %rbx
jmp 0x329e
movq %r13, 0x18(%rsp)
leaq 0x111b(%rip), %rdi      # 0x43d7
callq 0x1050
xorl %eax, %eax
callq 0x2058
movq 0x3e59(%rip), %rdi      # 0x7128
leaq 0x10bf(%rip), %rsi      # 0x4395
callq 0x20b8
movq %rax, %r15
movl $0x20000000, %edi       # imm = 0x20000000
callq 0x232d
movq %rax, %r13
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %ebx, %ebx
movq %r15, %rdi
callq 0x1120
testl %eax, %eax
jne 0x33e4
movl $0x20000000, %esi       # imm = 0x20000000
movq %r13, %rdi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x1060
movq %r13, %r12
cmpb $0x0, (%r12)
je 0x32f7
movl $0xff, %eax
movl $0xff, %ecx
subq $0x1, %rcx
jb 0x3349
movq 0x3e85(%rip), %rdx      # 0x71c0
movb -0x1(%rdx,%rax), %sil
movb %sil, (%rdx,%rax)
movq %rcx, %rax
jmp 0x332e
movb (%r12), %al
movq 0x3e6c(%rip), %rcx      # 0x71c0
movb %al, (%rcx)
xorl %ebp, %ebp
cmpl %ebp, %r14d
je 0x336d
movsbl (%r12), %edi
movl %ebp, %esi
callq 0x309c
incl %ebp
jmp 0x3358
incq %r12
incl %ebx
xorl %eax, %eax
callq 0x203b
subsd 0x10(%rsp), %xmm0
ucomisd 0xfc1(%rip), %xmm0      # 0x4348
jbe 0x331d
xorl %eax, %eax
callq 0x203b
cvtsi2sd %ebx, %xmm1
movapd %xmm0, %xmm4
movapd %xmm0, %xmm2
movsd 0xfac(%rip), %xmm3      # 0x4350
mulsd %xmm3, %xmm2
divsd %xmm1, %xmm2
movapd %xmm1, %xmm0
divsd %xmm3, %xmm0
mulsd 0xd3c(%rip), %xmm0      # 0x40f8
subsd %xmm4, %xmm2
leaq 0xfd7(%rip), %rdi       # 0x439e
movl %ebx, %esi
movl $0x36d5393c, %edx       # imm = 0x36D5393C
movsd %xmm4, 0x10(%rsp)
movapd %xmm4, %xmm1
movb $0x3, %al
callq 0x10b0
jmp 0x331d
movq %r15, %rdi
callq 0x1090
leaq 0x3d7d(%rip), %r15      # 0x7170
movq (%r15), %rbx
movq 0x3dcb(%rip), %rax      # 0x71c8
leaq 0x20(%rsp), %rdx
movq %rax, (%rdx)
movq 0x3dc4(%rip), %rax      # 0x71d0
movq %rax, 0x8(%rdx)
movq 0x8(%rsp), %rsi
notl %esi
leaq -0x307(%rip), %rcx      # 0x3117
leaq -0x2e9(%rip), %r8       # 0x313c
xorl %edi, %edi
callq 0x2d1c
movq %rbx, (%r15)
leaq 0xf88(%rip), %rbx       # 0x43be
xorl %r12d, %r12d
movq 0x18(%rsp), %r13
cmpq %r12, %r14
je 0x346b
movq 0x3d7e(%rip), %rax      # 0x71c8
movq (%rax,%r12,8), %rdx
movq 0x3d7b(%rip), %rax      # 0x71d0
movl (%rax,%r12,4), %ecx
movq %r13, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1110
incq %r12
jmp 0x343e
xorl %eax, %eax
callq 0x22dd
movq %rax, %xmm1
punpckldq 0xea1(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xea9(%rip), %xmm1      # 0x4330
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
mulsd 0xea5(%rip), %xmm0      # 0x4340
movq (%r15), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
xorl %edx, %edx
divq %rcx
leaq 0xf16(%rip), %rsi       # 0x43c5
movq %r13, %rdi
movq %rax, %rdx
movb $0x1, %al
callq 0x1110
xorl %eax, %eax
callq 0x203b
leaq 0xf07(%rip), %rdi       # 0x43d1
movb $0x1, %al
callq 0x10b0
xorl %eax, %eax
callq 0x2251
movq %r13, %rdi
callq 0x1090
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq