// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movl 0x4(%rsi), %r8d
leal -0x2(%r8), %eax
cmpl $0x3, %eax
jae 0x3f15
movq %rbx, %rsi
callq 0x55f9
movq %rax, %rbx
cmpl $0x2, (%rax)
jne 0x3f35
movq %rbx, %rax
jmp 0x3fe6
leaq 0xc3a5(%rip), %rdi      # 0x102c1
leaq 0xc3d8(%rip), %rsi      # 0x102fb
pushq $0x2
popq %rdx
pushq $0x4
popq %rcx
xorl %eax, %eax
callq 0xdb45
jmp 0x3fd8
movq 0x8(%rbx), %rax
movq (%rax), %rcx
movl (%rcx), %edi
cmpl $0xc, %edi
ja 0x3fb9
movl $0x1030, %ecx           # imm = 0x1030
btl %edi, %ecx
jae 0x3fb9
movq 0x8(%rax), %rcx
movl (%rcx), %edi
testl %edi, %edi
je 0x3f86
callq 0xd97c
movq %rax, %r14
xorl %edi, %edi
callq 0xd97c
leaq 0xc1d4(%rip), %rdi      # 0x10141
leaq 0xc387(%rip), %rsi      # 0x102fb
pushq $0x1
popq %rdx
movq %r14, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0xdb45
jmp 0x3fd8
movl 0x4(%rbx), %ebp
cmpl $0x3, %ebp
jl 0x3ffe
movq 0x10(%rax), %rcx
movl (%rcx), %edi
testl %edi, %edi
je 0x3ff5
callq 0xd97c
movq %rax, %r14
xorl %edi, %edi
callq 0xd97c
leaq 0xc193(%rip), %rdi      # 0x10141
leaq 0xc346(%rip), %rsi      # 0x102fb
pushq $0x2
jmp 0x3f76
callq 0xd97c
leaq 0xc2a3(%rip), %rdi      # 0x10268
leaq 0xc32f(%rip), %rsi      # 0x102fb
xorl %edx, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0xdb45
movq %rax, %r14
movq %rbx, %rdi
callq 0xded1
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x3, %ebp
jne 0x40cd
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe2bf
movq %rax, %r14
movq 0x8(%rbx), %rax
movq (%rax), %rcx
cmpl $0x3, %ebp
jl 0x4024
movq 0x8(%rax), %rdx
movl 0x18(%rdx), %r15d
jmp 0x4028
movl 0x10(%r14), %r15d
movl 0x18(%rcx), %ebp
testb %r12b, %r12b
je 0x403a
movq 0x10(%rax), %rax
movl 0x18(%rax), %r13d
jmp 0x403e
pushq $0x1
popq %r13
movq %rbx, %rdi
callq 0xded1
testl %ebp, %ebp
jns 0x404e
addl 0x10(%r14), %ebp
testl %r15d, %r15d
jns 0x4057
addl 0x10(%r14), %r15d
testl %r13d, %r13d
movl %ebp, %r12d
cmovsl %r15d, %r12d
cmovsl %ebp, %r15d
movl %r13d, %ecx
negl %ecx
cmovsl %r13d, %ecx
leal 0x1(%r12), %eax
leal 0x1(%r15), %edi
cmpl %r12d, %r15d
cmovgel %r12d, %edi
cmovgel %r15d, %eax
xorl %edx, %edx
movl $0x0, %esi
testl %edi, %edi
js 0x4096
movl 0x10(%r14), %esi
cmpl %esi, %edi
cmovll %edi, %esi
testl %eax, %eax
js 0x40a3
movl 0x10(%r14), %edx
cmpl %edx, %eax
cmovll %eax, %edx
movq %r14, %rdi
callq 0xe486
movq %rax, %rbx
cmpl %r12d, %r15d
jge 0x3f0d
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe41c
movq 0x18(%rax), %rax
movl (%rax), %edi
testl %edi, %edi
je 0x40fb
callq 0xd97c
movq %rax, %r14
xorl %edi, %edi
callq 0xd97c
leaq 0xc054(%rip), %rdi      # 0x10141
leaq 0xc207(%rip), %rsi      # 0x102fb
pushq $0x3
jmp 0x3f76
movb $0x1, %r12b
cmpl $0x0, 0x18(%rax)
jne 0x4001
leaq 0xc1f2(%rip), %rdi      # 0x10301
leaq 0xc1e5(%rip), %rsi      # 0x102fb
xorl %eax, %eax
callq 0xdb45
jmp 0x3fd8