// File: /workspace/github_cpp_makefile_star2_O1/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 $0x128, %rsp            # imm = 0x128
movq %rsi, %rbp
movq %rdi, %r15
movq %rsi, %rdi
callq 0xca8d
testb %al, %al
je 0xb7a3
movq (%rbp), %rdi
addq $0xc, %rdi
callq 0xca67
movl %eax, %ebx
movq (%r15), %rax
movq 0x8(%r15), %rcx
subq %rax, %rcx
je 0xb16a
sarq $0x3, %rcx
movl $0x1, %edx
xorl %esi, %esi
movq (%rax,%rsi,8), %rsi
cmpl %ebx, 0x1a0(%rsi)
je 0xb16c
movl %edx, %esi
incl %edx
cmpq %rsi, %rcx
ja 0xb155
xorl %esi, %esi
testq %rsi, %rsi
je 0xb35c
movq (%rsi), %rax
leaq 0xa8(%rsp), %rdi
callq *0x40(%rax)
movq (%rbp), %rdx
leaq 0x34(%rdx), %rsi
leaq 0x90(%rsp), %rax
addq 0x8(%rbp), %rdx
movq %rax, -0x10(%rax)
leaq 0x80(%rsp), %rdi
callq 0xbdaa
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq $0x0, 0x70(%rsp)
leaq 0x18(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x625f(%rip), %rsi      # 0x11428
leaq 0x625a(%rip), %rdx      # 0x1142a
leaq 0x8(%rsp), %rdi
callq 0xbdaa
leaq 0x80(%rsp), %rdi
leaq 0x60(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0xcd15
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0xb208
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x43a0
movq 0x60(%rsp), %rdi
xorl %r14d, %r14d
cmpq %rdi, 0x68(%rsp)
je 0xb3c6
movq %rbp, 0xa0(%rsp)
leaq 0x8(%rsp), %r15
xorl %ebp, %ebp
movq $0x0, 0x38(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
shlq $0x5, %rbp
addq %rbp, %rdi
movl $0x1, %ecx
leaq 0x59ab(%rip), %rsi      # 0x10c03
xorl %edx, %edx
callq 0x4570
cmpq $-0x1, %rax
je 0xb32d
movq %r13, %rbx
addq 0x60(%rsp), %rbp
movq %r15, %rdi
movq %rbp, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x43c0
cmpq $0x0, 0x10(%rsp)
je 0xb2b2
movl $0x1, %ebp
xorl %r12d, %r12d
movq 0x8(%rsp), %r13
movsbl (%r13,%r12), %edi
callq 0x4510
movb %al, (%r13,%r12)
movl %ebp, %r12d
incl %ebp
cmpq %r12, 0x10(%rsp)
ja 0xb291
movq %r15, %rdi
leaq 0x616f(%rip), %rsi      # 0x1142b
callq 0x40f0
testl %eax, %eax
je 0xb303
movq %r15, %rdi
leaq 0x5cf7(%rip), %rsi      # 0x10fc6
callq 0x40f0
testl %eax, %eax
movq %rbx, %r13
je 0xb30f
movq %r15, %rdi
leaq 0x6153(%rip), %rsi      # 0x11438
callq 0x40f0
testl %eax, %eax
movq 0x30(%rsp), %rax
movzbl %al, %eax
movl $0x1, %ecx
cmovel %ecx, %eax
movq %rax, 0x30(%rsp)
jmp 0xb316
movb $0x1, %al
movq %rax, 0x28(%rsp)
movq %rbx, %r13
jmp 0xb316
movb $0x1, %al
movq %rax, 0x38(%rsp)
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0xb32d
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x43a0
incl %r14d
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rax
subq %rdi, %rax
sarq $0x5, %rax
movq %r14, %rbp
cmpq %r14, %rax
ja 0xb245
movq 0x28(%rsp), %r14
movq 0xa0(%rsp), %rbp
jmp 0xb3d8
movq 0xbc8d(%rip), %r14      # 0x16ff0
leaq 0x6087(%rip), %rsi      # 0x113f1
movl $0x2a, %edx
movq %r14, %rdi
callq 0x4410
movl %ebx, %esi
movq %r14, %rdi
callq 0x4240
movq %rax, %rbx
leaq 0x6091(%rip), %rsi      # 0x1141c
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 %ebp, %ebp
jmp 0xb78e
movq $0x0, 0x30(%rsp)
movq $0x0, 0x38(%rsp)
movq 0x8(%rbp), %rax
movq %rax, 0x28(%rsp)
movq 0xd0(%rsp), %rbx
movq 0xf0(%rsp), %r12
movq 0x110(%rsp), %r15
movl $0x30, %edi
callq 0x4380
movq %rbp, %rdx
movq %rax, %rbp
addl $0x10, %ebx
xorl %eax, %eax
testb $0x1, %r14b
cmovnel %eax, %ebx
movq 0x28(%rsp), %rcx
addl %ebx, %ecx
addl $0x8, %r15d
testb $0x1, 0x38(%rsp)
cmovnel %eax, %r15d
addl $0x8, %r12d
testb $0x1, 0x30(%rsp)
cmovnel %eax, %r12d
addl %r15d, %r12d
addl %ecx, %r12d
movups (%rdx), %xmm0
movups 0x10(%rdx), %xmm1
movups 0x20(%rdx), %xmm2
movups %xmm2, 0x20(%rbp)
movups %xmm1, 0x10(%rbp)
movups %xmm0, (%rbp)
movq %r12, %rdi
movq %rdx, %rbx
callq 0x4030
movq %rax, %r15
movq %rax, (%rbp)
movq %r12, 0x8(%rbp)
movq (%rbx), %rsi
movl 0x30(%rsi), %eax
movl %eax, 0x30(%r15)
movups (%rsi), %xmm0
movups 0x10(%rsi), %xmm1
movups 0x20(%rsi), %xmm2
movups %xmm2, 0x20(%r15)
movups %xmm1, 0x10(%r15)
movups %xmm0, (%r15)
leaq 0x34(%r15), %rdi
addq $0x34, %rsi
movq 0x28(%rsp), %rbx
leaq -0x34(%rbx), %rdx
callq 0x42c0
addq %rbx, %r15
testb $0x1, %r14b
jne 0xb567
leaq 0xc8(%rsp), %rdx
leaq 0x5f7f(%rip), %rsi      # 0x1143d
leaq 0x40(%rsp), %rdi
callq 0xb86f
leaq 0x5f59(%rip), %rsi      # 0x11428
leaq 0x40(%rsp), %rdi
callq 0x4740
movq %r13, 0x8(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xb4fc
movq %rdx, 0x8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x18(%rsp)
jmp 0xb504
movups (%rcx), %xmm0
movups %xmm0, (%r13)
movq 0x8(%rax), %rdx
movq %rdx, 0x10(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xb537
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x43a0
movq 0x8(%rsp), %r12
movq 0x10(%rsp), %r14
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x42c0
cmpq %r13, %r12
je 0xb564
movq 0x18(%rsp), %rsi
incq %rsi
movq %r12, %rdi
callq 0x43a0
addq %r14, %r15
testb $0x1, 0x38(%rsp)
jne 0xb62a
leaq 0x108(%rsp), %rdx
leaq 0x5ecb(%rip), %rsi      # 0x1144c
leaq 0x40(%rsp), %rdi
callq 0xb86f
leaq 0x5e96(%rip), %rsi      # 0x11428
leaq 0x40(%rsp), %rdi
callq 0x4740
movq %r13, 0x8(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xb5bf
movq %rdx, 0x8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x18(%rsp)
jmp 0xb5c7
movups (%rcx), %xmm0
movups %xmm0, (%r13)
movq 0x8(%rax), %rdx
movq %rdx, 0x10(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xb5fa
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x43a0
movq 0x8(%rsp), %r12
movq 0x10(%rsp), %r14
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x42c0
cmpq %r13, %r12
je 0xb627
movq 0x18(%rsp), %rsi
incq %rsi
movq %r12, %rdi
callq 0x43a0
addq %r14, %r15
testb $0x1, 0x30(%rsp)
jne 0xb6eb
leaq 0xe8(%rsp), %rdx
leaq 0x5e0f(%rip), %rsi      # 0x11453
leaq 0x40(%rsp), %rdi
callq 0xb86f
leaq 0x5dd3(%rip), %rsi      # 0x11428
leaq 0x40(%rsp), %rdi
callq 0x4740
movq %r13, 0x8(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xb682
movq %rdx, 0x8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x18(%rsp)
jmp 0xb68a
movups (%rcx), %xmm0
movups %xmm0, (%r13)
movq 0x8(%rax), %rdx
movq %rdx, 0x10(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xb6bd
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x43a0
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %r15, %rdi
callq 0x42c0
addq 0x10(%rsp), %r15
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0xb6eb
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x43a0
movq (%rbp), %rax
addq 0x8(%rbp), %rax
cmpq %rax, %r15
jne 0xb7c2
leaq 0x60(%rsp), %rdi
callq 0xb8f8
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0xb72b
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x43a0
leaq 0x118(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xb74c
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x43a0
leaq 0xf8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xb76d
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x43a0
leaq 0xd8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xb78e
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x43a0
movq %rbp, %rax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5bf4(%rip), %rdi      # 0x1139e
leaq 0x5891(%rip), %rsi      # 0x11042
leaq 0x5c00(%rip), %rcx      # 0x113b8
movl $0x25a, %edx            # imm = 0x25A
callq 0x4210
leaq 0x5c91(%rip), %rdi      # 0x1145a
leaq 0x5872(%rip), %rsi      # 0x11042
leaq 0x5be1(%rip), %rcx      # 0x113b8
movl $0x2a7, %edx            # imm = 0x2A7
callq 0x4210
jmp 0xb7e9
jmp 0xb828
jmp 0xb7e9
jmp 0xb828
movq %rax, %rbx
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xb82b
movq 0x50(%rsp), %rsi
jmp 0xb815
jmp 0xb828
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0xb82b
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x43a0
jmp 0xb82b
jmp 0xb828
movq %rax, %rbx
jmp 0xb85a
jmp 0xb828
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0xb8f8
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0xb85a
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x43a0
leaq 0xa8(%rsp), %rdi
callq 0x6cf4
movq %rbx, %rdi
callq 0x46e0