// File: 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:
subq $0xb8, %rsp
movl $0x0, 0xb4(%rsp)
movl %edi, 0xb0(%rsp)
movq %rsi, 0xa8(%rsp)
movl $0x1, 0xa4(%rsp)
movl $0x3e8, 0xa0(%rsp) # imm = 0x3E8
movb $0x0, 0x9f(%rsp)
movb $0x0, 0x9e(%rsp)
movl $0x0, 0x98(%rsp)
movl $0x1, 0x60(%rsp)
movl 0x60(%rsp), %eax
cmpl 0xb0(%rsp), %eax
jge 0x313c
movq 0xa8(%rsp), %rax
movslq 0x60(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rdi
leaq 0x132d7(%rip), %rsi     # 0x16294
callq 0x2240
cmpl $0x0, %eax
jne 0x2fd4
movb $0x1, 0x9f(%rsp)
jmp 0x312a
movq 0x50(%rsp), %rdi
leaq 0x132b7(%rip), %rsi     # 0x16297
callq 0x2240
cmpl $0x0, %eax
jne 0x2ff7
movb $0x1, 0x9e(%rsp)
jmp 0x3128
movq 0x50(%rsp), %rdi
leaq 0x13297(%rip), %rsi     # 0x1629a
callq 0x2240
cmpl $0x0, %eax
jne 0x305c
movl 0x60(%rsp), %eax
addl $0x1, %eax
cmpl 0xb0(%rsp), %eax
jge 0x3047
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
movq 0xa8(%rsp), %rax
movslq 0x60(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x2280
movl %eax, 0x98(%rsp)
jmp 0x3057
movq 0xa8(%rsp), %rax
movq (%rax), %rdi
callq 0x2eb0
jmp 0x3126
movq 0x50(%rsp), %rdi
leaq 0x13235(%rip), %rsi     # 0x1629d
callq 0x2240
cmpl $0x0, %eax
jne 0x30be
movl 0x60(%rsp), %eax
addl $0x1, %eax
cmpl 0xb0(%rsp), %eax
jge 0x30ac
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
movq 0xa8(%rsp), %rax
movslq 0x60(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x2280
movl %eax, 0xa0(%rsp)
jmp 0x30bc
movq 0xa8(%rsp), %rax
movq (%rax), %rdi
callq 0x2eb0
jmp 0x3124
movq 0x50(%rsp), %rdi
leaq 0x131d6(%rip), %rsi     # 0x162a0
callq 0x2240
cmpl $0x0, %eax
jne 0x3120
movl 0x60(%rsp), %eax
addl $0x1, %eax
cmpl 0xb0(%rsp), %eax
jge 0x310e
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
movq 0xa8(%rsp), %rax
movslq 0x60(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x2280
movl %eax, 0xa4(%rsp)
jmp 0x311e
movq 0xa8(%rsp), %rax
movq (%rax), %rdi
callq 0x2eb0
jmp 0x3122
jmp 0x313c
jmp 0x3124
jmp 0x3126
jmp 0x3128
jmp 0x312a
jmp 0x312c
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
jmp 0x2f8a
movl 0x60(%rsp), %eax
addl $0x3, %eax
cmpl 0xb0(%rsp), %eax
je 0x315c
movq 0xa8(%rsp), %rax
movq (%rax), %rdi
callq 0x2eb0
movq 0xa8(%rsp), %rax
movl 0x60(%rsp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x60(%rsp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x90(%rsp)
movq 0xa8(%rsp), %rax
movl 0x60(%rsp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x60(%rsp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2120
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x8c(%rsp)
movq 0xa8(%rsp), %rax
movl 0x60(%rsp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x60(%rsp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x80(%rsp)
movss 0x8c(%rsp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x12e65(%rip), %xmm0    # 0x16050
ucomisd %xmm1, %xmm0
ja 0x320c
movss 0x8c(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x12e52(%rip), %xmm1    # 0x16058
ucomisd %xmm1, %xmm0
jbe 0x3231
movss 0x8c(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x13083(%rip), %rdi     # 0x162a3
movb $0x1, %al
callq 0x2040
movl $0x1, %edi
callq 0x2200
movq 0x90(%rsp), %rdi
leaq 0x1308c(%rip), %rsi     # 0x162cc
callq 0x2240
cmpl $0x0, %eax
jne 0x32b9
movl $0x13e0, %edi           # imm = 0x13E0
callq 0x21a0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x38(%rsp)
movss 0x8c(%rsp), %xmm0
movl 0xa0(%rsp), %edx
movl 0xa4(%rsp), %ecx
movzbl 0x9e(%rsp), %esi
andl $0x1, %esi
callq 0x5300
jmp 0x3288
movq 0x38(%rsp), %rax
movq %rax, 0x78(%rsp)
jmp 0x35ff
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x48(%rsp)
movl %eax, 0x44(%rsp)
movl $0x13e0, %esi           # imm = 0x13E0
callq 0x21b0
jmp 0x37d7
movq 0x90(%rsp), %rdi
leaq 0x13011(%rip), %rsi     # 0x162d9
callq 0x2240
cmpl $0x0, %eax
jne 0x333a
movl $0x13e0, %edi           # imm = 0x13E0
callq 0x21a0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x30(%rsp)
movss 0x8c(%rsp), %xmm0
movl 0xa4(%rsp), %edx
movzbl 0x9e(%rsp), %esi
andl $0x1, %esi
callq 0x5450
jmp 0x3309
movq 0x30(%rsp), %rax
movq %rax, 0x78(%rsp)
jmp 0x35fd
movq 0x30(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x48(%rsp)
movl %eax, 0x44(%rsp)
movl $0x13e0, %esi           # imm = 0x13E0
callq 0x21b0
jmp 0x37d7
movq 0x90(%rsp), %rdi
leaq 0x12f9e(%rip), %rsi     # 0x162e7
callq 0x2240
cmpl $0x0, %eax
jne 0x33b4
movl $0x13d8, %edi           # imm = 0x13D8
callq 0x21a0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x28(%rsp)
movss 0x8c(%rsp), %xmm0
movzbl 0x9e(%rsp), %esi
andl $0x1, %esi
callq 0x37f0
jmp 0x3383
movq 0x28(%rsp), %rax
movq %rax, 0x78(%rsp)
jmp 0x35fb
movq 0x28(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x48(%rsp)
movl %eax, 0x44(%rsp)
movl $0x13d8, %esi           # imm = 0x13D8
callq 0x21b0
jmp 0x37d7
movq 0x90(%rsp), %rdi
leaq 0x12f33(%rip), %rsi     # 0x162f6
callq 0x2240
cmpl $0x0, %eax
jne 0x3445
testb $0x1, 0x9e(%rsp)
jne 0x33ef
leaq 0x12f23(%rip), %rdi     # 0x16301
movb $0x0, %al
callq 0x2040
movl $0x1, %edi
callq 0x2200
movl $0x13d8, %edi           # imm = 0x13D8
callq 0x21a0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x20(%rsp)
movss 0x8c(%rsp), %xmm0
callq 0x3850
jmp 0x3414
movq 0x20(%rsp), %rax
movq %rax, 0x78(%rsp)
jmp 0x35f9
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x48(%rsp)
movl %eax, 0x44(%rsp)
movl $0x13d8, %esi           # imm = 0x13D8
callq 0x21b0
jmp 0x37d7
movq 0x90(%rsp), %rdi
leaq 0x12e9c(%rip), %rsi     # 0x162f0
callq 0x2240
cmpl $0x0, %eax
jne 0x34d6
testb $0x1, 0x9e(%rsp)
jne 0x3480
leaq 0x12e8c(%rip), %rdi     # 0x162fb
movb $0x0, %al
callq 0x2040
movl $0x1, %edi
callq 0x2200
movl $0x13d8, %edi           # imm = 0x13D8
callq 0x21a0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x18(%rsp)
movss 0x8c(%rsp), %xmm0
callq 0x38a0
jmp 0x34a5
movq 0x18(%rsp), %rax
movq %rax, 0x78(%rsp)
jmp 0x35f7
movq 0x18(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x48(%rsp)
movl %eax, 0x44(%rsp)
movl $0x13d8, %esi           # imm = 0x13D8
callq 0x21b0
jmp 0x37d7
movq 0x90(%rsp), %rdi
leaq 0x12e4b(%rip), %rsi     # 0x16330
callq 0x2240
cmpl $0x0, %eax
jne 0x3567
testb $0x1, 0x9e(%rsp)
je 0x3511
leaq 0x12e38(%rip), %rdi     # 0x16338
movb $0x0, %al
callq 0x2040
movl $0x1, %edi
callq 0x2200
movl $0x13d8, %edi           # imm = 0x13D8
callq 0x21a0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x10(%rsp)
movss 0x8c(%rsp), %xmm0
callq 0x38f0
jmp 0x3536
movq 0x10(%rsp), %rax
movq %rax, 0x78(%rsp)
jmp 0x35f5
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x48(%rsp)
movl %eax, 0x44(%rsp)
movl $0x13d8, %esi           # imm = 0x13D8
callq 0x21b0
jmp 0x37d7
movq 0x90(%rsp), %rdi
leaq 0x12df2(%rip), %rsi     # 0x16368
callq 0x2240
cmpl $0x0, %eax
jne 0x35d3
movl $0x13d8, %edi           # imm = 0x13D8
callq 0x21a0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x8(%rsp)
movss 0x8c(%rsp), %xmm0
callq 0x3930
jmp 0x35a5
movq 0x8(%rsp), %rax
movq %rax, 0x78(%rsp)
jmp 0x35f3
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x48(%rsp)
movl %eax, 0x44(%rsp)
movl $0x13d8, %esi           # imm = 0x13D8
callq 0x21b0
jmp 0x37d7
movq 0x90(%rsp), %rsi
leaq 0x12d8e(%rip), %rdi     # 0x16370
movb $0x0, %al
callq 0x2040
movl $0x1, %edi
callq 0x2200
jmp 0x35f5
jmp 0x35f7
jmp 0x35f9
jmp 0x35fb
jmp 0x35fd
jmp 0x35ff
callq 0x2e70
movsd %xmm0, 0x68(%rsp)
movq 0x78(%rsp), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
testb $0x1, 0x9f(%rsp)
je 0x3629
movq 0x78(%rsp), %rdi
callq 0x4960
movl $0x0, 0x60(%rsp)
movl 0x60(%rsp), %eax
cmpl 0x98(%rsp), %eax
jge 0x3655
movq 0x78(%rsp), %rdi
callq 0x4c30
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
jmp 0x3631
callq 0x2e70
subsd 0x68(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x64(%rsp)
movq 0x78(%rsp), %rdi
addq $0x13b8, %rdi           # imm = 0x13B8
callq 0x3970
movl %eax, 0x5c(%rsp)
movq 0x80(%rsp), %rdi
leaq 0x12d06(%rip), %rsi     # 0x16394
callq 0x21f0
movq %rax, 0x70(%rsp)
cmpq $0x0, 0x70(%rsp)
jne 0x36c0
movq 0x80(%rsp), %rsi
leaq 0x12ce8(%rip), %rdi     # 0x16397
movb $0x0, %al
callq 0x2040
movl $0x1, %edi
callq 0x2200
movq 0x70(%rsp), %rcx
leaq 0x5c(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x2320
movq 0x70(%rsp), %rcx
leaq 0x64(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x2320
movq 0x70(%rsp), %rcx
leaq 0x8c(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x2320
movq 0x70(%rsp), %rcx
leaq 0x9e(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x2320
movl $0x0, 0x60(%rsp)
movl 0x60(%rsp), %eax
cmpl 0x5c(%rsp), %eax
jge 0x3776
movq 0x78(%rsp), %rdi
addq $0x13b8, %rdi           # imm = 0x13B8
movslq 0x60(%rsp), %rsi
callq 0x3990
movq %rax, %rdi
movq 0x70(%rsp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x2320
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
jmp 0x3732
movq 0x70(%rsp), %rdi
callq 0x2190
movq 0x80(%rsp), %rsi
movl 0x5c(%rsp), %edx
movss 0x64(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
cvtsi2ssl 0x5c(%rsp), %xmm1
divss 0x64(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
leaq 0x12c0c(%rip), %rdi     # 0x163b9
movb $0x2, %al
callq 0x2040
movq 0x78(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x37cd
movq (%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
xorl %eax, %eax
addq $0xb8, %rsp
retq
movq 0x48(%rsp), %rdi
callq 0x22f0
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)