// File: /workspace/github_cpp_makefile_star2_O2/AndreRenaud[P]simple_uart/simple_uart.c
// Function: simple_uart_open
struct simple_uart *simple_uart_open(const char *device, int speed, const char *mode_string)
{
    struct simple_uart *retval;

#ifdef _WIN32
    HANDLE port;
    DWORD mode = GENERIC_READ | GENERIC_WRITE;
    char full_port_name[32];

    snprintf(full_port_name, sizeof(full_port_name), "\\\\.\\%s", device);
    full_port_name[sizeof(full_port_name) - 1] = '\0';

    port = CreateFile(full_port_name, mode, 0, NULL, OPEN_EXISTING, 0, NULL);
    if (port == INVALID_HANDLE_VALUE)
        return NULL;
    retval = calloc(1, sizeof(struct simple_uart));
    if (!retval) {
        CloseHandle(port);
        return NULL;
    }
    retval->port = port;
#else
    int fd;
    int mode = O_RDWR | O_NDELAY | O_NOCTTY;

    fd = open(device, mode);

    if (fd == -1)
        return NULL;

    signal(SIGIO, SIG_IGN); // so we don't get those 'I/O possible' lines

    fcntl(fd, F_SETFL, 0);
    retval = calloc(1, sizeof(struct simple_uart));
    if (!retval) {
        close(fd);
        return NULL;
    }
    retval->fd = fd;
#endif
    int r = simple_uart_set_config(retval, speed, mode_string);
    if (r < 0 && r != -ENOTTY) {
        simple_uart_close(retval);
        return NULL;
    }
    return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdx, %r15
movl %esi, %r14d
xorl %ebx, %ebx
movl $0x902, %esi            # imm = 0x902
xorl %eax, %eax
callq 0x2320
cmpl $-0x1, %eax
je 0x595f
movl %eax, %ebp
pushq $0x1d
popq %rdi
pushq $0x1
popq %rbx
movq %rbx, %rsi
callq 0x2240
pushq $0x4
popq %rsi
movl %ebp, %edi
xorl %edx, %edx
xorl %eax, %eax
callq 0x20f0
pushq $0x10
popq %rsi
movq %rbx, %rdi
callq 0x2210
testq %rax, %rax
je 0x5658
movq %rax, %rbx
movl %ebp, (%rax)
movb $0x1, %r13b
cmpl $0x4b0, %r14d           # imm = 0x4B0
je 0x56c8
cmpl $0x3d0900, %r14d        # imm = 0x3D0900
je 0x56b8
cmpl $0x12c0, %r14d          # imm = 0x12C0
je 0x56f0
cmpl $0x2580, %r14d          # imm = 0x2580
je 0x56dc
cmpl $0x4b00, %r14d          # imm = 0x4B00
je 0x56b4
pushq $0xf
popq %r12
cmpl $0x9600, %r14d          # imm = 0x9600
je 0x56f4
cmpl $0xe100, %r14d          # imm = 0xE100
je 0x5697
cmpl $0x1c200, %r14d         # imm = 0x1C200
je 0x5664
cmpl $0x38400, %r14d         # imm = 0x38400
je 0x56cc
cmpl $0x70800, %r14d         # imm = 0x70800
je 0x567f
cmpl $0x7a120, %r14d         # imm = 0x7A120
je 0x566f
cmpl $0x8ca00, %r14d         # imm = 0x8CA00
je 0x56e0
cmpl $0xe1000, %r14d         # imm = 0xE1000
je 0x5687
cmpl $0xf4240, %r14d         # imm = 0xF4240
je 0x56e8
cmpl $0x119400, %r14d        # imm = 0x119400
je 0x56a7
cmpl $0x16e360, %r14d        # imm = 0x16E360
je 0x56d4
cmpl $0x1e8480, %r14d        # imm = 0x1E8480
je 0x56c0
cmpl $0x2625a0, %r14d        # imm = 0x2625A0
je 0x5677
cmpl $0x2dc6c0, %r14d        # imm = 0x2DC6C0
je 0x569f
cmpl $0x3567e0, %r14d        # imm = 0x3567E0
je 0x568f
cmpl $0x960, %r14d           # imm = 0x960
jne 0x56af
pushq $0xb
jmp 0x56f2
movl %ebp, %edi
callq 0x21d0
jmp 0x595d
movl $0x1002, %r12d          # imm = 0x1002
jmp 0x56f4
movl $0x1005, %r12d          # imm = 0x1005
jmp 0x56f4
movl $0x100c, %r12d          # imm = 0x100C
jmp 0x56f4
movl $0x1004, %r12d          # imm = 0x1004
jmp 0x56f4
movl $0x1007, %r12d          # imm = 0x1007
jmp 0x56f4
movl $0x100e, %r12d          # imm = 0x100E
jmp 0x56f4
movl $0x1001, %r12d          # imm = 0x1001
jmp 0x56f4
movl $0x100d, %r12d          # imm = 0x100D
jmp 0x56f4
movl $0x1009, %r12d          # imm = 0x1009
jmp 0x56f4
xorl %r13d, %r13d
jmp 0x56f4
pushq $0xe
jmp 0x56f2
movl $0x100f, %r12d          # imm = 0x100F
jmp 0x56f4
movl $0x100b, %r12d          # imm = 0x100B
jmp 0x56f4
pushq $0x9
jmp 0x56f2
movl $0x1003, %r12d          # imm = 0x1003
jmp 0x56f4
movl $0x100a, %r12d          # imm = 0x100A
jmp 0x56f4
pushq $0xd
jmp 0x56f2
movl $0x1006, %r12d          # imm = 0x1006
jmp 0x56f4
movl $0x1008, %r12d          # imm = 0x1008
jmp 0x56f4
pushq $0xc
popq %r12
leaq 0x20(%rsp), %rsi
movl %ebp, %edi
callq 0x2300
testl %eax, %eax
js 0x5939
movq %rbx, 0x18(%rsp)
movl %r13d, 0xc(%rsp)
leaq 0x20(%rsp), %r13
movq %r13, %rdi
movl %r12d, %esi
callq 0x2140
movq %r13, %rdi
movl %r12d, %esi
callq 0x2340
movl $0xfffff37f, %r12d      # imm = 0xFFFFF37F
andl 0x8(%r13), %r12d
leal 0x880(%r12), %r13d
pushq $0x4e
popq %rsi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
jne 0x576e
callq 0x23f0
movq (%rax), %rbx
movl 0x138(%rbx), %esi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
je 0x5974
andl $0xfffffaff, %r13d      # imm = 0xFFFFFAFF
pushq $0x32
popq %rsi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
jne 0x57a4
callq 0x23f0
movq (%rax), %rax
movl 0xc8(%rax), %esi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
je 0x59b4
orl $0x40, %r13d
pushq $0x57
popq %rsi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
movq 0x18(%rsp), %rbx
jne 0x57d8
callq 0x23f0
movq (%rax), %rax
movl 0x15c(%rax), %esi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
je 0x57dd
orb $-0x80, 0x2c(%rsp)
pushq $0x38
popq %rsi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
jne 0x580e
callq 0x23f0
movq (%rax), %r12
movl 0xe0(%r12), %esi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
je 0x59bd
orl $0x30, %r13d
pushq $0x46
popq %rsi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
jne 0x5841
callq 0x23f0
movq (%rax), %rax
movl 0x118(%rax), %esi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
je 0x59ef
orl $0x80000000, %r13d      # imm = 0x80000000
leaq 0x20(%rsp), %r12
movl %r13d, 0x8(%r12)
andb $-0x1c, 0xc(%r12)
movabsq $-0x100001dc1, %rax     # imm = 0xFFFFFFFEFFFFE23F
andq %rax, (%r12)
andw $0x0, 0x16(%r12)
pushq $0x2
popq %rsi
movl %ebp, %edi
callq 0x2160
xorl %r13d, %r13d
movl %ebp, %edi
xorl %esi, %esi
movq %r12, %rdx
callq 0x2310
testl %eax, %eax
js 0x5932
pushq $0x52
popq %rsi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
jne 0x58b9
callq 0x23f0
movq (%rax), %rax
movl 0x148(%rax), %esi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
je 0x58d9
leaq 0x60(%rsp), %rdx
movl $0x13, (%rdx)
xorl %r13d, %r13d
movl $0x542f, %esi           # imm = 0x542F
movl %ebp, %edi
xorl %eax, %eax
callq 0x21c0
testl %eax, %eax
js 0x5932
cmpb $0x0, 0xc(%rsp)
jne 0x595f
xorl %r13d, %r13d
leaq 0x60(%rsp), %rdx
movl $0x541e, %esi           # imm = 0x541E
movl %ebp, %edi
xorl %eax, %eax
callq 0x21c0
testl %eax, %eax
js 0x5932
movl 0x7c(%rsp), %eax
cltd
idivl %r14d
testl %eax, %eax
je 0x59fb
movl $0xffffefcf, %ecx       # imm = 0xFFFFEFCF
leaq 0x60(%rsp), %rdx
andl 0x10(%rdx), %ecx
orl $0x30, %ecx
movl %ecx, 0x10(%rdx)
movl %eax, 0x18(%rdx)
movl $0x541f, %esi           # imm = 0x541F
movl %ebp, %edi
xorl %eax, %eax
callq 0x21c0
testl %eax, %eax
jns 0x595f
callq 0x2060
jmp 0x5941
callq 0x2060
xorl %r13d, %r13d
subl (%rax), %r13d
testl %r13d, %r13d
setns %al
cmpl $-0x19, %r13d
sete %cl
orb %al, %cl
jne 0x595f
movq %rbx, %rdi
callq 0x54b1
xorl %ebx, %ebx
movq %rbx, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, 0x10(%rsp)
pushq $0x45
popq %rsi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
jne 0x59a1
movq 0x10(%rsp), %rax
movl 0x114(%rax), %esi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
je 0x5a04
andl $0xfffff8ff, %r13d      # imm = 0xFFFFF8FF
orl $0x100, %r13d           # imm = 0x100
jmp 0x5775
andl $-0x41, %r13d
jmp 0x57a8
andl $-0x31, %r13d
pushq $0x37
popq %rsi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
jne 0x59e6
movl 0xdc(%r12), %esi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
je 0x5a3f
orl $0x20, %r13d
jmp 0x5812
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
jmp 0x5848
pushq $-0x16
popq %r13
jmp 0x5944
pushq $0x4f
popq %rsi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
jne 0x5a30
movq 0x10(%rsp), %rax
movl 0x13c(%rax), %esi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
je 0x5775
orl $0xb80, %r12d           # imm = 0xB80
movl %r12d, %r13d
jmp 0x5775
pushq $0x36
popq %rsi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
jne 0x5a68
movl 0xd8(%r12), %esi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
je 0x5812
orl $0x10, %r13d
jmp 0x5812