user: Converte this assembly snippet into its equivalent C code:

```asm
<timed_write>:
  endbr64
  push   %r15
  push   %r14
  push   %r13
  push   %r12
  push   %rbp
  mov    %edi,%ebp
  push   %rbx
  mov    %ecx,%ebx
  sub    $0x58,%rsp
  mov    %rsi,0x10(%rsp)
  mov    %rdx,0x18(%rsp)
  mov    %fs:0x28,%rax
  mov    %rax,0x48(%rsp)
  xor    %eax,%eax
  call   <fd_get_flags@plt>
  cmp    $0xffffffff,%eax
  je     L14
  mov    %eax,%esi
  mov    %ebp,%edi
  mov    %eax,%r13d
  call   <fd_set_nonblocking@plt>
  test   %rax,%rax
  jne    L14
  test   %ebx,%ebx
  lea    0x1(%rbp),%r15d
  lea    0x2c(%rsp),%r12
  mov    $0xffffffff,%eax
  movq   $0x0,0x8(%rsp)
  cmove  %eax,%ebx
  lea    0x30(%rsp),%r14
L149:
  mov    %r12,%rdi
  call   <FD_ZERO@plt>
  mov    %r12,%rsi
  mov    %ebp,%edi
  call   <FD_SET@plt>
  xor    %ecx,%ecx
  xor    %esi,%esi
  mov    %r14,%r8
  mov    %r12,%rdx
  mov    %r15d,%edi
  mov    %ebx,0x30(%rsp)
  movq   $0x0,0x38(%rsp)
  call   <select@plt>
  test   %eax,%eax
  je     L67
  cmp    $0xffffffff,%eax
  jne    L48
  mov    D38(%rip),%rax
  mov    D29(%rip),%rdx
  mov    (%rax),%rax
  cmp    %rax,(%rdx)
  je     L149
L74:
  mov    $0xffffffff,%r12d
L103:
  mov    %r13d,%esi
  mov    %ebp,%edi
  call   <fd_clear_nonblocking@plt>
L137:
  mov    0x48(%rsp),%rax
  sub    %fs:0x28,%rax
  jne    L128
  add    $0x58,%rsp
  mov    %r12d,%eax
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
  xchg   %ax,%ax
L48:
  mov    0x8(%rsp),%rax
  mov    0x18(%rsp),%rdx
  mov    %ebp,%edi
  mov    0x10(%rsp),%rsi
  sub    %rax,%rdx
  add    %rax,%rsi
  call   <write@plt>
  cmp    $0xffffffff,%eax
  je     L120
  test   %eax,%eax
  je     L80
  cltq
  add    %rax,0x8(%rsp)
  jmp    L149
  nopl   (%rax)
L120:
  mov    D29(%rip),%rax
  mov    D38(%rip),%rdx
  mov    (%rax),%rax
  cmp    (%rdx),%rax
  je     L149
  mov    D26(%rip),%rdx
  cmp    (%rdx),%rax
  jne    L74
  jmp    L149
  nopw   0x0(%rax,%rax,1)
L67:
  mov    0x8(%rsp),%rax
  mov    %eax,%r12d
  test   %rax,%rax
  jne    L103
  mov    D27(%rip),%rax
  mov    $0xffffffff,%r12d
  mov    (%rax),%rdx
  mov    D29(%rip),%rax
  mov    %rdx,(%rax)
  jmp    L103
L14:
  mov    $0xffffffff,%r12d
  jmp    L137
L80:
  mov    0x8(%rsp),%r12d
  jmp    L103
L128:
  call   <__stack_chk_fail@plt>
```

assistant: ```c
ssize_t timed_write(int fd, const void *buf, size_t size, unsigned int timeout) {
  int ret;
  int flags;
  fd_set set;
  size_t have;
  ssize_t count;
  struct timeval wait_time;
  flags = fd_get_flags(fd);
  if (flags == -1)
    return -1;
  if (fd_set_nonblocking(fd, flags))
    return -1;
  if (!timeout)
    timeout = (unsigned int)-1;
  have = 0;
  for (;;) {
    FD_ZERO(&set);
    FD_SET(fd, &set);
    wait_time.tv_sec = timeout;
    wait_time.tv_usec = 0;
    ret = select(fd + 1, NULL, &set, NULL, &wait_time);
    if (ret == 0) {
      if (!have) {
        errno = ETIMEDOUT;
        have = -1;
      }
      break;
    } else if (ret == -1) {
      if (errno == EINTR) {
        continue;
      } else {
        have = -1;
        break;
      }
    } else {
      count = write(fd, buf + have, size - have);
      if (count == -1) {
        if (errno == EINTR || errno == EAGAIN) {
          continue;
        } else {
          have = -1;
          break;
        }
      } else if (count == 0) {
        break;
      } else {
        have += (size_t)count;
      }
    }
  }
  (void)fd_clear_nonblocking(fd, flags);
  return have;
}
```