// File: /workspace/github_c_makefile_star2_O1/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)
movq 0x20497ba(%rip), %r15   # 0x2059e30
testq %r15, %r15
je 0x106d6
movl 0x20497b7(%rip), %ebx   # 0x2059e38
movl %ebx, 0x8(%r15)
testl %ebx, %ebx
je 0x106c3
movq 0x18(%r15), %r13
leaq 0x20497b4(%rip), %r12   # 0x2059e48
xorl %ebp, %ebp
movq (%r12), %rdx
cmpq %r13, %rdx
jne 0x106b1
movq -0x8(%r12), %rdi
movq 0x10(%r15), %rsi
callq 0x3070
testl %eax, %eax
je 0x106bf
incq %rbp
addq $0x10, %r12
cmpq %rbp, %rbx
jne 0x10696
jmp 0x106c3
movl %ebp, 0x8(%r15)
cmpl %ebx, 0x8(%r15)
je 0x10883
movq 0x28(%r15), %r15
testq %r15, %r15
jne 0x10681
movq 0x2049753(%rip), %rbx   # 0x2059e30
testq %rbx, %rbx
movq (%rsp), %r13
je 0x107d5
xorl %eax, %eax
leaq 0x204d74d(%rip), %r15   # 0x205de40
leaq 0x204df46(%rip), %r12   # 0x205e640
cmpb $0x38, 0x20(%rbx)
movl 0x8(%rbx), %ecx
jne 0x1070a
movzwl (%r15,%rcx,2), %ecx
jmp 0x1070e
movl (%r12,%rcx,4), %ecx
cmpl $0x100, %ecx            # imm = 0x100
jb 0x107af
movb 0x6(%rbx), %cl
cmpb $0x1, %cl
jne 0x107af
incb %cl
movb %cl, 0x6(%rbx)
movl (%rbx), %eax
leaq (%rax,%r13), %rsi
leaq (%rax,%r13), %rdi
incq %rdi
movl (%r14), %edx
subl %eax, %edx
callq 0x3240
incl (%r14)
movq 0x28(%rbx), %rax
testq %rax, %rax
je 0x10756
incl (%rax)
movq 0x28(%rax), %rax
jmp 0x10749
movl 0x20496dc(%rip), %ecx   # 0x2059e38
testq %rcx, %rcx
je 0x10789
movl (%rbx), %eax
xorl %edx, %edx
movl %ecx, %edi
leal (%rdi,%rdx), %r8d
shrl %r8d
cmpl %eax, (%r12,%r8,4)
leal 0x1(%r8), %esi
cmovbel %edi, %r8d
cmoval %edx, %esi
movl %esi, %edx
movl %r8d, %edi
cmpl %r8d, %esi
jb 0x10767
jmp 0x1078b
xorl %esi, %esi
movl $0x1, %eax
cmpl %ecx, %esi
jae 0x107af
movl %esi, %edx
subq %rdx, %rcx
shlq $0x2, %rdx
xorl %esi, %esi
leaq (%rdx,%rsi,4), %rdi
incl (%r12,%rdi)
incq %rsi
cmpq %rsi, %rcx
jne 0x1079f
movq 0x28(%rbx), %rbx
testq %rbx, %rbx
jne 0x106fa
testl %eax, %eax
je 0x107d5
movq 0x2049669(%rip), %rbx   # 0x2059e30
movl $0x0, %eax
testq %rbx, %rbx
jne 0x106fa
movq 0x2049654(%rip), %rdi   # 0x2059e30
testq %rdi, %rdi
je 0x10874
leaq 0x204d654(%rip), %r15   # 0x205de40
leaq 0x204de4d(%rip), %r12   # 0x205e640
leaq 0x39be(%rip), %r14      # 0x141b8
movl (%rdi), %ebp
movl 0x8(%rdi), %ebx
movb 0x20(%rdi), %r13b
movq 0x28(%rdi), %rax
movq %rax, 0x2049622(%rip)   # 0x2059e30
callq 0x3040
cmpb $0x38, %r13b
jne 0x10820
movzwl (%r15,%rbx,2), %ebx
jmp 0x1082c
movl (%r12,%rbx,4), %ebx
cmpl $0x10000, %ebx          # imm = 0x10000
jae 0x1085c
cmpl $0x100, %ebx            # imm = 0x100
jb 0x10845
leal -0x1(%rbp), %eax
movq (%rsp), %rcx
incb (%rcx,%rax)
movb %bh, (%rcx,%rbp)
incl %ebp
jmp 0x10849
movq (%rsp), %rcx
movl %ebp, %eax
movb %bl, (%rcx,%rax)
movq 0x20495db(%rip), %rdi   # 0x2059e30
testq %rdi, %rdi
jne 0x107fa
jmp 0x10874
movq 0x8795(%rip), %rax      # 0x18ff8
movq (%rax), %rdi
movq %r14, %rsi
movl %ebx, %edx
xorl %eax, %eax
callq 0x31d0
jmp 0x1082c
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x420a