// File: /workspace/github_c_makefile_star2_O1/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 0x34b0
movq %rdi, %rbx
leaq 0x11140(%rip), %rax     # 0x143c3
movq %rsi, %rdi
movq %rax, %rsi
callq 0x2230
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x34c1
movq $0x0, 0x8(%rsp)
movq (%rbx), %rdi
incq %rdi
callq 0x22b0
testq %rax, %rax
je 0x34c6
movq %rax, %r15
cmpq $0x0, 0x8(%rbx)
jne 0x32f6
movl $0x800, %edi            # imm = 0x800
callq 0x22b0
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x34d0
xorl %r14d, %r14d
callq 0xfa6c
movq 0x8(%rbx), %rcx
movq %rax, (%rcx,%r14,8)
incq %r14
cmpq $0x100, %r14            # imm = 0x100
jne 0x32dd
cmpq $0x0, 0x10(%rbx)
jne 0x3306
callq 0xfa6c
movq %rax, 0x10(%rbx)
movq $0x0, 0x18(%rsp)
movq (%rbx), %rsi
addq $0x2, %rsi
movl $0x1, %edi
callq 0x2110
testq %rax, %rax
je 0x34cb
movq %rax, %r12
leaq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x2290
cmpq $-0x1, %rax
je 0x3485
movq %r12, %rax
incq %rax
movq %rax, 0x20(%rsp)
movq %r15, %rbp
incq %rbp
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x20c0
movb $0x0, -0x1(%r14,%rax)
movq 0x8(%rsp), %rax
movb (%rax), %r14b
movq (%rbx), %rcx
testq %rcx, %rcx
setne %sil
je 0x33b9
movb 0x1(%rax), %dl
testb %dl, %dl
je 0x33aa
leaq -0x1(%rcx), %rsi
xorl %edi, %edi
movb %dl, (%r15,%rdi)
cmpq %rdi, %rsi
je 0x33b9
movb 0x2(%rax,%rdi), %dl
incq %rdi
testb %dl, %dl
jne 0x338f
cmpq %rcx, %rdi
setb %sil
movb $0x0, (%r15,%rcx)
testb %sil, %sil
je 0x33be
jmp 0x3467
movb $0x0, (%r15,%rcx)
movb (%rax), %al
movb %al, (%r12)
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x21c0
movq 0x10(%rbx), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0xfad0
movq (%rbx), %rax
cmpb $0x0, -0x1(%r15,%rax)
setne %cl
testb %r14b, %r14b
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x3467
movl $0x1, %r13d
addq 0x8(%rsp), %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 0xfad0
incq 0x18(%rbx)
movq (%rbx), %r14
cmpq $0x2, %r14
jb 0x343e
leaq -0x1(%r14), %rdx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x23a0
movq 0x8(%rsp), %rcx
leaq (%rcx,%r13), %rax
movb (%r14,%rax), %al
movb %al, -0x1(%r15,%r14)
movq (%rbx), %rax
cmpb $0x0, -0x1(%r15,%rax)
je 0x3467
movb (%rcx,%r13), %r14b
incq %r13
testb %r14b, %r14b
jne 0x33fe
leaq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x2290
cmpq $-0x1, %rax
jne 0x335b
movq 0x10(%rsp), %rdi
callq 0x21f0
movq 0x8(%rsp), %rdi
callq 0x2240
movq %r12, %rdi
callq 0x2240
movq %r15, %rdi
callq 0x2240
movl $0x1, %eax
jmp 0x34b2
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