// File: /workspace/github_cpp_makefile_star2_O3/cpearce[P]OggIndex/src/SkeletonEncoder.cpp
// Function: SkeletonEncoder::UpdateFisbone(ogg_packet*)
ogg_packet*
SkeletonEncoder::UpdateFisbone(ogg_packet* original) {
  assert(IsFisbonePacket(original));

  ogg_uint32_t serialno = LEUint32(original->packet + FISBONE_SERIALNO_OFFSET);
  Decoder* decoder = FindTrack(serialno);
  if (!decoder) {
    cerr << "ERROR: incoming fisbone packet for stream " << serialno << " is unknown" << endl;
    return 0;
  }
  FisboneInfo info = decoder->GetFisboneInfo();

  // Extract the message header fields, ensure we include the all compulsory
  // fields. These are: Content-Type, Role, Name.
  const char* x = (const char*)(original->packet + FISBONE_HEADER_OFFSET);
  string header(x, original->bytes - FISBONE_HEADER_OFFSET);
  vector<string> headers;
  Tokenize(header, headers, "\r\n");

  bool hasContentType = false;
  bool hasRole = false;
  bool hasName = false;
  for (unsigned i=0; i<headers.size(); i++) {
    string::size_type colon = headers[i].find_first_of(":");
    if (colon == string::npos) {
      continue;
    }
    string id = headers[i].substr(0, colon);
    ToLower(id);
    if (id.compare("content-type") == 0) {
      hasContentType = true;
    } else if (id.compare("name") == 0) {
      hasName = true;
    } else if (id.compare("role") == 0) {
      hasRole = true;
    }
  }

  unsigned size = original->bytes;

  if (!hasContentType)
    size += strlen("Content-Type: ") + info.mContentType.size() + 2;
  if (!hasName)
    size += strlen("Name: ") + info.mName.size() + 2;
  if (!hasRole)
    size += strlen("Role: ") + info.mRole.size() + 2;

  ogg_packet* packet = new ogg_packet();
  memcpy(packet, original, sizeof(ogg_packet));
  packet->packet = new unsigned char[size];
  packet->bytes = size;

  // Copy stuff up to headers
  memcpy(packet->packet, original->packet, FISBONE_HEADER_OFFSET);

  unsigned char* h = packet->packet + FISBONE_HEADER_OFFSET;

  // Write any existing headers.
  memcpy(h, original->packet+FISBONE_HEADER_OFFSET, original->bytes - FISBONE_HEADER_OFFSET);
  h += original->bytes - FISBONE_HEADER_OFFSET;

  if (!hasContentType) {
    // Write a content type...
    string s = "Content-Type: " + info.mContentType + "\r\n";
    memcpy(h, s.c_str(), s.size());
    h += s.size();
  }
  if (!hasName) {
    // Write a name...
    string s = "Name: " + info.mName + "\r\n";
    memcpy(h, s.c_str(), s.size());
    h += s.size();
  }
  if (!hasRole) {
    // Write a role...
    string s = "Role: " + info.mRole + "\r\n";
    memcpy(h, s.c_str(), s.size());
    h += s.size();
  }
  assert(h == packet->packet + packet->bytes);
  return packet;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rsi, %r12
movq %rdi, %r15
movq %rsi, %rdi
callq 0xc803
testb %al, %al
je 0xb55b
movq (%r12), %rdi
addq $0xc, %rdi
callq 0xc7dd
movl %eax, %ebx
movq (%r15), %rax
movq 0x8(%r15), %rcx
subq %rax, %rcx
je 0xaf3c
sarq $0x3, %rcx
movl $0x1, %edx
xorl %esi, %esi
movq (%rax,%rsi,8), %rsi
cmpl %ebx, 0x1a0(%rsi)
je 0xafa6
movl %edx, %esi
incl %edx
cmpq %rsi, %rcx
ja 0xaf27
movq 0xc0ad(%rip), %r14      # 0x16ff0
leaq 0x6497(%rip), %rsi      # 0x113e1
movl $0x2a, %edx
movq %r14, %rdi
callq 0x4410
movl %ebx, %esi
movq %r14, %rdi
callq 0x4240
movq %rax, %rbx
leaq 0x64a1(%rip), %rsi      # 0x1140c
movl $0xb, %edx
movq %rax, %rdi
callq 0x4410
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4360
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4250
xorl %ebx, %ebx
jmp 0xb546
movq (%rsi), %rax
leaq 0x98(%rsp), %rdi
callq *0x40(%rax)
movq (%r12), %rdx
leaq 0x34(%rdx), %rsi
leaq 0x80(%rsp), %rax
addq 0x8(%r12), %rdx
movq %rax, -0x10(%rax)
leaq 0x70(%rsp), %rdi
callq 0xbb60
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movq $0x0, 0x60(%rsp)
leaq 0x10(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x6420(%rip), %rsi      # 0x11418
leaq 0x641b(%rip), %rdx      # 0x1141a
movq %rsp, %rdi
callq 0xbb60
leaq 0x70(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movq %rsp, %rdx
callq 0xca83
movq %r12, 0x90(%rsp)
movq (%rsp), %rdi
cmpq %r14, %rdi
je 0xb037
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x43a0
movq 0x50(%rsp), %rdi
xorl %r13d, %r13d
cmpq %rdi, 0x58(%rsp)
je 0xb15a
movq %rsp, %r15
xorl %ebp, %ebp
movq $0x0, 0x28(%rsp)
movq $0x0, 0x20(%rsp)
xorl %ebx, %ebx
shlq $0x5, %rbp
addq %rbp, %rdi
movl $0x1, %ecx
leaq 0x5b8d(%rip), %rsi      # 0x10c03
xorl %edx, %edx
callq 0x4560
cmpq $-0x1, %rax
je 0xb138
addq 0x50(%rsp), %rbp
movq %r15, %rdi
movq %rbp, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x43c0
cmpq $0x0, 0x8(%rsp)
je 0xb0ca
movl $0x1, %ebp
xorl %r12d, %r12d
movq (%rsp), %r14
movsbl (%r14,%r12), %edi
callq 0x4500
movb %al, (%r14,%r12)
movl %ebp, %r12d
incl %ebp
cmpq %r12, 0x8(%rsp)
ja 0xb0ac
movq %r15, %rdi
leaq 0x6347(%rip), %rsi      # 0x1141b
callq 0x40f0
testl %eax, %eax
je 0xb112
movq %r15, %rdi
leaq 0x5eca(%rip), %rsi      # 0x10fb1
callq 0x40f0
testl %eax, %eax
je 0xb116
movq %r15, %rdi
leaq 0x632e(%rip), %rsi      # 0x11428
callq 0x40f0
testl %eax, %eax
sete %al
movq 0x20(%rsp), %rcx
orb %al, %cl
movq %rcx, 0x20(%rsp)
jmp 0xb11d
movb $0x1, %bl
jmp 0xb11d
movb $0x1, %al
movq %rax, 0x28(%rsp)
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0xb138
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x43a0
incl %r13d
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rax
subq %rdi, %rax
sarq $0x5, %rax
movq %r13, %rbp
cmpq %r13, %rax
ja 0xb063
jmp 0xb16e
xorl %ebx, %ebx
movq $0x0, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
movq 0x90(%rsp), %r14
movq 0x8(%r14), %rax
movq %rax, 0x68(%rsp)
movq 0xc0(%rsp), %rbp
movq 0xe0(%rsp), %r15
movq 0x100(%rsp), %r12
movl $0x30, %edi
callq 0x4380
movq %rax, %r13
addl $0x10, %ebp
xorl %eax, %eax
testb $0x1, %bl
cmovnel %eax, %ebp
movq 0x68(%rsp), %rcx
addl %ebp, %ecx
addl $0x8, %r12d
testb $0x1, 0x28(%rsp)
cmovnel %eax, %r12d
addl $0x8, %r15d
testb $0x1, 0x20(%rsp)
cmovnel %eax, %r15d
addl %r12d, %r15d
addl %ecx, %r15d
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups 0x20(%r14), %xmm2
movups %xmm2, 0x20(%r13)
movups %xmm1, 0x10(%r13)
movups %xmm0, (%r13)
movq %r15, %rdi
callq 0x4030
movq %rax, %r12
movq %rax, (%r13)
movq %r15, 0x8(%r13)
movq (%r14), %rsi
movl 0x30(%rsi), %eax
movl %eax, 0x30(%r12)
movups (%rsi), %xmm0
movups 0x10(%rsi), %xmm1
movups 0x20(%rsi), %xmm2
movups %xmm2, 0x20(%r12)
movups %xmm1, 0x10(%r12)
movups %xmm0, (%r12)
leaq 0x34(%r12), %rdi
addq $0x34, %rsi
movq 0x68(%rsp), %rbp
leaq -0x34(%rbp), %rdx
callq 0x42c0
movq %r12, %r14
addq %rbp, %r14
testb $0x1, %bl
movq %r13, %rbx
jne 0xb315
leaq 0xb8(%rsp), %rdx
leaq 0x61c8(%rip), %rsi      # 0x1142d
leaq 0x30(%rsp), %rdi
callq 0xb623
leaq 0x10(%rsp), %r13
leaq 0x619d(%rip), %rsi      # 0x11418
leaq 0x30(%rsp), %rdi
callq 0x4730
movq %r13, (%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xb2a6
movq %rdx, (%rsp)
movq (%rcx), %rdx
movq %rdx, 0x10(%rsp)
jmp 0xb2ae
movups (%rcx), %xmm0
movups %xmm0, (%r13)
movq 0x8(%rax), %rdx
movq %rdx, 0x8(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xb2e1
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x43a0
movq (%rsp), %rbp
movq 0x8(%rsp), %r13
movq %r14, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x42c0
leaq 0x10(%rsp), %rax
cmpq %rax, %rbp
je 0xb312
movq 0x10(%rsp), %rsi
incq %rsi
movq %rbp, %rdi
callq 0x43a0
addq %r13, %r14
leaq 0x10(%rsp), %r13
testb $0x1, 0x28(%rsp)
jne 0xb3e4
leaq 0xf8(%rsp), %rdx
leaq 0x6108(%rip), %rsi      # 0x1143c
leaq 0x30(%rsp), %rdi
callq 0xb623
leaq 0x60d3(%rip), %rsi      # 0x11418
leaq 0x30(%rsp), %rdi
callq 0x4730
movq %r13, (%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xb370
movq %rdx, (%rsp)
movq (%rcx), %rdx
movq %rdx, 0x10(%rsp)
jmp 0xb378
movups (%rcx), %xmm0
movups %xmm0, (%r13)
movq 0x8(%rax), %rdx
movq %rdx, 0x8(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xb3ab
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x43a0
movq (%rsp), %rbp
movq 0x8(%rsp), %r13
movq %r14, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x42c0
leaq 0x10(%rsp), %rax
cmpq %rax, %rbp
je 0xb3dc
movq 0x10(%rsp), %rsi
incq %rsi
movq %rbp, %rdi
callq 0x43a0
addq %r13, %r14
leaq 0x10(%rsp), %r13
testb $0x1, 0x20(%rsp)
jne 0xb4ab
leaq 0xd8(%rsp), %rdx
leaq 0x6045(%rip), %rsi      # 0x11443
leaq 0x30(%rsp), %rdi
callq 0xb623
leaq 0x6009(%rip), %rsi      # 0x11418
leaq 0x30(%rsp), %rdi
callq 0x4730
movq %r13, (%rsp)
movq (%rax), %rsi
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rsi
je 0xb43a
movq %rsi, (%rsp)
movq (%rcx), %rdx
movq %rdx, 0x10(%rsp)
jmp 0xb445
movups (%rcx), %xmm0
movups %xmm0, (%r13)
movq %r13, %rsi
movq 0x8(%rax), %rdx
movq %rdx, 0x8(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xb481
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x43a0
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %r14, %rdi
callq 0x42c0
addq 0x8(%rsp), %r14
movq (%rsp), %rdi
cmpq %r13, %rdi
je 0xb4a4
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x43a0
movq (%rbx), %r12
movq 0x8(%rbx), %r15
addq %r12, %r15
cmpq %r15, %r14
jne 0xb57a
leaq 0x50(%rsp), %rdi
callq 0xb6ac
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0xb4e3
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x43a0
leaq 0x108(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xb504
movq 0x108(%rsp), %rsi
incq %rsi
callq 0x43a0
leaq 0xe8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xb525
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x43a0
leaq 0xc8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xb546
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x43a0
movq %rbx, %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5e2c(%rip), %rdi      # 0x1138e
leaq 0x5ac9(%rip), %rsi      # 0x11032
leaq 0x5e38(%rip), %rcx      # 0x113a8
movl $0x25a, %edx            # imm = 0x25A
callq 0x4210
leaq 0x5ec9(%rip), %rdi      # 0x1144a
leaq 0x5aaa(%rip), %rsi      # 0x11032
leaq 0x5e19(%rip), %rcx      # 0x113a8
movl $0x2a7, %edx            # imm = 0x2A7
callq 0x4210
jmp 0xb5a1
jmp 0xb5df
jmp 0xb5a1
jmp 0xb5df
movq %rax, %rbx
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xb5e2
movq 0x40(%rsp), %rsi
jmp 0xb5cc
jmp 0xb5df
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r14, %rdi
je 0xb5e2
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x43a0
jmp 0xb5e2
jmp 0xb5df
movq %rax, %rbx
jmp 0xb60e
jmp 0xb5df
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0xb6ac
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0xb60e
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x43a0
leaq 0x98(%rsp), %rdi
callq 0x6c34
movq %rbx, %rdi
callq 0x46d0