// File: baziotis[P]minijava-cpp/lex.cpp
// Function: scan_int()
void scan_int() {
    token.kind = TOK::INTLIT;
    // Find base
    int base = 10;
    if (*input == '0') {
        ++input;
        if (tolower(*input) == 'x') {
            base = 16;
            ++input;
            if (!(charmap[*input]&HEX)) {
                syntax_error("Invalid hex integer literal.");
                return;
            }
        } else if (charmap[*input]&OCT) {
            base = 8;
        } else if (charmap[*input]&(DEC|HEX)) {
            syntax_error("Invalid octal integer literal.");
            token.kind = TOK::INTLIT;
            token.val = 0;
            return;
        }
    }

    // Compute value.
    int val = 0;
    while (true) {
        int digit = digit_from_char[tolower(*input)];
        if (digit == 0 && *input != '0') {
            break;
        }
        if (digit >= base) {
            syntax_error("Digit `", *input, "` out of range for base ", base);
            break;
        }
        // You can choose to either continue on overflow
        // or eat the remaining digits and set a standard value
        // like 0.
        constexpr int int_max = std::numeric_limits<int>::max();
        if (val > (int_max - digit) / 10) {
            warning("Integer literal overflow.");
            // eat remaining digits
            digit = digit_from_char[*input];
            for(;;) {
                if(charmap[*input]&DEC) {
                    ++input;
                } else {
                    digit = digit_from_char[*input];
                    if(digit != 0 && digit < base) {
                        ++input;
                    } else {
                        break;
                    }
                }
            }
            val = int_max;
            break;
        }
        val = val * base + digit;
        ++input;
    }
    token.val = val;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movl $0x2, 0x206a6(%rip)     # 0x246a8
movl $0xa, -0x4(%rbp)
movq 0x206a8(%rip), %rax     # 0x246b8
movsbl (%rax), %eax
cmpl $0x30, %eax
jne 0x40fc
movq 0x20695(%rip), %rax     # 0x246b8
addq $0x1, %rax
movq %rax, 0x2068a(%rip)     # 0x246b8
movq 0x20683(%rip), %rax     # 0x246b8
movsbl (%rax), %edi
callq 0x2180
cmpl $0x78, %eax
jne 0x408c
movl $0x10, -0x4(%rbp)
movq 0x20668(%rip), %rax     # 0x246b8
addq $0x1, %rax
movq %rax, 0x2065d(%rip)     # 0x246b8
movq 0x20656(%rip), %rax     # 0x246b8
movsbq (%rax), %rcx
leaq 0x20653(%rip), %rax     # 0x246c0
movzbl (%rax,%rcx), %eax
andl $0x8, %eax
cmpl $0x0, %eax
jne 0x408a
leaq 0x15b1d(%rip), %rdi     # 0x19b9d
callq 0x4270
jmp 0x4264
jmp 0x40fa
movq 0x20625(%rip), %rax     # 0x246b8
movsbq (%rax), %rcx
leaq 0x20622(%rip), %rax     # 0x246c0
movzbl (%rax,%rcx), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x40b3
movl $0x8, -0x4(%rbp)
jmp 0x40f8
movq 0x205fe(%rip), %rax     # 0x246b8
movsbq (%rax), %rcx
leaq 0x205fb(%rip), %rax     # 0x246c0
movzbl (%rax,%rcx), %eax
andl $0xc, %eax
cmpl $0x0, %eax
je 0x40f6
leaq 0x15ae2(%rip), %rdi     # 0x19bba
callq 0x4270
movl $0x2, 0x205c1(%rip)     # 0x246a8
movl $0x0, 0x205bf(%rip)     # 0x246b0
jmp 0x4264
jmp 0x40f8
jmp 0x40fa
jmp 0x40fc
movl $0x0, -0x8(%rbp)
movq 0x205ae(%rip), %rax     # 0x246b8
movsbl (%rax), %edi
callq 0x2180
movslq %eax, %rcx
leaq 0x206a4(%rip), %rax     # 0x247c0
movzwl (%rax,%rcx,2), %eax
movl %eax, -0xc(%rbp)
cmpl $0x0, -0xc(%rbp)
jne 0x413d
movq 0x20588(%rip), %rax     # 0x246b8
movsbl (%rax), %eax
cmpl $0x30, %eax
je 0x413d
jmp 0x425b
movl -0xc(%rbp), %eax
cmpl -0x4(%rbp), %eax
jl 0x416c
movq 0x2056c(%rip), %rax     # 0x246b8
movb (%rax), %al
movl -0x4(%rbp), %ecx
leaq 0x15a81(%rip), %rdi     # 0x19bd9
leaq 0x15a82(%rip), %rdx     # 0x19be1
movsbl %al, %esi
callq 0x42a0
jmp 0x425b
movl $0x7fffffff, -0x10(%rbp) # imm = 0x7FFFFFFF
movl -0x8(%rbp), %eax
movl %eax, -0x14(%rbp)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subl -0xc(%rbp), %eax
movl $0xa, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x14(%rbp), %eax
cmpl %ecx, %eax
jle 0x4237
leaq 0x15a5d(%rip), %rdi     # 0x19bfa
callq 0x42f0
movq 0x2050f(%rip), %rax     # 0x246b8
movsbq (%rax), %rcx
leaq 0x2060c(%rip), %rax     # 0x247c0
movzwl (%rax,%rcx,2), %eax
movl %eax, -0xc(%rbp)
movq 0x204f6(%rip), %rax     # 0x246b8
movsbq (%rax), %rcx
leaq 0x204f3(%rip), %rax     # 0x246c0
movzbl (%rax,%rcx), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x41ed
movq 0x204d8(%rip), %rax     # 0x246b8
addq $0x1, %rax
movq %rax, 0x204cd(%rip)     # 0x246b8
jmp 0x422c
movq 0x204c4(%rip), %rax     # 0x246b8
movsbq (%rax), %rcx
leaq 0x205c1(%rip), %rax     # 0x247c0
movzwl (%rax,%rcx,2), %eax
movl %eax, -0xc(%rbp)
cmpl $0x0, -0xc(%rbp)
je 0x4228
movl -0xc(%rbp), %eax
cmpl -0x4(%rbp), %eax
jge 0x4228
movq 0x2049d(%rip), %rax     # 0x246b8
addq $0x1, %rax
movq %rax, 0x20492(%rip)     # 0x246b8
jmp 0x422a
jmp 0x422e
jmp 0x422c
jmp 0x41bb
movl $0x7fffffff, -0x8(%rbp) # imm = 0x7FFFFFFF
jmp 0x425b
movl -0x8(%rbp), %eax
imull -0x4(%rbp), %eax
addl -0xc(%rbp), %eax
movl %eax, -0x8(%rbp)
movq 0x2046d(%rip), %rax     # 0x246b8
addq $0x1, %rax
movq %rax, 0x20462(%rip)     # 0x246b8
jmp 0x4103
movl -0x8(%rbp), %eax
movl %eax, 0x2044c(%rip)     # 0x246b0
addq $0x20, %rsp
popq %rbp
retq
nopw (%rax,%rax)