// File: /workspace/github_c_makefile_star2_O3/Ziddykins[P]Dawn/src/analyzer.c
// Function: analyze
int analyze(Analyzer a, char const * fn) {
    if(!a)
        return 0;
    struct analyzer * ca = a;
    FILE * file;
    CALLEXIT(!(file = fopen(fn, "r")))

    char * word = 0; //malloc(MAX_WORD_LEN)
    char * prediction;
    CALLEXIT(!(prediction = malloc(ca->markov_tier+1))) //next n characters, \0 terminated
    if(!ca->tlists) {
        CALLEXIT(!(ca->tlists = malloc(CHAR_LEN * sizeof *ca->tlists)))
        for(size_t i = 0; i < CHAR_LEN; i++) {
            ca->tlists[i] = init_token_list();
        }
    }
    if(!ca->start) {
        ca->start = init_token_list();
    }
    size_t len = 0;
    char * start;
    CALLEXIT(!(start = calloc(1, ca->markov_tier+2)))
    while(getline(&word, &len, file) != -1) { //fscanf(file, "%"MAX_WORD_LEN_LITERAL"s", word) != EOF
        word[strlen(word)-1] = '\0';
        char cur_char = word[0];

        //initialize prediction
        size_t i;
        for(i = 0; i < ca->markov_tier && word[i+1] != '\0'; i++) {
            prediction[i] = word[i+1];
        }
        prediction[ca->markov_tier] = '\0';

        //word too short?
        if(i < ca->markov_tier) {
            continue;
        }
        start[0] = word[0];
        strcpy(start+1, prediction);
        inc_token(ca->start, start, 0);

        for(i = 1; prediction[ca->markov_tier-1] != '\0' && cur_char != '\0'; i++) {
            //add markov prediction
            int end = 0;

            if(word[i+ca->markov_tier] == '\0') {
                end = 1;
            }
            inc_token(ca->tlists[(unsigned char)cur_char], prediction, end);
            ca->tokens++;
            //shift prediction
            for(size_t pos = 1; pos < ca->markov_tier; pos++) {
                prediction[pos-1] = prediction[pos];
            }
            prediction[ca->markov_tier-1] = word[i+ca->markov_tier];

            //get next character
            cur_char = word[i];
        }
    }
    fclose(file);
    free(word);
    free(start);
    free(prediction);
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
testq %rdi, %rdi
je 0x349a
movq %rdi, %rbx
leaq 0x1116c(%rip), %rax     # 0x143f3
movq %rsi, %rdi
movq %rax, %rsi
callq 0x2230
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x34ab
movq $0x0, (%rsp)
movq (%rbx), %rdi
incq %rdi
callq 0x22b0
testq %rax, %rax
je 0x34b0
movq %rax, %r15
cmpq $0x0, 0x8(%rbx)
jne 0x32f9
movl $0x800, %edi            # imm = 0x800
callq 0x22b0
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x34ba
xorl %r14d, %r14d
callq 0xfd24
movq 0x8(%rbx), %rcx
movq %rax, (%rcx,%r14,8)
incq %r14
cmpq $0x100, %r14            # imm = 0x100
jne 0x32e0
cmpq $0x0, 0x10(%rbx)
jne 0x3309
callq 0xfd24
movq %rax, 0x10(%rbx)
movq $0x0, 0x18(%rsp)
movq (%rbx), %rsi
addq $0x2, %rsi
movl $0x1, %edi
callq 0x2110
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x34b5
movq %rsp, %rdi
leaq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x2290
cmpq $-0x1, %rax
je 0x346e
movq 0x8(%rsp), %rax
incq %rax
movq %rax, 0x20(%rsp)
movq %r15, %rbp
incq %rbp
movq (%rsp), %r14
movq %r14, %rdi
callq 0x20c0
movb $0x0, -0x1(%r14,%rax)
movq (%rsp), %rax
movb (%rax), %r14b
movq (%rbx), %rcx
testq %rcx, %rcx
je 0x339d
xorl %edx, %edx
movb 0x1(%rax,%rdx), %sil
testb %sil, %sil
je 0x344d
movb %sil, (%r15,%rdx)
incq %rdx
cmpq %rdx, %rcx
jne 0x3383
movb $0x0, (%r15,%rcx)
movb (%rax), %al
movq 0x8(%rsp), %r12
movb %al, (%r12)
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x21c0
movq 0x10(%rbx), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0xfd7f
movq (%rbx), %r12
cmpb $0x0, -0x1(%r15,%r12)
setne %al
testb %r14b, %r14b
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x3452
movl $0x1, %r13d
movq (%rsp), %rax
addq %r12, %rax
xorl %edx, %edx
cmpb $0x0, (%r13,%rax)
sete %dl
movq 0x8(%rbx), %rax
movzbl %r14b, %ecx
movq (%rax,%rcx,8), %rdi
movq %r15, %rsi
callq 0xfd7f
incq 0x18(%rbx)
movq (%rbx), %r12
cmpq $0x2, %r12
jb 0x342a
leaq -0x1(%r12), %rdx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x23a0
movq (%rsp), %rax
leaq (%rax,%r13), %rcx
movb (%r12,%rcx), %cl
movb %cl, -0x1(%r15,%r12)
testb %cl, %cl
je 0x3452
movb (%rax,%r13), %r14b
incq %r13
testb %r14b, %r14b
jne 0x33eb
jmp 0x3452
movb $0x0, (%r15,%rcx)
movq %rsp, %rdi
leaq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x2290
cmpq $-0x1, %rax
jne 0x3360
movq 0x10(%rsp), %rdi
callq 0x21f0
movq (%rsp), %rdi
callq 0x2240
movq 0x8(%rsp), %rdi
callq 0x2240
movq %r15, %rdi
callq 0x2240
movl $0x1, %eax
jmp 0x349c
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2504
callq 0x24ef
callq 0x24c5
callq 0x24da