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

```asm
<readranges>:
  endbr64
  push   %r15
  push   %r14
  push   %r13
  push   %r12
  push   %rbp
  push   %rbx
  sub    $0x38,%rsp
  mov    %fs:0x28,%rax
  mov    %rax,0x28(%rsp)
  xor    %eax,%eax
  movq   $0x0,0x8(%rsp)
  test   %edi,%edi
  jne    L146
  call   <getgid@plt>
  lea    D6(%rip),%rdi
  mov    %eax,%esi
  call   <string@plt>
  lea    D56(%rip),%rdi
L124:
  lea    D80(%rip),%rsi
  mov    %rax,0x10(%rsp)
  call   <fopen@plt>
  mov    %rax,%rbp
  test   %rax,%rax
  je     L108
  lea    D24(%rip),%rdi
  call   <getenv@plt>
  mov    %rax,%rbx
  test   %rax,%rax
  je     L51
L57:
  mov    %rbx,%rdi
  call   <getpwnam@plt>
  test   %rax,%rax
  je     L95
  mov    (%rax),%r12
  xor    %eax,%eax
  call   <getuid@plt>
  cmp    %rax,%r12
  je     L133
L95:
  xor    %eax,%eax
  call   <getuid@plt>
  mov    %rax,%rdi
  call   <getpwuid@plt>
  mov    %rax,%rbx
  test   %rax,%rax
  je     L94
L61:
  mov    0x8(%rbx),%rbx
L133:
  xor    %eax,%eax
  lea    0x20(%rsp),%r13
  lea    0x8(%rsp),%r12
  call   <endpwent@plt>
L96:
  lea    0x18(%rsp),%r15
  mov    %rsp,%r14
  cs nopw 0x0(%rax,%rax,1)
L127:
  mov    %rbp,%rdx
  mov    %r13,%rsi
  mov    %r12,%rdi
  call   <getline@plt>
  test   %rax,%rax
  js     L162
  mov    %rbx,%rdi
  call   <strlen@plt>
  mov    0x8(%rsp),%rdi
  mov    %rbx,%rsi
  mov    %rax,%rdx
  call   <strncmp@plt>
  test   %rax,%rax
  jne    L127
  mov    %rbx,%rdi
  call   <strlen@plt>
  add    0x8(%rsp),%rax
  mov    %r15,%r8
  mov    %r14,%rcx
  lea    0x4(%rsp),%rdx
  mov    %rax,%rdi
  lea    D89(%rip),%rsi
  call   <sscanf@plt>
  cmp    $0x1,%eax
  jle    L127
  mov    %rbx,%rdi
  call   <strlen@plt>
  add    0x8(%rsp),%rax
  add    0x18(%rsp),%rax
  lea    D37(%rip),%rdi
  movsbl 0x1(%rax),%esi
  call   <strchr@plt>
  test   %rax,%rax
  je     L127
  mov    (%rsp),%ecx
  mov    0x4(%rsp),%edx
  lea    0x10(%rsp),%rdi
  lea    D54(%rip),%rsi
  call   <append@plt>
  jmp    L96
  nopl   0x0(%rax,%rax,1)
L162:
  mov    0x8(%rsp),%rdi
  call   <free@plt>
  mov    %rbp,%rdi
  call   <fclose@plt>
L108:
  mov    0x10(%rsp),%rax
  mov    0x28(%rsp),%rdx
  sub    %fs:0x28,%rdx
  jne    L49
  add    $0x38,%rsp
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
  nopw   0x0(%rax,%rax,1)
L146:
  xor    %eax,%eax
  call   <getuid@plt>
  lea    D6(%rip),%rdi
  mov    %eax,%esi
  call   <string@plt>
  lea    D104(%rip),%rdi
  jmp    L124
  nopl   0x0(%rax)
L94:
  lea    D29(%rip),%rdx
  xor    %esi,%esi
  mov    $0x1,%edi
  call   <error@plt>
  jmp    L61
L51:
  lea    D98(%rip),%rdi
  call   <getenv@plt>
  mov    %rax,%rbx
  test   %rax,%rax
  jne    L57
  xor    %eax,%eax
  call   <getlogin@plt>
  mov    %rax,%rbx
  test   %rax,%rax
  je     L95
  jmp    L57
L49:
  call   <__stack_chk_fail@plt>
```

tool_calls: name: disassemble_data
arguments: data_tag: D6
datatype: string
name: disassemble_data
arguments: data_tag: D56
datatype: string
name: disassemble_data
arguments: data_tag: D80
datatype: string
name: disassemble_data
arguments: data_tag: D24
datatype: string
name: disassemble_data
arguments: data_tag: D89
datatype: string
name: disassemble_data
arguments: data_tag: D37
datatype: string
name: disassemble_data
arguments: data_tag: D54
datatype: string
name: disassemble_data
arguments: data_tag: D104
datatype: string
name: disassemble_data
arguments: data_tag: D29
datatype: string
name: disassemble_data
arguments: data_tag: D98
datatype: string

tool: D6: %u:1

tool: D56: /etc/subgid

tool: D80: r

tool: D24: USER

tool: D89: :%u:%u%zn

tool: D37: :


tool: D54: ,%u:%u

tool: D104: /etc/subuid

tool: D29: Failed to validate your username

tool: D98: LOGNAME

assistant: ```c
char *readranges(int type) {
  char *line = NULL, *range, *user;
  size_t end, size;
  struct passwd *passwd;
  unsigned length, start;
  FILE *file;
  range = string("%u:1", ((unsigned)((type) == 0 ? getgid() : getuid())));
  if (!(file = fopen(((type) == 0 ? "/etc/subgid" : "/etc/subuid"), "r")))
    return range;
  user = getenv("USER");
  user = user ? user : getenv("LOGNAME");
  user = user ? user : getlogin();
  if (!user || !(passwd = getpwnam(user)) || passwd->pw_uid != getuid()) {
    if (!(passwd = getpwuid(getuid())))
      error(1, 0, "Failed to validate your username");
    user = passwd->pw_name;
  }
  endpwent();
  while (getline(&line, &size, file) >= 0) {
    if (strncmp(line, user, strlen(user)))
      continue;
    if (sscanf(line + strlen(user), ":%u:%u%zn", &start, &length, &end) < 2)
      continue;
    if (strchr(":\n", line[end + strlen(user) + 1]))
      append(&range, ",%u:%u", start, length);
  }
  free(line);
  fclose(file);
  return range;
}
```