// File: /workspace/github_c_makefile_star2_O2/wjmelements[P]evm/src/scan.c
// Function: scanFinalize
void scanFinalize(op_t *begin, uint32_t *programLength) {
    // set label indices
    node_t *node = head;
    while (node) {
        node->jump.labelIndex = labelCount;
        for (uint32_t i = 0; i < labelCount; i++) {
            if (labels[i].length != node->jump.label.length) {
                continue;
            }
            if (0 == strncmp(labels[i].start, node->jump.label.start, labels[i].length)) {
                node->jump.labelIndex = i;
                break;
            }
        }
        if (node->jump.labelIndex == labelCount) {
            fputs("Unmatched label: ", stderr);
            for (const char *c = node->jump.label.start; isLowerCase(*c); c++) {
                putc(*c, stderr);
            }
            putc('\n', stderr);
            exit(1);
        }
        node = node->next;
    }

    // loop through jumps, extending jump.len
    int again;
    do {
        again = 0;
        node = head;
        while (node) {
            uint32_t pushValue;
            if (node->type == CODESIZE) {
                pushValue = dataSizes[node->jump.labelIndex];
            } else {
                pushValue = labelLocations[node->jump.labelIndex];
            }
            if (pushValue > 255 && node->jump.len == 1) {
                again = 1;
                node->jump.len++;
                shiftProgram(begin, programLength, node->jump.programCounter, 1);
                // shift jump.programCounter for subsequent jumps
                node_t *follower = node->next;
                while (follower) {
                    follower->jump.programCounter++;
                    follower = follower->next;
                }
                // change jump locations
                for (uint32_t i = firstLabelAfter(node->jump.programCounter); i < labelCount; i++) {
                    labelLocations[i]++;
                }
            }
            node = node->next;
        }
    } while (again);

    while (!labelQueueEmpty()) {
        op_t type;
        jump_t jump = labelQueuePop(&type);
        uint32_t pushValue;
        if (type == CODESIZE) {
            pushValue = dataSizes[jump.labelIndex];
            if (pushValue > 0xffff) {
                fprintf(stderr, "Unsupported data size %u\n", pushValue);
            }
        } else {
            pushValue = labelLocations[jump.labelIndex];
            if (pushValue > 0xffff) {
                fprintf(stderr, "Unsupported label location %u\n", pushValue);
            }
        }
        if (pushValue > 0xff) {
            begin[jump.programCounter - 1]++; // PUSH1 -> PUSH2
            begin[jump.programCounter] = pushValue / 256;
            begin[jump.programCounter + 1] = pushValue % 256;
        } else {
            begin[jump.programCounter] = pushValue;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, (%rsp)
movl 0x20499c7(%rip), %r15d  # 0x2056e38
leaq 0x20499b8(%rip), %rax   # 0x2056e30
leaq 0x20499c9(%rip), %r12   # 0x2056e48
movq (%rax), %rbx
testq %rbx, %rbx
je 0xd50f
movl %r15d, 0x8(%rbx)
movq %r12, %rbp
xorl %r13d, %r13d
cmpq %r13, %r15
je 0xd4cb
movq (%rbp), %rdx
cmpq 0x18(%rbx), %rdx
jne 0xd4b5
movq -0x8(%rbp), %rdi
movq 0x10(%rbx), %rsi
callq 0x3070
testl %eax, %eax
je 0xd4be
incq %r13
addq $0x10, %rbp
jmp 0xd495
movl %r13d, 0x8(%rbx)
leaq 0x28(%rbx), %rax
cmpl %r13d, %r15d
jne 0xd47f
movq 0x8b26(%rip), %r14      # 0x15ff8
movq (%r14), %rsi
leaq 0x3cca(%rip), %rdi      # 0x111a6
callq 0x3130
movq 0x10(%rbx), %rbx
movzbl (%rbx), %edi
leal -0x7b(%rdi), %eax
cmpb $-0x1b, %al
jbe 0xd4fc
movq (%r14), %rsi
callq 0x31c0
incq %rbx
jmp 0xd4e5
movq (%r14), %rsi
pushq $0xa
popq %rdi
callq 0x31c0
pushq $0x1
popq %rdi
callq 0x3280
xorl %eax, %eax
leaq 0x2049918(%rip), %r13   # 0x2056e30
leaq 0x204d921(%rip), %r15   # 0x205ae40
leaq 0x204e11a(%rip), %r12   # 0x205b640
movq %r13, %rbx
movq (%rsp), %rbp
movq (%rbx), %rbx
testq %rbx, %rbx
je 0xd545
cmpb $0x38, 0x20(%rbx)
movl 0x8(%rbx), %ecx
jne 0xd556
movzwl (%r15,%rcx,2), %ecx
jmp 0xd55a
movq %r13, %rbx
testl %eax, %eax
movl $0x0, %eax
jne 0xd52d
jmp 0xd5e3
movl (%r12,%rcx,4), %ecx
cmpl $0x100, %ecx            # imm = 0x100
jb 0xd5da
cmpb $0x1, 0x6(%rbx)
jne 0xd5da
movb $0x2, 0x6(%rbx)
movl (%rbx), %eax
leaq (%rax,%rbp), %rsi
leaq (%rax,%rbp), %rdi
incq %rdi
movl (%r14), %edx
subl %eax, %edx
callq 0x3240
incl (%r14)
movq %rbx, %rax
movq 0x28(%rax), %rax
testq %rax, %rax
je 0xd596
incl (%rax)
jmp 0xd589
movl (%rbx), %ecx
movl 0x204989a(%rip), %eax   # 0x2056e38
xorl %edx, %edx
movl %eax, %esi
cmpl %esi, %edx
jae 0xd5c1
leal (%rdx,%rsi), %edi
shrl %edi
cmpl %ecx, (%r12,%rdi,4)
leal 0x1(%rdi), %r8d
cmovbel %esi, %edi
cmoval %edx, %r8d
movl %edi, %esi
movl %r8d, %edx
jmp 0xd5a2
movl %edx, %ecx
leaq (%r12,%rcx,4), %rdx
cmpq %rax, %rcx
jae 0xd5d7
incl (%rdx)
incq %rcx
addq $0x4, %rdx
jmp 0xd5c7
pushq $0x1
popq %rax
addq $0x28, %rbx
jmp 0xd52d
leaq 0x3bce(%rip), %r14      # 0x111b8
movq 0x204983f(%rip), %rdi   # 0x2056e30
testq %rdi, %rdi
je 0xd664
movl (%rdi), %ebp
movl 0x8(%rdi), %ebx
movb 0x20(%rdi), %r13b
movq 0x28(%rdi), %rax
movq %rax, 0x2049826(%rip)   # 0x2056e30
callq 0x3040
cmpb $0x38, %r13b
jne 0xd61c
movzwl (%r15,%rbx,2), %ebx
jmp 0xd628
movl (%r12,%rbx,4), %ebx
cmpl $0x10000, %ebx          # imm = 0x10000
jae 0xd64c
cmpl $0x100, %ebx            # imm = 0x100
jb 0xd641
leal -0x1(%rbp), %eax
movq (%rsp), %rcx
incb (%rcx,%rax)
movb %bh, (%rcx,%rbp)
incl %ebp
jmp 0xd645
movq (%rsp), %rcx
movl %ebp, %eax
movb %bl, (%rcx,%rax)
jmp 0xd5ea
movq 0x89a5(%rip), %rax      # 0x15ff8
movq (%rax), %rdi
movq %r14, %rsi
movl %ebx, %edx
xorl %eax, %eax
callq 0x31d0
jmp 0xd630
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq