// File: /workspace/github_c_makefile_star2_O3/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 0x4739
movq %rbx, %rsi
callq 0x5f58
movq %rax, %rbx
cmpl $0x2, (%rax)
jne 0x475d
movq %rbx, %rax
jmp 0x4813
leaq 0xdb81(%rip), %rdi      # 0x122c1
leaq 0xdbb4(%rip), %rsi      # 0x122fb
movl $0x2, %edx
movl $0x4, %ecx
xorl %eax, %eax
callq 0xfe6b
jmp 0x4805
movq 0x8(%rbx), %rax
movq (%rax), %rcx
movl (%rcx), %edi
cmpl $0xc, %edi
ja 0x47e6
movl $0x1030, %ecx           # imm = 0x1030
btl %edi, %ecx
jae 0x47e6
movq 0x8(%rax), %rcx
movl (%rcx), %edi
testl %edi, %edi
je 0x47b0
callq 0xfc74
movq %rax, %r14
xorl %edi, %edi
callq 0xfc74
leaq 0xd9ac(%rip), %rdi      # 0x12141
leaq 0xdb5f(%rip), %rsi      # 0x122fb
movl $0x1, %edx
movq %r14, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0xfe6b
jmp 0x4805
movl 0x4(%rbx), %ebp
cmpl $0x3, %ebp
jl 0x482b
movq 0x10(%rax), %rcx
movl (%rcx), %edi
testl %edi, %edi
je 0x4822
callq 0xfc74
movq %rax, %r14
xorl %edi, %edi
callq 0xfc74
leaq 0xd969(%rip), %rdi      # 0x12141
leaq 0xdb1c(%rip), %rsi      # 0x122fb
movl $0x2, %edx
jmp 0x47a1
callq 0xfc74
leaq 0xda76(%rip), %rdi      # 0x12268
leaq 0xdb02(%rip), %rsi      # 0x122fb
xorl %edx, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0xfe6b
movq %rax, %r14
movq %rbx, %rdi
callq 0x1029f
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x3, %ebp
jne 0x48f7
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1075b
movq %rax, %r14
movq 0x8(%rbx), %rax
movq (%rax), %rcx
cmpl $0x3, %ebp
jl 0x4851
movq 0x8(%rax), %rdx
movl 0x18(%rdx), %r15d
jmp 0x4855
movl 0x10(%r14), %r15d
movl 0x18(%rcx), %r13d
movl $0x1, %ebp
testb %r12b, %r12b
je 0x486a
movq 0x10(%rax), %rax
movl 0x18(%rax), %ebp
movq %rbx, %rdi
callq 0x1029f
testl %r13d, %r13d
jns 0x487b
addl 0x10(%r14), %r13d
testl %r15d, %r15d
jns 0x4884
addl 0x10(%r14), %r15d
testl %ebp, %ebp
movl %r13d, %r12d
cmovsl %r15d, %r12d
cmovsl %r13d, %r15d
movl %ebp, %ecx
negl %ecx
cmovsl %ebp, %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 0x48c0
movl 0x10(%r14), %esi
cmpl %esi, %edi
cmovll %edi, %esi
testl %eax, %eax
js 0x48cd
movl 0x10(%r14), %edx
cmpl %edx, %eax
cmovll %eax, %edx
movq %r14, %rdi
callq 0x109a3
movq %rax, %rbx
cmpl %r12d, %r15d
jge 0x4731
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x108e4
movq 0x18(%rax), %rax
movl (%rax), %edi
testl %edi, %edi
je 0x4928
callq 0xfc74
movq %rax, %r14
xorl %edi, %edi
callq 0xfc74
leaq 0xd82a(%rip), %rdi      # 0x12141
leaq 0xd9dd(%rip), %rsi      # 0x122fb
movl $0x3, %edx
jmp 0x47a1
movb $0x1, %r12b
cmpl $0x0, 0x18(%rax)
jne 0x482e
leaq 0xd9c5(%rip), %rdi      # 0x12301
leaq 0xd9b8(%rip), %rsi      # 0x122fb
xorl %eax, %eax
callq 0xfe6b
jmp 0x4805