// File: /workspace/github_c_makefile_star2_O0/bokunimowakaru[P]petit15term/lpc21isp/lpc21isp.c
// Function: OpenSerialPort
static void OpenSerialPort(ISP_ENVIRONMENT *IspEnvironment)
{
    IspEnvironment->fdCom = open(IspEnvironment->serial_port, O_RDWR | O_NOCTTY | O_NONBLOCK);

    if (IspEnvironment->fdCom < 0)
    {
        int err = errno;
        DebugPrintf(1, "Can't open COM-Port %s ! (Error: %dd (0x%X))\n", IspEnvironment->serial_port, err, err);
        exit(2);
    }

    DebugPrintf(3, "COM-Port %s opened...\n", IspEnvironment->serial_port);

    /* clear input & output buffers, then switch to "blocking mode" */
    tcflush(IspEnvironment->fdCom, TCOFLUSH);
    tcflush(IspEnvironment->fdCom, TCIFLUSH);
    fcntl(IspEnvironment->fdCom, F_SETFL, fcntl(IspEnvironment->fdCom, F_GETFL) & ~O_NONBLOCK);

    tcgetattr(IspEnvironment->fdCom, &IspEnvironment->oldtio); /* save current port settings */

    bzero(&IspEnvironment->newtio, sizeof(IspEnvironment->newtio));
    IspEnvironment->newtio.c_cflag = CS8 | CLOCAL | CREAD;

#if defined(__FreeBSD__) || defined(__OpenBSD__)

    if(cfsetspeed(&IspEnvironment->newtio,(speed_t) strtol(IspEnvironment->baud_rate,NULL,10))) {
                  DebugPrintf(1, "baudrate %s not supported\n", IspEnvironment->baud_rate);
                  exit(3);
              };
#else

#ifdef __APPLE__
#define NEWTERMIOS_SETBAUDARTE(bps) IspEnvironment->newtio.c_ispeed = IspEnvironment->newtio.c_ospeed = bps;
#else
#define NEWTERMIOS_SETBAUDARTE(bps) IspEnvironment->newtio.c_cflag |= bps;
#endif

    switch (atol(IspEnvironment->baud_rate))
    {
#ifdef B1152000
          case 1152000: NEWTERMIOS_SETBAUDARTE(B1152000); break;
#endif // B1152000
#ifdef B576000
          case  576000: NEWTERMIOS_SETBAUDARTE(B576000); break;
#endif // B576000
#ifdef B230400
          case  230400: NEWTERMIOS_SETBAUDARTE(B230400); break;
#endif // B230400
#ifdef B115200
          case  115200: NEWTERMIOS_SETBAUDARTE(B115200); break;
#endif // B115200
#ifdef B57600
          case   57600: NEWTERMIOS_SETBAUDARTE(B57600); break;
#endif // B57600
#ifdef B38400
          case   38400: NEWTERMIOS_SETBAUDARTE(B38400); break;
#endif // B38400
#ifdef B19200
          case   19200: NEWTERMIOS_SETBAUDARTE(B19200); break;
#endif // B19200
#ifdef B9600
          case    9600: NEWTERMIOS_SETBAUDARTE(B9600); break;
#endif // B9600

          // Special value
          // case   32000: NEWTERMIOS_SETBAUDARTE(32000); break;

          default:
              {
                  DebugPrintf(1, "unknown baudrate %s\n", IspEnvironment->baud_rate);
                  exit(3);
              }
    }

#endif

    IspEnvironment->newtio.c_iflag = IGNPAR | IGNBRK | IXON | IXOFF;
    IspEnvironment->newtio.c_oflag = 0;

    /* set input mode (non-canonical, no echo,...) */
    IspEnvironment->newtio.c_lflag = 0;

    cfmakeraw(&IspEnvironment->newtio);
    IspEnvironment->newtio.c_cc[VTIME]    = 1;   /* inter-character timer used */
    IspEnvironment->newtio.c_cc[VMIN]     = 0;   /* blocking read until 0 chars received */

    tcflush(IspEnvironment->fdCom, TCIFLUSH);
    if(tcsetattr(IspEnvironment->fdCom, TCSANOW, &IspEnvironment->newtio))
    {
       DebugPrintf(1, "Could not change serial port behaviour (wrong baudrate?)\n");
       exit(3);
    }

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rdi
movl $0x902, %esi            # imm = 0x902
movb $0x0, %al
callq 0x4210
movl %eax, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x80(%rax)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x80(%rax)
jge 0x53ef
callq 0x4050
movl (%rax), %eax
movl %eax, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rdx
movl -0xc(%rbp), %ecx
movl -0xc(%rbp), %r8d
movl $0x1, %edi
leaq 0x6017(%rip), %rsi      # 0xb3f5
movb $0x0, %al
callq 0x44e0
movl $0x2, %edi
callq 0x4250
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rdx
leaq 0x6025(%rip), %rsi      # 0xb423
xorl %eax, %eax
movl %eax, -0x28(%rbp)
movb %al, -0x19(%rbp)
movl $0x3, %edi
movl %edi, -0x24(%rbp)
callq 0x44e0
movq -0x8(%rbp), %rax
movl 0x80(%rax), %edi
movl $0x1, %esi
callq 0x40b0
movl -0x28(%rbp), %esi
movq -0x8(%rbp), %rax
movl 0x80(%rax), %edi
callq 0x40b0
movl -0x24(%rbp), %esi
movb -0x19(%rbp), %al
movq -0x8(%rbp), %rcx
movl 0x80(%rcx), %edi
movl %edi, -0x20(%rbp)
callq 0x4080
movl -0x20(%rbp), %edi
movl %eax, %edx
movb -0x19(%rbp), %al
andl $0xfffff7ff, %edx       # imm = 0xFFFFF7FF
movl $0x4, %esi
callq 0x4080
movq -0x8(%rbp), %rsi
movl 0x80(%rsi), %edi
addq $0x84, %rsi
callq 0x41e0
movq -0x8(%rbp), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0xec(%rax)
movups %xmm0, 0xe0(%rax)
movups %xmm0, 0xd0(%rax)
movups %xmm0, 0xc0(%rax)
movq -0x8(%rbp), %rax
movl $0x8b0, 0xc8(%rax)      # imm = 0x8B0
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x4200
movq %rax, %rcx
movq %rcx, -0x18(%rbp)
subq $0x2580, %rax           # imm = 0x2580
je 0x55f3
jmp 0x54d2
movq -0x18(%rbp), %rax
subq $0x4b00, %rax           # imm = 0x4B00
je 0x55de
jmp 0x54e4
movq -0x18(%rbp), %rax
subq $0x9600, %rax           # imm = 0x9600
je 0x55c9
jmp 0x54f6
movq -0x18(%rbp), %rax
subq $0xe100, %rax           # imm = 0xE100
je 0x55b1
jmp 0x5508
movq -0x18(%rbp), %rax
subq $0x1c200, %rax          # imm = 0x1C200
je 0x5599
jmp 0x551a
movq -0x18(%rbp), %rax
subq $0x38400, %rax          # imm = 0x38400
je 0x557e
jmp 0x5528
movq -0x18(%rbp), %rax
subq $0x8ca00, %rax          # imm = 0x8CA00
je 0x5563
jmp 0x5536
movq -0x18(%rbp), %rax
subq $0x119400, %rax         # imm = 0x119400
jne 0x5608
jmp 0x5548
movq -0x8(%rbp), %rax
movl 0xc8(%rax), %ecx
orl $0x1009, %ecx           # imm = 0x1009
movl %ecx, 0xc8(%rax)
jmp 0x562d
movq -0x8(%rbp), %rax
movl 0xc8(%rax), %ecx
orl $0x1006, %ecx           # imm = 0x1006
movl %ecx, 0xc8(%rax)
jmp 0x562d
movq -0x8(%rbp), %rax
movl 0xc8(%rax), %ecx
orl $0x1003, %ecx           # imm = 0x1003
movl %ecx, 0xc8(%rax)
jmp 0x562d
movq -0x8(%rbp), %rax
movl 0xc8(%rax), %ecx
orl $0x1002, %ecx           # imm = 0x1002
movl %ecx, 0xc8(%rax)
jmp 0x562d
movq -0x8(%rbp), %rax
movl 0xc8(%rax), %ecx
orl $0x1001, %ecx           # imm = 0x1001
movl %ecx, 0xc8(%rax)
jmp 0x562d
movq -0x8(%rbp), %rax
movl 0xc8(%rax), %ecx
orl $0xf, %ecx
movl %ecx, 0xc8(%rax)
jmp 0x562d
movq -0x8(%rbp), %rax
movl 0xc8(%rax), %ecx
orl $0xe, %ecx
movl %ecx, 0xc8(%rax)
jmp 0x562d
movq -0x8(%rbp), %rax
movl 0xc8(%rax), %ecx
orl $0xd, %ecx
movl %ecx, 0xc8(%rax)
jmp 0x562d
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rdx
movl $0x1, %edi
leaq 0x5e1e(%rip), %rsi      # 0xb43a
movb $0x0, %al
callq 0x44e0
movl $0x3, %edi
callq 0x4250
movq -0x8(%rbp), %rax
movl $0x1405, 0xc0(%rax)     # imm = 0x1405
movq -0x8(%rbp), %rax
movl $0x0, 0xc4(%rax)
movq -0x8(%rbp), %rax
movl $0x0, 0xcc(%rax)
movq -0x8(%rbp), %rdi
addq $0xc0, %rdi
callq 0x41c0
movq -0x8(%rbp), %rax
movb $0x1, 0xd6(%rax)
movq -0x8(%rbp), %rax
movb $0x0, 0xd7(%rax)
movq -0x8(%rbp), %rax
movl 0x80(%rax), %edi
xorl %esi, %esi
callq 0x40b0
movq -0x8(%rbp), %rax
movl 0x80(%rax), %edi
movq -0x8(%rbp), %rdx
addq $0xc0, %rdx
xorl %esi, %esi
callq 0x41f0
cmpl $0x0, %eax
je 0x56cc
movl $0x1, %edi
leaq 0x5d94(%rip), %rsi      # 0xb44f
movb $0x0, %al
callq 0x44e0
movl $0x3, %edi
callq 0x4250
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)