// File: tenup-coin[P]seeder/dns.c
// Function: write_name
int static write_name(unsigned char** outpos, const unsigned char *outend, const char *name, int offset) {
  while (*name != 0) {
    char *dot = strchr(name, '.');
    const char *fin = dot;
    if (!dot) fin = name + strlen(name);
    if (fin - name > 63) return -1;
    if (fin == name) return -3;
    if (outend - *outpos < fin - name + 2) return -2;
    *((*outpos)++) = fin - name;
    memcpy(*outpos, name, fin - name);
    *outpos += fin - name;
    if (!dot) break;
    name = dot + 1;
  }
  if (offset < 0) {
    // no reference
    if (outend == *outpos) return -2;
    *((*outpos)++) = 0;
  } else {
    if (outend - *outpos < 2) return -2;
    *((*outpos)++) = (offset >> 8) | 0xC0;
    *((*outpos)++) = offset & 0xFF;
  }
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x6882
movq -0x20(%rbp), %rdi
movl $0x2e, %esi
callq 0x41c0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x67b7
movq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rdi
callq 0x4210
movq %rax, %rcx
movq -0x40(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x20(%rbp), %rcx
subq %rcx, %rax
cmpq $0x3f, %rax
jle 0x67d4
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6915
movq -0x38(%rbp), %rax
cmpq -0x20(%rbp), %rax
jne 0x67ea
movl $0xfffffffd, -0x4(%rbp) # imm = 0xFFFFFFFD
jmp 0x6915
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
subq %rcx, %rax
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %rdx
subq %rdx, %rcx
addq $0x2, %rcx
cmpq %rcx, %rax
jge 0x6818
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x6915
movq -0x38(%rbp), %rax
movq -0x20(%rbp), %rcx
subq %rcx, %rax
movb %al, %cl
movq -0x10(%rbp), %rdx
movq (%rdx), %rax
movq %rax, %rsi
addq $0x1, %rsi
movq %rsi, (%rdx)
movb %cl, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x20(%rbp), %rax
subq %rax, %rdx
callq 0x4420
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %rax
subq %rax, %rcx
movq -0x10(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
cmpq $0x0, -0x30(%rbp)
jne 0x6871
jmp 0x6882
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x6767
cmpl $0x0, -0x24(%rbp)
jge 0x68b4
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jne 0x689e
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x6915
movq -0x10(%rbp), %rcx
movq (%rcx), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, (%rcx)
movb $0x0, (%rax)
jmp 0x690e
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
subq %rcx, %rax
cmpq $0x2, %rax
jge 0x68d1
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x6915
movl -0x24(%rbp), %eax
sarl $0x8, %eax
orl $0xc0, %eax
movb %al, %cl
movq -0x10(%rbp), %rdx
movq (%rdx), %rax
movq %rax, %rsi
addq $0x1, %rsi
movq %rsi, (%rdx)
movb %cl, (%rax)
movl -0x24(%rbp), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x10(%rbp), %rdx
movq (%rdx), %rax
movq %rax, %rsi
addq $0x1, %rsi
movq %rsi, (%rdx)
movb %cl, (%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nop