// File: /workspace/github_c_makefile_star2_O3/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, 0x20(%rsp)
xorl %r14d, %r14d
testq %rdi, %rdi
je 0x2ad9
movq %rdi, %r15
movq $0x0, 0x10(%rsp)
movq %rdi, %r12
movq $0x0, 0x8(%rsp)
movq $0x0, 0x18(%rsp)
movq %r12, %rdi
callq 0x24aa
movl %eax, %r13d
xorl %ebp, %ebp
leal 0x1(%r13), %eax
cmpl $0x3b, %eax
ja 0x2929
movb $0x43, %r14b
leaq 0x39a8(%rip), %rcx      # 0x6004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb (%r12), %r14b
xorl %ebx, %ebx
jmp 0x26b7
movl (%r12), %eax
leal -0x10(%rax), %ecx
cmpl $0xe, %ecx
ja 0x2707
leaq 0x3a70(%rip), %rdx      # 0x60f4
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movb $0x11, %r14b
jmp 0x28e7
cmpl $0x2c, %r13d
je 0x26a4
movq %r12, %rax
cmpl $0x2a, %r13d
jne 0x26ad
movq 0x10(%r12), %rax
movq 0x10(%rax), %rax
movb (%r12), %r14b
movq 0x10(%rax), %rax
movb (%rax), %bl
xorl %eax, %eax
jmp 0x2907
movb (%r12), %r14b
movq 0x10(%r12), %rax
movl (%rax), %ebx
movq 0x10(%rax), %rax
movq 0x10(%rax), %rax
movzwl (%rax), %eax
movl $0x5, %edx
movq %r12, %rcx
movq 0x10(%rcx), %rcx
decl %edx
jne 0x26dc
movzbl %al, %edx
movl (%rcx), %eax
shll $0x8, %eax
orl %edx, %eax
jmp 0x2907
movb (%r12), %r14b
movq 0x10(%r12), %rax
movq 0x8(%rax), %rdi
xorl %ebx, %ebx
jmp 0x28fa
cmpl $0x51, %eax
je 0x28e4
cmpl $0x53, %eax
jne 0x2726
movb $0x54, %r14b
jmp 0x28e7
movb (%r12), %r14b
jmp 0x28d0
xorl %r14d, %r14d
jmp 0x28e7
movb $0x1b, %r14b
jmp 0x28e7
movb $0x1d, %r14b
jmp 0x28e7
movb $0x1f, %r14b
jmp 0x28e7
movb $0x13, %r14b
jmp 0x28e7
movb $0x19, %r14b
jmp 0x28e7
movb $0x15, %r14b
jmp 0x28e7
movb $0x17, %r14b
jmp 0x28e7
movq 0x8(%r12), %rdi
movzwl 0x10(%rsp), %esi
callq 0x2559
movq %rax, %rcx
movq 0x8(%rsp), %rdx
testq %rdx, %rdx
je 0x278e
movq 0x18(%rsp), %rcx
movq %rax, 0x10(%rcx)
movq %rdx, %rcx
movq 0x10(%rsp), %rdx
addl $0x2, %edx
movq %rdx, 0x10(%rsp)
movq %rcx, 0x8(%rsp)
jmp 0x296c
movq 0x8(%r12), %rdi
movzwl 0x10(%rsp), %esi
callq 0x2559
movq %rax, %r14
movq %rax, %rbx
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x27d0
movq 0x18(%rsp), %rcx
movq %r14, 0x10(%rcx)
movq %rax, %rbx
movq 0x10(%r12), %rax
movq 0x8(%rax), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2290
rolw $0x8, %ax
movq 0x10(%rsp), %rdx
movslq %edx, %rcx
movq 0x20(%rsp), %rsi
movw %ax, (%rsi,%rcx)
addl $0x2, %edx
movq %rdx, 0x10(%rsp)
movq %rbx, 0x8(%rsp)
movq %r14, 0x18(%rsp)
jmp 0x2929
movb $0x35, %r14b
jmp 0x28d0
movq 0x8(%r12), %rdi
movzwl 0x10(%rsp), %esi
callq 0x2559
movq %rax, %rdx
movq %rax, %rcx
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x2841
movq 0x18(%rsp), %rcx
movq %rdx, 0x10(%rcx)
movq %rax, %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%r12), %r14
movq 0x10(%rsp), %rbx
movslq %ebx, %rax
addq 0x20(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x8(%r14), %rdi
movq %rdi, 0x28(%rsp)
movq %rdx, 0x18(%rsp)
callq 0x2100
leaq 0x1(%rax), %rdx
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x21e0
movq 0x8(%r14), %rdi
callq 0x2100
addl %eax, %ebx
incl %ebx
movq %rbx, 0x10(%rsp)
jmp 0x2929
movb $0x33, %r14b
jmp 0x28d0
movq 0x8(%r12), %rdi
movzwl 0x10(%rsp), %esi
callq 0x2559
cmpq $0x0, 0x8(%rsp)
je 0x2967
movq 0x18(%rsp), %rcx
movq %rax, 0x10(%rcx)
jmp 0x296c
movb $0x31, %r14b
jmp 0x28d0
movb $0x37, %r14b
movq 0x10(%r12), %rax
movb (%rax), %bl
movq 0x10(%rax), %rax
movq 0x10(%rax), %rax
movzbl (%rax), %eax
jmp 0x2907
movb $0x52, %r14b
movq 0x10(%r12), %rax
movb (%rax), %bl
movq 0x10(%rax), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2290
rolw $0x8, %ax
movq 0x10(%rsp), %rdx
movslq %edx, %rcx
movq 0x20(%rsp), %rsi
movb %r14b, (%rsi,%rcx)
movb %bl, 0x1(%rsi,%rcx)
movw %ax, 0x2(%rsi,%rcx)
addl $0x4, %edx
movq %rdx, 0x10(%rsp)
movslq %r13d, %rax
leaq 0x684d(%rip), %rcx      # 0x9180
leaq (%rcx,%rax,8), %rcx
movq $-0x1, %rdx
movl %ebp, %eax
cmpb $-0x71, 0x1(%rcx,%rdx)
leaq 0x1(%rdx), %rdx
leal 0x1(%rax), %ebp
jne 0x293e
testq %rdx, %rdx
je 0x295c
movq 0x10(%r12), %r12
decl %eax
jne 0x2953
testq %r12, %r12
jne 0x2638
jmp 0x2973
movq %rax, 0x8(%rsp)
movq %rax, 0x18(%rsp)
jmp 0x2929
xorl %r14d, %r14d
testq %r15, %r15
je 0x2ad9
leaq 0x37aa(%rip), %r13      # 0x6130
movq %r15, %rdi
callq 0x24aa
movl %eax, %ebp
cmpl $0x39, %eax
ja 0x2a24
movl %ebp, %ebx
movslq (%r13,%rbx,4), %rcx
addq %r13, %rcx
movq %r15, %rax
jmpq *%rcx
movq 0x10(%rax), %rax
movq 0x8(%rax), %r12
cmpq $0x0, 0x8(%rsp)
je 0x2aeb
movq %r14, %r13
movq 0x8(%rsp), %r14
movq (%r14), %rdi
movq %r12, %rsi
callq 0x21a0
testl %eax, %eax
je 0x29e1
movq 0x10(%r14), %r14
testq %r14, %r14
jne 0x29c4
jmp 0x2aeb
movzwl 0x8(%r14), %eax
cmpl $0x23, %ebp
ja 0x2ad1
movabsq $0xaaaa00000, %rcx      # imm = 0xAAAA00000
btq %rbx, %rcx
movq %r13, %r14
jae 0x2a08
subl %r14d, %eax
addl $-0x4, %eax
leaq 0x3721(%rip), %r13      # 0x6130
movslq %r14d, %rcx
rolw $0x8, %ax
movq 0x20(%rsp), %rdx
movw %ax, 0x2(%rdx,%rcx)
addl $0x4, %r14d
movslq %ebp, %rax
leaq 0x6752(%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 0x2a3b
testq %rdx, %rdx
je 0x2a58
movq 0x10(%r15), %r15
decl %eax
jne 0x2a50
testq %r15, %r15
jne 0x2986
jmp 0x2ad9
movq 0x10(%r15), %rax
movq 0x10(%rax), %rax
jmp 0x29a8
movq 0x10(%r15), %rax
movq 0x8(%rax), %rdi
callq 0x2100
addl %eax, %r14d
incl %r14d
jmp 0x2a24
movq 0x10(%r15), %rax
movq 0x8(%rax), %r12
cmpq $0x0, 0x8(%rsp)
je 0x2af3
movq 0x8(%rsp), %rbx
movq (%rbx), %rdi
movq %r12, %rsi
callq 0x21a0
testl %eax, %eax
je 0x2ab4
movq 0x10(%rbx), %rbx
testq %rbx, %rbx
jne 0x2a9a
jmp 0x2af3
movzwl 0x8(%rbx), %eax
rolw $0x8, %ax
movslq %r14d, %rcx
movq 0x20(%rsp), %rdx
movw %ax, (%rdx,%rcx)
addl $0x2, %r14d
jmp 0x2a24
movq %r13, %r14
jmp 0x2a08
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x235c
movq %r12, %rdi
callq 0x2380
movq %r12, %rdi
callq 0x22f0