// File: /workspace/github_cpp_makefile_star2_O2/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, %rbx
movq %rdi, %r14
movq %rsi, %rdi
callq 0xaf4b
testb %al, %al
je 0x9f78
movq (%rbx), %rdi
addq $0xc, %rdi
callq 0xaf25
movl %eax, %ebp
movq %r14, %rdi
movl %eax, %esi
callq 0x9afa
testq %rax, %rax
je 0x9dd2
movq (%rax), %rcx
leaq 0xa8(%rsp), %rdi
movq %rax, %rsi
callq *0x40(%rcx)
movq (%rbx), %rdx
leaq 0x34(%rdx), %rsi
leaq 0x98(%rsp), %rax
addq 0x8(%rbx), %rdx
movq %rax, -0x10(%rax)
leaq 0x88(%rsp), %rdi
callq 0xa34a
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
andq $0x0, 0x70(%rsp)
leaq 0x5832(%rip), %rsi      # 0xf3e8
leaq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0xa09e
movq %rbx, 0x80(%rsp)
leaq 0x88(%rsp), %rdi
leaq 0x60(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0xb1d0
leaq 0x40(%rsp), %r14
movq %r14, %rdi
callq 0x4170
xorl %r15d, %r15d
leaq 0x5008(%rip), %r12      # 0xec03
movq $0x0, (%rsp)
movq $0x0, 0x8(%rsp)
xorl %ebx, %ebx
movl %ebx, %ebx
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rax
subq %rdi, %rax
sarq $0x5, %rax
cmpq %rbx, %rax
jbe 0x9ce3
movq %rbx, %rbp
shlq $0x5, %rbp
addq %rbp, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x41c0
cmpq $-0x1, %rax
je 0x9cdc
addq 0x60(%rsp), %rbp
movq %r14, %rdi
movq %rbp, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x4390
xorl %eax, %eax
movl %eax, %ebp
cmpq %rbp, 0x48(%rsp)
jbe 0x9c82
movq 0x40(%rsp), %r13
movsbl (%r13,%rbp), %edi
callq 0x44c0
movb %al, (%r13,%rbp)
leal 0x1(%rbp), %eax
jmp 0x9c5f
movq %r14, %rdi
leaq 0x575f(%rip), %rsi      # 0xf3eb
callq 0x40f0
testl %eax, %eax
je 0x9cc8
movq %r14, %rdi
leaq 0x5312(%rip), %rsi      # 0xefb1
callq 0x40f0
testl %eax, %eax
je 0x9ccd
movq %r14, %rdi
leaq 0x5746(%rip), %rsi      # 0xf3f8
callq 0x40f0
testl %eax, %eax
sete %al
movq (%rsp), %rcx
orb %al, %cl
movq %rcx, (%rsp)
jmp 0x9cd4
movb $0x1, %r15b
jmp 0x9cd4
movb $0x1, %al
movq %rax, 0x8(%rsp)
movq %r14, %rdi
callq 0x4170
incl %ebx
jmp 0x9c0e
movq 0x80(%rsp), %r13
movq 0x8(%r13), %rax
movq %rax, 0x38(%rsp)
movq 0xd0(%rsp), %r12
movq 0xf0(%rsp), %r14
movq 0x110(%rsp), %rbx
pushq $0x30
popq %rdi
callq 0x4350
movq %rax, %rbp
addl $0x10, %r12d
xorl %eax, %eax
testb $0x1, %r15b
cmovnel %eax, %r12d
movq 0x38(%rsp), %rcx
addl %r12d, %ecx
addl $0x8, %ebx
testb $0x1, 0x8(%rsp)
cmovnel %eax, %ebx
addl $0x8, %r14d
testb $0x1, (%rsp)
cmovnel %eax, %r14d
addl %ebx, %r14d
addl %ecx, %r14d
movups (%r13), %xmm0
movups 0x10(%r13), %xmm1
movups 0x20(%r13), %xmm2
movups %xmm2, 0x20(%rbp)
movups %xmm1, 0x10(%rbp)
movups %xmm0, (%rbp)
movq %r14, %rdi
callq 0x4030
movq %rax, %r12
movq %rax, (%rbp)
movq %rbp, 0x30(%rsp)
movq %r14, 0x8(%rbp)
movq (%r13), %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 0x38(%rsp), %r13
leaq -0x34(%r13), %rdx
callq 0x42b0
movq %r12, %rbx
addq %r13, %rbx
testb $0x1, %r15b
je 0x9e0e
movq 0x30(%rsp), %r13
jmp 0x9e6e
movq 0xb217(%rip), %rdi      # 0x14ff0
leaq 0x55d1(%rip), %rsi      # 0xf3b1
callq 0x4340
movl %ebp, %esi
movq %rax, %rdi
callq 0x4240
leaq 0x55e6(%rip), %rsi      # 0xf3dc
movq %rax, %rdi
callq 0x4340
movq %rax, %rdi
callq 0x41b0
xorl %r13d, %r13d
jmp 0x9f63
leaq 0xc8(%rsp), %rdx
leaq 0x55e0(%rip), %rsi      # 0xf3fd
leaq 0x10(%rsp), %rdi
callq 0xa0f8
leaq 0x55ba(%rip), %rdx      # 0xf3e8
leaq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xa0d9
movq 0x30(%rsp), %r13
leaq 0x10(%rsp), %rdi
callq 0x4170
leaq 0x40(%rsp), %r15
movq (%r15), %rsi
movq 0x8(%r15), %rbp
movq %rbx, %rdi
movq %rbp, %rdx
callq 0x42b0
addq %rbp, %rbx
movq %r15, %rdi
callq 0x4170
testb $0x1, 0x8(%rsp)
jne 0x9ed0
leaq 0x108(%rsp), %rdx
leaq 0x5588(%rip), %rsi      # 0xf40c
leaq 0x10(%rsp), %rdi
callq 0xa0f8
leaq 0x5553(%rip), %rdx      # 0xf3e8
leaq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xa0d9
leaq 0x10(%rsp), %rdi
callq 0x4170
leaq 0x40(%rsp), %r15
movq (%r15), %rsi
movq 0x8(%r15), %rbp
movq %rbx, %rdi
movq %rbp, %rdx
callq 0x42b0
addq %rbp, %rbx
movq %r15, %rdi
callq 0x4170
testb $0x1, (%rsp)
jne 0x9f37
leaq 0xe8(%rsp), %rdx
leaq 0x552e(%rip), %rsi      # 0xf413
leaq 0x10(%rsp), %rdi
callq 0xa0f8
leaq 0x54f2(%rip), %rdx      # 0xf3e8
leaq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xa0d9
leaq 0x10(%rsp), %rdi
callq 0x4170
leaq 0x40(%rsp), %r15
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq %rbx, %rdi
callq 0x42b0
addq 0x8(%r15), %rbx
movq %r15, %rdi
callq 0x4170
movq (%r13), %r12
movq 0x8(%r13), %r14
addq %r12, %r14
cmpq %r14, %rbx
jne 0x9f97
leaq 0x60(%rsp), %rdi
callq 0xa16e
leaq 0x88(%rsp), %rdi
callq 0x4170
leaq 0xa8(%rsp), %rdi
callq 0x6744
movq %r13, %rax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x53df(%rip), %rdi      # 0xf35e
leaq 0x507c(%rip), %rsi      # 0xf002
leaq 0x53eb(%rip), %rcx      # 0xf378
movl $0x25a, %edx            # imm = 0x25A
callq 0x4210
leaq 0x547c(%rip), %rdi      # 0xf41a
leaq 0x505d(%rip), %rsi      # 0xf002
leaq 0x53cc(%rip), %rcx      # 0xf378
movl $0x2a7, %edx            # imm = 0x2A7
callq 0x4210
jmp 0x9fbe
jmp 0x9fe2
jmp 0x9fbe
jmp 0x9fe2
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
jmp 0x9fd2
jmp 0x9fe2
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x4170
jmp 0x9fe5
jmp 0x9fe2
movq %rax, %rbx
jmp 0x9ffc
jmp 0x9fe2
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0xa16e
leaq 0x88(%rsp), %rdi
callq 0x4170
leaq 0xa8(%rsp), %rdi
callq 0x6744
movq %rbx, %rdi
callq 0x4680
nop