// File: /workspace/github_c_makefile_star2_O2/gregtour[P]minimal-compiler/lexer.c
// Function: LexSource
L_TOKEN* LexSource(const char*    file,
                   char**         stripped,
                   GRAMMAR_TABLE  table)
{
    FILE* input;
    char* buffer;
    char* format;
    int*  lineNumbers;
    int   line;
    int   size;
    int   read;
    int   i, j;
    int   a, b;

    L_TOKEN* start = NULL;
    L_TOKEN* end = NULL;

    // read source file
    // printf("Loading source file from %s...\n", file);
    input = fopen(file, "rb");
    if (input == 0) {
        printf("Could not open source file.\n");
        return 0;
    }
    fseek(input, 0, SEEK_END);
    size = ftell(input);
    rewind(input);
    buffer = (char*)malloc(size+1);
    read = fread(buffer, 1, size, input);
    buffer[size] = 0;
    fclose(input);
    
    // printf("Read %i of %i bytes.\n", read, size);

    if (size == 0) {
        printf("Unable to read source file.\n");
        return 0;
    }
    
    // count lines
    lineNumbers = (int*)malloc((size+1)*sizeof(int));
    line = 0;
    for (i = 0; i <= size; i++)
    {
        lineNumbers[i] = line;
        if (buffer[i] == '\n')
            line++;
    }

    // strip clean
    format = (char*)malloc(size+1);
    j = 0;
    for (i = 0; i < size; i++)
    {
        if (buffer[i] == '/' &&
            buffer[i+1] == '/')
        {
            // inline comments
            while (i < size && buffer[i] != '\n') i++;
            i--;
        }
        else if (buffer[i] == '/' &&
                 buffer[i+1] == '*')
        {
            // multiline comments
            i++; i++;
            while (i < size - 1 &&
                   (buffer[i] != '*' ||
                    buffer[i+1] != '/'))
            {
                i++;
            }
            i++;
        }
#ifdef IGNORE_MACROS
        else if (buffer[i] == '#')
        {
            while (i < size && buffer[i] != '\n') i++;
            i--;
        }
#endif
        else if (isSpace(buffer[i]))
        {
            // whitespace
            format[j] = ' ';
            lineNumbers[j] = lineNumbers[i];

            while (i < size && isSpace(buffer[i]))
            {
                if (buffer[i] == '\n') {
                    format[j] = '\n';
                    lineNumbers[j] = lineNumbers[i];
                }
                i++;
            }
            i--;
            j++;
        }
        else if (buffer[i] == '"' ||
                 buffer[i] == '\'')
        {
            char quote = buffer[i];
            // string
            format[j++] = buffer[i++];
            while (i < size - 1 && buffer[i] != quote)
            {
                if (buffer[i] == '\\')
                {
                    lineNumbers[j] = lineNumbers[i];
                    format[j++] = buffer[i++];
                }
                lineNumbers[j] = lineNumbers[i];
                format[j++] = buffer[i++];
            }
            lineNumbers[j] = lineNumbers[i];
            format[j++] = buffer[i];
        }
        else
        {
            // character
            lineNumbers[j] = lineNumbers[i];
            format[j] = buffer[i];
            j++;
        }
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %rbx
andq $0x0, -0x60(%rbp)
andq $0x0, -0x58(%rbp)
leaq 0x321e(%rip), %rsi      # 0x7142
callq 0x2120
testq %rax, %rax
je 0x4573
movq %rax, %r15
movq %rbx, -0x68(%rbp)
pushq $0x2
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x2110
movq %r15, %rdi
callq 0x20f0
movq %rax, %r13
movq %r15, %rdi
callq 0x20a0
movq %r13, %rax
shlq $0x20, %rax
movabsq $0x100000000, %r12      # imm = 0x100000000
addq %rax, %r12
movq %r12, %rdi
sarq $0x20, %rdi
movq %rdi, -0x38(%rbp)
callq 0x2100
movq %rax, %rbx
movslq %r13d, %r14
pushq $0x1
popq %rsi
movq %rax, %rdi
movq %r14, %rdx
movq %r15, %rcx
callq 0x2060
movb $0x0, (%rbx,%r14)
movq %r15, %rdi
callq 0x2070
testl %r14d, %r14d
je 0x457c
sarq $0x1e, %r12
movq %r12, %rdi
callq 0x2100
movslq %r13d, %r14
xorl %edx, %edx
xorl %r12d, %r12d
cmpq %r14, %rdx
jg 0x3fd8
xorl %ecx, %ecx
cmpb $0xa, (%rbx,%rdx)
movl %r12d, (%rax,%rdx,4)
sete %cl
addl %ecx, %r12d
incq %rdx
jmp 0x3fbe
movq -0x38(%rbp), %rdi
movq %rax, -0x40(%rbp)
callq 0x2100
movq -0x40(%rbp), %r11
leal -0x1(%r13), %edx
movslq %edx, %rcx
xorl %esi, %esi
xorl %r15d, %r15d
movq %rax, -0x38(%rbp)
movl %r15d, -0x2c(%rbp)
movslq %r15d, %rax
movq %rax, -0x48(%rbp)
cmpl %r13d, %esi
jge 0x41f4
movslq %esi, %rdi
movb (%rbx,%rdi), %r8b
movzbl %r8b, %r9d
leal -0x20(%r9), %r10d
cmpl $0x7, %r10d
jbe 0x4087
leal -0x9(%r9), %r10d
cmpl $0x2, %r10d
movq -0x38(%rbp), %rax
jb 0x410f
cmpl $0xd, %r9d
je 0x410f
cmpl $0x2f, %r9d
jne 0x415c
movzbl 0x1(%rbx,%rdi), %r9d
cmpl $0x2a, %r9d
je 0x41c6
cmpl $0x2f, %r9d
jne 0x415c
movq %r14, %r8
subq %rdi, %r8
addq %rbx, %rdi
xorl %r9d, %r9d
cmpq %r9, %r8
je 0x41b1
cmpb $0xa, (%rdi,%r9)
je 0x41b6
incq %r9
jmp 0x406e
leaq 0x343e(%rip), %rax      # 0x74cc
movslq (%rax,%r10,4), %r9
addq %rax, %r9
movq -0x38(%rbp), %rax
jmpq *%r9
movl %r8d, %r9d
movl %esi, %edi
movl -0x2c(%rbp), %r15d
movq -0x48(%rbp), %r10
movb %r9b, (%rax,%r10)
movslq %r15d, %r10
movslq %edi, %rdi
leaq 0x1(%rdi), %rsi
movb 0x1(%rbx,%rdi), %r9b
cmpl %edx, %esi
jge 0x4194
cmpb %r8b, %r9b
je 0x4191
cmpb $0x5c, %r9b
jne 0x40f9
movl 0x4(%r11,%rdi,4), %esi
movl %esi, 0x4(%r11,%r10,4)
addl $0x2, %r15d
movq -0x38(%rbp), %rax
movb $0x5c, 0x1(%rax,%r10)
movb 0x2(%rbx,%rdi), %r9b
addq $0x2, %rdi
movq %rdi, %rsi
jmp 0x4102
incl %r15d
movl %esi, %edi
movq -0x38(%rbp), %rax
movl (%r11,%rsi,4), %esi
movslq %r15d, %r10
movl %esi, (%r11,%r10,4)
jmp 0x40a9
movl -0x2c(%rbp), %r15d
movslq %r15d, %rsi
movb $0x20, (%rax,%rsi)
movl (%r11,%rdi,4), %r8d
movl %r8d, (%r11,%rsi,4)
cmpq %r14, %rdi
jge 0x4155
movzbl (%rbx,%rdi), %r8d
cmpl $0x9, %r8d
je 0x4150
cmpl $0x20, %r8d
je 0x4150
cmpl $0xd, %r8d
je 0x4150
cmpl $0xa, %r8d
jne 0x41ad
movb $0xa, (%rax,%rsi)
movl (%r11,%rdi,4), %r8d
movl %r8d, (%r11,%rsi,4)
incq %rdi
jmp 0x4122
movl %r13d, %esi
decl %esi
jmp 0x416f
movl (%r11,%rdi,4), %edi
movl -0x2c(%rbp), %r15d
movslq %r15d, %r9
movl %edi, (%r11,%r9,4)
movb %r8b, (%rax,%r9)
incl %r15d
jmp 0x41bf
movq %r14, %r8
subq %rdi, %r8
addq %rbx, %rdi
xorl %r9d, %r9d
cmpq %r9, %r8
je 0x41b1
cmpb $0xa, (%rdi,%r9)
je 0x41b6
incq %r9
jmp 0x4180
movl %r8d, %r9d
movl 0x4(%r11,%rdi,4), %edi
movl %edi, 0x4(%r11,%r10,4)
addl $0x2, %r15d
movq -0x38(%rbp), %rax
movb %r9b, 0x1(%rax,%r10)
jmp 0x41bf
movl %edi, %esi
jmp 0x4158
movl %r13d, %esi
jmp 0x41b9
addl %r9d, %esi
movl -0x2c(%rbp), %r15d
decl %esi
incl %esi
jmp 0x3ff9
addl $0x2, %esi
movslq %esi, %rdi
cmpl %edi, %edx
movl %edi, %esi
cmovgl %edx, %esi
cmpq %rcx, %rdi
jge 0x41ec
cmpb $0x2a, (%rbx,%rdi)
jne 0x41e5
cmpb $0x2f, 0x1(%rbx,%rdi)
je 0x41ea
incq %rdi
jmp 0x41d3
movl %edi, %esi
incl %esi
movl -0x2c(%rbp), %r15d
jmp 0x41bf
movq %rbx, %rdi
callq 0x2030
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %r8
movq -0x48(%rbp), %rdi
movb $0x0, (%r8,%rdi)
xorl %r13d, %r13d
leaq -0x60(%rbp), %r15
cmpl -0x2c(%rbp), %r13d
jge 0x453e
movl %r13d, %esi
movslq %r13d, %r14
movl (%rax,%r14,4), %eax
movq %rax, -0x50(%rbp)
leaq (%r8,%r14), %rbx
movzbl (%r8,%r14), %r12d
movl %r12d, %eax
andb $-0x21, %al
addb $-0x5b, %al
cmpb $-0x1a, %al
jae 0x428f
leal -0x3a(%r12), %eax
cmpb $-0xa, %al
jae 0x42d6
cmpl $0x27, %r12d
je 0x425d
cmpl $0x22, %r12d
jne 0x43ff
leal 0x1(%r13), %eax
movl -0x2c(%rbp), %ecx
cmpl %eax, %ecx
cmovgl %ecx, %eax
incq %r14
movl %r13d, %ebx
movl %ebx, %edx
cmpq %rdi, %r14
jge 0x43c9
leal 0x1(%rdx), %ebx
leaq 0x1(%r14), %rcx
cmpb %r12b, (%r8,%r14)
movq %rcx, %r14
jne 0x426f
jmp 0x43cb
movq %rdi, %rcx
subq %r14, %rcx
xorl %eax, %eax
movq -0x50(%rbp), %r12
cmpq %rax, %rcx
je 0x42ce
movb (%rbx,%rax), %dl
movl %edx, %edi
andb $-0x21, %dil
addb $-0x5b, %dil
cmpb $-0x1b, %dil
ja 0x42c9
leal -0x30(%rdx), %edi
cmpb $0xa, %dil
setae %dil
cmpb $0x5f, %dl
setne %dl
testb %dil, %dl
jne 0x42f9
incq %rax
jmp 0x429b
movl -0x2c(%rbp), %eax
movl %eax, %r13d
jmp 0x42fc
movq %rdi, %rax
subq %r14, %rax
xorl %r14d, %r14d
movq -0x50(%rbp), %r12
cmpq %r14, %rax
je 0x434a
movb (%rbx,%r14), %cl
addb $-0x3a, %cl
cmpb $-0xa, %cl
jb 0x4352
incq %r14
jmp 0x42e3
addl %eax, %r13d
leaq 0x10(%rbp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r8, %rdi
movl %r13d, %edx
movl %r12d, %ecx
callq 0x3ddc
movq %r15, %rdi
leaq -0x58(%rbp), %rsi
movq %rax, %rdx
callq 0x3b28
movq -0x38(%rbp), %r8
decl %r13d
movq -0x40(%rbp), %rax
movq -0x48(%rbp), %rdi
jmp 0x44a2
movl -0x2c(%rbp), %eax
movl %eax, %r14d
jmp 0x4355
addl %r13d, %r14d
movslq %r14d, %rax
cmpb $0x2e, (%r8,%rax)
jne 0x4392
leal 0x1(%r14), %ecx
movl -0x2c(%rbp), %edx
cmpl %ecx, %edx
cmovgl %edx, %ecx
incq %rax
movl %r14d, %r13d
cmpq %rdi, %rax
jge 0x4476
movb (%r8,%rax), %dl
addb $-0x3a, %dl
leal 0x1(%r13), %r14d
incq %rax
cmpb $-0xb, %dl
ja 0x436e
jmp 0x4479
movq %r8, %rdi
movl %r13d, %esi
movl %r14d, %edx
movl %r12d, %ecx
callq 0x3c0c
movq %r15, %rdi
leaq -0x58(%rbp), %rsi
movq %rax, %rdx
callq 0x3b28
movq -0x48(%rbp), %rdi
movq -0x38(%rbp), %r8
movq -0x40(%rbp), %rax
decl %r14d
movl %r14d, %r13d
jmp 0x44a2
movl %eax, %ebx
addl $0x2, %edx
movq %r8, %rdi
movl %r13d, %esi
movq -0x50(%rbp), %r12
movl %r12d, %ecx
callq 0x3ca2
movq %r15, %rdi
leaq -0x58(%rbp), %rsi
movq %rax, %rdx
callq 0x3b28
movq -0x48(%rbp), %rdi
movq -0x38(%rbp), %r8
movl %ebx, %r13d
jmp 0x449e
movsbl %r12b, %edi
callq 0x3ec0
testl %eax, %eax
je 0x44aa
movq -0x48(%rbp), %rax
subq %r14, %rax
movq %rax, -0x70(%rbp)
xorl %r12d, %r12d
movl %r13d, %r14d
xorl %r15d, %r15d
cmpq %r12, -0x70(%rbp)
je 0x44cb
movsbl (%rbx,%r12), %edi
callq 0x3ec0
testl %eax, %eax
je 0x44cf
incq %r12
leaq 0x10(%rbp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rbx, %rdi
movl %r12d, %esi
callq 0x30dd
incl %r14d
testl %eax, %eax
cmovnel %r14d, %r15d
jmp 0x4424
movl %ecx, %r14d
movq %r8, %rdi
movl %r14d, %edx
movl %r12d, %ecx
callq 0x3c57
movq %r15, %rdi
leaq -0x58(%rbp), %rsi
movq %rax, %rdx
callq 0x3b28
movq -0x48(%rbp), %rdi
movq -0x38(%rbp), %r8
movq -0x40(%rbp), %rax
incl %r13d
jmp 0x4214
cmpl $0xa, %r12d
movq -0x48(%rbp), %rdi
je 0x452d
cmpl $0x20, %r12d
movq -0x50(%rbp), %r12
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %r8
je 0x44a2
jmp 0x459d
movl -0x2c(%rbp), %r14d
testl %r15d, %r15d
cmovnel %r15d, %r14d
leaq 0x10(%rbp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq -0x38(%rbp), %rbx
movq %rbx, %rdi
movl %r13d, %esi
movl %r14d, %edx
movq -0x50(%rbp), %r12
movl %r12d, %ecx
callq 0x3cf5
leaq -0x60(%rbp), %r15
movq %r15, %rdi
leaq -0x58(%rbp), %rsi
movq %rax, %rdx
callq 0x3b28
movq %rbx, %r8
decl %r14d
movl %r14d, %r13d
jmp 0x433d
movq -0x50(%rbp), %r12
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %r8
jmp 0x44a2
movl %r12d, %edi
movq %rax, %r14
movq %r8, %r15
callq 0x3ba6
leaq -0x60(%rbp), %rbx
leaq -0x58(%rbp), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0x3b28
movq %r14, %rdi
callq 0x2030
movq -0x68(%rbp), %rax
movq %r15, (%rax)
movq (%rbx), %r14
jmp 0x458b
leaq 0x2fff(%rip), %rdi      # 0x7579
jmp 0x4583
leaq 0x2fda(%rip), %rdi      # 0x755d
callq 0x2050
xorl %r14d, %r14d
movq %r14, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x60(%rbp), %r15
movq %r15, %rdi
movq %r8, %r13
callq 0x45e9
incl %r12d
leaq 0x2f71(%rip), %rdi      # 0x7527
xorl %r14d, %r14d
movl %r12d, %esi
xorl %eax, %eax
callq 0x2090
movsbl (%rbx), %esi
leaq 0x2f74(%rip), %rdi      # 0x7541
xorl %eax, %eax
callq 0x2090
movq %r15, %rdi
movq %r13, %rsi
callq 0x4699
movq -0x68(%rbp), %rax
andq $0x0, (%rax)
jmp 0x458b