// File: /workspace/github_c_makefile_star2_O1/endeav0r[P]hsvm/src/assembler.c
// Function: assemble
uint16_t assemble (struct _token * tokens, unsigned char * mem)
{
    struct _labels labels;
    struct _label * label;
    struct _instruction ins;
    struct _token * next;
    struct _token * peek;
    int rule;
    int mem_i = 0;
    int copy_instruction = 0;
    uint16_t lval;

    labels.first = NULL;
    labels.last = NULL;

    // first pass to assemble
    next = tokens;
    while (next != NULL) {
        memset(&ins, 0, sizeof(struct _instruction));
        rule = match_rule(next);
        copy_instruction = 0;

        if (rule == -1) {
            fprintf(stderr, "couldn't match rule, line %d\n", next->line);
            for (peek = next;
                 (peek != NULL) && (peek->type != TOK_NEWLINE);
                 peek = peek->next) {
                fprintf(stderr, "%s ", token_str(peek));
            }
            fprintf(stderr, "\n");
            exit(-1);
        }

        switch (rule) {
        case RULE_MOV :
        case RULE_CMP :
            ins.opcode = next->type;
            peek = token_peek(next, 1);
            ins.operand_0 = peek->type;
            peek = token_peek(next, 3);
            ins.operand_1 = peek->type;
            copy_instruction = 1;
            break;

        case RULE_MOVLVAL    :
        case RULE_MOVLABEL   :
        case RULE_CMPLVAL    :
        case RULE_ADDLVAL    :
        case RULE_SUBLVAL    :
        case RULE_MULLVAL    :
        case RULE_DIVLVAL    :
        case RULE_MODLVAL    :
        case RULE_ANDLVAL    :
        case RULE_ORLVAL     :
        case RULE_XORLVAL    :
            if (next->type == OP_MOV) ins.opcode = OP_MOVLVAL;
            else if (next->type == OP_CMP) ins.opcode = OP_CMPLVAL;
            else if (next->type == OP_ADD) ins.opcode = OP_ADDLVAL;
            else if (next->type == OP_SUB) ins.opcode = OP_SUBLVAL;
            else if (next->type == OP_MUL) ins.opcode = OP_MULLVAL;
            else if (next->type == OP_DIV) ins.opcode = OP_DIVLVAL;
            else if (next->type == OP_MOD) ins.opcode = OP_MODLVAL;
            else if (next->type == OP_AND) ins.opcode = OP_ANDLVAL;
            else if (next->type == OP_OR ) ins.opcode = OP_ORLVAL;
            else if (next->type == OP_XOR) ins.opcode = OP_XORLVAL;
            peek = token_peek(next, 1);
            ins.operand_0 = peek->type;
            peek = token_peek(next, 3);
            lval = strtoul(peek->str, NULL, 0);
            lval = htons(lval);
            ins.lval = lval;
            copy_instruction = 1;
            break;

        case RULE_LOAD  :
        case RULE_STOR  :
        case RULE_LOADB :
        case RULE_STORB :
        case RULE_IN    :
        case RULE_OUT   :
        case RULE_PUSH  :
        case RULE_POP   :
            ins.opcode = next->type;
            if ((rule == RULE_STOR) || (rule == RULE_STORB))
                peek = token_peek(next, 3);
            else
                peek = token_peek(next, 1);
            ins.operand_0 = peek->type;
            copy_instruction = 1;
            break;

        case RULE_ADD :
        case RULE_SUB :
        case RULE_MUL :
        case RULE_DIV :
        case RULE_MOD :
        case RULE_AND :
        case RULE_OR  :
        case RULE_XOR :
            ins.opcode = next->type;
            peek = token_peek(next, 1);
            ins.operand_0 = peek->type;
            peek = token_peek(next, 3);
            ins.operand_1 = peek->type;
            peek = token_peek(next, 5);
            ins.operand_2 = peek->type;
            copy_instruction = 1;
            break;

        case RULE_JMP     :
        case RULE_JE      :
        case RULE_JNE     :
        case RULE_JL      :
        case RULE_JLE     :
        case RULE_JG      :
        case RULE_JGE     :
        case RULE_CALL    :
        case RULE_RET     :
        case RULE_HLT     :
        case RULE_SYSCALL :
        case RULE_NOP     :
        case RULE_PUSHLABEL :
            if (rule == RULE_PUSHLABEL)
                ins.opcode = OP_PUSHLVAL;
            else
                ins.opcode = next->type;
            copy_instruction = 1;
            break;

        case RULE_LOADR  :
        case RULE_STORR  :
        case RULE_LOADBR :
        case RULE_STORBR :
            if (rule == RULE_LOADR)
                ins.opcode = OP_LOADR;
            else if (rule == RULE_STORR)
                ins.opcode = OP_STORR;
            else if (rule == RULE_LOADBR)
                ins.opcode = OP_LOADBR;
            else if (rule == RULE_STORBR)
                ins.opcode = OP_STORBR;
            peek = token_peek(next, 1);
            ins.operand_0 = peek->type;
            peek = token_peek(next, 3);
            ins.operand_1 = peek->type;
            copy_instruction = 1;
            break;

        case RULE_PUSHLVAL :
        case RULE_JMPLVAL  :
        case RULE_JELVAL   :
        case RULE_JNELVAL  :
        case RULE_JLLVAL   :
        case RULE_JLELVAL  :
        case RULE_JGLVAL   :
        case RULE_JGELVAL  :
            if (rule == RULE_PUSHLVAL) ins.opcode = OP_PUSHLVAL;
            else ins.opcode = next->type;
            peek = token_peek(next, 1);
            lval = strtoul(peek->str, NULL, 0);
            lval = htons(lval);
            ins.lval = lval;
            copy_instruction = 1;
            break;

        case RULE_LABELNUM :
            labels_append(&labels, label_create(next->str, mem_i));

            peek = token_peek(next, 1);
            lval = strtoul(peek->str, NULL, 0);
            lval = htons(lval);
            memcpy(&(mem[mem_i]), &lval, 2);
            mem_i += 2;
            break;

        case RULE_LABELSTR :
            labels_append(&labels, label_create(next->str, mem_i));

            peek = token_peek(next, 1);
            memcpy(&(mem[mem_i]), peek->str, strlen(peek->str) + 1);
            mem_i += strlen(peek->str) + 1;
            break;

        case RULE_LABELLABEL :
            labels_append(&labels, label_create(next->str, mem_i));
            mem_i += 2;
            break;

        case RULE_LABEL :
            labels_append(&labels, label_create(next->str, mem_i));
            break;
        }

        if (copy_instruction) {
            memcpy(&(mem[mem_i]), &ins, sizeof(struct _instruction));
            mem_i += sizeof(struct _instruction);
        }
        next = advance_token(next, rule_size(rule));
    }

    // second pass to patch labels
    mem_i = 0;
    next = tokens;
    while (next != NULL) {
        rule = match_rule(next);
        switch (rule) {
        case RULE_JMP       :
        case RULE_JE        :
        case RULE_JNE       :
        case RULE_JL        :
        case RULE_JLE       :
        case RULE_JG        :
        case RULE_JGE       :
        case RULE_CALL      :
        case RULE_LOAD      :
        case RULE_LOADB     :
        case RULE_STOR      :
        case RULE_STORB     :
        case RULE_MOVLABEL  :
        case RULE_PUSHLABEL :

            if ((rule == RULE_LOAD) || (rule == RULE_MOVLABEL))
                peek = token_peek(next, 3);
            else
                peek = token_peek(next, 1);

            label = labels_search(&labels, peek->str);
            if (label == NULL) {
                fprintf(stderr, "could not find label: %s\n", peek->str);
                exit(-1);
            }

            memcpy(&ins, &(mem[mem_i]), sizeof(struct _instruction));

            ins.lval = label->location;
            if (    (rule == RULE_JMP)   || (rule == RULE_JE)  || (rule == RULE_JNE)
                 || (rule == RULE_JL)    || (rule == RULE_JLE)
                 || (rule == RULE_JG)    || (rule == RULE_JGE)
                 || (rule == RULE_CALL)) {
                ins.lval = ins.lval - mem_i - 4;
            }
            ins.lval = htons(ins.lval);

            memcpy(&(mem[mem_i]), &ins, sizeof(struct _instruction));

        case RULE_MOV       :
        case RULE_MOVLVAL   :
        case RULE_CMP       :
        case RULE_CMPLVAL   :
        case RULE_ADD       :
        case RULE_ADDLVAL   :
        case RULE_SUB       :
        case RULE_SUBLVAL   :
        case RULE_MUL       :
        case RULE_MULLVAL   :
        case RULE_DIV       :
        case RULE_DIVLVAL   :
        case RULE_MOD       :
        case RULE_MODLVAL   :
        case RULE_AND       :
        case RULE_ANDLVAL   :
        case RULE_OR        :
        case RULE_ORLVAL    :
        case RULE_XOR       :
        case RULE_XORLVAL   :
        case RULE_JMPLVAL   :
        case RULE_JELVAL    :
        case RULE_JNELVAL   :
        case RULE_JLLVAL    :
        case RULE_JLELVAL   :
        case RULE_JGLVAL    :
        case RULE_JGELVAL   :
        case RULE_CALLR     :
        case RULE_RET       :
        case RULE_LOADR     :
        case RULE_LOADBR    :
        case RULE_STORR     :
        case RULE_STORBR    :
        case RULE_IN        :
        case RULE_OUT       :
        case RULE_PUSH      :
        case RULE_PUSHLVAL  :
        case RULE_POP       :
        case RULE_HLT       :
        case RULE_SYSCALL   :
        case RULE_NOP       :
            mem_i += sizeof(struct _instruction);
            break;

        case RULE_LABELLABEL :
            peek = token_peek(next, 1);

            label = labels_search(&labels, peek->str);
            if (label == NULL) {
                fprintf(stderr, "could not find label: %s\n", peek->str);
                exit(-1);
            }
            ins.lval = htons(label->location);
            memcpy(&(mem[mem_i]), &(ins.lval), 2);

        case RULE_LABELNUM :
            mem_i += 2;
            break;

        case RULE_LABELSTR :
            peek = token_peek(next, 1);
            mem_i += strlen(peek->str) + 1;
            break;
        }
        next = advance_token(next, rule_size(rule));
    }

    return mem_i;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, 0x18(%rsp)
movq %rdi, %r14
xorl %r13d, %r13d
testq %rdi, %rdi
je 0x29cc
movq %r14, %r15
movq $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movq %r15, %rdi
callq 0x24aa
movl %eax, %r12d
movb $0x1, %bl
xorl %eax, %eax
xorl %ebp, %ebp
leal 0x1(%r12), %ecx
cmpl $0x3b, %ecx
ja 0x282f
leaq 0x39af(%rip), %rdx      # 0x6004
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movb (%r15), %bpl
xorl %edi, %edi
jmp 0x2716
movl (%r15), %eax
leal -0x10(%rax), %ecx
cmpl $0xe, %ecx
movq %r13, 0x30(%rsp)
ja 0x271d
leaq 0x3a71(%rip), %rdx      # 0x60f4
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movb $0x11, %bpl
jmp 0x292a
cmpl $0x2c, %r12d
je 0x26a0
cmpl $0x2a, %r12d
jne 0x270c
movl $0x3, %ecx
movq %r15, %rax
movq 0x10(%rax), %rax
decl %ecx
jne 0x26a8
jmp 0x2710
movl (%r15), %ebp
movq 0x10(%r15), %rax
movl (%rax), %edi
movl $0x3, %ecx
movq %r15, %rax
movq 0x10(%rax), %rax
decl %ecx
jne 0x26c3
movzwl (%rax), %eax
movl $0x5, %edx
movq %r15, %rcx
movq 0x10(%rcx), %rcx
decl %edx
jne 0x26d6
movzbl %al, %edx
movl (%rcx), %eax
shll $0x8, %eax
orl %edx, %eax
jmp 0x2923
movb (%r15), %bpl
movq 0x10(%r15), %rax
movq 0x8(%rax), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2290
xorl %edi, %edi
rolw $0x8, %ax
jmp 0x2923
movq 0x10(%r15), %rax
movb (%r15), %bpl
movb (%rax), %dil
xorl %eax, %eax
jmp 0x2923
cmpl $0x51, %eax
je 0x2927
cmpl $0x53, %eax
jne 0x2751
movb $0x54, %bpl
jmp 0x292a
movl (%r15), %ebp
movq 0x10(%r15), %rax
movl (%rax), %edi
movl $0x3, %ecx
movq %r15, %rax
movq 0x10(%rax), %rax
decl %ecx
jne 0x2744
jmp 0x2920
xorl %ebp, %ebp
jmp 0x292a
movb $0x1b, %bpl
jmp 0x292a
movb $0x1d, %bpl
jmp 0x292a
movb $0x1f, %bpl
jmp 0x292a
movb $0x13, %bpl
jmp 0x292a
movb $0x19, %bpl
jmp 0x292a
movb $0x15, %bpl
jmp 0x292a
movb $0x17, %bpl
jmp 0x292a
movq 0x8(%r15), %rdi
movzwl %r13w, %esi
callq 0x2559
movq %rax, %rcx
movq 0x8(%rsp), %rdx
testq %rdx, %rdx
je 0x27b6
movq 0x10(%rsp), %rcx
movq %rax, 0x10(%rcx)
movq %rdx, %rcx
addl $0x2, %r13d
xorl %ebp, %ebp
movq %rax, 0x10(%rsp)
movq %rcx, 0x8(%rsp)
jmp 0x29c6
movq %r12, 0x20(%rsp)
movq 0x8(%r15), %rdi
movzwl %r13w, %esi
callq 0x2559
movq %rax, %r12
movq %rax, %rcx
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x27f9
movq 0x10(%rsp), %rcx
movq %r12, 0x10(%rcx)
movq %rax, %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%r15), %rax
movq 0x8(%rax), %rdi
xorl %ebp, %ebp
xorl %esi, %esi
xorl %edx, %edx
callq 0x2290
rolw $0x8, %ax
movslq %r13d, %rcx
movq 0x18(%rsp), %rdx
movw %ax, (%rdx,%rcx)
addl $0x2, %r13d
movq %r12, 0x10(%rsp)
jmp 0x28b4
xorl %edi, %edi
jmp 0x295c
movb $0x43, %bpl
jmp 0x26f0
movq %r12, 0x20(%rsp)
movq 0x8(%r15), %rdi
movzwl %r13w, %esi
callq 0x2559
movq %rax, %rdx
movq %rax, %rcx
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x286c
movq 0x10(%rsp), %rcx
movq %rdx, 0x10(%rcx)
movq %rax, %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%r15), %r12
movslq %r13d, %rbp
addq 0x18(%rsp), %rbp
movq 0x8(%r12), %rdi
movq %rdi, 0x28(%rsp)
movq %rdx, 0x10(%rsp)
callq 0x2100
leaq 0x1(%rax), %rdx
movq %rbp, %rdi
movq 0x28(%rsp), %rsi
callq 0x21e0
movq 0x8(%r12), %rdi
callq 0x2100
addl %eax, %r13d
incl %r13d
xorl %ebp, %ebp
xorl %edi, %edi
xorl %eax, %eax
movq 0x20(%rsp), %r12
jmp 0x295c
movb $0x37, %bpl
jmp 0x290a
movq 0x8(%r15), %rdi
movzwl %r13w, %esi
callq 0x2559
cmpq $0x0, 0x8(%rsp)
je 0x29ba
movq 0x10(%rsp), %rcx
movq %rax, 0x10(%rcx)
xorl %ebp, %ebp
movq %rax, 0x10(%rsp)
jmp 0x29c6
movb $0x33, %bpl
jmp 0x290a
movb $0x35, %bpl
jmp 0x290a
movb $0x43, %bpl
jmp 0x2661
movb $0x31, %bpl
movq 0x10(%r15), %rax
movl (%rax), %edi
movl $0x3, %ecx
movq %r15, %rax
movq 0x10(%rax), %rax
decl %ecx
jne 0x2918
movzbl (%rax), %eax
xorl %ebx, %ebx
jmp 0x295c
movb $0x52, %bpl
movq 0x10(%r15), %rax
movl (%rax), %r13d
movl $0x3, %ecx
movq %r15, %rax
movq 0x10(%rax), %rax
decl %ecx
jne 0x2939
movq 0x8(%rax), %rdi
xorl %ebx, %ebx
xorl %esi, %esi
xorl %edx, %edx
callq 0x2290
rolw $0x8, %ax
movq %r13, %rdi
movq 0x30(%rsp), %r13
testb %bl, %bl
jne 0x297b
movslq %r13d, %r13
movq 0x18(%rsp), %rcx
movb %bpl, (%rcx,%r13)
movb %dil, 0x1(%rcx,%r13)
movw %ax, 0x2(%rcx,%r13)
addl $0x4, %r13d
movslq %r12d, %rax
leaq 0x67fb(%rip), %rcx      # 0x9180
leaq (%rcx,%rax,8), %rcx
movq $-0x1, %rdx
xorl %esi, %esi
movl %esi, %eax
cmpb $-0x71, 0x1(%rcx,%rdx)
leaq 0x1(%rdx), %rdx
leal 0x1(%rax), %esi
jne 0x2992
testq %rdx, %rdx
je 0x29af
movq 0x10(%r15), %r15
decl %eax
jne 0x29a7
testq %r15, %r15
jne 0x262f
jmp 0x29d5
xorl %ebp, %ebp
movq %rax, 0x10(%rsp)
movq %rax, 0x8(%rsp)
xorl %edi, %edi
xorl %eax, %eax
jmp 0x295c
movq $0x0, 0x8(%rsp)
xorl %r15d, %r15d
testq %r14, %r14
je 0x2b59
leaq 0x6798(%rip), %r13      # 0x9180
leaq 0x3741(%rip), %r12      # 0x6130
movq %r14, %rdi
callq 0x24aa
movl %eax, %ebp
cmpl $0x39, %eax
ja 0x2a88
movl %ebp, %ebx
movslq (%r12,%rbx,4), %rax
addq %r12, %rax
jmpq *%rax
movq 0x10(%r14), %rax
movq 0x8(%rax), %r12
movq 0x8(%rsp), %r13
testq %r13, %r13
je 0x2a35
movq (%r13), %rdi
movq %r12, %rsi
callq 0x21a0
testl %eax, %eax
je 0x2a38
movq 0x10(%r13), %r13
jmp 0x2a1a
xorl %r13d, %r13d
testq %r13, %r13
je 0x2b6b
movzwl 0x8(%r13), %eax
cmpl $0x23, %ebp
ja 0x2b1b
movabsq $0xaaaa00000, %rcx      # imm = 0xAAAA00000
btq %rbx, %rcx
leaq 0x671c(%rip), %r13      # 0x9180
leaq 0x36c5(%rip), %r12      # 0x6130
jae 0x2a73
subl %r15d, %eax
addl $-0x4, %eax
movslq %r15d, %rcx
rolw $0x8, %ax
movq 0x18(%rsp), %rdx
movw %ax, 0x2(%rdx,%rcx)
addl $0x4, %r15d
movslq %ebp, %rax
leaq (,%rax,8), %rcx
addq %r13, %rcx
movq $-0x1, %rdx
xorl %esi, %esi
movl %esi, %eax
cmpb $-0x71, 0x1(%rcx,%rdx)
leaq 0x1(%rdx), %rdx
leal 0x1(%rax), %esi
jne 0x2a9f
testq %rdx, %rdx
je 0x2abc
movq 0x10(%r14), %r14
decl %eax
jne 0x2ab4
testq %r14, %r14
jne 0x29ef
jmp 0x2b59
movl $0x3, %ecx
movq %r14, %rax
movq 0x10(%rax), %rax
decl %ecx
jne 0x2ad2
jmp 0x2a11
movq 0x10(%r14), %rax
movq 0x8(%rax), %rdi
callq 0x2100
addl %eax, %r15d
incl %r15d
jmp 0x2a88
movq 0x10(%r14), %rax
movq 0x8(%rax), %r12
movq 0x8(%rsp), %rbx
testq %rbx, %rbx
je 0x2b2e
movq (%rbx), %rdi
movq %r12, %rsi
callq 0x21a0
testl %eax, %eax
je 0x2b30
movq 0x10(%rbx), %rbx
jmp 0x2b01
leaq 0x665e(%rip), %r13      # 0x9180
leaq 0x3607(%rip), %r12      # 0x6130
jmp 0x2a73
xorl %ebx, %ebx
testq %rbx, %rbx
je 0x2b7b
movzwl 0x8(%rbx), %eax
rolw $0x8, %ax
movslq %r15d, %rcx
movq 0x18(%rsp), %rdx
movw %ax, (%rdx,%rcx)
leaq 0x35e0(%rip), %r12      # 0x6130
addl $0x2, %r15d
jmp 0x2a88
movl %r15d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x235c
movq %r15, %rdi
callq 0x22f0
movq %r12, %rdi
callq 0x2380