// File: /workspace/github_cpp_makefile_star2_O3/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 $0x98, %rsp
movq %rdx, %r15
movl %esi, %r14d
xorl %ebx, %ebx
movl $0x902, %esi            # imm = 0x902
xorl %eax, %eax
callq 0x2320
cmpl $-0x1, %eax
je 0x5f17
movl %eax, %ebp
movl $0x1, %esi
movl $0x1d, %edi
callq 0x2240
movl %ebp, %edi
movl $0x4, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x20f0
movl $0x1, %edi
movl $0x10, %esi
callq 0x2210
testq %rax, %rax
je 0x5ac7
movq %rax, %rbx
movl %ebp, (%rax)
movb $0x1, %r13b
cmpl $0x7a11f, %r14d         # imm = 0x7A11F
jle 0x5ad3
cmpl $0x16e35f, %r14d        # imm = 0x16E35F
jle 0x5b1b
cmpl $0x2dc6bf, %r14d        # imm = 0x2DC6BF
jg 0x5b99
cmpl $0x16e360, %r14d        # imm = 0x16E360
je 0x5c55
cmpl $0x1e8480, %r14d        # imm = 0x1E8480
je 0x5c7d
cmpl $0x2625a0, %r14d        # imm = 0x2625A0
jne 0x5c95
movl $0x100c, %r12d          # imm = 0x100C
jmp 0x5c9e
movl %ebp, %edi
callq 0x21d0
jmp 0x5f15
cmpl $0x95ff, %r14d          # imm = 0x95FF
jg 0x5b5a
cmpl $0x12bf, %r14d          # imm = 0x12BF
jle 0x5bf0
cmpl $0x12c0, %r14d          # imm = 0x12C0
je 0x5c45
cmpl $0x2580, %r14d          # imm = 0x2580
je 0x5c6d
cmpl $0x4b00, %r14d          # imm = 0x4B00
jne 0x5c95
movl $0xe, %r12d
jmp 0x5c9e
cmpl $0xe0fff, %r14d         # imm = 0xE0FFF
jle 0x5bcb
cmpl $0xe1000, %r14d         # imm = 0xE1000
je 0x5c35
cmpl $0xf4240, %r14d         # imm = 0xF4240
je 0x5c3d
cmpl $0x119400, %r14d        # imm = 0x119400
jne 0x5c95
movl $0x1009, %r12d          # imm = 0x1009
jmp 0x5c9e
cmpl $0x1c1ff, %r14d         # imm = 0x1C1FF
jle 0x5c1b
cmpl $0x1c200, %r14d         # imm = 0x1C200
je 0x5c4d
cmpl $0x38400, %r14d         # imm = 0x38400
je 0x5c75
cmpl $0x70800, %r14d         # imm = 0x70800
jne 0x5c95
movl $0x1004, %r12d          # imm = 0x1004
jmp 0x5c9e
cmpl $0x2dc6c0, %r14d        # imm = 0x2DC6C0
je 0x5c5d
cmpl $0x3567e0, %r14d        # imm = 0x3567E0
je 0x5c85
cmpl $0x3d0900, %r14d        # imm = 0x3D0900
jne 0x5c95
movl $0x100f, %r12d          # imm = 0x100F
jmp 0x5c9e
cmpl $0x7a120, %r14d         # imm = 0x7A120
je 0x5c65
cmpl $0x8ca00, %r14d         # imm = 0x8CA00
jne 0x5c95
movl $0x1006, %r12d          # imm = 0x1006
jmp 0x5c9e
movl $0x9, %r12d
cmpl $0x4b0, %r14d           # imm = 0x4B0
je 0x5c9e
cmpl $0x960, %r14d           # imm = 0x960
jne 0x5c95
movl $0xb, %r12d
jmp 0x5c9e
cmpl $0x9600, %r14d          # imm = 0x9600
je 0x5c8d
cmpl $0xe100, %r14d          # imm = 0xE100
jne 0x5c95
movl $0x1001, %r12d          # imm = 0x1001
jmp 0x5c9e
movl $0x1007, %r12d          # imm = 0x1007
jmp 0x5c9e
movl $0x1008, %r12d          # imm = 0x1008
jmp 0x5c9e
movl $0xc, %r12d
jmp 0x5c9e
movl $0x1002, %r12d          # imm = 0x1002
jmp 0x5c9e
movl $0x100a, %r12d          # imm = 0x100A
jmp 0x5c9e
movl $0x100d, %r12d          # imm = 0x100D
jmp 0x5c9e
movl $0x1005, %r12d          # imm = 0x1005
jmp 0x5c9e
movl $0xd, %r12d
jmp 0x5c9e
movl $0x1003, %r12d          # imm = 0x1003
jmp 0x5c9e
movl $0x100b, %r12d          # imm = 0x100B
jmp 0x5c9e
movl $0x100e, %r12d          # imm = 0x100E
jmp 0x5c9e
movl $0xf, %r12d
jmp 0x5c9e
movl $0xf, %r12d
xorl %r13d, %r13d
leaq 0x10(%rsp), %rsi
movl %ebp, %edi
callq 0x2300
testl %eax, %eax
js 0x5ef1
movl %r14d, 0x4(%rsp)
movl %r13d, (%rsp)
leaq 0x10(%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
movq %r15, %rdi
movl $0x4e, %esi
callq 0x2170
testq %rax, %rax
jne 0x5d1a
callq 0x23f0
movq (%rax), %r14
movl 0x138(%r14), %esi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
je 0x5f2c
andl $0xfffffaff, %r13d      # imm = 0xFFFFFAFF
movq %r15, %rdi
movl $0x32, %esi
callq 0x2170
testq %rax, %rax
jne 0x5d52
callq 0x23f0
movq (%rax), %rax
movl 0xc8(%rax), %esi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
je 0x5f6e
orl $0x40, %r13d
movq %r15, %rdi
movl $0x57, %esi
callq 0x2170
testq %rax, %rax
movl 0x4(%rsp), %r14d
jne 0x5d88
callq 0x23f0
movq (%rax), %rax
movl 0x15c(%rax), %esi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
je 0x5d8d
orb $-0x80, 0x1c(%rsp)
movq %r15, %rdi
movl $0x38, %esi
callq 0x2170
testq %rax, %rax
jne 0x5dc0
callq 0x23f0
movq (%rax), %r12
movl 0xe0(%r12), %esi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
je 0x5f77
orl $0x30, %r13d
movq %r15, %rdi
movl $0x46, %esi
callq 0x2170
testq %rax, %rax
jne 0x5df5
callq 0x23f0
movq (%rax), %rax
movl 0x118(%rax), %esi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
je 0x5fab
orl $0x80000000, %r13d      # imm = 0x80000000
leaq 0x10(%rsp), %r12
movl %r13d, 0x8(%r12)
andb $-0x1c, 0xc(%r12)
movabsq $-0x100001dc1, %rax     # imm = 0xFFFFFFFEFFFFE23F
andq %rax, (%r12)
movw $0x0, 0x16(%r12)
movl %ebp, %edi
movl $0x2, %esi
callq 0x2160
xorl %r13d, %r13d
movl %ebp, %edi
xorl %esi, %esi
movq %r12, %rdx
callq 0x2310
testl %eax, %eax
js 0x5eea
movq %r15, %rdi
movl $0x52, %esi
callq 0x2170
testq %rax, %rax
jne 0x5e72
callq 0x23f0
movq (%rax), %rax
movl 0x148(%rax), %esi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
je 0x5e92
leaq 0x50(%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 0x5eea
cmpb $0x0, (%rsp)
jne 0x5f17
xorl %r13d, %r13d
leaq 0x50(%rsp), %rdx
movl $0x541e, %esi           # imm = 0x541E
movl %ebp, %edi
xorl %eax, %eax
callq 0x21c0
testl %eax, %eax
js 0x5eea
movl 0x6c(%rsp), %eax
cltd
idivl %r14d
testl %eax, %eax
je 0x5fb7
movl $0xffffefcf, %ecx       # imm = 0xFFFFEFCF
leaq 0x50(%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 0x5f17
callq 0x2060
jmp 0x5ef9
callq 0x2060
xorl %r13d, %r13d
subl (%rax), %r13d
testl %r13d, %r13d
setns %al
cmpl $-0x19, %r13d
sete %cl
orb %al, %cl
jne 0x5f17
movq %rbx, %rdi
callq 0x59cf
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, 0x8(%rsp)
movq %r15, %rdi
movl $0x45, %esi
callq 0x2170
testq %rax, %rax
jne 0x5f5b
movq 0x8(%rsp), %rax
movl 0x114(%rax), %esi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
je 0x5fc2
andl $0xfffff8ff, %r13d      # imm = 0xFFFFF8FF
orl $0x100, %r13d           # imm = 0x100
jmp 0x5d21
andl $-0x41, %r13d
jmp 0x5d56
andl $-0x31, %r13d
movq %r15, %rdi
movl $0x37, %esi
callq 0x2170
testq %rax, %rax
jne 0x5fa2
movl 0xdc(%r12), %esi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
je 0x5fff
orl $0x20, %r13d
jmp 0x5dc4
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
jmp 0x5dfc
movl $0xffffffea, %r13d      # imm = 0xFFFFFFEA
jmp 0x5efc
movq %r15, %rdi
movl $0x4f, %esi
callq 0x2170
testq %rax, %rax
jne 0x5ff0
movq 0x8(%rsp), %rax
movl 0x13c(%rax), %esi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
je 0x5d21
orl $0xb80, %r12d           # imm = 0xB80
movl %r12d, %r13d
jmp 0x5d21
movq %r15, %rdi
movl $0x36, %esi
callq 0x2170
testq %rax, %rax
jne 0x602a
movl 0xd8(%r12), %esi
movq %r15, %rdi
callq 0x2170
testq %rax, %rax
je 0x5dc4
orl $0x10, %r13d
jmp 0x5dc4