// File: /workspace/github_c_makefile_star2_O3/pavel-demin[P]ft8d-c/ft8d.c
// Function: main
int main(int argc, char **argv)
{
  FILE *fp;
  double dialfreq;
  int i, j, freq;
  sync_t *curr, *next, temp;
  char *date, *time, *suffix, call[12], grid[5];
  real_t dt, a[4] = {0.35875, 0.48829, 0.14128, 0.01168};

  if(argc != 2)
  {
    return EXIT_FAILURE;
  }

  if((fp = fopen(argv[1], "rb")) == NULL)
  {
    fprintf(stderr, "Cannot open input file %s.\n", argv[1]);
    return EXIT_FAILURE;
  }

  suffix = strstr(argv[1], ".c2");
  *suffix = 0;
  time = suffix - 4;
  *(suffix - 5) = 0;
  date = suffix - 11;

  signal = malloc(sizeof(complex_t) * 60000);
  map = malloc(sizeof(real_t) * NSYM * NFFT);
  list = malloc(sizeof(sync_t) * NFFT);

  buffer = pffft_aligned_malloc(sizeof(complex_t) * NFFT);
  setup = pffft_new_setup(NFFT, PFFFT_COMPLEX);

  for(i = 0; i < NSPS; ++i)
  {
    window[i] = a[0] -
      a[1] * cosf(2.0 * M_PI * i / (NSPS - 1)) +
      a[2] * cosf(4.0 * M_PI * i / (NSPS - 1)) -
      a[3] * cosf(6.0 * M_PI * i / (NSPS - 1));
  }

  fread(&dialfreq, 1, 8, fp);

  for(i = 0; i < 4; ++i)
  {
    fread(signal, 1, 480000, fp);

    sync();

    for(j = 2; j < NFFT - 2; ++j)
    {
      curr = &list[j];
      next = &list[j + 1];

      if(curr->k == 0 || curr->s < 2.5) continue;

      if(next->k != 0 && next->s > curr->s)
      {
        temp = *curr;
        *curr = *next;
        *next = temp;
      }

      process(curr);

      if(!decode(30) || !unpack(call, grid)) continue;

      next->k = 0;

      dt = curr->j * NSTP / 4.0e3 - 0.5;
      freq = floor(dialfreq + curr->i * 4.0e3 / NFFT - 2.0e3 + 0.5);
      printf("%6s %4s%02d %5.1f %3d %5.2f %8d %11s %4s\n", date, time, i * 15, curr->s, snr(curr), dt, freq, call, grid);
    }
  }

  return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl $0x1, %ebx
cmpl $0x2, %edi
jne 0x7208
movq %rsi, %r14
movq 0x8(%rsi), %rdi
addq $0x8, %r14
leaq 0x1af4(%rip), %rsi      # 0x8987
callq 0x1110
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x7219
movq (%r14), %rdi
leaq 0x1af6(%rip), %rsi      # 0x89a6
callq 0x1140
xorl %ebx, %ebx
movb %bl, (%rax)
movq %rax, %r15
movb %bl, -0x5(%rax)
movl $0x75300, %edi          # imm = 0x75300
callq 0x10f0
movq %rax, 0x6300(%rip)      # 0xd1d0
movl $0xb70200, %edi         # imm = 0xB70200
callq 0x10f0
movq %rax, 0x62ff(%rip)      # 0xd1e0
movl $0xfa00, %edi           # imm = 0xFA00
callq 0x10f0
movq %rax, 0x62f6(%rip)      # 0xd1e8
movl $0x6400, %edi           # imm = 0x6400
callq 0x14b5
movq %rax, 0x49bd(%rip)      # 0xb8c0
movl $0xc80, %edi            # imm = 0xC80
movl $0x1, %esi
callq 0x2450
movq %rax, 0x62bf(%rip)      # 0xd1d8
leaq 0x49b0(%rip), %r14      # 0xb8d0
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
movsd %xmm0, 0x10(%rsp)
mulsd 0x1183(%rip), %xmm0     # 0x80b8
movsd 0x1a03(%rip), %xmm1     # 0x8940
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
callq 0x1040
mulss 0x19a6(%rip), %xmm0     # 0x88f8
addss 0x19a2(%rip), %xmm0     # 0x88fc
movss %xmm0, 0x8(%rsp)
movsd 0x10(%rsp), %xmm0
mulsd 0x19da(%rip), %xmm0     # 0x8948
divsd 0x19ca(%rip), %xmm0     # 0x8940
cvtsd2ss %xmm0, %xmm0
callq 0x1040
mulss 0x1979(%rip), %xmm0     # 0x8900
addss 0x8(%rsp), %xmm0
movss %xmm0, 0x8(%rsp)
movsd 0x10(%rsp), %xmm0
mulsd 0x19af(%rip), %xmm0     # 0x8950
divsd 0x1997(%rip), %xmm0     # 0x8940
cvtsd2ss %xmm0, %xmm0
callq 0x1040
mulss 0x194a(%rip), %xmm0     # 0x8904
addss 0x8(%rsp), %xmm0
movss %xmm0, (%r14)
incq %rbx
addq $0x4, %r14
cmpq $0x640, %rbx            # imm = 0x640
jne 0x6f20
leaq -0x4(%r15), %rax
movq %rax, 0x30(%rsp)
addq $-0xb, %r15
movq %r15, 0x38(%rsp)
leaq 0x60(%rsp), %rbx
movl $0x1, %esi
movl $0x8, %edx
movq %rbx, %rdi
movq 0x20(%rsp), %rcx
callq 0x1050
xorl %r14d, %r14d
movsd (%rbx), %xmm0
movsd %xmm0, 0x28(%rsp)
leaq 0x6c(%rsp), %rbp
leaq 0x5b(%rsp), %rbx
movq 0x61ab(%rip), %rdi      # 0xd1d0
movl $0x1, %esi
movl $0x75300, %edx          # imm = 0x75300
movq 0x20(%rsp), %rcx
callq 0x1050
callq 0x61c8
leal (%r14,%r14,4), %eax
leal (%rax,%rax,2), %eax
movl %eax, 0x1c(%rsp)
xorl %r15d, %r15d
movq 0x6195(%rip), %r12      # 0xd1e8
cmpl $0x0, 0x30(%r12,%r15)
je 0x71e8
movss 0x38(%r12,%r15), %xmm0
movss 0x189a(%rip), %xmm1     # 0x8908
ucomiss %xmm0, %xmm1
ja 0x71e8
leaq (%r12,%r15), %rdi
addq $0x28, %rdi
cmpl $0x0, 0x1c(%rdi)
je 0x70c3
movss 0x4c(%r12,%r15), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x70c3
leaq (%r12,%r15), %rax
addq $0x3c, %rax
movl 0x10(%rdi), %ecx
movl %ecx, 0x50(%rsp)
movups (%rdi), %xmm0
movaps %xmm0, 0x40(%rsp)
movl 0x10(%rax), %ecx
movl %ecx, 0x10(%rdi)
movups (%rax), %xmm0
movups %xmm0, (%rdi)
movl 0x50(%rsp), %ecx
movl %ecx, 0x10(%rax)
movaps 0x40(%rsp), %xmm0
movups %xmm0, (%rax)
callq 0x6434
movl $0x1e, %edi
callq 0x66df
testl %eax, %eax
je 0x71e8
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x6bc4
testl %eax, %eax
je 0x71e8
movl $0x0, 0x44(%r12,%r15)
movl 0x2c(%r12,%r15), %eax
shll $0x6, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x184b(%rip), %xmm1     # 0x8958
divsd %xmm1, %xmm0
addsd 0x1847(%rip), %xmm0     # 0x8960
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x10(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0x28(%r12,%r15), %xmm0
mulsd %xmm1, %xmm0
divsd 0x182f(%rip), %xmm0     # 0x8968
addsd 0x28(%rsp), %xmm0
addsd 0x1829(%rip), %xmm0     # 0x8970
movsd 0x17e9(%rip), %xmm1     # 0x8938
addsd %xmm1, %xmm0
callq 0x1130
movq %rbp, %r13
cvttsd2si %xmm0, %ebp
xorps %xmm0, %xmm0
cvtss2sd 0x38(%r12,%r15), %xmm0
movsd %xmm0, 0x8(%rsp)
xorps %xmm0, %xmm0
cvtss2sd 0x34(%r12,%r15), %xmm0
addsd 0x179f(%rip), %xmm0     # 0x8920
cvtsd2ss %xmm0, %xmm0
callq 0x10d0
cvtss2sd %xmm0, %xmm0
mulsd 0x1792(%rip), %xmm0     # 0x8928
addsd 0x1792(%rip), %xmm0     # 0x8930
addsd 0x1792(%rip), %xmm0     # 0x8938
callq 0x1130
cvttsd2si %xmm0, %r8d
xorps %xmm1, %xmm1
cvtss2sd 0x10(%rsp), %xmm1
leaq 0x17ea(%rip), %rdi      # 0x89aa
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rdx
movl 0x1c(%rsp), %ecx
movsd 0x8(%rsp), %xmm0
movl %ebp, %r9d
movq %r13, %rbp
movb $0x2, %al
pushq %rbx
pushq %r13
callq 0x1080
addq $0x10, %rsp
addq $0x14, %r15
cmpq $0xf9b0, %r15           # imm = 0xF9B0
jne 0x704c
incl %r14d
cmpl $0x4, %r14d
jne 0x701e
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x1170
jmp 0x7208