// File: /workspace/github_c_makefile_star2_O2/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, 0x4(%rsp)
movq %r8, %rbp
movq %rcx, 0x8(%rsp)
movq %rdx, 0x10(%rsp)
movq %rsi, %r13
movq %rdi, %r14
xorl %r15d, %r15d
leaq 0x69b74(%rip), %r12     # 0x7a8b0
leaq 0x6a4cd(%rip), %rbx     # 0x7b210
cmpq $0x54, %r15
je 0x10d5d
movq 0x8(%r12,%r15,2), %rdi
callq 0x10f33
movl %eax, (%r15,%rbx)
addq $0x4, %r15
jmp 0x10d43
movabsq $0x100000000, %r15      # imm = 0x100000000
callq 0x3060
andl $0x0, (%rax)
movq 0x50(%rsp), %rax
movq %rax, 0x268(%r14)
movq %r13, %r12
movq %r13, 0x258(%r14)
movb $0x0, 0x8(%r14)
movl 0x4(%rsp), %eax
movb %al, 0x9(%r14)
andq $0x0, 0x10(%r14)
testq %rbp, %rbp
je 0x10de4
movb $0x1, 0x8(%r14)
movq %rbp, %rdi
callq 0x3130
movq %rax, %r13
shlq $0x20, %rax
leaq (%rax,%r15), %rsi
sarq $0x20, %rsi
movq %r12, %rdi
callq 0x4144
movq %rax, 0x10(%r14)
movq %rax, %rdi
movq %rbp, %rsi
callq 0x30a0
movq 0x10(%r14), %rax
movslq %r13d, %rcx
movb $0x0, (%rax,%rcx)
movq 0x8(%rsp), %rbx
jmp 0x10ea4
leaq 0x5a565(%rip), %rsi     # 0x6b350
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x3320
movq %rax, %rbp
testq %rax, %rax
jne 0x10e19
movq 0x268(%r14), %rcx
movq 0x20(%rcx), %rdi
leaq 0x5a541(%rip), %rsi     # 0x6b353
movq %rbx, %rdx
xorl %eax, %eax
callq *(%rcx)
pushq $0x2
popq %rdx
movq %rbp, %rdi
xorl %esi, %esi
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
addq %r15, %rsi
sarq $0x20, %rsi
callq 0x4144
movq %rax, 0x10(%r14)
movslq %r13d, %r15
pushq $0x1
popq %rsi
movq %rax, %rdi
movq %r15, %rdx
movq %rbp, %rcx
callq 0x30d0
cmpl %r15d, %eax
je 0x10e89
movq 0x268(%r14), %rcx
movq 0x20(%rcx), %rdi
leaq 0x5a4e5(%rip), %rsi     # 0x6b367
movq %rbx, %rdx
xorl %eax, %eax
callq *(%rcx)
movq 0x10(%r14), %rax
movb $0x0, (%rax,%r15)
movq %rbp, %rdi
callq 0x3120
movabsq $0x100000000, %r15      # imm = 0x100000000
movq %rbx, %rdi
callq 0x3130
leaq 0x1(%rax), %rsi
movq %r12, %rdi
callq 0x4144
movq %rax, (%r14)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x30a0
movq %r15, 0x18(%r14)
pushq $0x1
popq %rbx
movl %ebx, 0x20(%r14)
andl $0x0, 0x28(%r14)
andq $0x0, 0x30(%r14)
leaq 0x28(%r14), %rsi
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x138(%r14)
leaq 0x140(%r14), %rdi
movl $0x118, %edx            # imm = 0x118
callq 0x3260
movq 0x10(%rsp), %rcx
movq %rcx, 0x260(%r14)
movq (%r14), %rax
movq %rax, (%rcx)
leaq 0x55493(%rip), %rax     # 0x663ad
movq %rax, 0x8(%rcx)
movl %ebx, 0x10(%rcx)
movl %r13d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq