// File: /workspace/github_c_makefile_star2_O1/jiribenes[P]scheme/src/write.c
// Function: write
void write(FILE *f, value_t val) {
    if (IS_NUM(val)) {
        write_number(f, AS_NUM(val));
    } else if (IS_NIL(val)) {
        fprintf(f, "()");
    } else if (IS_TRUE(val)) {
        fprintf(f, "#t");
    } else if (IS_FALSE(val)) {
        fprintf(f, "#f");
    } else if (IS_UNDEFINED(val)) {
        fprintf(f, "#<undefined>");
    } else if (IS_VOID(val)) {
        fprintf(f, "#<void>");
    } else if (IS_EOF(val)) {
        fprintf(f, "#<eof>");
    } else if (IS_PTR(val)) {
        if (IS_CONS(val)) {
            cons_t *cons = AS_CONS(val);

            fprintf(f, "(");
            write_cons(f, cons);
            fprintf(f, ")");
        } else if (IS_STRING(val)) {
            string_t *str = AS_STRING(val);
            write_string(f, str);
        } else if (IS_SYMBOL(val)) {
            symbol_t *sym = AS_SYMBOL(val);
            fprintf(f, "%s", sym->name);
        } else if (IS_PRIMITIVE(val)) {
            // TODO: A lot of repetition going on here, can we shorten this?
            primitive_t *prim = AS_PRIMITIVE(val);
            fprintf(f, "#<primitive ");
            if (prim->name != NULL) {
                display(f, PTR_VAL(prim->name));
            } else {
                fprintf(f, "?");
            }
            fprintf(f, ">");
        } else if (IS_FUNCTION(val)) {
            fprintf(f, "#<function ");
            function_t *fn = AS_FUNCTION(val);
            if (fn->name != NULL) {
                display(f, PTR_VAL(fn->name));
            } else {
                fprintf(f, "?");
            }
            fprintf(f, " ");
            write(f, fn->params);
            fprintf(f, ">");
        } else if (IS_MACRO(val)) {
            fprintf(f, "#<macro ");
            function_t *mac = AS_MACRO(val);
            if (mac->name != NULL) {
                display(f, PTR_VAL(mac->name));
            } else {
                fprintf(f, "?");
            }
            fprintf(f, ">");
        } else if (IS_VECTOR(val)) {
            vector_t *vec = AS_VECTOR(val);
            write_vector(f, vec);
        } else if (IS_ENV(val)) {
            env_t *env = (env_t *) AS_PTR(val);
            fprintf(f, "#<");
            if (env == NULL) {
                fprintf(f, "top level ");
            }
            fprintf(f, "environment>");
        } else {
            fprintf(f, "#<unknown ptrvalue>");
        }
    } else {
        fprintf(f, "#<unknown value>");
    }
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rsi, %r15
movq %rdi, %rbx
movq %rsi, %rax
notq %rax
movabsq $0x7ffc000000000000, %rcx # imm = 0x7FFC000000000000
testq %rcx, %rax
jne 0x5bf3
movabsq $-0x7ffc000000000001, %rax # imm = 0x8003FFFFFFFFFFFF
addq %r15, %rax
cmpq $0x5, %rax
ja 0x5ca1
leaq 0x214a(%rip), %rcx      # 0x7d28
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x2152(%rip), %rdi      # 0x7d40
jmp 0x5c78
movq %r15, %xmm0
ucomisd %xmm0, %xmm0
jp 0x5e73
btrq $0x3f, %r15
movabsq $0x7ff0000000000000, %rax # imm = 0x7FF0000000000000
cmpq %rax, %r15
jne 0x5c29
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x5c43
leaq 0x21ac(%rip), %rdi      # 0x7dd3
jmp 0x5c86
leaq 0x21b1(%rip), %rsi      # 0x7de1
movq %rbx, %rdi
movb $0x1, %al
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x1130
leaq 0x2190(%rip), %rdi      # 0x7dda
jmp 0x5c86
leaq 0x2103(%rip), %rdi      # 0x7d56
movl $0x7, %esi
jmp 0x5c8b
leaq 0x20e5(%rip), %rdi      # 0x7d46
jmp 0x5c78
leaq 0x20df(%rip), %rdi      # 0x7d49
movl $0xc, %esi
jmp 0x5c8b
leaq 0x20cb(%rip), %rdi      # 0x7d43
movl $0x2, %esi
jmp 0x5c8b
leaq 0x20d8(%rip), %rdi      # 0x7d5e
movl $0x6, %esi
movl $0x1, %edx
movq %rbx, %rcx
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x11c0
movabsq $-0x4000000000000, %r12 # imm = 0xFFFC000000000000
leaq -0x1(%r12), %rax
cmpq %rax, %r15
jbe 0x5ce5
movabsq $0x3ffffffffffff, %r13  # imm = 0x3FFFFFFFFFFFF
movq %r15, %r14
andq %r13, %r14
cmpl $0x0, (%r14)
je 0x5cf3
cmpl $0x1, (%r14)
jne 0x5d15
movq %rbx, %rdi
movq %r14, %rsi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x5f52
leaq 0x20ca(%rip), %rdi      # 0x7db6
movl $0x10, %esi
jmp 0x5c8b
movl $0x28, %edi
movq %rbx, %rsi
callq 0x1100
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5e7f
movl $0x29, %edi
jmp 0x5e08
cmpl $0x2, (%r14)
jne 0x5d33
addq $0x20, %r14
movq %r14, %rdi
movq %rbx, %rsi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x10d0
cmpl $0x3, (%r14)
jne 0x5d6b
leaq 0x2025(%rip), %rdi      # 0x7d65
movl $0xc, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x11c0
movq 0x10(%r14), %rsi
testq %rsi, %rsi
je 0x5db4
orq %r12, %rsi
movq %rbx, %rdi
callq 0x600a
jmp 0x5e03
cmpl $0x4, (%r14)
jne 0x5da0
leaq 0x1ffa(%rip), %rdi      # 0x7d72
movl $0xb, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x11c0
movq 0x10(%r14), %rsi
testq %rsi, %rsi
je 0x5ddd
orq %r12, %rsi
movq %rbx, %rdi
callq 0x600a
jmp 0x5dea
cmpl $0x5, (%r14)
jne 0x5dc3
leaq 0x1fd1(%rip), %rdi      # 0x7d7e
movl $0x8, %esi
jmp 0x5d45
movl $0x3f, %edi
movq %rbx, %rsi
callq 0x1100
jmp 0x5e03
cmpl $0x6, (%r14)
jne 0x5e19
movq %rbx, %rdi
movq %r14, %rsi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x607c
movl $0x3f, %edi
movq %rbx, %rsi
callq 0x1100
movl $0x20, %edi
movq %rbx, %rsi
callq 0x1100
movq 0x20(%r14), %rsi
movq %rbx, %rdi
callq 0x5b9c
movl $0x3e, %edi
movq %rbx, %rsi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x1100
cmpl $0x7, (%r14)
jne 0x5e62
leaq 0x1f61(%rip), %rdi      # 0x7d87
movl $0x2, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x11c0
testq %r13, %r15
jne 0x5e56
leaq 0x1f46(%rip), %rdi      # 0x7d8a
movl $0xa, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x11c0
leaq 0x1f38(%rip), %rdi      # 0x7d95
jmp 0x5c6a
leaq 0x1f39(%rip), %rdi      # 0x7da2
movl $0x13, %esi
jmp 0x5c8b
leaq 0x1f52(%rip), %rdi      # 0x7dcc
jmp 0x5c86