// File: /workspace/github_c_makefile_star2_O1/bokunimowakaru[P]petit15term/lpc21isp/lpcterm.c
// Function: Terminal
void Terminal(ISP_ENVIRONMENT *IspEnvironment)
{
    if (IspEnvironment->TerminalAfterUpload || IspEnvironment->TerminalOnly)
    {
        int           ch = 0;
        char buffer[128];
        int           fdlogfile = -1;
        unsigned long realsize;

        // When logging is switched on, output terminal output to lpc21isp.log
        if (IspEnvironment->LogFile)
        {
            fdlogfile = open("lpc21isp.log", O_RDWR | O_BINARY | O_CREAT | O_TRUNC, 0777);
        }

        PrepareKeyboardTtySettings();

        DebugPrintf(1, "Terminal started (press Escape to abort)\n\n");
        fflush(stdout);

        do
        {
            ReceiveComPort(IspEnvironment, buffer, sizeof(buffer) - 1, &realsize, 0,200);          // Check for received characters

            if (realsize)
            {
                write(1, buffer, realsize);
                fflush(stdout);
                if (IspEnvironment->LogFile)     // When logging is turned on, then copy output to logfile
                {
                    write(fdlogfile, buffer, realsize);
                }
            }

            // check for keypress, and write any out the port.
            if (kbhit())
            {
                ch = getch();
                if (ch == 0x1b)
                {
                    break;
                }
                buffer[0] = (unsigned char)ch;
                buffer[1] = 0;

                if (IspEnvironment->LocalEcho)
                {
                    write(1, buffer, 1);
                }

                SendComPort(IspEnvironment, buffer);
            }
        }
        while (ch != 0x1b);

        DebugPrintf(1, "\n\nTerminal stopped\n\n");
        fflush(stdout);

        ResetKeyboardTtySettings();

        if (IspEnvironment->LogFile)
        {
            close(fdlogfile);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x90, %rsp
movq %rdi, %rbx
cmpb $0x0, 0x31(%rdi)
jne 0x81cb
cmpb $0x0, 0x30(%rbx)
je 0x82e7
cmpb $0x0, 0xc(%rbx)
je 0x81ed
leaq 0x37d9(%rip), %rdi      # 0xb9b1
movl $0x242, %esi            # imm = 0x242
movl $0x1ff, %edx            # imm = 0x1FF
xorl %eax, %eax
callq 0x4200
movl %eax, %ebp
jmp 0x81f2
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
callq 0x4644
leaq 0x37c0(%rip), %rsi      # 0xb9be
movl $0x1, %edi
xorl %eax, %eax
callq 0x43fd
movq 0x5dc7(%rip), %r12      # 0xdfd8
movq (%r12), %rdi
callq 0x4180
leaq 0x10(%rsp), %r14
leaq 0x8(%rsp), %r15
movl $0x7f, %edx
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rcx
xorl %r8d, %r8d
movl $0xc8, %r9d
callq 0x4723
movq 0x8(%rsp), %rdx
testq %rdx, %rdx
je 0x8275
movl $0x1, %edi
movq %r14, %rsi
callq 0x4090
movq (%r12), %rdi
callq 0x4180
cmpb $0x0, 0xc(%rbx)
je 0x8275
movq 0x8(%rsp), %rdx
movl %ebp, %edi
movq %r14, %rsi
callq 0x4090
callq 0x45de
testl %eax, %eax
je 0x8224
callq 0x45bc
cmpl $0x1b, %eax
je 0x82b9
movb %al, 0x10(%rsp)
movb $0x0, 0x11(%rsp)
cmpb $0x0, 0x32(%rbx)
je 0x82a9
movl $0x1, %edx
movl $0x1, %edi
movq %r14, %rsi
callq 0x4090
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4595
jmp 0x8224
leaq 0x3729(%rip), %rsi      # 0xb9e9
movl $0x1, %edi
xorl %eax, %eax
callq 0x43fd
movq (%r12), %rdi
callq 0x4180
callq 0x46a3
cmpb $0x0, 0xc(%rbx)
je 0x82e7
movl %ebp, %edi
callq 0x4100
addq $0x90, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq