// File: /workspace/github_c_makefile_star2_O0/jiribenes[P]scheme/src/core.c
// Function: lambda
static value_t lambda(vm_t *vm, env_t *env, value_t args) {
    // (lambda (<params...>) <body...>)

    if (IS_NIL(AS_CONS(args)->car)) {
        // no parameters -> (lambda () <body...>)
        value_t body = AS_CONS(args)->cdr;
        function_t *func = function_new(vm, env, NIL_VAL, body);
        return PTR_VAL(func);
    }

    if (IS_SYMBOL(AS_CONS(args)->car)) {
        // variadic -> (lambda param <body...>)
        value_t param = AS_CONS(args)->car;
        value_t body = AS_CONS(args)->cdr;
        function_t *func = function_new(vm, env, param, body);
        return PTR_VAL(func);
    }

    // TODO: Rewrite using SCM_FOREACH macro
    for (cons_t *cons = AS_CONS(AS_CONS(args)->car);;
         cons = AS_CONS(cons->cdr)) {
        if (!IS_CONS(cons->cdr) && !IS_NIL(cons->cdr) && IS_SYMBOL(cons->cdr)) {
            // variadic (lambda (a b . rest) <body...>)
            break;
        }

        if (!IS_SYMBOL(cons->car)) {
            error_runtime(vm, "lambda: all parameters must be symbols!");
            return NIL_VAL;
        }


        if (IS_NIL(cons->cdr)) {
            break;
        }
    }

    value_t params = AS_CONS(args)->car;
    value_t body = AS_CONS(args)->cdr;
    function_t *func = function_new(vm, env, params, body);
    return PTR_VAL(func);
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
movq %rdx, 0x48(%rsp)
movabsq $0x3ffffffffffff, %rax  # imm = 0x3FFFFFFFFFFFF
andq 0x48(%rsp), %rax
movabsq $0x7ffc000000000001, %rcx # imm = 0x7FFC000000000001
cmpq %rcx, 0x10(%rax)
jne 0x2fe1
movabsq $0x3ffffffffffff, %rax  # imm = 0x3FFFFFFFFFFFF
andq 0x48(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x40(%rsp)
movq 0x58(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x40(%rsp), %rcx
movabsq $0x7ffc000000000001, %rdx # imm = 0x7FFC000000000001
callq 0x69f0
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rdi
callq 0x4d10
movq %rax, 0x60(%rsp)
jmp 0x319b
movabsq $0x3ffffffffffff, %rax  # imm = 0x3FFFFFFFFFFFF
andq 0x48(%rsp), %rax
movq 0x10(%rax), %rdi
movl $0x2, %esi
callq 0x4d50
testb $0x1, %al
jne 0x3004
jmp 0x3066
movabsq $0x3ffffffffffff, %rax  # imm = 0x3FFFFFFFFFFFF
andq 0x48(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x30(%rsp)
movabsq $0x3ffffffffffff, %rax  # imm = 0x3FFFFFFFFFFFF
andq 0x48(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x28(%rsp)
movq 0x58(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq 0x28(%rsp), %rcx
callq 0x69f0
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rdi
callq 0x4d10
movq %rax, 0x60(%rsp)
jmp 0x319b
movabsq $0x3ffffffffffff, %rcx  # imm = 0x3FFFFFFFFFFFF
andq 0x48(%rsp), %rcx
movabsq $0x3ffffffffffff, %rax  # imm = 0x3FFFFFFFFFFFF
andq 0x10(%rcx), %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rdi
xorl %esi, %esi
callq 0x4d50
testb $0x1, %al
jne 0x30cc
movq 0x18(%rsp), %rax
movabsq $0x7ffc000000000001, %rcx # imm = 0x7FFC000000000001
cmpq %rcx, 0x18(%rax)
je 0x30cc
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rdi
movl $0x2, %esi
callq 0x4d50
testb $0x1, %al
jne 0x30ca
jmp 0x30cc
jmp 0x3140
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rdi
movl $0x2, %esi
callq 0x4d50
testb $0x1, %al
jne 0x310a
movq 0x58(%rsp), %rdi
leaq 0x7282(%rip), %rsi      # 0xa371
movb $0x0, %al
callq 0x73a0
movabsq $0x7ffc000000000001, %rax # imm = 0x7FFC000000000001
movq %rax, 0x60(%rsp)
jmp 0x319b
movq 0x18(%rsp), %rax
movabsq $0x7ffc000000000001, %rcx # imm = 0x7FFC000000000001
cmpq %rcx, 0x18(%rax)
jne 0x3121
jmp 0x3140
jmp 0x3123
movq 0x18(%rsp), %rcx
movabsq $0x3ffffffffffff, %rax  # imm = 0x3FFFFFFFFFFFF
andq 0x18(%rcx), %rax
movq %rax, 0x18(%rsp)
jmp 0x3088
movabsq $0x3ffffffffffff, %rax  # imm = 0x3FFFFFFFFFFFF
andq 0x48(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
movabsq $0x3ffffffffffff, %rax  # imm = 0x3FFFFFFFFFFFF
andq 0x48(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x58(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rcx
callq 0x69f0
movq %rax, (%rsp)
movq (%rsp), %rdi
callq 0x4d10
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rax
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)