// File: /workspace/github_c_makefile_star2_O2/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 0x2d90
cmpl $0x9, %ebx
ja 0x8073
movl %eax, %ebp
movl %ebx, %eax
leaq 0x1dae(%rip), %rcx      # 0x9c00
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movslq %ebp, %rax
leaq 0x2225b(%rip), %rcx     # 0x2a0c0
movq (%rcx,%rax,8), %rdi
testq %rdi, %rdi
je 0x80d9
pushq $0x1
popq %rsi
callq 0x58a0
jmp 0x80d9
movslq %ebp, %r15
leaq 0x22237(%rip), %r12     # 0x2a0c0
cmpq $0x0, (%r12,%r15,8)
je 0x80d9
movswl 0x2(%r14), %edi
leaq 0x1f9e0(%rip), %r14     # 0x27880
movw %di, (%r14)
callq 0x2b28
movswl (%r14), %eax
shrl %eax
movswq %ax, %rax
shlq $0x4, %rax
leaq 0x6372(%rip), %rcx      # 0xe230
movq 0x8(%rcx,%rax), %rdi
movq (%r12,%r15,8), %rsi
callq 0x20c0
movq (%r12,%r15,8), %rsi
cmpl $0x8, %ebx
jne 0x8088
movq %rsi, %rdi
callq 0x2190
jmp 0x80d9
movslq %ebp, %rax
leaq 0x221d0(%rip), %rcx     # 0x2a0c0
movq (%rcx,%rax,8), %rdi
testq %rdi, %rdi
je 0x7efe
callq 0x7d3c
leaq 0x200bd(%rip), %rax     # 0x27fc2
movzwl (%rax), %eax
jmp 0x80db
movswl (%r14), %edi
callq 0x2d90
movl %eax, %ebx
movswl 0x2(%r14), %edi
leaq 0x1f95c(%rip), %r12     # 0x27880
movw %di, (%r12)
callq 0x2b28
movswl (%r12), %eax
shrl %eax
movswq %ax, %rax
shlq $0x4, %rax
leaq 0x62ec(%rip), %r13      # 0xe230
movq 0x8(%r13,%rax), %r15
leaq 0x1d1a(%rip), %rsi      # 0x9c6a
movq %r15, %rdi
callq 0x2150
testl %eax, %eax
je 0x8092
leaq 0x1d09(%rip), %rsi      # 0x9c70
movq %r15, %rdi
callq 0x2150
testl %eax, %eax
je 0x809b
leaq 0x1cf9(%rip), %rsi      # 0x9c77
movq %r15, %rdi
callq 0x2150
testl %eax, %eax
je 0x80a4
movswl 0x4(%r14), %edi
movw %di, (%r12)
callq 0x2b28
movswl (%r12), %eax
shrl %eax
movswq %ax, %rax
shlq $0x4, %rax
movq 0x8(%r13,%rax), %rsi
movq %r15, %rdi
callq 0x21b0
jmp 0x80ae
movslq %ebp, %rbx
leaq 0x220f8(%rip), %r14     # 0x2a0c0
movq (%r14,%rbx,8), %rdi
testq %rdi, %rdi
je 0x7fd6
callq 0x2090
andq $0x0, (%r14,%rbx,8)
jmp 0x80d9
leaq 0x1c97(%rip), %rdi      # 0x9c7e
leaq 0x16cb(%rip), %rsi      # 0x96b9
callq 0x4938
movslq %ebp, %r15
leaq 0x220c3(%rip), %r12     # 0x2a0c0
movq (%r12,%r15,8), %r14
testq %r14, %r14
je 0x80d9
movq %rsp, %rbx
movb $0x0, (%rbx)
xorl %eax, %eax
movq 0x4fb7(%rip), %r13      # 0xcfd0
cltq
leaq (%rsp,%rax), %rdi
movl $0x200, %esi            # imm = 0x200
movq %r14, %rdx
callq 0x2110
testq %rax, %rax
je 0x80d9
movq (%r12,%r15,8), %r14
cmpq (%r13), %r14
jne 0x8055
movq %rbx, %rdi
callq 0x20a0
decl %eax
cltq
cmpb $0xa, (%rsp,%rax)
jne 0x8055
movb $0x0, (%rsp,%rax)
movq %rbx, %rdi
callq 0x20a0
decq %rax
movslq %eax, %rcx
cmpb $0x5c, (%rsp,%rcx)
je 0x8019
movq %rsp, %rdi
callq 0x374c
jmp 0x80db
leaq 0x1449(%rip), %rdi      # 0x94c3
leaq 0x1c20(%rip), %rsi      # 0x9ca1
callq 0x4938
jmp 0x80d9
pushq $0xa
popq %rdi
callq 0x20f0
jmp 0x80d9
movq 0x4f37(%rip), %rax      # 0xcfd0
jmp 0x80ab
movq 0x4f1e(%rip), %rax      # 0xcfc0
jmp 0x80ab
movq 0x4f45(%rip), %rax      # 0xcff0
movq (%rax), %rax
movslq %ebx, %rcx
leaq 0x22008(%rip), %rdx     # 0x2a0c0
movq %rax, (%rdx,%rcx,8)
testq %rax, %rax
je 0x80d9
movl %ebx, %edi
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2d9b
xorl %eax, %eax
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq