// File: /workspace/github_c_makefile_star2_O3/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 0x2048af1(%rip), %r15   # 0x2058e30
testq %r15, %r15
je 0x103a6
movl 0x2048aed(%rip), %r13d  # 0x2058e38
movl %r13d, 0x8(%r15)
movl $0x0, %ebp
testl %r13d, %r13d
je 0x10394
movq 0x18(%r15), %r12
leaq 0x2048ae4(%rip), %rbx   # 0x2058e48
xorl %ebp, %ebp
cmpq %r12, (%rbx)
jne 0x1037f
movq -0x8(%rbx), %rdi
movq 0x10(%r15), %rsi
movq %r12, %rdx
callq 0x3070
testl %eax, %eax
je 0x10390
incq %rbp
addq $0x10, %rbx
cmpq %rbp, %r13
jne 0x10366
jmp 0x10533
movl %ebp, 0x8(%r15)
cmpl %r13d, %ebp
je 0x10533
movq 0x28(%r15), %r15
testq %r15, %r15
jne 0x1034b
leaq 0x204ca93(%rip), %r15   # 0x205ce40
leaq 0x204d28c(%rip), %r12   # 0x205d640
movq (%rsp), %rbx
movq 0x2048a71(%rip), %r13   # 0x2058e30
testq %r13, %r13
je 0x10524
xorl %eax, %eax
cmpb $0x38, 0x20(%r13)
movl 0x8(%r13), %ecx
jne 0x103dc
movzwl (%r15,%rcx,2), %ecx
jmp 0x103e0
movl (%r12,%rcx,4), %ecx
cmpl $0x100, %ecx            # imm = 0x100
jb 0x10484
cmpb $0x1, 0x6(%r13)
jne 0x10484
movb $0x2, 0x6(%r13)
movl (%r13), %eax
leaq (%rbx,%rax), %rsi
leaq (%rbx,%rax), %rdi
incq %rdi
movl (%r14), %edx
subl %eax, %edx
callq 0x3240
incl (%r14)
movq 0x28(%r13), %rax
testq %rax, %rax
je 0x10429
incl (%rax)
movq 0x28(%rax), %rax
jmp 0x1041c
movl 0x2048a09(%rip), %ecx   # 0x2058e38
testq %rcx, %rcx
je 0x1045e
movl (%r13), %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 0x1043c
jmp 0x10460
xorl %esi, %esi
movl $0x1, %eax
cmpl %ecx, %esi
jae 0x10484
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 0x10474
movq 0x28(%r13), %r13
testq %r13, %r13
jne 0x103ca
testl %eax, %eax
jne 0x103b8
movq 0x2048990(%rip), %rdi   # 0x2058e30
testq %rdi, %rdi
je 0x10524
leaq 0x3d0c(%rip), %r14      # 0x141b8
movl (%rdi), %ebp
movl 0x8(%rdi), %ebx
movb 0x20(%rdi), %r13b
movq 0x28(%rdi), %rax
movq %rax, 0x2048970(%rip)   # 0x2058e30
callq 0x3040
cmpb $0x38, %r13b
jne 0x104d2
movzwl (%r15,%rbx,2), %ebx
jmp 0x104de
movl (%r12,%rbx,4), %ebx
cmpl $0x10000, %ebx          # imm = 0x10000
jae 0x10508
movq (%rsp), %rcx
cmpl $0x100, %ebx            # imm = 0x100
jb 0x104f5
leal -0x1(%rbp), %eax
incb (%rcx,%rax)
movb %bh, (%rcx,%rbp)
incl %ebp
movl %ebp, %eax
movb %bl, (%rcx,%rax)
movq 0x204892f(%rip), %rdi   # 0x2058e30
testq %rdi, %rdi
jne 0x104ac
jmp 0x10524
movq 0x7ae9(%rip), %rax      # 0x17ff8
movq (%rax), %rdi
movq %r14, %rsi
movl %ebx, %edx
xorl %eax, %eax
callq 0x31d0
movq (%rsp), %rcx
jmp 0x104ea
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x426c