// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %r9d, %r13d
movq %r8, %rbp
movq %rcx, 0x8(%rsp)
movq %rdx, %rbx
movq %rsi, 0x10(%rsp)
movq %rdi, %r14
movq 0x50(%rsp), %r15
xorl %eax, %eax
leaq 0x7418a(%rip), %rcx     # 0x8e8b0
leaq 0x74ae3(%rip), %rdx     # 0x8f210
movq 0x8(%rcx,%rax,8), %rsi
movb (%rsi), %r8b
testb %r8b, %r8b
je 0x1a760
incq %rsi
movl $0x1505, %edi           # imm = 0x1505
movl %edi, %r9d
shll $0x5, %r9d
addl %edi, %r9d
movsbl %r8b, %edi
addl %r9d, %edi
movb (%rsi), %r8b
incq %rsi
testb %r8b, %r8b
jne 0x1a742
jmp 0x1a765
movl $0x1505, %edi           # imm = 0x1505
movl %edi, (%rdx,%rax,4)
incq %rax
cmpq $0x15, %rax
jne 0x1a72d
movabsq $0x100000000, %r12      # imm = 0x100000000
callq 0x3060
movl $0x0, (%rax)
movq %r15, 0x268(%r14)
movq 0x10(%rsp), %r15
movq %r15, 0x258(%r14)
movb $0x0, 0x8(%r14)
movb %r13b, 0x9(%r14)
movq $0x0, 0x10(%r14)
testq %rbp, %rbp
je 0x1a7f5
movb $0x1, 0x8(%r14)
movq %rbp, %rdi
callq 0x3130
movq %rax, %r13
shlq $0x20, %rax
leaq (%rax,%r12), %rsi
sarq $0x20, %rsi
movq %r15, %rdi
callq 0xcca8
movq %rax, 0x10(%r14)
movq %rax, %rdi
movq %rbp, %rsi
callq 0x30a0
movq 0x10(%r14), %rax
movslq %r13d, %rcx
movb $0x0, (%rax,%rcx)
movq %r15, %rbp
jmp 0x1a8c9
leaq 0x5dbb4(%rip), %rsi     # 0x783b0
movq 0x8(%rsp), %rdi
callq 0x3320
movq %rax, %rbp
testq %rax, %rax
jne 0x1a829
movq 0x268(%r14), %rcx
movq 0x20(%rcx), %rdi
leaq 0x5db93(%rip), %rsi     # 0x783b3
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq *(%rcx)
movq %rbp, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x32f0
movq %rbp, %rdi
callq 0x3230
movq %rax, %r13
movq %rbp, %rdi
callq 0x3160
movq 0x258(%r14), %rdi
movq %r13, %rsi
shlq $0x20, %rsi
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %rsi
sarq $0x20, %rsi
callq 0xcca8
movq %rax, 0x10(%r14)
movslq %r13d, %r15
movl $0x1, %esi
movq %rax, %rdi
movq %r15, %rdx
movq %rbp, %rcx
callq 0x30d0
cmpl %r15d, %eax
je 0x1a8a9
movq 0x268(%r14), %rcx
movq 0x20(%rcx), %rdi
leaq 0x5db27(%rip), %rsi     # 0x783c7
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq *(%rcx)
movq 0x10(%r14), %rax
movb $0x0, (%rax,%r15)
movq %rbp, %rdi
callq 0x3120
movabsq $0x100000000, %r12      # imm = 0x100000000
movq 0x10(%rsp), %rbp
movq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0x3130
leaq 0x1(%rax), %rsi
movq %rbp, %rdi
callq 0xcca8
movq %rax, (%r14)
movq %rax, %rdi
movq %r15, %rsi
callq 0x30a0
movq %r12, 0x18(%r14)
movl $0x1, %ebp
movl %ebp, 0x20(%r14)
leaq 0x28(%r14), %rsi
movl $0x0, 0x28(%r14)
movq $0x0, 0x30(%r14)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x138(%r14)
leaq 0x140(%r14), %rdi
movl $0x118, %edx            # imm = 0x118
callq 0x3260
movq %rbx, 0x260(%r14)
movq (%r14), %rax
movq %rax, (%rbx)
leaq 0x58a66(%rip), %rax     # 0x733ad
movq %rax, 0x8(%rbx)
movl %ebp, 0x10(%rbx)
movl %r13d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq