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

```asm
<fgetsr>:
  endbr64
  push   %rbp
  mov    %rsp,%rbp
  sub    $0x40,%rsp
  mov    %rdi,-0x28(%rbp)
  mov    %rsi,-0x30(%rbp)
  mov    %rdx,-0x38(%rbp)
  mov    %fs:0x28,%rax
  mov    %rax,-0x8(%rbp)
  xor    %eax,%eax
  movl   $0x1,-0x18(%rbp)
  cmpq   $0x1,-0x30(%rbp)
  jbe    L85
  mov    -0x28(%rbp),%rax
  mov    %rax,%rdi
  call   <ftello@plt>
  mov    %eax,-0x1c(%rbp)
  cmpl   $0xffffffff,-0x1c(%rbp)
  je     L85
  cmpl   $0x0,-0x1c(%rbp)
  jne    L127
L85:
  mov    $0x0,%eax
  jmp    L76
L127:
  mov    -0x30(%rbp),%rax
  sub    $0x1,%rax
  mov    %rax,-0x10(%rbp)
  mov    -0x38(%rbp),%rdx
  mov    -0x10(%rbp),%rax
  add    %rdx,%rax
  movb   $0x0,(%rax)
L110:
  mov    D18(%rip),%rax
  mov    (%rax),%edx
  subl   $0x1,-0x1c(%rbp)
  mov    -0x1c(%rbp),%ecx
  mov    -0x28(%rbp),%rax
  mov    %ecx,%esi
  mov    %rax,%rdi
  call   <fseeko@plt>
  test   %rax,%rax
  jne    L79
  mov    -0x28(%rbp),%rax
  mov    %rax,%rdi
  call   <fgetc@plt>
  mov    %eax,-0x14(%rbp)
  mov    D21(%rip),%rax
  mov    (%rax),%eax
  cmp    %eax,-0x14(%rbp)
  jne    L78
L79:
  mov    $0x0,%eax
  jmp    L76
L78:
  cmpl   $0xa,-0x14(%rbp)
  jne    L59
  cmpl   $0x0,-0x18(%rbp)
  je     L139
L59:
  movl   $0x0,-0x18(%rbp)
  cmpl   $0xd,-0x14(%rbp)
  je     L66
  cmpl   $0xa,-0x14(%rbp)
  je     L66
  mov    -0x14(%rbp),%eax
  mov    %eax,-0x20(%rbp)
  cmpq   $0x0,-0x10(%rbp)
  jne    L128
  mov    -0x30(%rbp),%rax
  lea    -0x2(%rax),%rdx
  mov    -0x38(%rbp),%rax
  lea    0x1(%rax),%rcx
  mov    -0x38(%rbp),%rax
  mov    %rax,%rsi
  mov    %rcx,%rdi
  call   <memmove@plt>
  addq   $0x1,-0x10(%rbp)
L128:
  subq   $0x1,-0x10(%rbp)
  mov    -0x38(%rbp),%rdx
  mov    -0x10(%rbp),%rax
  lea    (%rdx,%rax,1),%rcx
  lea    -0x20(%rbp),%rax
  mov    $0x1,%edx
  mov    %rax,%rsi
  mov    %rcx,%rdi
  call   <memcpy@plt>
L66:
  cmpl   $0x0,-0x1c(%rbp)
  jne    L110
  mov    D18(%rip),%rax
  mov    (%rax),%edx
  mov    -0x28(%rbp),%rax
  mov    $0x0,%esi
  mov    %rax,%rdi
  call   <fseeko@plt>
  jmp    L29
L139:
  nop
L29:
  mov    -0x30(%rbp),%rax
  sub    -0x10(%rbp),%rax
  mov    %rax,%rdx
  mov    -0x38(%rbp),%rcx
  mov    -0x10(%rbp),%rax
  add    %rax,%rcx
  mov    -0x38(%rbp),%rax
  mov    %rcx,%rsi
  mov    %rax,%rdi
  call   <memmove@plt>
  mov    -0x30(%rbp),%rax
  sub    -0x10(%rbp),%rax
  sub    $0x1,%rax
L76:
  mov    -0x8(%rbp),%rdx
  sub    %fs:0x28,%rdx
  je     L88
  call   <__stack_chk_fail@plt>
L88:
  leave
  ret
```

assistant: ```c
size_t fgetsr(FILE *fs, size_t n, char *buf) {
  off_t fpos;
  size_t bufpos;
  bool first = true;
  if (n <= 1 || (fpos = ftello(fs)) == -1 || fpos == 0) {
    return 0;
  }
  bufpos = n - 1;
  buf[bufpos] = '\0';
  while (true) {
    int c;
    if (fseeko(fs, --fpos, SEEK_SET) != 0 || (c = fgetc(fs)) == EOF) {
      return 0;
    }
    if (c == '\n' && !first) {
      break;
    }
    first = false;
    if (c != '\r' && c != '\n') {
      uint8_t ch = (uint8_t)c;
      if (bufpos == 0) {
        memmove(buf + 1, buf, n - 2);
        ++bufpos;
      }
      memcpy(buf + --bufpos, &ch, 1);
    }
    if (fpos == 0) {
      fseeko(fs, 0, SEEK_SET);
      break;
    }
  }
  memmove(buf, buf + bufpos, n - bufpos);
  return n - bufpos - 1;
}
```