// File: /workspace/github_c_makefile_star2_O1/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 0x47d3
movq %rbx, %rsi
callq 0x6214
movq %rax, %rbx
cmpl $0x2, (%rax)
jne 0x47f7
movq %rbx, %rax
jmp 0x48ad
leaq 0xdae7(%rip), %rdi      # 0x122c1
leaq 0xdb1a(%rip), %rsi      # 0x122fb
movl $0x2, %edx
movl $0x4, %ecx
xorl %eax, %eax
callq 0x1032f
jmp 0x489f
movq 0x8(%rbx), %rax
movq (%rax), %rcx
movl (%rcx), %edi
cmpl $0xc, %edi
ja 0x4880
movl $0x1030, %ecx           # imm = 0x1030
btl %edi, %ecx
jae 0x4880
movq 0x8(%rax), %rcx
movl (%rcx), %edi
testl %edi, %edi
je 0x484a
callq 0x10138
movq %rax, %r14
xorl %edi, %edi
callq 0x10138
leaq 0xd912(%rip), %rdi      # 0x12141
leaq 0xdac5(%rip), %rsi      # 0x122fb
movl $0x1, %edx
movq %r14, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x1032f
jmp 0x489f
movl 0x4(%rbx), %ebp
cmpl $0x3, %ebp
jl 0x48bc
movq 0x10(%rax), %rcx
movl (%rcx), %edi
testl %edi, %edi
je 0x48bc
callq 0x10138
movq %rax, %r14
xorl %edi, %edi
callq 0x10138
leaq 0xd8cf(%rip), %rdi      # 0x12141
leaq 0xda82(%rip), %rsi      # 0x122fb
movl $0x2, %edx
jmp 0x483b
callq 0x10138
leaq 0xd9dc(%rip), %rdi      # 0x12268
leaq 0xda68(%rip), %rsi      # 0x122fb
xorl %edx, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0x1032f
movq %rax, %r14
movq %rbx, %rdi
callq 0x10767
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x4, %ebp
jl 0x48fc
movq 0x18(%rax), %rax
movl (%rax), %edi
testl %edi, %edi
je 0x48f2
callq 0x10138
movq %rax, %r14
xorl %edi, %edi
callq 0x10138
leaq 0xd860(%rip), %rdi      # 0x12141
leaq 0xda13(%rip), %rsi      # 0x122fb
movl $0x3, %edx
jmp 0x483b
cmpl $0x0, 0x18(%rax)
je 0x49d1
movq %rbx, %rdi
xorl %esi, %esi
callq 0x10c23
movq 0x8(%rbx), %rsi
movq (%rsi), %rcx
cmpl $0x3, %ebp
jl 0x491c
movq 0x8(%rsi), %rdx
movl 0x18(%rdx), %r15d
jmp 0x4920
movl 0x10(%rax), %r15d
movq %rax, %r12
movl 0x18(%rcx), %r14d
movl $0x1, %r13d
cmpl $0x4, %ebp
jl 0x493a
movq 0x10(%rsi), %rax
movl 0x18(%rax), %r13d
movq %rbx, %rdi
callq 0x10767
testl %r14d, %r14d
js 0x494c
movq %r12, %rdi
jmp 0x4954
movq %r12, %rdi
addl 0x10(%r12), %r14d
testl %r15d, %r15d
jns 0x495d
addl 0x10(%rdi), %r15d
testl %r13d, %r13d
movl %r14d, %r12d
cmovsl %r15d, %r12d
cmovsl %r14d, %r15d
movl %r13d, %ecx
negl %ecx
cmovsl %r13d, %ecx
leal 0x1(%r12), %eax
leal 0x1(%r15), %r8d
cmpl %r12d, %r15d
cmovgel %r12d, %r8d
cmovgel %r15d, %eax
xorl %edx, %edx
movl $0x0, %esi
testl %r8d, %r8d
js 0x499e
movl 0x10(%rdi), %esi
cmpl %esi, %r8d
cmovll %r8d, %esi
testl %eax, %eax
js 0x49aa
movl 0x10(%rdi), %edx
cmpl %edx, %eax
cmovll %eax, %edx
callq 0x10e6b
movq %rax, %rbx
cmpl %r12d, %r15d
jge 0x47cb
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10dac
leaq 0xd929(%rip), %rdi      # 0x12301
leaq 0xd91c(%rip), %rsi      # 0x122fb
xorl %eax, %eax
callq 0x1032f
jmp 0x489f