// File: /workspace/github_c_makefile_star2_O2/DrYerzinia[P]C_DigiMode/src/packet/packet_main.c
// Function: main
int main(int argc, char **argv){

	#ifdef EMSCRIPTEN
		EM_ASM(
				if(typeof window === 'undefined'){
					FS.mount(NODEFS, { root: '.' }, '/');
				}
		);
	#endif

	struct arguments arguments;

	arguments.filename = NULL;

	arguments.sample_rate = 44100;
	arguments.bit_rate = 1200;

	arguments.frequency_0 = 1200;
	arguments.frequency_1 = 2200;

	arguments.show_errors = false;
	arguments.raw = false;

	arguments.offset = 0.0925;
	arguments.noise_floor = 0;

	error_t parsed = argp_parse(&argp, argc, argv, 0, 0, &arguments);

	if(parsed != 0)
		return parsed;

	FILE *input_file = NULL;

	if(arguments.filename == NULL)
		input_file = stdin;

	else
		input_file = fopen(arguments.filename, "r");

	// Read and process data from file
	if(input_file != NULL){

		AFSK_Demodulator demod;
		AFSK_Demodulator_init(
			&demod,
			arguments.sample_rate,
			arguments.bit_rate,
			arguments.offset,
			arguments.noise_floor,
			arguments.frequency_0,
			arguments.frequency_1
		);

		signed char byte;
		while(!feof(input_file)){

			byte = getc(input_file);

			char_array *data = AFSK_Demodulator_proccess_byte(&demod, byte);

			if(data){

				APRSPacket new_packet;
				APRSPacket_from_data(&new_packet, data);
				free(data);
				print_packet(&new_packet, arguments.show_errors, arguments.raw);
				APRSPacket_destroy(&new_packet);

			}

		}

		fclose(input_file);

		AFSK_Demodulator_destroy(&demod);

	}

	// File coulden't open
	else {

		fprintf(stderr, "Couldn't open input file\n");
		return -1;

	}

	putchar('\n');

	return 0;

}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rsp, %r9
andq $0x0, (%r9)
movaps 0x12fe(%rip), %xmm0     # 0x3040
movups %xmm0, 0x8(%r9)
andw $0x0, 0x18(%r9)
movq %rsi, %rdx
movl %edi, %esi
movabsq $0x3dbd70a400000000, %rax # imm = 0x3DBD70A400000000
movq %rax, 0x1c(%r9)
leaq 0x3379(%rip), %rdi      # 0x50e0
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x1f48
testl %eax, %eax
jne 0x1e4a
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x1d93
leaq 0x153c(%rip), %rsi      # 0x32c5
callq 0x1120
movq %rax, %rbx
jmp 0x1d9d
movq 0x323e(%rip), %rax      # 0x4fd8
movq (%rax), %rbx
testq %rbx, %rbx
je 0x1e59
movl 0x10(%rsp), %esi
cvtsi2ssl 0x1c(%rsp), %xmm1
movzwl 0x14(%rsp), %edx
movss 0x20(%rsp), %xmm0
movzwl 0x8(%rsp), %ecx
movzwl 0xc(%rsp), %r8d
leaq 0x58(%rsp), %r14
movq %r14, %rdi
callq 0x16cf
leaq 0x28(%rsp), %r15
movq %rbx, %rdi
callq 0x10e0
testl %eax, %eax
jne 0x1e2e
movq %rbx, %rdi
callq 0x1160
movsbl %al, %esi
movq %r14, %rdi
callq 0x1768
testq %rax, %rax
je 0x1dd8
movq %rax, %r12
movq %r15, %rdi
movq %rax, %rsi
callq 0x146c
movq %r12, %rdi
callq 0x1030
movzbl 0x18(%rsp), %esi
movzbl 0x19(%rsp), %edx
movq %r15, %rdi
callq 0x1cd2
movq %r15, %rdi
callq 0x1573
jmp 0x1dd8
movq %rbx, %rdi
callq 0x1080
leaq 0x58(%rsp), %rdi
callq 0x171f
pushq $0xa
popq %rdi
callq 0x1040
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq 0x3198(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x12f2(%rip), %rdi      # 0x315c
pushq $0x19
popq %rsi
pushq $0x1
popq %rdx
callq 0x1150
pushq $-0x1
popq %rax
jmp 0x1e4a