user: Please show me the C implementation that matches the assembly code listed below:

```asm
<serial_open>:
  endbr64
  push   %r13
  push   %r12
  push   %rbp
  mov    %esi,%ebp
  sub    $0x30,%rsp
  mov    %fs:0x28,%rax
  mov    %rax,0x28(%rsp)
  mov    D123(%rip),%rax
  mov    (%rax),%esi
  mov    D94(%rip),%rax
  or     (%rax),%esi
  mov    D178(%rip),%rax
  or     (%rax),%esi
  call   <open@plt>
  mov    %eax,%r12d
  test   %eax,%eax
  jg     L4
L31:
  mov    0x28(%rsp),%rax
  sub    %fs:0x28,%rax
  jne    L21
  add    $0x30,%rsp
  mov    %r12d,%eax
  pop    %rbp
  pop    %r12
  pop    %r13
  ret
  nopl   (%rax)
L4:
  mov    %rsp,%r13
  xor    %esi,%esi
  mov    $0x20,%edx
  mov    %r13,%rdi
  call   <memset@plt>
  mov    %r13,%rsi
  mov    %r12d,%edi
  call   <tcgetattr@plt>
  test   %rax,%rax
  jne    L63
  mov    %ebp,%esi
  mov    %r13,%rdi
  call   <cfsetospeed@plt>
  test   %rax,%rax
  jne    L63
  mov    %ebp,%esi
  mov    %r13,%rdi
  call   <cfsetispeed@plt>
  test   %rax,%rax
  jne    L63
  mov    D77(%rip),%rax
  mov    D39(%rip),%rdx
  mov    %r12d,%edi
  movq   $0x0,0x18(%rsp)
  movq   $0x0,0x10(%rsp)
  mov    D177(%rip),%rcx
  mov    (%rax),%eax
  not    %eax
  and    (%rsp),%eax
  or     (%rdx),%eax
  mov    %eax,(%rsp)
  mov    D24(%rip),%rax
  mov    D122(%rip),%rdx
  mov    (%rax),%eax
  mov    (%rdx),%edx
  or     (%rcx),%edx
  not    %edx
  not    %eax
  and    0x4(%rsp),%eax
  and    %edx,%eax
  mov    D74(%rip),%rdx
  mov    %eax,0x4(%rsp)
  mov    0x8(%rsp),%rax
  mov    (%rdx),%rdx
  movl   $0x1,(%rax,%rdx,4)
  mov    D89(%rip),%rdx
  mov    (%rdx),%rdx
  movl   $0x5,(%rax,%rdx,4)
  mov    D187(%rip),%rdx
  mov    D34(%rip),%rax
  mov    (%rax),%eax
  or     (%rdx),%eax
  mov    D168(%rip),%rdx
  or     (%rdx),%eax
  mov    D83(%rip),%rdx
  not    %eax
  and    %eax,0x4(%rsp)
  mov    (%rsp),%eax
  or     (%rdx),%eax
  mov    D3(%rip),%rdx
  mov    D157(%rip),%rcx
  or     (%rdx),%eax
  mov    D42(%rip),%rdx
  mov    (%rdx),%edx
  or     (%rcx),%edx
  not    %edx
  and    %edx,%eax
  mov    D63(%rip),%rdx
  mov    (%rdx),%edx
  not    %edx
  and    %edx,%eax
  mov    %r13,%rdx
  mov    %eax,(%rsp)
  mov    D152(%rip),%rax
  mov    (%rax),%esi
  call   <tcsetattr@plt>
  test   %rax,%rax
  je     L31
  nopl   0x0(%rax)
L63:
  mov    $0xffffffff,%r12d
  jmp    L31
L21:
  call   <__stack_chk_fail@plt>
```

assistant: ```c
int serial_open(const char *device, speed_t speed) {
  int fd = open(device, O_RDWR | O_NOCTTY | O_SYNC);
  if (fd > 0) {
    struct termios tty;
    memset(&tty, 0, sizeof tty);
    if (tcgetattr(fd, &tty) != 0) {
      return -1;
    }
    if (cfsetospeed(&tty, speed) != 0) {
      return -1;
    }
    if (cfsetispeed(&tty, speed) != 0) {
      return -1;
    }
    tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
    tty.c_iflag &= ~IGNBRK;
    tty.c_iflag &= ~INLCR;
    tty.c_iflag &= ~ICRNL;
    tty.c_lflag = 0;
    tty.c_oflag = 0;
    tty.c_cc[VMIN] = 1;
    tty.c_cc[VTIME] = 5;
    tty.c_iflag &= ~(IXON | IXOFF | IXANY);
    tty.c_cflag |= (CLOCAL | CREAD);
    tty.c_cflag &= ~(PARENB | PARODD);
    tty.c_cflag &= ~CSTOPB;
    if (tcsetattr(fd, TCSANOW, &tty) != 0) {
      return -1;
    }
  }
  return fd;
}
```