// File: rofl0r[P]ascc/cs_parser.cpp
// Function: deal_with_end_of_do(long*, long*, ccCompiledScript*, ccInternalList*, int*)
int deal_with_end_of_do (long *nested_info, long *nested_start, ccCompiledScript *scrip, ccInternalList *targ, int *nestlevel) {
    int cursym;
    int nested_level;

    cursym = targ->getnext();
    nested_level = nestlevel[0];
    scrip->flush_line_numbers();
    if (sym.get_type(cursym) != SYM_WHILE) {
        cc_error("Do without while");
        return -1;
    }
    if (sym.get_type(targ->peeknext()) != SYM_OPENPARENTHESIS) {
        cc_error("expected '('");
        return -1;
    }
    scrip->flush_line_numbers();
    if (evaluate_expression(targ, scrip, 1, false))
        return -1;
    if (sym.get_type(targ->peeknext()) != SYM_SEMICOLON) {
        cc_error("expected ';'");
        return -1;
    }
    targ->getnext();
    // Jump back to the start of the loop while the condition is true
    scrip->write_cmd1(SCMD_JNZ, -((scrip->codesize + 2) - nested_start[nested_level]));
    // Write the correct location for the end of the loop
    scrip->code[nested_info[nested_level]] = (scrip->codesize - nested_info[nested_level]) - 1;
    nestlevel[0]--;

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x28(%rbp), %rdi
callq 0x1de00
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x38(%rbp)
movq -0x20(%rbp), %rdi
callq 0x25fb0
movl -0x34(%rbp), %esi
leaq 0x3acac(%rip), %rdi     # 0x42870
callq 0x1e1e0
cmpl $0x1f, %eax
je 0x7be8
leaq 0x29de5(%rip), %rdi     # 0x319ba
movb $0x0, %al
callq 0x1bac0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7cf9
movq -0x28(%rbp), %rdi
callq 0x1dd60
movl %eax, %esi
leaq 0x3ac76(%rip), %rdi     # 0x42870
callq 0x1e1e0
cmpl $0x5, %eax
je 0x7c1e
leaq 0x29dc0(%rip), %rdi     # 0x319cb
movb $0x0, %al
callq 0x1bac0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7cf9
movq -0x20(%rbp), %rdi
callq 0x25fb0
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x7d10
cmpl $0x0, %eax
je 0x7c4c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7cf9
movq -0x28(%rbp), %rdi
callq 0x1dd60
movl %eax, %esi
leaq 0x3ac12(%rip), %rdi     # 0x42870
callq 0x1e1e0
cmpl $0x28, %eax
je 0x7c7f
leaq 0x29d69(%rip), %rdi     # 0x319d8
movb $0x0, %al
callq 0x1bac0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7cf9
movq -0x28(%rbp), %rdi
callq 0x1de00
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movl 0x20(%rax), %eax
addl $0x2, %eax
movslq %eax, %rcx
movq -0x18(%rbp), %rax
movslq -0x38(%rbp), %rdx
subq (%rax,%rdx,8), %rcx
xorl %eax, %eax
subq %rcx, %rax
movl %eax, %edx
movl $0x46, %esi
callq 0x25200
movq -0x20(%rbp), %rax
movslq 0x20(%rax), %rax
movq -0x10(%rbp), %rcx
movslq -0x38(%rbp), %rdx
subq (%rcx,%rdx,8), %rax
subq $0x1, %rax
movl %eax, %edx
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
movslq -0x38(%rbp), %rsi
movq (%rcx,%rsi,8), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x30(%rbp), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)