// File: /workspace/github_c_makefile_star2_O1/afi-lambda[P]a_little_smalltalk/src/unixio.c
// Function: ioPrimitive
object ioPrimitive(int number, object *arguments)
{
    int i, j;
    char *p, buffer[1024];
    object returnedObject;

    returnedObject = nilobj;

    i = intValue(arguments[0]);

    switch (number)
    {
    case 0: /* file open */
        i = intValue(arguments[0]);
        p = charPtr(arguments[1]);
        if (streq(p, "stdin"))
            fp[i] = stdin;
        else if (streq(p, "stdout"))
            fp[i] = stdout;
        else if (streq(p, "stderr"))
            fp[i] = stderr;
        else
        {
            fp[i] = fopen(p, charPtr(arguments[2]));
        }
        if (fp[i] == NULL)
            returnedObject = nilobj;
        else
            returnedObject = newInteger(i);
        break;

    case 1: /* file close - recover slot */
        if (fp[i])
            fclose(fp[i]);
        fp[i] = NULL;
        break;

    case 2: /* file size */
    case 3: /* file in */
        if (fp[i])
            fileIn(fp[i], true);
        break;

    case 4: /* get character */
        sysError("file operation not implemented yet", "");

    case 5: /* get string */
        if (!fp[i])
            break;
        j = 0;
        buffer[j] = '\0';
        while (1)
        {
            if (fgets(&buffer[j], 512, fp[i]) == NULL)
                return (nilobj); /* end of file */
            if (fp[i] == stdin)
            {
                /* delete the newline */
                j = strlen(buffer);
                if (buffer[j - 1] == '\n')
                    buffer[j - 1] = '\0';
            }
            j = strlen(buffer) - 1;
            if (buffer[j] != '\\')
                break;
            /* else we loop again */
        }
        returnedObject = newStString(buffer);
        break;

    case 7: /* write an object image */
        if (fp[i])
            imageWrite(fp[i]);
        returnedObject = trueobj;
        break;

    case 8: /* print no return */
    case 9: /* print string */
        if (!fp[i])
            break;
        fputs(charPtr(arguments[1]), fp[i]);
        if (number == 8)
            fflush(fp[i]);
        else
            fputc('\n', fp[i]);
        break;

    default:
        sysError("unknown primitive", "filePrimitive");
    }

