// File: knei-knurow[P]lidar-visualizations/src/cloud-grabbers.cpp
// Function: CloudFileSeriesGrabber::read(Cloud&)
bool CloudFileSeriesGrabber::read(Cloud& cloud) {
  if (!status_)
    return false;

  if (std::chrono::steady_clock::now() < next_cloud_time_)
    std::this_thread::sleep_until(next_cloud_time_);

  if (cloud.size == 0)
    next_cloud_time_ = std::chrono::steady_clock::now();

  cloud = Cloud();
  while (file_.good()) {
    std::string line;
    std::getline(file_, line);
    PointCyl pt_cyl;
    std::stringstream sline(line);

    if (line.empty() || line[0] == '#') {
      continue;
    } else if (line[0] == '!') {
      long long _, delay_ms = 0;
      char __;
      sline >> __ >> _ >> delay_ms;
      next_cloud_time_ = std::chrono::steady_clock::now() + std::chrono::milliseconds(delay_ms);
      break;
    }
    sline >> pt_cyl.angle >> pt_cyl.dist;

    if (pt_cyl.dist > cloud.max) {
      cloud.max = pt_cyl.dist;
      cloud.max_idx = cloud.size;
    }
    if (pt_cyl.dist < cloud.min && pt_cyl.dist > 0) {
      cloud.min = pt_cyl.dist;
      cloud.min_idx = cloud.size;
    }

    cloud.size++;
    cloud.pts_cyl.push_back(pt_cyl);
    cloud.pts_cart.push_back(pt_cyl.to_cart());
  }

  if (cloud.size == 0 && clouds_cnt_ == 0) {
    std::cerr << "Error: File does not contain a valid cloud." << std::endl;
    status_ = false;
  }

  clouds_cnt_++;

  if (cloud.size == 0) {
    std::cerr << "Cloud series end." << std::endl;
    next_cloud_time_ += std::chrono::milliseconds(1000);
    file_.clear();
    file_.seekg(0);
    status_ = open();
  }

  return status_;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x310, %rsp            # imm = 0x310
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x2a8(%rbp)
testb $0x1, 0x8(%rax)
jne 0xbc3d
movb $0x0, -0x1(%rbp)
jmp 0xc1a2
callq 0x44a0
movq -0x2a8(%rbp), %rsi
movq %rax, -0x20(%rbp)
addq $0x240, %rsi            # imm = 0x240
leaq -0x20(%rbp), %rdi
callq 0xc3a0
testb $0x1, %al
jne 0xbc63
jmp 0xbc76
movq -0x2a8(%rbp), %rdi
addq $0x240, %rdi            # imm = 0x240
callq 0xc3e0
movq -0x18(%rbp), %rax
cmpq $0x0, 0x48(%rax)
jne 0xbc9f
callq 0x44a0
movq %rax, %rcx
movq -0x2a8(%rbp), %rax
movq %rcx, -0x28(%rbp)
movq -0x28(%rbp), %rcx
movq %rcx, 0x240(%rax)
leaq -0x78(%rbp), %rdi
xorl %esi, %esi
movl $0x50, %edx
callq 0x4200
leaq -0x78(%rbp), %rdi
callq 0x8040
movq -0x18(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0xc430
leaq -0x78(%rbp), %rdi
callq 0x8190
movq -0x2a8(%rbp), %rax
movq %rax, %rdi
addq $0x30, %rdi
movq 0x30(%rax), %rax
addq -0x18(%rax), %rdi
callq 0x45c0
testb $0x1, %al
jne 0xbcf2
jmp 0xc042
leaq -0x98(%rbp), %rdi
movq %rdi, -0x2b0(%rbp)
callq 0x44c0
movq -0x2a8(%rbp), %rdi
movq -0x2b0(%rbp), %rsi
addq $0x30, %rdi
callq 0x4080
jmp 0xbd1e
leaq -0xac(%rbp), %rdi
callq 0xc1e0
movl $0x10, %edi
movl $0x8, %esi
callq 0xc200
movl %eax, -0x2b4(%rbp)
jmp 0xbd41
movl -0x2b4(%rbp), %edx
leaq -0x238(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x4550
jmp 0xbd5c
leaq -0x98(%rbp), %rdi
callq 0x45a0
testb $0x1, %al
jne 0xbd94
xorl %eax, %eax
movl %eax, %esi
leaq -0x98(%rbp), %rdi
callq 0x46a0
movq %rax, -0x2c0(%rbp)
jmp 0xbd85
movq -0x2c0(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0xbddd
movl $0x2, -0x23c(%rbp)
jmp 0xbfef
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0xc031
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0x238(%rbp), %rdi
callq 0x42c0
jmp 0xc031
xorl %eax, %eax
movl %eax, %esi
leaq -0x98(%rbp), %rdi
callq 0x46a0
movq %rax, -0x2c8(%rbp)
jmp 0xbdf6
movq -0x2c8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x21, %eax
jne 0xbed0
movq $0x0, -0x250(%rbp)
leaq -0x238(%rbp), %rdi
leaq -0x251(%rbp), %rsi
callq 0x4490
movq %rax, -0x2d0(%rbp)
jmp 0xbe30
movq -0x2d0(%rbp), %rdi
leaq -0x248(%rbp), %rsi
callq 0x4410
movq %rax, -0x2d8(%rbp)
jmp 0xbe4c
movq -0x2d8(%rbp), %rdi
leaq -0x250(%rbp), %rsi
callq 0x4410
jmp 0xbe61
callq 0x44a0
movq %rax, -0x268(%rbp)
leaq -0x270(%rbp), %rdi
leaq -0x250(%rbp), %rsi
callq 0xc4f0
jmp 0xbe82
leaq -0x268(%rbp), %rdi
leaq -0x270(%rbp), %rsi
callq 0xc4a0
movq %rax, -0x2e0(%rbp)
jmp 0xbe9e
movq -0x2a8(%rbp), %rax
movq -0x2e0(%rbp), %rcx
movq %rcx, -0x260(%rbp)
movq -0x260(%rbp), %rcx
movq %rcx, 0x240(%rax)
movl $0x3, -0x23c(%rbp)
jmp 0xbfef
jmp 0xbed2
leaq -0x238(%rbp), %rdi
leaq -0xac(%rbp), %rsi
callq 0x4090
movq %rax, -0x2e8(%rbp)
jmp 0xbeee
movq -0x2e8(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x4090
jmp 0xbf03
movss -0xa8(%rbp), %xmm0
movq -0x18(%rbp), %rax
ucomiss 0x34(%rax), %xmm0
jbe 0xbf36
movss -0xa8(%rbp), %xmm0
movq -0x18(%rbp), %rax
movss %xmm0, 0x34(%rax)
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x38(%rax)
movss -0xa8(%rbp), %xmm1
movq -0x18(%rbp), %rax
movss 0x30(%rax), %xmm0
ucomiss %xmm1, %xmm0
jbe 0xbf7d
movss -0xa8(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0xbf7d
movss -0xa8(%rbp), %xmm0
movq -0x18(%rbp), %rax
movss %xmm0, 0x30(%rax)
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rcx
incq %rcx
movq %rcx, 0x48(%rax)
movq -0x18(%rbp), %rdi
leaq -0xac(%rbp), %rsi
callq 0xc220
jmp 0xbf9e
movq -0x18(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x2f8(%rbp)
leaq -0xac(%rbp), %rdi
callq 0xb150
movq %rax, -0x2f0(%rbp)
jmp 0xbfc2
movq -0x2f8(%rbp), %rdi
movq -0x2f0(%rbp), %rax
movq %rax, -0x278(%rbp)
leaq -0x278(%rbp), %rsi
callq 0xc290
jmp 0xbfe5
movl $0x0, -0x23c(%rbp)
leaq -0x238(%rbp), %rdi
callq 0x42c0
leaq -0x98(%rbp), %rdi
callq 0x4230
movl -0x23c(%rbp), %eax
movl %eax, -0x2fc(%rbp)
testl %eax, %eax
je 0xc02c
jmp 0xc019
movl -0x2fc(%rbp), %eax
subl $0x2, %eax
je 0xbcce
jmp 0xc02a
jmp 0xc042
jmp 0xbcce
leaq -0x98(%rbp), %rdi
callq 0x4230
jmp 0xc1b0
movq -0x18(%rbp), %rax
cmpq $0x0, 0x48(%rax)
jne 0xc08b
movq -0x2a8(%rbp), %rax
cmpq $0x0, 0x238(%rax)
jne 0xc08b
movq 0x10f8b(%rip), %rdi     # 0x1cff0
leaq 0x74b8(%rip), %rsi      # 0x13524
callq 0x4360
movq %rax, %rdi
movq 0x10f45(%rip), %rsi     # 0x1cfc0
callq 0x43a0
movq -0x2a8(%rbp), %rax
movb $0x0, 0x8(%rax)
movq -0x2a8(%rbp), %rax
movq 0x238(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x238(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x48(%rax)
jne 0xc193
movq 0x10f36(%rip), %rdi     # 0x1cff0
leaq 0x748f(%rip), %rsi      # 0x13550
callq 0x4360
movq %rax, %rdi
movq 0x10ef0(%rip), %rsi     # 0x1cfc0
callq 0x43a0
movl $0x3e8, -0x28c(%rbp)    # imm = 0x3E8
leaq -0x288(%rbp), %rdi
leaq -0x28c(%rbp), %rsi
callq 0xc510
leaq -0x280(%rbp), %rdi
leaq -0x288(%rbp), %rsi
callq 0xc530
movq -0x2a8(%rbp), %rdi
addq $0x240, %rdi            # imm = 0x240
leaq -0x280(%rbp), %rsi
callq 0xc570
movq -0x2a8(%rbp), %rax
movq %rax, %rdi
addq $0x30, %rdi
movq 0x30(%rax), %rax
addq -0x18(%rax), %rdi
xorl %esi, %esi
callq 0x45b0
movq -0x2a8(%rbp), %rax
addq $0x30, %rax
movq %rax, -0x308(%rbp)
leaq -0x2a0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xc5a0
movq -0x308(%rbp), %rdi
movq -0x2a0(%rbp), %rsi
movq -0x298(%rbp), %rdx
callq 0x4460
movq -0x2a8(%rbp), %rdi
callq 0xbaf0
movb %al, %cl
movq -0x2a8(%rbp), %rax
andb $0x1, %cl
movb %cl, 0x8(%rax)
movq -0x2a8(%rbp), %rax
movb 0x8(%rax), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x310, %rsp            # imm = 0x310
popq %rbp
retq
movq -0xa0(%rbp), %rdi
callq 0x4610
nopl (%rax)