// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq 0x2049389(%rip), %rax   # 0x2060e20
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x17bdf
movl 0x204937c(%rip), %ecx   # 0x2060e28
movq -0x18(%rbp), %rax
movl %ecx, 0x8(%rax)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl 0x2049365(%rip), %eax   # 0x2060e28
jae 0x17b4a
movl -0x1c(%rbp), %eax
movl %eax, %ecx
leaq 0x204935b(%rip), %rax   # 0x2060e30
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
cmpq 0x18(%rcx), %rax
je 0x17aec
jmp 0x17b3c
movl -0x1c(%rbp), %eax
movl %eax, %ecx
leaq 0x2049338(%rip), %rax   # 0x2060e30
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movl -0x1c(%rbp), %eax
movl %eax, %ecx
leaq 0x204931a(%rip), %rax   # 0x2060e30
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdx
callq 0x3070
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x17b3a
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8(%rax)
jmp 0x17b4a
jmp 0x17b3c
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x17aba
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
cmpl 0x20492d1(%rip), %eax   # 0x2060e28
jne 0x17bce
movq 0x8498(%rip), %rax      # 0x1fff8
movq (%rax), %rsi
leaq 0x47fc(%rip), %rdi      # 0x1c366
callq 0x3130
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movsbl (%rax), %edi
callq 0x17e20
cmpl $0x0, %eax
je 0x17bb0
movq -0x28(%rbp), %rax
movsbl (%rax), %edi
movq 0x845e(%rip), %rax      # 0x1fff8
movq (%rax), %rsi
callq 0x31d0
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x17b7b
movq 0x8441(%rip), %rax      # 0x1fff8
movq (%rax), %rsi
movl $0xa, %edi
callq 0x31d0
movl $0x1, %edi
callq 0x3280
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0x17a9b
jmp 0x17be1
movl $0x0, -0x2c(%rbp)
movq 0x2049231(%rip), %rax   # 0x2060e20
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x17d03
movq -0x18(%rbp), %rax
movzbl 0x20(%rax), %eax
cmpl $0x38, %eax
jne 0x17c24
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %ecx
leaq 0x204d215(%rip), %rax   # 0x2064e30
movzwl (%rax,%rcx,2), %eax
movl %eax, -0x30(%rbp)
jmp 0x17c3a
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %ecx
leaq 0x204d9fc(%rip), %rax   # 0x2065630
movl (%rax,%rcx,4), %eax
movl %eax, -0x30(%rbp)
cmpl $0xff, -0x30(%rbp)
jbe 0x17cf2
movq -0x18(%rbp), %rax
movzbl 0x6(%rax), %eax
cmpl $0x1, %eax
jne 0x17cf2
movl $0x1, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movb 0x6(%rax), %cl
addb $0x1, %cl
movb %cl, 0x6(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rax
movl (%rax), %edx
movl $0x1, %ecx
callq 0x17e50
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x17cb0
movq -0x38(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x17c90
movq -0x18(%rbp), %rax
movl (%rax), %edi
callq 0x17eb0
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl 0x2049161(%rip), %eax   # 0x2060e28
jae 0x17cf0
movl -0x3c(%rbp), %eax
movl %eax, %ecx
leaq 0x204d95b(%rip), %rax   # 0x2065630
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
leaq 0x204d94e(%rip), %rax   # 0x2065630
movl %edx, (%rax,%rcx,4)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x17cbe
jmp 0x17cf2
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0x17bf3
jmp 0x17d05
cmpl $0x0, -0x2c(%rbp)
jne 0x17be1
jmp 0x17d11
callq 0x18970
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x17d27
jmp 0x17e0d
leaq -0x60(%rbp), %rdi
leaq -0x3d(%rbp), %rsi
callq 0x17f10
movzbl -0x3d(%rbp), %eax
cmpl $0x38, %eax
jne 0x17d76
movl -0x58(%rbp), %eax
movl %eax, %ecx
leaq 0x204d0e7(%rip), %rax   # 0x2064e30
movzwl (%rax,%rcx,2), %eax
movl %eax, -0x64(%rbp)
cmpl $0xffff, -0x64(%rbp)    # imm = 0xFFFF
jbe 0x17d74
movq 0x8298(%rip), %rax      # 0x1fff8
movq (%rax), %rdi
movl -0x64(%rbp), %edx
leaq 0x460b(%rip), %rsi      # 0x1c378
movb $0x0, %al
callq 0x31e0
jmp 0x17dae
movl -0x58(%rbp), %eax
movl %eax, %ecx
leaq 0x204d8ae(%rip), %rax   # 0x2065630
movl (%rax,%rcx,4), %eax
movl %eax, -0x64(%rbp)
cmpl $0xffff, -0x64(%rbp)    # imm = 0xFFFF
jbe 0x17dac
movq 0x8260(%rip), %rax      # 0x1fff8
movq (%rax), %rdi
movl -0x64(%rbp), %edx
leaq 0x45ed(%rip), %rsi      # 0x1c392
movb $0x0, %al
callq 0x31e0
jmp 0x17dae
cmpl $0xff, -0x64(%rbp)
jbe 0x17df9
movq -0x8(%rbp), %rax
movl -0x60(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %dl
addb $0x1, %dl
movb %dl, (%rax,%rcx)
movl -0x64(%rbp), %eax
shrl $0x8, %eax
movb %al, %dl
movq -0x8(%rbp), %rax
movl -0x60(%rbp), %ecx
movb %dl, (%rax,%rcx)
movl -0x64(%rbp), %eax
andl $0xff, %eax
movb %al, %dl
movq -0x8(%rbp), %rax
movl -0x60(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
jmp 0x17e08
movl -0x64(%rbp), %eax
movb %al, %dl
movq -0x8(%rbp), %rax
movl -0x60(%rbp), %ecx
movb %dl, (%rax,%rcx)
jmp 0x17d11
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)