// File: /workspace/github_cpp_makefile_star2_O2/ddunbar[P]PDSample/PDSample.cpp
// Function: main
int main(int argc, char **argv)
{
	int multiplier = 1, minMaxThrows = 1000;
	bool maximize = false;
	bool isTiled = false;
	int relax = 0;
	char *method;
	float radius;
	char *outputPath;
	PDSampler *sampler;
	FILE *output;
	double startTime;
	float elapTime;
	int i, N;

	for (i=1; i<argc; i++) {
		char *arg = argv[i];

		if (strcmp(arg,"-m")==0) {
			maximize = true;
		} else if (strcmp(arg,"-t")==0) {
			isTiled = true;
		} else if (strcmp(arg,"-r")==0) {
			if (i+1<argc) {
				i++;
				relax = atoi(argv[i]);
			} else {
				usage(argv[0]);
			}
		} else if (strcmp(arg,"-N")==0) {
			if (i+1<argc) {
				i++;
				minMaxThrows = atoi(argv[i]);
			} else {
				usage(argv[0]);
			}
		} else if (strcmp(arg,"-M")==0) {
			if (i+1<argc) {
				i++;
				multiplier = atoi(argv[i]);
			} else {
				usage(argv[0]);
			}
		} else {
			break;
		}
	}

	if (i+3!=argc) usage(argv[0]);
	
	method = argv[i++];
	radius = (float) strtod(argv[i++],NULL);
	outputPath = argv[i++];

	if (radius<0.0005 || radius>.2) {
		printf("Radius (%f) is outside allowable range.\n", radius);
		exit(1);
	}

	if (!strcmp(method, "DartThrowing")) {
		sampler = new DartThrowing(radius, isTiled, minMaxThrows, multiplier);
	} else if (!strcmp(method, "BestCandidate")) {
		sampler = new BestCandidate(radius, isTiled, multiplier);
	} else if (!strcmp(method, "Boundary")) {
		sampler = new BoundarySampler(radius, isTiled);
	} else if (!strcmp(method, "Pure")) {
		if (!isTiled) {
			printf("Pure sampler does not support untiled domain.\n");
			exit(1);
		}
		sampler = new PureSampler(radius);
	} else if (!strcmp(method, "LinearPure")) {
		if (!isTiled) {
			printf("LinearPure sampler does not support untiled domain.\n");
			exit(1);
		}
		sampler = new LinearPureSampler(radius);
	} else if (!strcmp(method, "Penrose")) {
		if (isTiled) {
			printf("Penrose sampler does not support tiled domain.\n");
			exit(1);
		}
		sampler = new PenroseSampler(radius);
	} else if (!strcmp(method, "Uniform")) {
		sampler = new UniformSampler(radius);
	} else {
		printf("Unrecognized sampling method (%s).\n", method);
		exit(1);
	}

	startTime = timeInSeconds();
	sampler->complete();
	if (maximize) sampler->maximize();
	for (i=0; i<relax; i++) sampler->relax();
	elapTime = (float) (timeInSeconds() - startTime);

	N = (int) sampler->points.size();
	output = fopen(outputPath,"wb");
	if (!output) {
		printf("Unable to open output file (%s).\n", outputPath);
		exit(1);
	} 

	fwrite(&N, 4, 1, output);
	fwrite(&elapTime, 4, 1, output);
	fwrite(&radius, 4, 1, output);
	fwrite(&isTiled, 4, 1, output);
	for (i=0; i<N; i++) {
		fwrite(&sampler->points[i], 8, 1, output);
	}
	fclose(output);
	printf("Wrote: %s (%d points, %fs, %6.1f pts/s)\n", outputPath, N, elapTime, N/elapTime);

	delete sampler;

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r14
movl %edi, %r15d
pushq $0x1
popq %rbx
movl $0x3e8, 0x28(%rsp)      # imm = 0x3E8
xorl %r13d, %r13d
movl $0x0, 0x14(%rsp)
movl $0x0, 0x18(%rsp)
movl %ebx, %ebp
movslq %ebp, %r12
cmpl %r15d, %ebp
jge 0x293e
movq (%r14,%r12,8), %rax
cmpb $0x2d, (%rax)
jne 0x293e
cmpb $0x6d, 0x1(%rax)
jne 0x28a7
cmpb $0x0, 0x2(%rax)
je 0x28f2
cmpb $0x74, 0x1(%rax)
jne 0x28b3
cmpb $0x0, 0x2(%rax)
je 0x28fa
cmpb $0x72, 0x1(%rax)
jne 0x28bf
cmpb $0x0, 0x2(%rax)
je 0x28ff
cmpb $0x4e, 0x1(%rax)
jne 0x28cb
cmpb $0x0, 0x2(%rax)
je 0x291c
cmpb $0x4d, 0x1(%rax)
jne 0x293e
cmpb $0x0, 0x2(%rax)
jne 0x293e
incl %ebp
cmpl %r15d, %ebp
jge 0x2cb3
movslq %ebp, %rax
movq (%r14,%rax,8), %rdi
callq 0x2290
movl %eax, %ebx
jmp 0x2937
movb $0x1, %al
movl %eax, 0x18(%rsp)
jmp 0x2937
movb $0x1, %r13b
jmp 0x2937
incl %ebp
cmpl %r15d, %ebp
jge 0x2cb3
movslq %ebp, %rax
movq (%r14,%rax,8), %rdi
callq 0x2290
movl %eax, 0x14(%rsp)
jmp 0x2937
incl %ebp
cmpl %r15d, %ebp
jge 0x2cb3
movslq %ebp, %rax
movq (%r14,%rax,8), %rdi
callq 0x2290
movl %eax, 0x28(%rsp)
incl %ebp
jmp 0x2882
movb %r13b, 0x13(%rsp)
addl $0x3, %ebp
cmpl %r15d, %ebp
jne 0x2cb3
movq (%r14,%r12,8), %r15
movq 0x8(%r14,%r12,8), %rdi
xorl %esi, %esi
callq 0x2120
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x34(%rsp)
movss %xmm0, 0xc(%rsp)
cvtss2sd %xmm0, %xmm0
movsd 0x66ed(%rip), %xmm1     # 0x9068
ucomisd %xmm0, %xmm1
ja 0x2cbb
ucomisd 0x66e3(%rip), %xmm0     # 0x9070
ja 0x2cbb
movq 0x10(%r14,%r12,8), %r14
leaq 0x6775(%rip), %rsi      # 0x9114
movq %r15, %rdi
callq 0x2240
testl %eax, %eax
je 0x2a59
leaq 0x676b(%rip), %rsi      # 0x9121
movq %r15, %rdi
callq 0x2240
testl %eax, %eax
je 0x2a86
leaq 0x6762(%rip), %rsi      # 0x912f
movq %r15, %rdi
callq 0x2240
testl %eax, %eax
je 0x2aaf
leaq 0x675a(%rip), %rsi      # 0x913e
movq %r15, %rdi
callq 0x2240
testl %eax, %eax
je 0x2ad3
leaq 0x673d(%rip), %rsi      # 0x9138
movq %r15, %rdi
callq 0x2240
testl %eax, %eax
je 0x2afa
leaq 0x6731(%rip), %rsi      # 0x9143
movq %r15, %rdi
callq 0x2240
testl %eax, %eax
je 0x2b21
leaq 0x6722(%rip), %rsi      # 0x914b
movq %r15, %rdi
callq 0x2240
testl %eax, %eax
jne 0x2cef
movl $0x13d8, %edi           # imm = 0x13D8
callq 0x2190
movq %rax, %r15
movq %rax, %rdi
movss 0xc(%rsp), %xmm0
callq 0x2db4
jmp 0x2b46
movl $0x13e0, %edi           # imm = 0x13E0
callq 0x2190
movq %rax, %r15
movzbl %r13b, %esi
andl $0x1, %esi
movq %rax, %rdi
movss 0xc(%rsp), %xmm0
movl 0x28(%rsp), %edx
movl %ebx, %ecx
callq 0x3ce6
jmp 0x2b46
movl $0x13e0, %edi           # imm = 0x13E0
callq 0x2190
movq %rax, %r15
movzbl %r13b, %esi
andl $0x1, %esi
movq %rax, %rdi
movss 0xc(%rsp), %xmm0
movl %ebx, %edx
callq 0x3dc6
jmp 0x2b46
movl $0x13d8, %edi           # imm = 0x13D8
callq 0x2190
movq %rax, %r15
movzbl %r13b, %esi
andl $0x1, %esi
movq %rax, %rdi
movss 0xc(%rsp), %xmm0
callq 0x2d3e
jmp 0x2b46
testb $0x1, %r13b
je 0x2ce6
movl $0x13d8, %edi           # imm = 0x13D8
callq 0x2190
movq %rax, %r15
movq %rax, %rdi
movss 0xc(%rsp), %xmm0
callq 0x2d5a
jmp 0x2b46
testb $0x1, %r13b
je 0x2cfb
movl $0x13d8, %edi           # imm = 0x13D8
callq 0x2190
movq %rax, %r15
movq %rax, %rdi
movss 0xc(%rsp), %xmm0
callq 0x2d78
jmp 0x2b46
testb $0x1, %r13b
jne 0x2d04
movl $0x13d8, %edi           # imm = 0x13D8
callq 0x2190
movq %rax, %r15
movq %rax, %rdi
movss 0xc(%rsp), %xmm0
callq 0x2d96
callq 0x27b8
movsd %xmm0, 0x28(%rsp)
movq (%r15), %rax
movq %r15, %rdi
callq *0x10(%rax)
testb $0x1, 0x18(%rsp)
je 0x2b69
movq %r15, %rdi
callq 0x3788
xorl %ebx, %ebx
movl 0x14(%rsp), %eax
testl %eax, %eax
cmovgl %eax, %ebx
subl $0x1, %ebx
jb 0x2b83
movq %r15, %rdi
callq 0x3946
jmp 0x2b74
callq 0x27b8
subsd 0x28(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x24(%rsp)
movq 0x13c0(%r15), %rax
subq 0x13b8(%r15), %rax
shrq $0x3, %rax
movl %eax, 0x30(%rsp)
leaq 0x65c2(%rip), %rsi      # 0x9177
movq %r14, 0x18(%rsp)
movq %r14, %rdi
callq 0x21e0
testq %rax, %rax
je 0x2ccb
movq %rax, %rbx
leaq 0x30(%rsp), %r13
pushq $0x4
popq %rbp
pushq $0x1
popq %r12
movq %r13, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %rax, %rcx
callq 0x2330
leaq 0x24(%rsp), %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x2330
leaq 0x34(%rsp), %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x2330
leaq 0x13(%rsp), %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x2330
movl (%r13), %eax
xorl %r14d, %r14d
testl %eax, %eax
movl $0x0, %ebp
movl %eax, 0x14(%rsp)
cmovgl %eax, %ebp
shlq $0x3, %rbp
pushq $0x8
popq %r13
cmpq %r14, %rbp
je 0x2c64
movq 0x13b8(%r15), %rdi
addq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x2330
addq $0x8, %r14
jmp 0x2c41
movq %rbx, %rdi
callq 0x2180
movl 0x14(%rsp), %edx
cvtsi2ss %edx, %xmm1
movss 0x24(%rsp), %xmm2
cvtss2sd %xmm2, %xmm0
divss %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
leaq 0x650f(%rip), %rdi      # 0x919c
movq 0x18(%rsp), %rsi
movb $0x2, %al
callq 0x2040
movq (%r15), %rax
movq %r15, %rdi
callq *0x8(%rax)
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r14), %rdi
callq 0x27ec
leaq 0x6429(%rip), %rdi      # 0x90eb
movb $0x1, %al
callq 0x2040
jmp 0x2cde
leaq 0x64a8(%rip), %rdi      # 0x917a
movq 0x18(%rsp), %rsi
xorl %eax, %eax
callq 0x2040
pushq $0x1
popq %rdi
callq 0x21f0
leaq 0x6699(%rip), %rdi      # 0x9386
jmp 0x2d0b
leaq 0x645d(%rip), %rdi      # 0x9153
movq %r15, %rsi
jmp 0x2cd7
leaq 0x667e(%rip), %rdi      # 0x9380
jmp 0x2d0b
leaq 0x66a9(%rip), %rdi      # 0x93b4
callq 0x2280
jmp 0x2cde
jmp 0x2d1a
jmp 0x2d1a
jmp 0x2d1a
jmp 0x2d1a
movq %rax, %rbx
movl $0x13d8, %esi           # imm = 0x13D8
jmp 0x2d2e
jmp 0x2d26
movq %rax, %rbx
movl $0x13e0, %esi           # imm = 0x13E0
movq %r15, %rdi
callq 0x21a0
movq %rbx, %rdi
callq 0x2300