// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movl $0x0, -0x4(%rbp)
movb $0x0, %al
callq 0x2d10
xorps %xmm0, %xmm0
movsd %xmm0, -0x10(%rbp)
movl $0x426f3c00, %edi       # imm = 0x426F3C00
leaq -0x1bcd(%rip), %rsi     # 0x2fb0
callq 0x2fc0
movl $0x100, %edi            # imm = 0x100
callq 0x32d0
movq %rax, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x11379e0, %edi        # imm = 0x11379E0
callq 0x32d0
movq %rax, 0x3618(%rip)      # 0x81c0
movq 0x3579(%rip), %rdi      # 0x8128
leaq 0x7e5(%rip), %rsi       # 0x539b
callq 0x2e80
movq %rax, -0x28(%rbp)
movq 0x3552(%rip), %rdi      # 0x8118
leaq 0x7d0(%rip), %rsi       # 0x539d
callq 0x2e80
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x2ed0
movl %eax, -0x20(%rbp)
cmpl $0x0, %eax
je 0x4c42
movl -0x20(%rbp), %edi
addl $0x1, %edi
callq 0x32d0
movq %rax, %rdx
movq 0x35c0(%rip), %rax      # 0x81c0
movslq -0x1c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x35b1(%rip), %rax      # 0x81c0
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
movl -0x1c(%rbp), %esi
leaq 0x76b(%rip), %rdi       # 0x539f
movl $0x5dc, %edx            # imm = 0x5DC
movb $0x0, %al
callq 0x10a0
jmp 0x4bd6
movq -0x30(%rbp), %rdi
callq 0x1070
movslq -0x1c(%rbp), %rax
shlq $0x2, %rax
movl %eax, %edi
callq 0x32d0
movq %rax, 0x3567(%rip)      # 0x81c8
movslq -0x1c(%rbp), %rax
shlq $0x2, %rax
movl %eax, %edi
callq 0x32d0
movq %rax, 0x3539(%rip)      # 0x81b0
movl $0x100, %edi            # imm = 0x100
callq 0x32d0
movq %rax, 0x3530(%rip)      # 0x81b8
movq 0x3529(%rip), %rdi      # 0x81b8
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x10b0
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x4cd9
movq 0x34ff(%rip), %rax      # 0x81b0
movslq -0x34(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movq 0x3505(%rip), %rax      # 0x81c8
movslq -0x34(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x4ca2
leaq 0x6c6(%rip), %rdi       # 0x53a6
movb $0x0, %al
callq 0x10a0
movb $0x0, %al
callq 0x2d60
movl $0x0, -0x38(%rbp)
movq 0x3424(%rip), %rdi      # 0x8120
leaq 0x69a(%rip), %rsi       # 0x539d
callq 0x2e80
movq %rax, -0x30(%rbp)
movl $0x20000000, -0x3c(%rbp) # imm = 0x20000000
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 0x4d39
jmp 0x4e72
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 0x4e6d
movl $0xff, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
jle 0x4da0
movq 0x343d(%rip), %rax      # 0x81b8
movl -0x54(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq 0x342a(%rip), %rax      # 0x81b8
movslq -0x54(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x54(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x4d6e
movq -0x50(%rbp), %rax
movb (%rax), %cl
movq 0x340b(%rip), %rax      # 0x81b8
movb %cl, (%rax)
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x4dda
movq -0x50(%rbp), %rax
movb (%rax), %al
movl -0x58(%rbp), %esi
movsbl %al, %edi
callq 0x4980
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x4db6
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 0x525(%rip), %xmm1      # 0x5328
ucomisd %xmm1, %xmm0
jbe 0x4e68
movb $0x0, %al
callq 0x2d30
movsd %xmm0, -0x10(%rbp)
movl -0x38(%rbp), %esi
cvtsi2sdl -0x38(%rbp), %xmm0
movsd 0x50b(%rip), %xmm1      # 0x5330
divsd %xmm1, %xmm0
movsd 0x2c7(%rip), %xmm1      # 0x50f8
mulsd %xmm1, %xmm0
movsd -0x10(%rbp), %xmm1
movsd 0x4ee(%rip), %xmm2      # 0x5330
mulsd -0x10(%rbp), %xmm2
cvtsi2sdl -0x38(%rbp), %xmm3
divsd %xmm3, %xmm2
subsd -0x10(%rbp), %xmm2
leaq 0x55d(%rip), %rdi       # 0x53b9
movl $0x36d5393c, %edx       # imm = 0x36D5393C
movb $0x3, %al
callq 0x10a0
jmp 0x4d57
jmp 0x4d1f
movq -0x30(%rbp), %rdi
callq 0x1070
leaq 0x32e6(%rip), %rax      # 0x8168
movq (%rax), %rax
movq %rax, -0x60(%rbp)
movq 0x3330(%rip), %rax      # 0x81c0
movq %rax, -0x70(%rbp)
movq 0x332d(%rip), %rax      # 0x81c8
movq %rax, -0x68(%rbp)
movl -0x1c(%rbp), %esi
subl $0x1, %esi
leaq -0x70(%rbp), %rdx
xorl %edi, %edi
leaq -0x482(%rip), %rcx      # 0x4a30
leaq -0x409(%rip), %r8       # 0x4ab0
callq 0x4400
movq -0x60(%rbp), %rcx
leaq 0x329f(%rip), %rax      # 0x8168
movq %rcx, (%rax)
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x4f15
movq -0x28(%rbp), %rdi
movq 0x32da(%rip), %rax      # 0x81c0
movslq -0x74(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq 0x32d3(%rip), %rax      # 0x81c8
movslq -0x74(%rbp), %rcx
movl (%rax,%rcx,4), %ecx
leaq 0x4d6(%rip), %rsi       # 0x53d9
movb $0x0, %al
callq 0x1100
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x4ed3
movq -0x28(%rbp), %rax
movq %rax, -0x80(%rbp)
xorl %eax, %eax
callq 0x3230
movq -0x80(%rbp), %rdi
movq %rax, %xmm0
movaps 0x40c(%rip), %xmm1      # 0x5340
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x410(%rip), %xmm1      # 0x5350
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x3c9(%rip), %xmm1      # 0x5320
divsd %xmm1, %xmm0
leaq 0x3206(%rip), %rax      # 0x8168
movq (%rax), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
xorl %edx, %edx
divq %rcx
movq %rax, %rdx
leaq 0x467(%rip), %rsi       # 0x53e0
movb $0x1, %al
callq 0x1100
movb $0x0, %al
callq 0x2d30
leaq 0x45e(%rip), %rdi       # 0x53ec
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