user: 请从给定的汇编指令推导出C源代码：

```asm
<timed_write>:
  endbr64
  push   %r15
  push   %r14
  push   %r13
  push   %r12
  push   %rbp
  push   %rbx
  sub    $0x48,%rsp
  mov    %edi,%ebx
  mov    %rsi,0x8(%rsp)
  mov    %rdx,%r15
  mov    %ecx,%r12d
  mov    %fs:0x28,%rax
  mov    %rax,0x38(%rsp)
  xor    %eax,%eax
  call   <fd_get_flags@plt>
  mov    %eax,%ebp
  cmp    $0xffffffff,%eax
  je     L95
  mov    %eax,%esi
  mov    %ebx,%edi
  call   <fd_set_nonblocking@plt>
  test   %rax,%rax
  jne    L87
  test   %r12d,%r12d
  mov    $0xffffffff,%eax
  cmove  %eax,%r12d
  mov    $0x0,%r14d
  lea    0x1c(%rsp),%r13
L41:
  mov    %r13,%rdi
  call   <FD_ZERO@plt>
  mov    %r13,%rsi
  mov    %ebx,%edi
  call   <FD_SET@plt>
  mov    %r12d,0x20(%rsp)
  movq   $0x0,0x28(%rsp)
  lea    0x1(%rbx),%edi
  lea    0x20(%rsp),%r8
  mov    $0x0,%ecx
  mov    %r13,%rdx
  mov    $0x0,%esi
  call   <select@plt>
  test   %eax,%eax
  je     L80
  cmp    $0xffffffff,%eax
  jne    L101
  mov    D31(%rip),%rdx
  mov    D61(%rip),%rax
  mov    (%rax),%rax
  cmp    %rax,(%rdx)
  je     L41
  mov    $0xffffffffffffffff,%r14
L61:
  mov    %ebp,%esi
  mov    %ebx,%edi
  call   <fd_clear_nonblocking@plt>
  mov    %r14d,%ebp
L95:
  mov    0x38(%rsp),%rax
  sub    %fs:0x28,%rax
  jne    L93
  mov    %ebp,%eax
  add    $0x48,%rsp
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
L80:
  test   %r14,%r14
  jne    L61
  mov    D11(%rip),%rax
  mov    (%rax),%rdx
  mov    D31(%rip),%rax
  mov    %rdx,(%rax)
  mov    $0xffffffffffffffff,%r14
  jmp    L61
L101:
  mov    %r15,%rdx
  sub    %r14,%rdx
  mov    0x8(%rsp),%rax
  lea    (%rax,%r14,1),%rsi
  mov    %ebx,%edi
  call   <write@plt>
  cmp    $0xffffffff,%eax
  je     L113
  test   %eax,%eax
  je     L61
  cltq
  add    %rax,%r14
  jmp    L41
L113:
  mov    D31(%rip),%rax
  mov    (%rax),%rax
  mov    D61(%rip),%rdx
  cmp    (%rdx),%rax
  je     L41
  mov    D36(%rip),%rdx
  cmp    (%rdx),%rax
  je     L41
  mov    $0xffffffffffffffff,%r14
  jmp    L61
L87:
  mov    $0xffffffff,%ebp
  jmp    L95
L93:
  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;
}
```