user: Please show me the C implementation that matches the assembly code listed below:

```asm
<ts_ip>:
  endbr64
  push   %r15
  push   %r14
  push   %r13
  mov    %rcx,%r13
  push   %r12
  mov    %edi,%r12d
  mov    %rsi,%rdi
  push   %rbp
  push   %rbx
  movslq %edx,%rbx
  sub    $0xe8,%rsp
  mov    %fs:0x28,%rdx
  mov    %rdx,0xd8(%rsp)
  xor    %edx,%edx
  lea    0xc(%rsi),%rdx
  add    %rbx,%rsi
  jmp    L277
  nopl   (%rax)
L972:
  test   $0xc0,%cl
  jne    L849
  add    $0x1,%ecx
  movzbl %cl,%ecx
  add    %rcx,%rdx
  cmp    %rsi,%rdx
  ja     L364
L277:
  movzbl (%rdx),%ecx
  test   %cl,%cl
  jne    L972
  lea    0x3(%rdx),%rcx
  sub    %rdi,%rcx
  cmp    %rbx,%rcx
  jg     L364
L832:
  movzwl 0x1(%rdx),%r11d
  add    $0x5,%rdx
  mov    %rdx,%rcx
  sub    %rdi,%rcx
  cmp    %rcx,%rbx
  jl     L364
  lea    (%rdi,%rbx,1),%rbp
  xor    %ecx,%ecx
  xor    %r14d,%r14d
L1000:
  mov    %ecx,%esi
  cmp    %rbp,%rdx
  jae    L133
  mov    %rdx,%r15
  jmp    L350
  nopl   0x0(%rax,%rax,1)
L996:
  test   $0xc0,%al
  jne    L793
  add    $0x1,%eax
  movzbl %al,%eax
  add    %rax,%r15
  cmp    %rbp,%r15
  ja     L364
L350:
  movzbl (%r15),%eax
  test   %al,%al
  jne    L996
L87:
  lea    0x3(%r15),%rax
  sub    %rdi,%rax
  cmp    %rax,%rbx
  jl     L364
  cmpw   $0x21,0x1(%r15)
  jne    L988
  lea    0x5(%r15),%rax
  sub    %rdi,%rax
  cmp    %rax,%rbx
  jl     L364
  cmpw   $0x1,0x3(%r15)
  jne    L988
  lea    0x9(%r15),%rax
  sub    %rdi,%rax
  cmp    %rax,%rbx
  jl     L364
  movzbl 0x5(%r15),%eax
  mov    0x5(%r15),%esi
  movzbl 0x7(%r15),%r9d
  movzbl 0x8(%r15),%r10d
  mov    %al,0x14(%rsp)
  movzbl 0x6(%r15),%eax
  mov    %al,0x8(%rsp)
  lea    0xb(%r15),%rax
  mov    %rax,%r14
  sub    %rdi,%r14
  cmp    %r14,%rbx
  jl     L364
  movzwl 0x9(%r15),%r14d
  add    %r14,%rax
  sub    %rdi,%rax
  cmp    %rax,%rbx
  jl     L364
  lea    0xd(%r15),%rax
  sub    %rdi,%rax
  cmp    %rax,%rbx
  jl     L364
  movzwl 0xb(%r15),%eax
  mov    %ax,0x40(%rsp,%rcx,2)
  lea    0xf(%r15),%rax
  sub    %rdi,%rax
  cmp    %rax,%rbx
  jl     L364
  movzwl 0xd(%r15),%eax
  lea    0x11(%r15),%r14
  mov    %ax,0x60(%rsp,%rcx,2)
  mov    %r14,%rax
  sub    %rdi,%rax
  cmp    %rax,%rbx
  jl     L364
  movzwl 0xf(%r15),%eax
  mov    %r14,0x80(%rsp,%rcx,8)
  mov    %ax,0x20(%rsp,%rcx,2)
  jmp    L893
L343:
  test   $0xc0,%al
  jne    L193
  add    $0x1,%eax
  movzbl %al,%eax
  add    %rax,%r14
  cmp    %rbp,%r14
  ja     L364
L893:
  movzbl (%r14),%eax
  test   %al,%al
  jne    L343
L1005:
  add    $0x1,%rcx
  lea    0x1(%r14),%r15
  mov    %rdx,%rax
  mov    $0x1,%r14d
  cmp    $0xa,%rcx
  je     L1087
  mov    %r15,%rdx
  jmp    L1000
L835:
  mov    0x80(%rsp,%r14,8),%rsi
  mov    %r8,%r13
L341:
  mov    %r13,%rdx
  call   <_ts_lcopy@plt>
L364:
  xor    %eax,%eax
L327:
  mov    0xd8(%rsp),%rdx
  sub    %fs:0x28,%rdx
  jne    L634
  add    $0xe8,%rsp
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
  nopl   0x0(%rax,%rax,1)
L849:
  add    $0x1,%rdx
  lea    0x3(%rdx),%rcx
  sub    %rdi,%rcx
  cmp    %rbx,%rcx
  jle    L832
  jmp    L364
  cs nopw 0x0(%rax,%rax,1)
L793:
  add    $0x1,%r15
  jmp    L87
L193:
  add    $0x1,%r14
  jmp    L1005
L1087:
  mov    %esi,%eax
  mov    %rax,0x18(%rsp)
  cmp    $0x1,%r11w
  je     L283
  cmp    $0x9,%r12d
  jg     L364
L283:
  mov    $0xa,%esi
L337:
  movzwl 0x40(%rsp),%eax
  xor    %r14d,%r14d
  xor    %r9d,%r9d
  mov    $0x2,%r15d
  mov    %eax,%ecx
  jmp    L679
  nopl   0x0(%rax,%rax,1)
L997:
  je     L1077
  xor    %r14d,%r14d
L252:
  movzwl 0x42(%rsp),%eax
  cmp    %cx,%ax
  jl     L170
  je     L486
  mov    %eax,%ecx
  mov    $0x1,%r14d
L170:
  cmp    $0x2,%esi
  je     L345
  movzwl 0x44(%rsp),%eax
  cmp    %ax,%cx
  jg     L1059
  je     L719
  mov    $0x2,%r14d
L144:
  cmp    $0x3,%esi
  je     L345
  movzwl 0x46(%rsp),%ecx
  cmp    %ax,%cx
  jl     L731
  je     L317
  mov    %ecx,%eax
  mov    $0x3,%r14d
L731:
  cmp    $0x4,%esi
  je     L345
  movzwl 0x48(%rsp),%ecx
  cmp    %ax,%cx
  jl     L485
  je     L459
  mov    %ecx,%eax
  mov    $0x4,%r14d
L485:
  cmp    $0x5,%esi
  je     L345
  movzwl 0x4a(%rsp),%ecx
  cmp    %ax,%cx
  jl     L154
  je     L734
  mov    %ecx,%eax
  mov    $0x5,%r14d
L154:
  cmp    $0x6,%esi
  je     L345
  movzwl 0x4c(%rsp),%ecx
  cmp    %ax,%cx
  jl     L1043
  je     L1093
  mov    %ecx,%eax
  mov    $0x6,%r14d
L1043:
  cmp    $0x7,%esi
  je     L345
  movzwl 0x4e(%rsp),%ecx
  cmp    %ax,%cx
  jl     L780
  je     L986
  mov    %ecx,%eax
  mov    $0x7,%r14d
L780:
  cmp    $0x8,%esi
  je     L345
  movzwl 0x50(%rsp),%ecx
  cmp    %ax,%cx
  jl     L1006
  je     L1009
  mov    %ecx,%eax
  mov    $0x8,%r14d
L1006:
  cmp    $0xa,%esi
  jne    L345
  cmp    %ax,0x52(%rsp)
  jl     L345
  je     L803
  mov    $0x9,%r14d
  xchg   %ax,%ax
L345:
  test   %r12d,%r12d
  je     L913
  movslq %r14d,%rax
  mov    $0xffffffff,%ecx
  mov    $0xffffffff,%r10d
  sub    $0x1,%r12d
  mov    %cx,0x60(%rsp,%rax,2)
  mov    %r10w,0x40(%rsp,%rax,2)
  movzwl 0x40(%rsp),%ecx
  mov    $0xffffffff,%eax
L679:
  cmp    %cx,%ax
  jle    L997
  mov    %eax,%ecx
  jmp    L252
L1059:
  mov    %ecx,%eax
  jmp    L144
L1077:
  movslq %r14d,%rax
  movzwl 0x60(%rsp),%r10d
  cmp    %r10w,0x60(%rsp,%rax,2)
  cmovle %r9,%r14
  jmp    L252
L486:
  movslq %r14d,%rax
  movzwl 0x62(%rsp),%r10d
  cmp    %r10w,0x60(%rsp,%rax,2)
  mov    $0x1,%eax
  cmovle %rax,%r14
  jmp    L170
L719:
  movslq %r14d,%rcx
  movzwl 0x60(%rsp,%rcx,2),%ecx
  cmp    %cx,0x64(%rsp)
  cmovge %r15,%r14
  jmp    L144
L317:
  movslq %r14d,%rcx
  movzwl 0x60(%rsp,%rcx,2),%ecx
  cmp    %cx,0x66(%rsp)
  mov    $0x3,%ecx
  cmovge %rcx,%r14
  jmp    L731
L459:
  movslq %r14d,%rcx
  movzwl 0x60(%rsp,%rcx,2),%ecx
  cmp    %cx,0x68(%rsp)
  mov    $0x4,%ecx
  cmovge %rcx,%r14
  jmp    L485
L734:
  movslq %r14d,%rcx
  movzwl 0x60(%rsp,%rcx,2),%ecx
  cmp    %cx,0x6a(%rsp)
  mov    $0x5,%ecx
  cmovge %rcx,%r14
  jmp    L154
L913:
  cmp    $0x1,%r11w
  je     L375
L907:
  movslq %r14d,%rax
  movswl 0x20(%rsp,%rax,2),%eax
  mov    %eax,(%r8)
L375:
  movabs $0xffffffff00000000,%rcx
  mov    %r13,%r8
  mov    %r12d,%r13d
  mov    0x18(%rsp),%r12
L1004:
  cmp    %rbp,%rdx
  jae    L835
  mov    %rdx,%r15
  jmp    L579
  nopl   0x0(%rax,%rax,1)
L541:
  test   $0xc0,%al
  jne    L671
  add    $0x1,%eax
  movzbl %al,%eax
  add    %rax,%r15
  cmp    %rbp,%r15
  ja     L364
L579:
  movzbl (%r15),%eax
  test   %al,%al
  jne    L541
L73:
  cmp    $0x1,%r11w
  jne    L1090
  xor    %eax,%eax
  test   %r13d,%r13d
  sete   %al
  sub    $0x1,%r13d
L968:
  lea    0x3(%r15),%rdx
  sub    %rdi,%rdx
  cmp    %rdx,%rbx
  jl     L364
  movzwl 0x1(%r15),%edx
  cmp    $0x5,%dx
  je     L86
  cmp    $0x1,%dx
  mov    $0x0,%edx
  cmovne %edx,%eax
L211:
  lea    0x5(%r15),%rdx
  sub    %rdi,%rdx
  cmp    %rdx,%rbx
  jl     L364
  xor    %edx,%edx
  cmpw   $0x1,0x3(%r15)
  cmovne %edx,%eax
  lea    0x9(%r15),%rdx
  sub    %rdi,%rdx
  cmp    %rdx,%rbx
  jl     L364
  mov    0x5(%r15),%edx
  and    %rcx,%r12
  or     %rdx,%r12
  lea    0xb(%r15),%rdx
  mov    %rdx,%rsi
  sub    %rdi,%rsi
  cmp    %rsi,%rbx
  jl     L364
  movzwl 0x9(%r15),%esi
  add    %rsi,%rdx
  mov    %rdx,%rsi
  sub    %rdi,%rsi
  cmp    %rsi,%rbx
  jl     L364
  test   %eax,%eax
  je     L1004
  lea    0xf(%r15),%rax
  sub    %rdi,%rax
  cmp    %rax,%rbx
  jl     L364
  mov    0xb(%r15),%edx
  mov    %r12,%rax
  and    %rcx,%rax
  or     %rdx,%rax
  jmp    L327
  nopl   (%rax)
L1093:
  movslq %r14d,%rcx
  movzwl 0x60(%rsp,%rcx,2),%ecx
  cmp    %cx,0x6c(%rsp)
  mov    $0x6,%ecx
  cmovge %rcx,%r14
  jmp    L1043
L986:
  movslq %r14d,%rcx
  movzwl 0x60(%rsp,%rcx,2),%ecx
  cmp    %cx,0x6e(%rsp)
  mov    $0x7,%ecx
  cmovge %rcx,%r14
  jmp    L780
L671:
  add    $0x1,%r15
  jmp    L73
L1009:
  movslq %r14d,%rcx
  movzwl 0x60(%rsp,%rcx,2),%ecx
  cmp    %cx,0x70(%rsp)
  mov    $0x8,%ecx
  cmovge %rcx,%r14
  jmp    L1006
L803:
  movslq %r14d,%rax
  movzwl 0x72(%rsp),%ecx
  cmp    %cx,0x60(%rsp,%rax,2)
  mov    $0x9,%eax
  cmovle %rax,%r14
  jmp    L345
L86:
  cmp    $0x1,%eax
  je     L128
  xor    %eax,%eax
  jmp    L211
L1090:
  movslq %r14d,%rax
  mov    %r8,0x18(%rsp)
  mov    0x80(%rsp,%rax,8),%rsi
  mov    %r11d,0x14(%rsp)
  mov    %rdi,0x8(%rsp)
  call   <_ts_lmatch@plt>
  mov    0x18(%rsp),%r8
  mov    0x14(%rsp),%r11d
  movabs $0xffffffff00000000,%rcx
  mov    0x8(%rsp),%rdi
  jmp    L968
L988:
  test   %r14b,%r14b
  je     L194
  movzbl %r10b,%ecx
  movzbl %r9b,%eax
  shl    $0x10,%rax
  shl    $0x18,%rcx
  or     %rax,%rcx
  mov    %rcx,%rax
  mov    0x8(%rsp),%rcx
  mov    %cl,%ah
  mov    0x14(%rsp),%al
  mov    %rax,0x18(%rsp)
L194:
  cmp    $0x1,%r11w
  je     L334
  test   %esi,%esi
  je     L364
  cmp    %r12d,%esi
  jle    L364
  xor    %r14d,%r14d
  cmp    $0x1,%esi
  jne    L337
  jmp    L907
  cs nopw 0x0(%rax,%rax,1)
L334:
  xor    %r14d,%r14d
  cmp    $0x1,%esi
  jg     L337
  jmp    L375
L133:
  test   %r14b,%r14b
  je     L522
  movzbl %r10b,%ecx
  movzbl %r9b,%edx
  shl    $0x10,%rdx
  shl    $0x18,%rcx
  or     %rdx,%rcx
  mov    %rcx,%rdx
  mov    0x8(%rsp),%rcx
  mov    %cl,%dh
  mov    0x14(%rsp),%dl
  mov    %rdx,0x18(%rsp)
L522:
  mov    %rax,%rdx
  jmp    L194
L128:
  lea    0x5(%r15),%rax
  mov    %r8,%r13
  sub    %rdi,%rax
  cmp    %rax,%rbx
  jl     L364
  lea    0x9(%r15),%rax
  sub    %rdi,%rax
  cmp    %rax,%rbx
  jl     L364
  lea    0xb(%r15),%rsi
  mov    %rsi,%rax
  sub    %rdi,%rax
  cmp    %rax,%rbx
  jl     L364
  movzwl 0x9(%r15),%eax
  add    %rsi,%rax
  sub    %rdi,%rax
  cmp    %rax,%rbx
  jl     L364
  jmp    L341
L634:
  call   <__stack_chk_fail@plt>
```

