// File: /workspace/github_c_makefile_star2_O2/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 0x27f5
movq %rdi, %rbx
leaq 0x10d76(%rip), %rax     # 0x133c3
movq %rsi, %rdi
movq %rax, %rsi
callq 0x2240
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x283b
andq $0x0, 0x8(%rsp)
movq (%rbx), %rdi
incq %rdi
callq 0x22c0
testq %rax, %rax
je 0x2844
movq %rax, %r15
cmpq $0x0, 0x8(%rbx)
je 0x2806
cmpq $0x0, 0x10(%rbx)
jne 0x269e
callq 0xea7c
movq %rax, 0x10(%rbx)
andq $0x0, 0x20(%rsp)
movq (%rbx), %rsi
addq $0x2, %rsi
pushq $0x1
popq %r14
movq %r14, %rdi
callq 0x2110
testq %rax, %rax
je 0x284d
movq %rax, %r12
incq %rax
movq %rax, 0x18(%rsp)
leaq 0x8(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x22a0
cmpq $-0x1, %rax
je 0x27cc
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
callq 0x20c0
movb $0x0, -0x1(%rbp,%rax)
movq 0x8(%rsp), %rax
movb (%rax), %bpl
movq (%rbx), %rcx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x2724
movb 0x1(%rax,%rdx), %sil
testb %sil, %sil
je 0x27c2
movb %sil, (%r15,%rdx)
incq %rdx
jmp 0x2708
movb $0x0, (%r15,%rcx)
movb (%rax), %al
movb %al, (%r12)
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x21c0
movq 0x10(%rbx), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0xeadf
movq (%rbx), %rax
movb -0x1(%r15,%rax), %cl
movq %r14, %r13
testb %cl, %cl
je 0x26cb
testb %bpl, %bpl
je 0x26cb
movq 0x8(%rsp), %rcx
addq %r13, %rcx
xorl %edx, %edx
cmpb $0x0, (%rax,%rcx)
sete %dl
movq 0x8(%rbx), %rax
movzbl %bpl, %ecx
movq (%rax,%rcx,8), %rdi
movq %r15, %rsi
callq 0xeadf
incq 0x18(%rbx)
movq (%rbx), %rax
movq %r14, %rcx
cmpq %rax, %rcx
jae 0x27a8
movb (%r15,%rcx), %dl
movb %dl, -0x1(%r15,%rcx)
incq %rcx
jmp 0x2795
movq 0x8(%rsp), %rdx
leaq (%rdx,%r13), %rcx
movb (%rax,%rcx), %cl
movb %cl, -0x1(%r15,%rax)
movb (%rdx,%r13), %bpl
incq %r13
jmp 0x2755
movb $0x0, (%r15,%rcx)
jmp 0x26cb
movq 0x10(%rsp), %rdi
callq 0x21f0
movq 0x8(%rsp), %rdi
callq 0x2250
movq %r12, %rdi
callq 0x2250
movq %r15, %rdi
callq 0x2250
pushq $0x1
popq %rax
jmp 0x27f7
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x800, %edi            # imm = 0x800
callq 0x22c0
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x2856
xorl %r14d, %r14d
cmpq $0x100, %r14            # imm = 0x100
je 0x268e
callq 0xea7c
movq 0x8(%rbx), %rcx
movq %rax, (%rcx,%r14,8)
incq %r14
jmp 0x281c
leaq 0xd82d(%rip), %rdi      # 0x1006f
jmp 0x285d
leaq 0xd863(%rip), %rdi      # 0x100ae
jmp 0x285d
leaq 0xd906(%rip), %rdi      # 0x1015a
jmp 0x285d
leaq 0xd8a1(%rip), %rdi      # 0x100fe
callq 0x2190
pushq $0x1
popq %rdi
callq 0x2270