    return (returnedObject);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x408, %rsp            # imm = 0x408
movq %rsi, %r14
movl %edi, %ebx
movswl (%rsi), %edi
callq 0x3141
cmpl $0x9, %ebx
ja 0x8f41
movl %eax, %ebp
movl %ebx, %eax
leaq 0xf04(%rip), %rcx       # 0x9c20
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movslq %ebp, %rax
leaq 0x213a1(%rip), %rcx     # 0x2a0d0
movq (%rcx,%rax,8), %rdi
testq %rdi, %rdi
je 0x8fa9
movl $0x1, %esi
callq 0x5c3c
jmp 0x8fa9
movslq %ebp, %r15
leaq 0x2137b(%rip), %r12     # 0x2a0d0
cmpq $0x0, (%r12,%r15,8)
je 0x8fa9
movswl 0x2(%r14), %edi
leaq 0x1eb24(%rip), %r14     # 0x27890
movw %di, (%r14)
callq 0x2f18
movswl (%r14), %eax
shrl %eax
movswq %ax, %rax
shlq $0x4, %rax
leaq 0x54b6(%rip), %rcx      # 0xe240
movq 0x8(%rcx,%rax), %rdi
movq (%r12,%r15,8), %rsi
callq 0x20c0
movq (%r12,%r15,8), %rsi
cmpl $0x8, %ebx
jne 0x8f56
movq %rsi, %rdi
callq 0x21b0
jmp 0x8fa9
movslq %ebp, %rax
leaq 0x21314(%rip), %rcx     # 0x2a0d0
movq (%rcx,%rax,8), %rdi
testq %rdi, %rdi
je 0x8dca
callq 0x8c10
leaq 0x1f201(%rip), %rax     # 0x27fd2
movzwl (%rax), %eax
jmp 0x8fab
movswl (%r14), %edi
callq 0x3141
movl %eax, %ebx
movswl 0x2(%r14), %edi
leaq 0x1eaa0(%rip), %r12     # 0x27890
movw %di, (%r12)
callq 0x2f18
movswl (%r12), %eax
shrl %eax
movswq %ax, %rax
shlq $0x4, %rax
leaq 0x5430(%rip), %r13      # 0xe240
movq 0x8(%r13,%rax), %r15
leaq 0xe6e(%rip), %rsi       # 0x9c8a
movq %r15, %rdi
callq 0x2160
testl %eax, %eax
je 0x8f62
leaq 0xe5d(%rip), %rsi       # 0x9c90
movq %r15, %rdi
callq 0x2160
testl %eax, %eax
je 0x8f6b
leaq 0xe4d(%rip), %rsi       # 0x9c97
movq %r15, %rdi
callq 0x2160
testl %eax, %eax
je 0x8f74
movswl 0x4(%r14), %edi
movw %di, (%r12)
callq 0x2f18
movswl (%r12), %eax
shrl %eax
movswq %ax, %rax
shlq $0x4, %rax
movq 0x8(%r13,%rax), %rsi
movq %r15, %rdi
callq 0x21d0
jmp 0x8f7e
movslq %ebp, %rbx
leaq 0x2123c(%rip), %r14     # 0x2a0d0
movq (%r14,%rbx,8), %rdi
testq %rdi, %rdi
je 0x8ea2
callq 0x2090
movq $0x0, (%r14,%rbx,8)
jmp 0x8fa9
leaq 0xde8(%rip), %rdi       # 0x9c9e
leaq 0x7f8(%rip), %rsi       # 0x96b5
callq 0x4dfc
movslq %ebp, %r14
leaq 0x21204(%rip), %r15     # 0x2a0d0
cmpq $0x0, (%r15,%r14,8)
je 0x8fa9
movq %rsp, %rbx
movb $0x0, (%rbx)
xorl %eax, %eax
movq 0x40ea(%rip), %r12      # 0xcfd0
cltq
leaq (%rsp,%rax), %rdi
movq (%r15,%r14,8), %rdx
movl $0x200, %esi            # imm = 0x200
callq 0x2120
testq %rax, %rax
je 0x8fa9
movq (%r15,%r14,8), %rax
cmpq (%r12), %rax
jne 0x8f23
movq %rbx, %rdi
callq 0x20a0
decl %eax
cltq
cmpb $0xa, (%rsp,%rax)
jne 0x8f23
movb $0x0, (%rsp,%rax)
movq %rbx, %rdi
callq 0x20a0
decq %rax
movslq %eax, %rcx
cmpb $0x5c, (%rsp,%rcx)
je 0x8ee6
movq %rsp, %rdi
callq 0x3b88
jmp 0x8fab
leaq 0x577(%rip), %rdi       # 0x94bf
leaq 0xd72(%rip), %rsi       # 0x9cc1
callq 0x4dfc
jmp 0x8fa9
movl $0xa, %edi
callq 0x2100
jmp 0x8fa9
movq 0x4067(%rip), %rax      # 0xcfd0
jmp 0x8f7b
movq 0x404e(%rip), %rax      # 0xcfc0
jmp 0x8f7b
movq 0x407d(%rip), %rax      # 0xcff8
movq (%rax), %rax
movslq %ebx, %rcx
leaq 0x21148(%rip), %rdx     # 0x2a0d0
movq %rax, (%rdx,%rcx,8)
testq %rax, %rax
je 0x8fa9
movl %ebx, %edi
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x314c
xorl %eax, %eax
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq