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

```asm
<tty_getw_irq>:
  endbr64
  push   %r12
  xor    %r12d,%r12d
  push   %rbp
  mov    %rdi,%rbp
  push   %rbx
  sub    $0x40,%rsp
  mov    %fs:0x28,%rax
  mov    %rax,0x38(%rsp)
  xor    %eax,%eax
  lea    0xf(%rsp),%ebx
  cs nopw 0x0(%rax,%rax,1)
L79:
  xor    %r8d,%r8d
  xor    %ecx,%ecx
  mov    $0x1,%edx
  mov    %ebx,%esi
  mov    $0xa,%edi
  call   <sys_call@plt>
  cmp    $0x1,%eax
  jne    L79
  movzbl 0xf(%rsp),%edi
  lea    -0x30(%rdi),%eax
  cmp    $0x9,%al
  jbe    L150
  cmp    $0xa,%dil
  je     L107
  cmp    $0xd,%dil
  je     L107
  cmp    $0x7f,%dil
  je     L55
  cmp    $0x20,%r12d
  jne    L79
L29:
  mov    $0x20,%ebx
  nopl   0x0(%rax)
L24:
  mov    $0x8,%edi
  call   <tty_putc@plt>
  mov    $0x20,%edi
  call   <tty_putc@plt>
  mov    $0x8,%edi
  call   <tty_putc@plt>
  sub    $0x1,%ebx
  jne    L24
L52:
  mov    $0x30,%edi
  xor    %r12d,%r12d
  call   <tty_putc@plt>
L166:
  mov    %r12d,0x0(%rbp)
  mov    0x38(%rsp),%rax
  sub    %fs:0x28,%rax
  jne    L69
  add    $0x40,%rsp
  xor    %eax,%eax
  pop    %rbx
  pop    %rbp
  pop    %r12
  ret
  nopl   0x0(%rax,%rax,1)
L107:
  cmp    $0x20,%r12d
  je     L29
  test   %r12d,%r12d
  je     L166
  lea    -0x1(%r12),%edx
  lea    0x10(%rsp),%rax
  xor    %esi,%esi
  xor    %ecx,%ecx
  lea    0x11(%rsp,%rdx,1),%r9
  mov    $0x1,%r8d
L45:
  mov    %ecx,%edx
  lea    (%rcx,%rcx,4),%edi
  movzbl (%rax),%ecx
  lea    -0x30(%rcx,%rdi,2),%ecx
  cmp    %edx,%ecx
  cmovb  %r8d,%esi
  add    $0x1,%rax
  cmp    %r9,%rax
  jne    L45
  xor    %ebx,%ebx
  test   %esi,%esi
  je     L177
  nopl   0x0(%rax)
L178:
  mov    $0x8,%edi
  add    $0x1,%ebx
  call   <tty_putc@plt>
  mov    $0x20,%edi
  call   <tty_putc@plt>
  mov    $0x8,%edi
  call   <tty_putc@plt>
  cmp    %r12d,%ebx
  jne    L178
  jmp    L52
  nopl   0x0(%rax,%rax,1)
L150:
  mov    %r12d,%eax
  add    $0x1,%r12d
  mov    %dil,0x10(%rsp,%rax,1)
  call   <tty_putc@plt>
  cmp    $0x20,%r12d
  jne    L79
  jmp    L29
  nopl   0x0(%rax,%rax,1)
L55:
  test   %r12d,%r12d
  je     L79
  mov    $0x8,%edi
  sub    $0x1,%r12d
  call   <tty_putc@plt>
  mov    $0x20,%edi
  call   <tty_putc@plt>
  mov    $0x8,%edi
  call   <tty_putc@plt>
  cmp    $0x20,%r12d
  jne    L79
  jmp    L29
L177:
  mov    %ecx,%r12d
  jmp    L166
L69:
  call   <__stack_chk_fail@plt>
```

assistant: ```c
unsigned int tty_getw_irq(unsigned int *val) {
  unsigned char buf[32];
  unsigned char byte;
  unsigned int save = 0;
  unsigned int dec = 0;
  unsigned int done = 0;
  unsigned int overflow = 0;
  unsigned int max = 0;
  unsigned int i;
  unsigned int ret;
  while (done == 0) {
    do {
      ret = sys_call(0x0A, (unsigned int)(&byte), 1, 0, 0);
    } while (ret != 1);
    if ((byte > 0x2F) && (byte < 0x3A)) {
      buf[max] = byte;
      max++;
      tty_putc(byte);
    } else if ((byte == 0x0A) || (byte == 0x0D)) {
      done = 1;
    } else if (byte == 0x7F) {
      if (max > 0) {
        max--;
        tty_putc(0x08);
        tty_putc(0x20);
        tty_putc(0x08);
      }
    }
    if (max == 32) {
      for (i = 0; i < max; i++) {
        tty_putc(0x08);
        tty_putc(0x20);
        tty_putc(0x08);
      }
      tty_putc(0x30);
      *val = 0;
      return 0;
    }
  }
  for (i = 0; i < max; i++) {
    dec = dec * 10 + (buf[i] - 0x30);
    if (dec < save)
      overflow = 1;
    save = dec;
  }
  if (overflow == 0) {
    *val = dec;
  } else {
    for (i = 0; i < max; i++) {
      tty_putc(0x08);
      tty_putc(0x20);
      tty_putc(0x08);
    }
    tty_putc(0x30);
    *val = 0;
  }
  return 0;
}
```