// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x38(%rbp)
movl $0xac44, -0x28(%rbp)    # imm = 0xAC44
movl $0x4b0, -0x24(%rbp)     # imm = 0x4B0
movl $0x4b0, -0x30(%rbp)     # imm = 0x4B0
movl $0x898, -0x2c(%rbp)     # imm = 0x898
movb $0x0, -0x20(%rbp)
movb $0x0, -0x1f(%rbp)
movss 0x16c3(%rip), %xmm0     # 0x4040
movss %xmm0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x8(%rbp), %esi
movq -0x10(%rbp), %rdx
leaq 0x3749(%rip), %rdi      # 0x60e0
xorl %ecx, %ecx
xorl %eax, %eax
movl %eax, %r8d
leaq -0x38(%rbp), %r9
callq 0x2d20
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
je 0x29bb
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2b0e
movq $0x0, -0x48(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x29da
movq 0x3607(%rip), %rax      # 0x5fd8
movq (%rax), %rax
movq %rax, -0x48(%rbp)
jmp 0x29ee
movq -0x38(%rbp), %rdi
leaq 0x18b4(%rip), %rsi      # 0x4299
callq 0x1110
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x2adc
movl -0x28(%rbp), %esi
movl -0x24(%rbp), %eax
movw %ax, %dx
movss -0x18(%rbp), %xmm0
cvtsi2ssl -0x1c(%rbp), %xmm1
movl -0x30(%rbp), %eax
movw %ax, %cx
movl -0x2c(%rbp), %eax
leaq -0xb8(%rbp), %rdi
movzwl %dx, %edx
movzwl %cx, %ecx
movzwl %ax, %r8d
callq 0x1d30
movq -0x48(%rbp), %rdi
callq 0x10d0
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x2a45
jmp 0x2ac5
movq -0x48(%rbp), %rdi
callq 0x1150
movb %al, -0xb9(%rbp)
leaq -0xb8(%rbp), %rdi
movsbl -0xb9(%rbp), %esi
callq 0x1e80
movq %rax, -0xc8(%rbp)
cmpq $0x0, -0xc8(%rbp)
je 0x2ac0
movq -0xc8(%rbp), %rsi
leaq -0xf8(%rbp), %rdi
callq 0x18b0
movq -0xc8(%rbp), %rdi
callq 0x1030
movb -0x20(%rbp), %cl
movb -0x1f(%rbp), %al
leaq -0xf8(%rbp), %rdi
andb $0x1, %cl
andb $0x1, %al
movzbl %cl, %esi
movzbl %al, %edx
callq 0x2870
leaq -0xf8(%rbp), %rdi
callq 0x1ac0
jmp 0x2a2b
movq -0x48(%rbp), %rdi
callq 0x1070
leaq -0xb8(%rbp), %rdi
callq 0x1e00
jmp 0x2afd
movq 0x3515(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1643(%rip), %rsi      # 0x4130
movb $0x0, %al
callq 0x10c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2b0e
movl $0xa, %edi
callq 0x1040
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw (%rax,%rax)