// File: /workspace/github_c_makefile_star2_O0/voithos[P]awl/src/builtins.c
// Function: builtin_slice
awlval* builtin_slice(awlenv* e, awlval* a) {
    AWLASSERT_RANGEARGCOUNT(a, 2, 4, "slice");
    EVAL_ARGS(e, a);
    AWLASSERT_ISORDEREDCOLLECTION(a, 0, "slice");
    AWLASSERT_TYPE(a, 1, AWLVAL_INT, "slice");

    bool end_arg_given = a->count > 2;
    if (end_arg_given) {
        AWLASSERT_TYPE(a, 2, AWLVAL_INT, "slice");
    }

    bool step_arg_given = a->count > 3;
    if (step_arg_given) {
        AWLASSERT_TYPE(a, 3, AWLVAL_INT, "slice");
        AWLASSERT_NONZERO(a, (int)a->cell[3]->lng, "slice");
    }

    awlval* collection = awlval_pop(a, 0);

    int start, end, step;
    bool reverse_slice = false;

    /* TODO: Index cast is unsafe here */
    start = (int)a->cell[0]->lng;
    end = end_arg_given ? (int)a->cell[1]->lng : collection->length;
    step = step_arg_given ? (int)a->cell[2]->lng : 1;

    awlval_del(a);

    /* Support negative indices to represent index from end */
    if (start < 0) {
        start = collection->length + start;
    }
    if (end < 0) {
        end = collection->length + end;
    }

    /* Handle negative step */
    if (step < 0) {
        step = -step;
        int temp = start;
        start = end;
        end = temp;
    }

    if (end < start) {
        reverse_slice = true;
        int temp = start + 1;
        start = end + 1;
        end = temp;
    }

    /* Constrain to collection bounds */
    start = start < 0 ? 0 :
        (start > collection->length ? collection->length : start);
    end = end < 0 ? 0 :
        (end > collection->length ? collection->length : end);

    collection = awlval_slice_step(collection, start, end, step);
    return reverse_slice ? awlval_reverse(collection) : collection;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x2, 0x4(%rax)
jl 0x51b7
movq -0x18(%rbp), %rax
cmpl $0x4, 0x4(%rax)
jle 0x51f8
movq -0x18(%rbp), %rax
movl 0x4(%rax), %r8d
leaq 0x150e3(%rip), %rdi     # 0x1a2a9
leaq 0x15116(%rip), %rsi     # 0x1a2e3
movl $0x2, %edx
movl $0x4, %ecx
movb $0x0, %al
callq 0x16600
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
callq 0x16cf0
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x562d
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x7c60
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x521f
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x562d
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
cmpl $0xc, (%rax)
je 0x5295
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
cmpl $0x5, (%rax)
je 0x5295
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
cmpl $0x4, (%rax)
je 0x5295
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movl (%rax), %edi
callq 0x161a0
movq %rax, %rcx
leaq 0x14fe5(%rip), %rdi     # 0x1a250
leaq 0x15071(%rip), %rsi     # 0x1a2e3
xorl %edx, %edx
movb $0x0, %al
callq 0x16600
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
callq 0x16cf0
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x562d
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
cmpl $0x0, (%rax)
je 0x52ff
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movl (%rax), %edi
callq 0x161a0
movq %rax, -0x80(%rbp)
xorl %edi, %edi
callq 0x161a0
movq -0x80(%rbp), %rcx
movq %rax, %r8
leaq 0x14e57(%rip), %rdi     # 0x1a129
leaq 0x1500a(%rip), %rsi     # 0x1a2e3
movl $0x1, %edx
movb $0x0, %al
callq 0x16600
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rdi
callq 0x16cf0
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x562d
movq -0x18(%rbp), %rax
cmpl $0x2, 0x4(%rax)
setg %al
andb $0x1, %al
movb %al, -0x31(%rbp)
testb $0x1, -0x31(%rbp)
je 0x5387
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
cmpl $0x0, (%rax)
je 0x5385
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
movl (%rax), %edi
callq 0x161a0
movq %rax, -0x88(%rbp)
xorl %edi, %edi
callq 0x161a0
movq -0x88(%rbp), %rcx
movq %rax, %r8
leaq 0x14dd1(%rip), %rdi     # 0x1a129
leaq 0x14f84(%rip), %rsi     # 0x1a2e3
movl $0x2, %edx
movb $0x0, %al
callq 0x16600
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rdi
callq 0x16cf0
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x562d
jmp 0x5387
movq -0x18(%rbp), %rax
cmpl $0x3, 0x4(%rax)
setg %al
andb $0x1, %al
movb %al, -0x41(%rbp)
testb $0x1, -0x41(%rbp)
je 0x5457
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rax
cmpl $0x0, (%rax)
je 0x5411
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rax
movl (%rax), %edi
callq 0x161a0
movq %rax, -0x90(%rbp)
xorl %edi, %edi
callq 0x161a0
movq -0x90(%rbp), %rcx
movq %rax, %r8
leaq 0x14d45(%rip), %rdi     # 0x1a129
leaq 0x14ef8(%rip), %rsi     # 0x1a2e3
movl $0x3, %edx
movb $0x0, %al
callq 0x16600
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rdi
callq 0x16cf0
movq -0x50(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x562d
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
cmpl $0x0, %eax
jne 0x5455
leaq 0x14ebc(%rip), %rdi     # 0x1a2e9
leaq 0x14eaf(%rip), %rsi     # 0x1a2e3
movb $0x0, %al
callq 0x16600
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rdi
callq 0x16cf0
movq -0x58(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x562d
jmp 0x5457
movq -0x18(%rbp), %rdi
xorl %esi, %esi
callq 0x174b0
movq %rax, -0x60(%rbp)
movb $0x0, -0x6d(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movl %eax, -0x64(%rbp)
testb $0x1, -0x31(%rbp)
je 0x549a
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rax
movl %eax, -0x94(%rbp)
jmp 0x54a7
movq -0x60(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
movl %eax, -0x68(%rbp)
testb $0x1, -0x41(%rbp)
je 0x54ce
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
movq 0x18(%rax), %rax
movl %eax, -0x98(%rbp)
jmp 0x54db
movl $0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0x54db
movl -0x98(%rbp), %eax
movl %eax, -0x6c(%rbp)
movq -0x18(%rbp), %rdi
callq 0x16cf0
cmpl $0x0, -0x64(%rbp)
jge 0x5500
movq -0x60(%rbp), %rax
movl 0x10(%rax), %eax
addl -0x64(%rbp), %eax
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x68(%rbp)
jge 0x5513
movq -0x60(%rbp), %rax
movl 0x10(%rax), %eax
addl -0x68(%rbp), %eax
movl %eax, -0x68(%rbp)
cmpl $0x0, -0x6c(%rbp)
jge 0x5533
xorl %eax, %eax
subl -0x6c(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x64(%rbp), %eax
movl %eax, -0x74(%rbp)
movl -0x68(%rbp), %eax
movl %eax, -0x64(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x64(%rbp), %eax
jge 0x5557
movb $0x1, -0x6d(%rbp)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
movl -0x78(%rbp), %eax
movl %eax, -0x68(%rbp)
cmpl $0x0, -0x64(%rbp)
jge 0x5567
xorl %eax, %eax
movl %eax, -0x9c(%rbp)
jmp 0x5597
movl -0x64(%rbp), %eax
movq -0x60(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jle 0x5582
movq -0x60(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0xa0(%rbp)
jmp 0x558b
movl -0x64(%rbp), %eax
movl %eax, -0xa0(%rbp)
movl -0xa0(%rbp), %eax
movl %eax, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x68(%rbp)
jge 0x55b0
xorl %eax, %eax
movl %eax, -0xa4(%rbp)
jmp 0x55e0
movl -0x68(%rbp), %eax
movq -0x60(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jle 0x55cb
movq -0x60(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0xa8(%rbp)
jmp 0x55d4
movl -0x68(%rbp), %eax
movl %eax, -0xa8(%rbp)
movl -0xa8(%rbp), %eax
movl %eax, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
movl %eax, -0x68(%rbp)
movq -0x60(%rbp), %rdi
movl -0x64(%rbp), %esi
movl -0x68(%rbp), %edx
movl -0x6c(%rbp), %ecx
callq 0x17860
movq %rax, -0x60(%rbp)
testb $0x1, -0x6d(%rbp)
je 0x5617
movq -0x60(%rbp), %rdi
callq 0x17720
movq %rax, -0xb0(%rbp)
jmp 0x5622
movq -0x60(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq
nopw (%rax,%rax)