// File: /workspace/github_cpp_makefile_star2_O1/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 $0x88, %rsp
movq %rdx, %r14
movl %esi, %ebp
xorl %ebx, %ebx
movl $0x902, %esi            # imm = 0x902
xorl %eax, %eax
callq 0x2320
cmpl $-0x1, %eax
je 0x60e1
movl %eax, %r15d
movl $0x1, %esi
movl $0x1d, %edi
callq 0x2240
movl %r15d, %edi
movl $0x4, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x20f0
movl $0x1, %edi
movl $0x10, %esi
callq 0x2210
testq %rax, %rax
je 0x5bc9
movq %rax, %rbx
movl %r15d, (%rax)
movb $0x1, %r13b
cmpl $0x7a11f, %ebp          # imm = 0x7A11F
jle 0x5bd6
cmpl $0x16e35f, %ebp         # imm = 0x16E35F
jle 0x5c19
cmpl $0x2dc6bf, %ebp         # imm = 0x2DC6BF
jg 0x5c8f
cmpl $0x16e360, %ebp         # imm = 0x16E360
je 0x5d42
cmpl $0x1e8480, %ebp         # imm = 0x1E8480
je 0x5d6a
cmpl $0x2625a0, %ebp         # imm = 0x2625A0
jne 0x5d82
movl $0x100c, %r12d          # imm = 0x100C
jmp 0x5d8b
movl %r15d, %edi
callq 0x21d0
jmp 0x60df
cmpl $0x95ff, %ebp           # imm = 0x95FF
jg 0x5c54
cmpl $0x12bf, %ebp           # imm = 0x12BF
jle 0x5ce1
cmpl $0x12c0, %ebp           # imm = 0x12C0
je 0x5d32
cmpl $0x2580, %ebp           # imm = 0x2580
je 0x5d5a
cmpl $0x4b00, %ebp           # imm = 0x4B00
jne 0x5d82
movl $0xe, %r12d
jmp 0x5d8b
cmpl $0xe0fff, %ebp          # imm = 0xE0FFF
jle 0x5cbe
cmpl $0xe1000, %ebp          # imm = 0xE1000
je 0x5d22
cmpl $0xf4240, %ebp          # imm = 0xF4240
je 0x5d2a
cmpl $0x119400, %ebp         # imm = 0x119400
jne 0x5d82
movl $0x1009, %r12d          # imm = 0x1009
jmp 0x5d8b
cmpl $0x1c1ff, %ebp          # imm = 0x1C1FF
jle 0x5d0a
cmpl $0x1c200, %ebp          # imm = 0x1C200
je 0x5d3a
cmpl $0x38400, %ebp          # imm = 0x38400
je 0x5d62
cmpl $0x70800, %ebp          # imm = 0x70800
jne 0x5d82
movl $0x1004, %r12d          # imm = 0x1004
jmp 0x5d8b
cmpl $0x2dc6c0, %ebp         # imm = 0x2DC6C0
je 0x5d4a
cmpl $0x3567e0, %ebp         # imm = 0x3567E0
je 0x5d72
cmpl $0x3d0900, %ebp         # imm = 0x3D0900
jne 0x5d82
movl $0x100f, %r12d          # imm = 0x100F
jmp 0x5d8b
cmpl $0x7a120, %ebp          # imm = 0x7A120
je 0x5d52
cmpl $0x8ca00, %ebp          # imm = 0x8CA00
jne 0x5d82
movl $0x1006, %r12d          # imm = 0x1006
jmp 0x5d8b
movl $0x9, %r12d
cmpl $0x4b0, %ebp            # imm = 0x4B0
je 0x5d8b
cmpl $0x960, %ebp            # imm = 0x960
jne 0x5d82
movl $0xb, %r12d
jmp 0x5d8b
cmpl $0x9600, %ebp           # imm = 0x9600
je 0x5d7a
cmpl $0xe100, %ebp           # imm = 0xE100
jne 0x5d82
movl $0x1001, %r12d          # imm = 0x1001
jmp 0x5d8b
movl $0x1007, %r12d          # imm = 0x1007
jmp 0x5d8b
movl $0x1008, %r12d          # imm = 0x1008
jmp 0x5d8b
movl $0xc, %r12d
jmp 0x5d8b
movl $0x1002, %r12d          # imm = 0x1002
jmp 0x5d8b
movl $0x100a, %r12d          # imm = 0x100A
jmp 0x5d8b
movl $0x100d, %r12d          # imm = 0x100D
jmp 0x5d8b
movl $0x1005, %r12d          # imm = 0x1005
jmp 0x5d8b
movl $0xd, %r12d
jmp 0x5d8b
movl $0x1003, %r12d          # imm = 0x1003
jmp 0x5d8b
movl $0x100b, %r12d          # imm = 0x100B
jmp 0x5d8b
movl $0x100e, %r12d          # imm = 0x100E
jmp 0x5d8b
movl $0xf, %r12d
jmp 0x5d8b
movl $0xf, %r12d
xorl %r13d, %r13d
leaq 0x4(%rsp), %rsi
movl %r15d, %edi
callq 0x2300
testl %eax, %eax
js 0x5fb3
movl %r13d, (%rsp)
leaq 0x4(%rsp), %r15
movq %r15, %rdi
movl %r12d, %esi
callq 0x2140
movq %r15, %rdi
movl %r12d, %esi
callq 0x2340
movl $0xfffff37f, %r12d      # imm = 0xFFFFF37F
andl 0x8(%r15), %r12d
leal 0x880(%r12), %r13d
movl %r13d, 0x8(%r15)
movq %r14, %rdi
movl $0x4e, %esi
callq 0x2170
testq %rax, %rax
jne 0x5e07
callq 0x23f0
movq (%rax), %r15
movl 0x138(%r15), %esi
movq %r14, %rdi
callq 0x2170
testq %rax, %rax
je 0x5fca
andl $0xfffffaff, %r13d      # imm = 0xFFFFFAFF
movl %r13d, 0xc(%rsp)
movq %r14, %rdi
movl $0x32, %esi
callq 0x2170
testq %rax, %rax
jne 0x5e44
callq 0x23f0
movq (%rax), %rax
movl 0xc8(%rax), %esi
movq %r14, %rdi
callq 0x2170
testq %rax, %rax
je 0x6007
movl 0xc(%rsp), %r15d
orl $0x40, %r15d
movl %r15d, 0xc(%rsp)
movq %r14, %rdi
movl $0x57, %esi
callq 0x2170
testq %rax, %rax
jne 0x5e7f
callq 0x23f0
movq (%rax), %rax
movl 0x15c(%rax), %esi
movq %r14, %rdi
callq 0x2170
testq %rax, %rax
je 0x5e84
orb $-0x80, 0x10(%rsp)
movl %r15d, %r12d
andl $-0x31, %r12d
movl %r12d, 0xc(%rsp)
movq %r14, %rdi
movl $0x38, %esi
callq 0x2170
testq %rax, %rax
jne 0x5ec2
callq 0x23f0
movq (%rax), %r13
movl 0xe0(%r13), %esi
movq %r14, %rdi
callq 0x2170
testq %rax, %rax
je 0x6015
orl $0x30, %r15d
movl %r15d, 0xc(%rsp)
movq %r14, %rdi
movl $0x46, %esi
callq 0x2170
testq %rax, %rax
jne 0x5efc
callq 0x23f0
movq (%rax), %rax
movl 0x118(%rax), %esi
movq %r14, %rdi
callq 0x2170
testq %rax, %rax
je 0x604b
movl $0x80000000, %eax       # imm = 0x80000000
orl 0xc(%rsp), %eax
movl (%rsp), %r13d
leaq 0x4(%rsp), %r15
movl %eax, 0x8(%r15)
andb $-0x1c, 0xc(%r15)
andl $0xffffe23f, (%r15)     # imm = 0xFFFFE23F
andb $-0x2, 0x4(%r15)
movw $0x0, 0x16(%r15)
movl (%rbx), %edi
movl $0x2, %esi
callq 0x2160
movl (%rbx), %edi
xorl %r12d, %r12d
xorl %esi, %esi
movq %r15, %rdx
callq 0x2310
testl %eax, %eax
js 0x5fbd
movq %r14, %rdi
movl $0x52, %esi
callq 0x2170
testq %rax, %rax
jne 0x5f7a
callq 0x23f0
movq (%rax), %rax
movl 0x148(%rax), %esi
movq %r14, %rdi
callq 0x2170
testq %rax, %rax
je 0x6059
leaq 0x40(%rsp), %rdx
movl $0x13, (%rdx)
movl (%rbx), %edi
xorl %r12d, %r12d
movl $0x542f, %esi           # imm = 0x542F
xorl %eax, %eax
callq 0x21c0
movl %eax, %r14d
testl %eax, %eax
jns 0x5fa5
callq 0x2060
subl (%rax), %r12d
testl %r14d, %r14d
jns 0x6059
jmp 0x60c6
callq 0x2060
xorl %r12d, %r12d
jmp 0x5fc2
callq 0x2060
subl (%rax), %r12d
jmp 0x60c6
movq %r14, %rdi
movl $0x45, %esi
callq 0x2170
testq %rax, %rax
jne 0x5ff4
movl 0x114(%r15), %esi
movq %r14, %rdi
callq 0x2170
testq %rax, %rax
je 0x60f6
andl $0xfffff8ff, %r13d      # imm = 0xFFFFF8FF
orl $0x100, %r13d           # imm = 0x100
jmp 0x5e0e
movl 0xc(%rsp), %r15d
andl $-0x41, %r15d
jmp 0x5e4d
movq %r14, %rdi
movl $0x37, %esi
callq 0x2170
testq %rax, %rax
jne 0x603f
movl 0xdc(%r13), %esi
movq %r14, %rdi
callq 0x2170
testq %rax, %rax
je 0x612f
orl $0x20, %r12d
movl %r12d, %r15d
jmp 0x5ec6
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl 0xc(%rsp), %eax
jmp 0x5f05
testb %r13b, %r13b
jne 0x60c3
movl (%rbx), %edi
leaq 0x40(%rsp), %rdx
movl $0x541e, %esi           # imm = 0x541E
xorl %eax, %eax
callq 0x21c0
testl %eax, %eax
js 0x60aa
movl 0x5c(%rsp), %eax
cltd
idivl %ebp
testl %eax, %eax
je 0x60b7
movl $0xffffefcf, %ecx       # imm = 0xFFFFEFCF
leaq 0x40(%rsp), %rdx
andl 0x10(%rdx), %ecx
orl $0x30, %ecx
movl %ecx, 0x10(%rdx)
movl %eax, 0x18(%rdx)
movl (%rbx), %edi
movl $0x541f, %esi           # imm = 0x541F
xorl %eax, %eax
callq 0x21c0
movb $0x1, %cl
testl %eax, %eax
jns 0x60bf
callq 0x2060
xorl %r12d, %r12d
subl (%rax), %r12d
jmp 0x60bd
movl $0xffffffea, %r12d      # imm = 0xFFFFFFEA
xorl %ecx, %ecx
testb %cl, %cl
je 0x60c6
xorl %r12d, %r12d
testl %r12d, %r12d
setns %al
cmpl $-0x19, %r12d
sete %cl
orb %al, %cl
jne 0x60e1
movq %rbx, %rdi
callq 0x5ad5
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
movl $0x4f, %esi
callq 0x2170
testq %rax, %rax
jne 0x6120
movl 0x13c(%r15), %esi
movq %r14, %rdi
callq 0x2170
testq %rax, %rax
je 0x5e13
orl $0xb80, %r12d           # imm = 0xB80
movl %r12d, %r13d
jmp 0x5e0e
movq %r14, %rdi
movl $0x36, %esi
callq 0x2170
testq %rax, %rax
jne 0x6159
movl 0xd8(%r13), %esi
movq %r14, %rdi
callq 0x2170
testq %rax, %rax
je 0x5ecb
orl $0x10, %r12d
jmp 0x6043