assistant: ```c
unsigned long int ts_ip(int which, unsigned char *a, int len, char *host, int *port) {
  unsigned char *rr, *srv_target[10], *buf = a;
  unsigned short int i;
  unsigned long int l;
  int srv = 0, srvs = 0, aflag = 0;
  short int srv_port[10], srv_pri[10], srv_weight[10];
  buf += 12;
  for (; *buf != 0 && !(*buf & 0xc0 && buf++); buf += *buf + 1)
    if ((buf + *buf + 1) > (a + len))
      return 0;
  buf++;
  ;
  if ((buf + 2) - a > len)
    return 0;
  ;
  ((char *)&i)[0] = *buf++;
  ((char *)&i)[1] = *buf++;
  ;
  if (i == 1)
    aflag = 1;
  if ((buf + 2) - a > len)
    return 0;
  ;
  ((char *)&i)[0] = *buf++;
  ((char *)&i)[1] = *buf++;
  ;
  while (buf < (a + len)) {
    rr = buf;
    for (; *buf != 0 && !(*buf & 0xc0 && buf++); buf += *buf + 1)
      if ((buf + *buf + 1) > (a + len))
        return 0;
    buf++;
    ;
    if ((buf + 2) - a > len)
      return 0;
    ;
    ((char *)&i)[0] = *buf++;
    ((char *)&i)[1] = *buf++;
    ;
    if (i != 33)
      break;
    if ((buf + 2) - a > len)
      return 0;
    ;
    ((char *)&i)[0] = *buf++;
    ((char *)&i)[1] = *buf++;
    ;
    if (i != 1)
      break;
    if ((buf + 4) - a > len)
      return 0;
    ;
    ((char *)&l)[0] = *buf++;
    ((char *)&l)[1] = *buf++;
    ((char *)&l)[2] = *buf++;
    ((char *)&l)[3] = *buf++;
    ;
    if ((buf + 2) - a > len)
      return 0;
    ;
    ((char *)&i)[0] = *buf++;
    ((char *)&i)[1] = *buf++;
    ;
    if ((buf + i) - a > len)
      return 0;
    ;
    if ((buf + 2) - a > len)
      return 0;
    ;
    ((char *)&i)[0] = *buf++;
    ((char *)&i)[1] = *buf++;
    ;
    srv_pri[srvs] = i;
    if ((buf + 2) - a > len)
      return 0;
    ;
    ((char *)&i)[0] = *buf++;
    ((char *)&i)[1] = *buf++;
    ;
    srv_weight[srvs] = i;
    if ((buf + 2) - a > len)
      return 0;
    ;
    ((char *)&i)[0] = *buf++;
    ((char *)&i)[1] = *buf++;
    ;
    srv_port[srvs] = i;
    srv_target[srvs] = buf;
    for (; *buf != 0 && !(*buf & 0xc0 && buf++); buf += *buf + 1)
      if ((buf + *buf + 1) > (a + len))
        return 0;
    buf++;
    ;
    srvs++;
    if (srvs == 10)
      break;
  }
  if (!aflag && (srvs == 0 || which >= srvs))
    return 0;
  buf = rr;
  while (srvs > 1) {
    int cur;
    for (cur = 0; cur < srvs; cur++) {
      if (srv_pri[srv] > srv_pri[cur] || (srv_pri[srv] == srv_pri[cur] && srv_weight[srv] > srv_weight[cur]))
        continue;
      srv = cur;
    }
    if (which == 0)
      break;
    srv_pri[srv] = srv_weight[srv] = -1;
    which--;
  }
  if (!aflag)
    *port = srv_port[srv];
  while (buf < (a + len)) {
    int match = 0;
    rr = buf;
    for (; *buf != 0 && !(*buf & 0xc0 && buf++); buf += *buf + 1)
      if ((buf + *buf + 1) > (a + len))
        return 0;
    buf++;
    ;
    if (aflag)
      match = (which-- == 0) ? 1 : 0;
    else
      match = _ts_lmatch(a, srv_target[srv], rr);
    if ((buf + 2) - a > len)
      return 0;
    ;
    ((char *)&i)[0] = *buf++;
    ((char *)&i)[1] = *buf++;
    ;
    if (i == 5 && match == 1) {
      if ((buf + 2) - a > len)
        return 0;
      ;
      ((char *)&i)[0] = *buf++;
      ((char *)&i)[1] = *buf++;
      ;
      if ((buf + 4) - a > len)
        return 0;
      ;
      ((char *)&l)[0] = *buf++;
      ((char *)&l)[1] = *buf++;
      ((char *)&l)[2] = *buf++;
      ((char *)&l)[3] = *buf++;
      ;
      if ((buf + 2) - a > len)
        return 0;
      ;
      ((char *)&i)[0] = *buf++;
      ((char *)&i)[1] = *buf++;
      ;
      if ((buf + i) - a > len)
        return 0;
      ;
      _ts_lcopy(a, buf, host);
      return 0;
    }
    if (i != 1)
      match = 0;
    if ((buf + 2) - a > len)
      return 0;
    ;
    ((char *)&i)[0] = *buf++;
    ((char *)&i)[1] = *buf++;
    ;
    if (i != 1)
      match = 0;
    if ((buf + 4) - a > len)
      return 0;
    ;
    ((char *)&l)[0] = *buf++;
    ((char *)&l)[1] = *buf++;
    ((char *)&l)[2] = *buf++;
    ((char *)&l)[3] = *buf++;
    ;
    if ((buf + 2) - a > len)
      return 0;
    ;
    ((char *)&i)[0] = *buf++;
    ((char *)&i)[1] = *buf++;
    ;
    if ((buf + i) - a > len)
      return 0;
    ;
    if (match) {
      if ((buf + 4) - a > len)
        return 0;
      ;
      ((char *)&l)[0] = *buf++;
      ((char *)&l)[1] = *buf++;
      ((char *)&l)[2] = *buf++;
      ((char *)&l)[3] = *buf++;
      return l;
    }
    buf += i;
  }
  _ts_lcopy(a, srv_target[srv], host);
  return 0;
}
```