// File: /workspace/github_c_makefile_star2_O2/canboat[P]canboat/analyzer/print.c
// Function: fieldPrintBinary
extern bool fieldPrintBinary(const Field   *field,
                             const char    *fieldName,
                             const uint8_t *data,
                             size_t         dataLen,
                             size_t         startBit,
                             size_t        *bits)
{
  size_t      i;
  size_t      remaining_bits;
  const char *s;

  if (!adjustDataLenStart(&data, &dataLen, &startBit))
  {
    return false;
  }

  if (*bits == 0 && strcmp(field->fieldType, "BINARY") == 0)
  {
    // The length is in the previous field. This is heuristically defined right now, it might change.
    // The only PGNs where this happens are AIS PGNs 129792, 129795 and 129797.
    *bits = g_previousFieldValue;
  }

  if (startBit + *bits > dataLen * 8)
  {
    *bits = dataLen * 8 - startBit;
  }

  if (showJson)
  {
    mprintf("\"");
  }
  remaining_bits = *bits;
  s              = "";
  for (i = 0; i < (*bits + 7) >> 3; i++)
  {
    uint8_t byte = data[i];

    if (i == 0 && startBit != 0)
    {
      byte = byte >> startBit; // Shift off older bits
      if (remaining_bits + startBit < 8)
      {
        byte = byte & ((1 << remaining_bits) - 1);
      }
      byte = byte << startBit; // Shift zeros back in
      remaining_bits -= (8 - startBit);
    }
    else
    {
      if (remaining_bits < 8)
      {
        // only the lower remaining_bits should be used
        byte = byte & ((1 << remaining_bits) - 1);
      }
      remaining_bits -= 8;
    }
    mprintf("%s%2.02X", s, byte);
    s = " ";
  }
  if (showJson)
  {
    mprintf("\"");
  }
  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %r15
movq %r8, %rbp
shrq $0x3, %rbp
movq %rcx, %rbx
subq %rbp, %rbx
jbe 0x1a235
movq %r9, %r14
movq %rdx, %r12
movl %r8d, %r13d
andl $0x7, %r13d
movq (%r9), %rax
testq %rax, %rax
jne 0x1a11e
movq %r8, (%rsp)
movq 0x8(%rdi), %rdi
leaq 0x5335(%rip), %rsi      # 0x1f438
callq 0xd1a0
testl %eax, %eax
je 0x1a110
xorl %eax, %eax
jmp 0x1a11a
movq 0x77731(%rip), %rax     # 0x91848
movq %rax, (%r14)
movq (%rsp), %r8
leaq (%rax,%r13), %rcx
shlq $0x3, %rbx
cmpq %rbx, %rcx
jbe 0x1a134
subq %r13, %rbx
movq %rbx, (%r14)
movq %rbx, %rax
movq %r15, 0x10(%rsp)
leaq 0x755b2(%rip), %rcx     # 0x8f6f2
cmpb $0x1, (%rcx)
jne 0x1a15c
leaq 0x12ca5(%rip), %rdi     # 0x2cdf1
xorl %eax, %eax
movq %r8, %rbx
callq 0x19194
movq %rbx, %r8
movq (%r14), %rax
movq %rbp, 0x8(%rsp)
addq %rbp, %r12
orq $-0x8, %r8
movq %r8, (%rsp)
leaq 0x822b(%rip), %rsi      # 0x2239e
leaq 0x828f(%rip), %r15      # 0x22409
xorl %ebp, %ebp
movq %rax, %rbx
addq $0x7, %rax
shrq $0x3, %rax
cmpq %rax, %rbp
jae 0x1a211
testq %r13, %r13
sete %cl
movzbl (%r12,%rbp), %eax
testq %rbp, %rbp
setne %dl
orb %cl, %dl
jne 0x1a1d0
movl %r13d, %ecx
shrl %cl, %eax
pushq $-0x1
popq %rdx
movl %ebx, %ecx
shll %cl, %edx
leaq (%rbx,%r13), %rcx
cmpq $0x8, %rcx
notl %edx
movl $0xff, %ecx
cmovael %ecx, %edx
andl %eax, %edx
movl %r13d, %ecx
shll %cl, %edx
movq (%rsp), %rcx
jmp 0x1a1ef
pushq $-0x1
popq %rdx
movl %ebx, %ecx
shll %cl, %edx
cmpq $0x8, %rbx
notb %dl
movzbl %dl, %ecx
movl $0xff, %edx
cmovael %edx, %ecx
andl %ecx, %eax
pushq $-0x8
popq %rcx
movl %eax, %edx
addq %rcx, %rbx
movzbl %dl, %edx
leaq 0x12a6a(%rip), %rdi     # 0x2cc66
xorl %eax, %eax
callq 0x19194
incq %rbp
movq (%r14), %rax
movq %r15, %rsi
jmp 0x1a17f
leaq 0x754da(%rip), %rax     # 0x8f6f2
cmpb $0x1, (%rax)
movq 0x10(%rsp), %r15
movq 0x8(%rsp), %rbp
jne 0x1a235
leaq 0x12bc3(%rip), %rdi     # 0x2cdf1
xorl %eax, %eax
callq 0x19194
cmpq %rbp, %r15
seta %al
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq