// File: /workspace/github_c_makefile_star2_O0/vtereshkov[P]umka-lang/src/umka_lexer.c
// Function: lexInit
int lexInit(Lexer *lex, Storage *storage, DebugInfo *debug, const char *fileName, const char *sourceString, bool trusted, Error *error)
{
    // Fill keyword hashes
    for (int i = 0; i < NUM_KEYWORDS; i++)
        keywordHash[i] = hash(spelling[TOK_BREAK + i]);

    // Initialize lexer
    errno = 0;

    lex->error = error;
    lex->storage = storage;
    lex->hasSourceString = false;
    lex->trusted = trusted;
    lex->buf = NULL;
    int bufLen = 0;

    if (sourceString)
    {
        // Read source from a string buffer
        lex->hasSourceString = true;

        bufLen = strlen(sourceString);
        lex->buf = storageAdd(lex->storage, bufLen + 1);
        strcpy(lex->buf, sourceString);
        lex->buf[bufLen] = 0;
    }
    else
    {
        // Read source from a file
        FILE *file = fopen(fileName, "rb");
        if (!file)
            lex->error->handler(lex->error->context, "Cannot open file %s", fileName);

        fseek(file, 0, SEEK_END);
        bufLen = ftell(file);
        rewind(file);

        lex->buf = storageAdd(lex->storage, bufLen + 1);
        if ((int)fread(lex->buf, 1, bufLen, file) != bufLen)
            lex->error->handler(lex->error->context, "Cannot read file %s", fileName);

        lex->buf[bufLen] = 0;
        fclose(file);
    }

    lex->fileName = storageAdd(storage, strlen(fileName) + 1);
    strcpy(lex->fileName, fileName);

    lex->bufPos = 0;
    lex->line = 1;
    lex->pos = 1;
    lex->tok.kind = TOK_NONE;
    lex->tok.strVal = NULL;
    lex->tok.line = lex->line;
    lex->tok.pos = lex->pos;
    lex->prevTok = lex->tok;
    lex->debug = debug;
    lex->debug->fileName = lex->fileName;
    lex->debug->fnName = "<unknown>";
    lex->debug->line = lex->line;

    return bufLen;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movb %r9b, %al
movq 0x10(%rbp), %r9
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
andb $0x1, %al
movb %al, -0x29(%rbp)
movl $0x0, -0x30(%rbp)
cmpl $0x15, -0x30(%rbp)
jge 0x1aa49
movl -0x30(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rcx
leaq 0x1cb81b(%rip), %rax    # 0x1e6240
movq (%rax,%rcx,8), %rdi
callq 0x1ad00
movl %eax, %edx
movslq -0x30(%rbp), %rcx
leaq 0x1cbe65(%rip), %rax    # 0x1e68a0
movl %edx, (%rax,%rcx,4)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x1aa0f
callq 0x3060
movl $0x0, (%rax)
movq 0x10(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x268(%rax)
movq -0x10(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x258(%rax)
movq -0x8(%rbp), %rax
movb $0x0, 0x8(%rax)
movb -0x29(%rbp), %cl
movq -0x8(%rbp), %rax
andb $0x1, %cl
movb %cl, 0x9(%rax)
movq -0x8(%rbp), %rax
movq $0x0, 0x10(%rax)
movl $0x0, -0x34(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x1aaff
movq -0x8(%rbp), %rax
movb $0x1, 0x8(%rax)
movq -0x28(%rbp), %rdi
callq 0x3120
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rax
movq 0x258(%rax), %rdi
movl -0x34(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x4b20
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x28(%rbp), %rsi
callq 0x30a0
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x34(%rbp), %rcx
movb $0x0, (%rax,%rcx)
jmp 0x1abf5
movq -0x20(%rbp), %rdi
leaq 0x1beb3e(%rip), %rsi    # 0x1d9648
callq 0x3320
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x1ab46
movq -0x8(%rbp), %rax
movq 0x268(%rax), %rax
movq (%rax), %rcx
movq -0x8(%rbp), %rax
movq 0x268(%rax), %rax
movq 0x20(%rax), %rdi
movq -0x20(%rbp), %rdx
leaq 0x1beb09(%rip), %rsi    # 0x1d964b
movb $0x0, %al
callq *%rcx
movq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x32f0
movq -0x40(%rbp), %rdi
callq 0x3240
movl %eax, -0x34(%rbp)
movq -0x40(%rbp), %rdi
callq 0x3160
movq -0x8(%rbp), %rax
movq 0x258(%rax), %rdi
movl -0x34(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x4b20
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdi
movslq -0x34(%rbp), %rdx
movq -0x40(%rbp), %rcx
movl $0x1, %esi
callq 0x30c0
cmpl -0x34(%rbp), %eax
je 0x1abdc
movq -0x8(%rbp), %rax
movq 0x268(%rax), %rax
movq (%rax), %rcx
movq -0x8(%rbp), %rax
movq 0x268(%rax), %rax
movq 0x20(%rax), %rdi
movq -0x20(%rbp), %rdx
leaq 0x1bea87(%rip), %rsi    # 0x1d965f
movb $0x0, %al
callq *%rcx
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x34(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x40(%rbp), %rdi
callq 0x3110
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3120
movq -0x48(%rbp), %rdi
movq %rax, %rsi
addq $0x1, %rsi
callq 0x4b20
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x30a0
movq -0x8(%rbp), %rax
movl $0x0, 0x18(%rax)
movq -0x8(%rbp), %rax
movl $0x1, 0x1c(%rax)
movq -0x8(%rbp), %rax
movl $0x1, 0x20(%rax)
movq -0x8(%rbp), %rax
movl $0x0, 0x28(%rax)
movq -0x8(%rbp), %rax
movq $0x0, 0x30(%rax)
movq -0x8(%rbp), %rax
movl 0x1c(%rax), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x138(%rax)
movq -0x8(%rbp), %rax
movl 0x20(%rax), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x13c(%rax)
movq -0x8(%rbp), %rdi
addq $0x140, %rdi            # imm = 0x140
movq -0x8(%rbp), %rsi
addq $0x28, %rsi
movl $0x118, %edx            # imm = 0x118
callq 0x3270
movq -0x18(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x260(%rax)
movq -0x8(%rbp), %rax
movq (%rax), %rcx
movq -0x8(%rbp), %rax
movq 0x260(%rax), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
movq 0x260(%rax), %rax
leaq 0x1b96d8(%rip), %rcx    # 0x1d43b5
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movl 0x1c(%rax), %ecx
movq -0x8(%rbp), %rax
movq 0x260(%rax), %rax
movl %ecx, 0x10(%rax)
movl -0x34(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nop