// File: rpavlik[P]edid-decode/edid-decode.cpp
// Function: parse_gtf(char*, gtf_parsed_data&)
static void parse_gtf(char *optarg, gtf_parsed_data &data)
{
	memset(&data, 0, sizeof(data));
	data.params_from_edid = true;
	data.C = 40;
	data.M = 600;
	data.K = 128;
	data.J = 20;

	while (*optarg != '\0') {
		int opt;
		double opt_val;

		opt = parse_gtf_subopt(&optarg, &opt_val);

		switch (opt) {
		case GTF_WIDTH:
			data.w = round(opt_val);
			break;
		case GTF_HEIGHT:
			data.h = round(opt_val);
			break;
		case GTF_FPS:
			data.freq = opt_val;
			data.ip_parm = gtf_ip_vert_freq;
			break;
		case GTF_HORFREQ:
			data.freq = opt_val;
			data.ip_parm = gtf_ip_hor_freq;
			break;
		case GTF_PIXCLK:
			data.freq = opt_val;
			data.ip_parm = gtf_ip_clk_freq;
			break;
		case GTF_INTERLACED:
			data.interlaced = true;
			break;
		case GTF_OVERSCAN:
			data.overscan = true;
			break;
		case GTF_SECONDARY:
			data.secondary = true;
			break;
		case GTF_C2:
			data.C = opt_val / 2.0;
			data.params_from_edid = false;
			break;
		case GTF_M:
			data.M = round(opt_val);
			data.params_from_edid = false;
			break;
		case GTF_K:
			data.K = round(opt_val);
			data.params_from_edid = false;
			break;
		case GTF_J2:
			data.J = opt_val / 2.0;
			data.params_from_edid = false;
			break;
		default:
			break;
		}
	}

	if (!data.w || !data.h) {
		fprintf(stderr, "Missing width and/or height.\n");
		usage();
		std::exit(EXIT_FAILURE);
	}
	if (!data.freq) {
		fprintf(stderr, "One of fps, horfreq or pixclk must be given.\n");
		usage();
		std::exit(EXIT_FAILURE);
	}
	if (!data.secondary)
		data.params_from_edid = false;
	if (data.interlaced && data.ip_parm == gtf_ip_vert_freq)
		data.freq /= 2;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
xorl %esi, %esi
movl $0x38, %edx
callq 0x41c0
movq -0x10(%rbp), %rax
movb $0x1, 0x33(%rax)
movq -0x10(%rbp), %rax
movsd 0x257ac(%rip), %xmm0    # 0x2f040
movsd %xmm0, 0x10(%rax)
movq -0x10(%rbp), %rax
movsd 0x257a3(%rip), %xmm0    # 0x2f048
movsd %xmm0, 0x18(%rax)
movq -0x10(%rbp), %rax
movsd 0x2579a(%rip), %xmm0    # 0x2f050
movsd %xmm0, 0x20(%rax)
movq -0x10(%rbp), %rax
movsd 0x25791(%rip), %xmm0    # 0x2f058
movsd %xmm0, 0x28(%rax)
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x9a54
leaq -0x8(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0xa110
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq %rax, -0x28(%rbp)
subq $0xb, %rax
ja 0x9a4d
movq -0x28(%rbp), %rax
leaq 0x258b0(%rip), %rcx     # 0x2f1b8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movsd -0x20(%rbp), %xmm0
callq 0x4160
cvttsd2si %xmm0, %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
jmp 0x9a4f
movsd -0x20(%rbp), %xmm0
callq 0x4160
cvttsd2si %xmm0, %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0x9a4f
movsd -0x20(%rbp), %xmm0
movq -0x10(%rbp), %rax
movsd %xmm0, 0x8(%rax)
movq -0x10(%rbp), %rax
movl $0x1, 0x34(%rax)
jmp 0x9a4f
movsd -0x20(%rbp), %xmm0
movq -0x10(%rbp), %rax
movsd %xmm0, 0x8(%rax)
movq -0x10(%rbp), %rax
movl $0x2, 0x34(%rax)
jmp 0x9a4f
movsd -0x20(%rbp), %xmm0
movq -0x10(%rbp), %rax
movsd %xmm0, 0x8(%rax)
movq -0x10(%rbp), %rax
movl $0x3, 0x34(%rax)
jmp 0x9a4f
movq -0x10(%rbp), %rax
movb $0x1, 0x31(%rax)
jmp 0x9a4f
movq -0x10(%rbp), %rax
movb $0x1, 0x30(%rax)
jmp 0x9a4f
movq -0x10(%rbp), %rax
movb $0x1, 0x32(%rax)
jmp 0x9a4f
movsd -0x20(%rbp), %xmm0
movsd 0x25638(%rip), %xmm1    # 0x2f010
divsd %xmm1, %xmm0
movq -0x10(%rbp), %rax
movsd %xmm0, 0x10(%rax)
movq -0x10(%rbp), %rax
movb $0x0, 0x33(%rax)
jmp 0x9a4f
movsd -0x20(%rbp), %xmm0
callq 0x4160
movq -0x10(%rbp), %rax
movsd %xmm0, 0x18(%rax)
movq -0x10(%rbp), %rax
movb $0x0, 0x33(%rax)
jmp 0x9a4f
movsd -0x20(%rbp), %xmm0
callq 0x4160
movq -0x10(%rbp), %rax
movsd %xmm0, 0x20(%rax)
movq -0x10(%rbp), %rax
movb $0x0, 0x33(%rax)
jmp 0x9a4f
movsd -0x20(%rbp), %xmm0
movsd 0x255da(%rip), %xmm1    # 0x2f010
divsd %xmm1, %xmm0
movq -0x10(%rbp), %rax
movsd %xmm0, 0x28(%rax)
movq -0x10(%rbp), %rax
movb $0x0, 0x33(%rax)
jmp 0x9a4f
jmp 0x9a4f
jmp 0x98cc
movq -0x10(%rbp), %rax
cmpl $0x0, (%rax)
je 0x9a67
movq -0x10(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jne 0x9a8e
movq 0x4156a(%rip), %rax     # 0x4afd8
movq (%rax), %rdi
leaq 0x27765(%rip), %rsi     # 0x311dd
movb $0x0, %al
callq 0x4450
callq 0x9650
movl $0x1, %edi
callq 0x43c0
movq -0x10(%rbp), %rax
movsd 0x8(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x9ac9
jp 0x9ac9
movq 0x4152f(%rip), %rax     # 0x4afd8
movq (%rax), %rdi
leaq 0x27748(%rip), %rsi     # 0x311fb
movb $0x0, %al
callq 0x4450
callq 0x9650
movl $0x1, %edi
callq 0x43c0
movq -0x10(%rbp), %rax
testb $0x1, 0x32(%rax)
jne 0x9adb
movq -0x10(%rbp), %rax
movb $0x0, 0x33(%rax)
movq -0x10(%rbp), %rax
testb $0x1, 0x31(%rax)
je 0x9b09
movq -0x10(%rbp), %rax
cmpl $0x1, 0x34(%rax)
jne 0x9b09
movq -0x10(%rbp), %rax
movsd 0x8(%rax), %xmm0
movsd 0x25510(%rip), %xmm1    # 0x2f010
divsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rax)
addq $0x30, %rsp
popq %rbp
retq
nop