// File: /workspace/github_c_makefile_star2_O3/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 0x3125
cmpl $0x9, %ebx
ja 0x8c82
movl %eax, %ebp
movl %ebx, %eax
leaq 0x11c4(%rip), %rcx      # 0x9c20
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movslq %ebp, %rax
leaq 0x21651(%rip), %rcx     # 0x2a0c0
movq (%rcx,%rax,8), %rdi
testq %rdi, %rdi
je 0x8cea
movl $0x1, %esi
callq 0x5bec
jmp 0x8cea
movslq %ebp, %r15
leaq 0x2162b(%rip), %r12     # 0x2a0c0
cmpq $0x0, (%r12,%r15,8)
je 0x8cea
movswl 0x2(%r14), %edi
leaq 0x1ede4(%rip), %r14     # 0x27890
movw %di, (%r14)
callq 0x2ed2
movswl (%r14), %eax
shrl %eax
movswq %ax, %rax
shlq $0x4, %rax
leaq 0x5776(%rip), %rcx      # 0xe240
movq 0x8(%rcx,%rax), %rdi
movq (%r12,%r15,8), %rsi
callq 0x20c0
movq (%r12,%r15,8), %rsi
cmpl $0x8, %ebx
jne 0x8c97
movq %rsi, %rdi
callq 0x21b0
jmp 0x8cea
movslq %ebp, %rax
leaq 0x215c4(%rip), %rcx     # 0x2a0c0
movq (%rcx,%rax,8), %rdi
testq %rdi, %rdi
je 0x8b0a
callq 0x8950
leaq 0x1f4c1(%rip), %rax     # 0x27fd2
movzwl (%rax), %eax
jmp 0x8cec
movswl (%r14), %edi
callq 0x3125
movl %eax, %ebx
movswl 0x2(%r14), %edi
leaq 0x1ed60(%rip), %r12     # 0x27890
movw %di, (%r12)
callq 0x2ed2
movswl (%r12), %eax
shrl %eax
movswq %ax, %rax
shlq $0x4, %rax
leaq 0x56f0(%rip), %r13      # 0xe240
movq 0x8(%r13,%rax), %r15
leaq 0x112e(%rip), %rsi      # 0x9c8a
movq %r15, %rdi
callq 0x2160
testl %eax, %eax
je 0x8ca3
leaq 0x111d(%rip), %rsi      # 0x9c90
movq %r15, %rdi
callq 0x2160
testl %eax, %eax
je 0x8cac
leaq 0x110d(%rip), %rsi      # 0x9c97
movq %r15, %rdi
callq 0x2160
testl %eax, %eax
je 0x8cb5
movswl 0x4(%r14), %edi
movw %di, (%r12)
callq 0x2ed2
movswl (%r12), %eax
shrl %eax
movswq %ax, %rax
shlq $0x4, %rax
movq 0x8(%r13,%rax), %rsi
movq %r15, %rdi
callq 0x21d0
jmp 0x8cbf
movslq %ebp, %rbx
leaq 0x214ec(%rip), %r14     # 0x2a0c0
movq (%r14,%rbx,8), %rdi
testq %rdi, %rdi
je 0x8be2
callq 0x2090
movq $0x0, (%r14,%rbx,8)
jmp 0x8cea
leaq 0x10a8(%rip), %rdi      # 0x9c9e
leaq 0xad0(%rip), %rsi       # 0x96cd
callq 0x4dec
movslq %ebp, %r15
leaq 0x214b4(%rip), %r12     # 0x2a0c0
movq (%r12,%r15,8), %r14
testq %r14, %r14
je 0x8cea
movq %rsp, %rbx
movb $0x0, (%rbx)
xorl %eax, %eax
movq 0x43a8(%rip), %r13      # 0xcfd0
cltq
leaq (%rsp,%rax), %rdi
movl $0x200, %esi            # imm = 0x200
movq %r14, %rdx
callq 0x2120
testq %rax, %rax
je 0x8cea
movq (%r12,%r15,8), %r14
cmpq (%r13), %r14
jne 0x8c64
movq %rbx, %rdi
callq 0x20a0
decl %eax
cltq
cmpb $0xa, (%rsp,%rax)
jne 0x8c64
movb $0x0, (%rsp,%rax)
movq %rbx, %rdi
callq 0x20a0
decq %rax
movslq %eax, %rcx
cmpb $0x5c, (%rsp,%rcx)
je 0x8c28
movq %rsp, %rdi
callq 0x3b64
jmp 0x8cec
leaq 0x84e(%rip), %rdi       # 0x94d7
leaq 0x1031(%rip), %rsi      # 0x9cc1
callq 0x4dec
jmp 0x8cea
movl $0xa, %edi
callq 0x2100
jmp 0x8cea
movq 0x4326(%rip), %rax      # 0xcfd0
jmp 0x8cbc
movq 0x430d(%rip), %rax      # 0xcfc0
jmp 0x8cbc
movq 0x4334(%rip), %rax      # 0xcff0
movq (%rax), %rax
movslq %ebx, %rcx
leaq 0x213f7(%rip), %rdx     # 0x2a0c0
movq %rax, (%rdx,%rcx,8)
testq %rax, %rax
je 0x8cea
movl %ebx, %edi
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3130
xorl %eax, %eax
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq