// File: /workspace/github_c_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %rbx
cmpb $0x0, 0x31(%rdi)
jne 0x7f65
cmpb $0x0, 0x30(%rbx)
je 0x8081
cmpb $0x0, 0xc(%rbx)
je 0x7f85
leaq 0x3a3c(%rip), %rdi      # 0xb9ae
movl $0x242, %esi            # imm = 0x242
movl $0x1ff, %edx            # imm = 0x1FF
xorl %eax, %eax
callq 0x4200
jmp 0x7f88
pushq $-0x1
popq %rax
movl %eax, 0x4(%rsp)
callq 0x4623
leaq 0x3a23(%rip), %rsi      # 0xb9bb
pushq $0x1
popq %r14
movl %r14d, %edi
xorl %eax, %eax
callq 0x43f9
movq 0x602b(%rip), %rbp      # 0xdfd8
movq (%rbp), %rdi
callq 0x4180
leaq 0x10(%rsp), %r15
pushq $0x7f
popq %r12
leaq 0x8(%rsp), %r13
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
movl $0xc8, %r9d
callq 0x46fa
movq 0x8(%rsp), %rdx
testq %rdx, %rdx
je 0x8013
movl %r14d, %edi
movq %r15, %rsi
callq 0x4090
movq (%rbp), %rdi
callq 0x4180
cmpb $0x0, 0xc(%rbx)
je 0x8013
movq 0x8(%rsp), %rdx
movl 0x4(%rsp), %edi
movq %r15, %rsi
callq 0x4090
callq 0x45d3
testl %eax, %eax
je 0x7fc4
callq 0x45b3
cmpl $0x1b, %eax
je 0x8053
movb %al, 0x10(%rsp)
movb $0x0, 0x11(%rsp)
cmpb $0x0, 0x32(%rbx)
je 0x8043
movl %r14d, %edi
movq %r15, %rsi
pushq $0x1
popq %rdx
callq 0x4090
movq %rbx, %rdi
movq %r15, %rsi
callq 0x458c
jmp 0x7fc4
leaq 0x398c(%rip), %rsi      # 0xb9e6
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x43f9
movq (%rbp), %rdi
callq 0x4180
callq 0x4680
cmpb $0x0, 0xc(%rbx)
je 0x8081
movl 0x4(%rsp), %edi
callq 0x4100
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq