// File: /workspace/github_c_makefile_star2_O2/antonblanchard[P]will-it-scale/main.c
// Function: main
int main(int argc, char *argv[])
{
	int opt_tasks = 1;
	int opt_iterations = 0;
	int iterations = 0;
	int i, n;
	char *m;
	static unsigned long long *results[MAX_TASKS];
	hwloc_topology_t topology;
	unsigned long long prev[MAX_TASKS] = {0, };
	unsigned long long total = 0;
	int fd[2];
	bool smt_affinity = false;
	struct args *args;
	bool verbose = false;

	while (1) {
		signed char c = getopt(argc, argv, "mt:s:hvn");
		if (c < 0)
			break;

		switch (c) {
			case 'm':
				smt_affinity = true;
				break;

			case 't':
				opt_tasks = atoi(optarg);
				if (opt_tasks > MAX_TASKS) {
					printf("tasks cannot exceed %d\n",
					       MAX_TASKS);
					exit(1);
				}
				break;

			case 's':
				opt_iterations = atoi(optarg);
				break;

			case 'v':
				verbose = true;
				break;

			case 'n':
				use_affinity = false;
				break;

			default:
				usage(argv[0]);
		}
	}

	if (optind < argc)
		usage(argv[0]);

	if (smt_affinity && (use_affinity == false))
		usage(argv[0]);

	m = initialise_shared_area(opt_tasks * MAX_CACHELINE_SIZE);
	for (i = 0; i < opt_tasks; i++)
		results[i] = (unsigned long long *)&m[i * MAX_CACHELINE_SIZE];

	if (pipe(fd) == -1) {
		perror("pipe");
		exit(1);
	}

	testcase_prepare(opt_tasks);

	hwloc_topology_init(&topology);
	hwloc_topology_load(topology);

	n = hwloc_get_nbobjs_by_type(topology,
			smt_affinity ? HWLOC_OBJ_PU : HWLOC_OBJ_CORE);
	if (n == 0) {
		printf("No Cores/PUs found. Try %s -m flag\n",
		       smt_affinity ? "removing" : "adding");
		exit(1);
	}
	if (n < 1) {
		perror("hwloc_get_nbobjs_by_type");
		exit(1);
	}

	args = malloc(opt_tasks * sizeof(struct args));
	if (!args) {
		perror("malloc");
		exit(1);
	}

	for (i = 0; i < opt_tasks; i++) {
		hwloc_obj_t obj;
		hwloc_cpuset_t old_cpuset;
		int flags = 0;

		args[i].func = testcase;
		args[i].arg1 = results[i];
		args[i].arg2 = i;
		args[i].poll_fd = fd[0];

		obj = hwloc_get_obj_by_type(topology,
				smt_affinity ? HWLOC_OBJ_PU : HWLOC_OBJ_CORE,
				i % n);

		if (hwloc_topology_dup(&args[i].topology, topology)) {
			perror("hwloc_topology_dup");
			exit(1);
		}

		if (!(args[i].cpuset = hwloc_bitmap_dup(obj->cpuset))) {
			perror("hwloc_bitmap_dup");
			exit(1);
		}

		old_cpuset = hwloc_bitmap_alloc();
		if (!old_cpuset) {
			perror("hwloc_bitmap_alloc");
			exit(1);
		}
#ifdef THREADS
		flags |= HWLOC_CPUBIND_THREAD;
#endif
		if (hwloc_get_cpubind(topology, old_cpuset, flags) < 0) {
			perror("hwloc_get_cpubind");
			exit(1);
		}

		if (use_affinity && hwloc_set_cpubind(topology, obj->cpuset, flags) < 0) {
			perror("hwloc_set_cpubind");
			exit(1);
		}

		new_task_affinity(&args[i]);

		if (use_affinity && hwloc_set_cpubind(topology, old_cpuset, flags) < 0) {
			perror("hwloc_set_cpubind");
			exit(1);
		}

		hwloc_bitmap_free(old_cpuset);
	}

	if (write(fd[1], &i, 1) != 1) {
		perror("write");
		exit(1);
	}

	hwloc_topology_destroy(topology);

	printf("testcase:%s\n", testcase_description);

	printf("warmup\n");

	while (1) {
		unsigned long long sum = 0, min = -1ULL, max = 0;

		sleep(1);

		for (i = 0; i < opt_tasks; i++) {
			unsigned long long val = *(results[i]);
			unsigned long long diff = val - prev[i];

			if (verbose)
				printf("%4d -> %llu\n", i, diff);

			if (diff < min)
				min = diff;

			if (diff > max)
				max = diff;

			sum += diff;
			prev[i] = val;
		}

		printf("min:%llu max:%llu total:%llu\n", min, max, sum);

		if (iterations == WARMUP_ITERATIONS)
			printf("measurement\n");

		if (iterations++ > WARMUP_ITERATIONS)
			total += sum;

		if (opt_iterations &&
		    (iterations > (opt_iterations + WARMUP_ITERATIONS))) {
			printf("average:%llu\n", total / opt_iterations);
			break;
		}
	}

	kill_tasks();

	for (i = 0; i < opt_tasks; i++) {
		hwloc_bitmap_free(args[i].cpuset);
		hwloc_topology_destroy(args[i].topology);
	}
	free(args);

	testcase_cleanup();
	exit(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4078, %rsp           # imm = 0x4078
movq %rsi, %r14
movl %edi, %r15d
leaq 0x70(%rsp), %rdi
movl $0x0, 0x4(%rsp)
movl $0x4000, %edx           # imm = 0x4000
xorl %esi, %esi
callq 0x2130
pushq $0x1
popq %rbp
leaq 0xb02(%rip), %r12       # 0x302c
leaq 0xad3(%rip), %rbx       # 0x3004
movq 0x2aa8(%rip), %r13      # 0x4fe0
movl $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movl %r15d, %edi
movq %r14, %rsi
movq %r12, %rdx
callq 0x2230
movsbl %al, %eax
testl %eax, %eax
js 0x25af
addl $-0x6d, %eax
cmpl $0x9, %eax
ja 0x25bb
movslq (%rbx,%rax,4), %rax
addq %rbx, %rax
jmpq *%rax
movb $0x1, 0xec5a(%rip)      # 0x111d0
jmp 0x2549
movq (%r13), %rdi
callq 0x2260
movl %eax, %ebp
cmpl $0x801, %eax            # imm = 0x801
jl 0x2549
jmp 0x26f0
movb $0x1, %al
movl %eax, 0x4(%rsp)
jmp 0x2549
movq (%r13), %rdi
callq 0x2260
movq %rax, 0x20(%rsp)
jmp 0x2549
movb $0x1, %al
movl %eax, 0x18(%rsp)
jmp 0x2549
movq 0x2a22(%rip), %rax      # 0x4fd8
cmpl %r15d, (%rax)
jge 0x25c3
movq (%r14), %rdi
callq 0x2b03
movl 0x18(%rsp), %r13d
testb $0x1, %r13b
je 0x25d7
cmpb $0x1, 0xebfb(%rip)      # 0x111d0
je 0x25bb
movaps 0xc72(%rip), %xmm0      # 0x3250
leaq 0x30(%rsp), %r14
movaps %xmm0, (%r14)
movabsq $0x5858585858585f, %rax # imm = 0x5858585858585F
movq %rax, 0x10(%r14)
callq 0x2290
movl %eax, %r15d
movq %r14, %rdi
callq 0x21d0
testl %eax, %eax
jns 0x2617
leaq 0xb6c(%rip), %rdi       # 0x317c
callq 0x2220
jmp 0x267f
movl %eax, %r14d
movl %ebp, %eax
shll $0x8, %eax
cltq
movslq %r15d, %rcx
addq %rcx, %rax
decq %rax
negl %ecx
movslq %ecx, %r15
andq %rax, %r15
movl %r14d, %edi
movq %r15, %rsi
callq 0x2110
cmpl $-0x1, %eax
jne 0x264b
leaq 0xb3b(%rip), %rdi       # 0x3184
jmp 0x2670
xorl %ebx, %ebx
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
xorl %edi, %edi
movq %r15, %rsi
movl %r14d, %r8d
xorl %r9d, %r9d
callq 0x20e0
cmpq $-0x1, %rax
jne 0x2687
leaq 0xb1e(%rip), %rdi       # 0x318e
callq 0x2220
leaq 0x30(%rsp), %rdi
callq 0x2080
pushq $0x1
popq %rdi
callq 0x22a0
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x2130
leaq 0x30(%rsp), %rdi
callq 0x2080
testl %ebp, %ebp
movl $0x0, %eax
cmovgl %ebp, %eax
movq %rax, 0x10(%rsp)
leaq (,%rax,8), %rax
leaq 0xab11(%rip), %r12      # 0xd1d0
cmpq %rbx, %rax
je 0x26d5
movq %r14, (%rbx,%r12)
addq $0x8, %rbx
addq $0x100, %r14            # imm = 0x100
jmp 0x26bf
leaq 0x58(%rsp), %rdi
callq 0x2150
cmpl $-0x1, %eax
jne 0x2708
leaq 0x962(%rip), %rdi       # 0x304d
jmp 0x2610
leaq 0x93e(%rip), %rdi       # 0x3035
movl $0x800, %esi            # imm = 0x800
xorl %eax, %eax
callq 0x20f0
jmp 0x267f
movslq %ebp, %r14
movq %r14, %rdi
callq 0x2b50
leaq 0x30(%rsp), %r15
movq %r15, %rdi
callq 0x2090
movq (%r15), %rdi
callq 0x20b0
movq (%r15), %r15
movzbl %r13b, %ebx
andl $0x1, %ebx
orl $0x2, %ebx
movq %r15, %rdi
movl %ebx, %esi
callq 0x2100
cmpl $-0x2, %eax
je 0x2786
cmpl $-0x1, %eax
je 0x275b
movq %r15, %rdi
movl %eax, %esi
callq 0x21a0
movl %eax, 0x8(%rsp)
testl %eax, %eax
jne 0x2784
leaq 0x914(%rip), %rax       # 0x3076
leaq 0x916(%rip), %rsi       # 0x307f
testb $0x1, %r13b
cmovneq %rax, %rsi
leaq 0x8da(%rip), %rdi       # 0x3052
xorl %eax, %eax
callq 0x20f0
jmp 0x267f
jg 0x2792
leaq 0x8f9(%rip), %rdi       # 0x3086
jmp 0x2610
imulq $0x30, %r14, %rdi
callq 0x21b0
testq %rax, %rax
jne 0x27ac
leaq 0x8f8(%rip), %rdi       # 0x309f
jmp 0x2610
xorl %r13d, %r13d
movq %rax, 0x60(%rsp)
movq %rax, %r14
cmpq %r13, 0x10(%rsp)
je 0x28ca
leaq 0x40c(%rip), %rax       # 0x2bd5
movq %rax, (%r14)
movq (%r12), %rax
movq %rax, 0x8(%r14)
movq %r13, 0x10(%r14)
movl 0x58(%rsp), %eax
movl %eax, 0x18(%r14)
movq 0x30(%rsp), %r15
movq %r15, %rdi
movl %ebx, %esi
callq 0x2100
cmpl $-0x3, %eax
jbe 0x27f8
xorl %ebp, %ebp
jmp 0x280e
movl %eax, %esi
movl %r13d, %eax
xorl %edx, %edx
divl 0x8(%rsp)
movq %r15, %rdi
callq 0x2040
movq %rax, %rbp
leaq 0x20(%r14), %rdi
movq %r15, %rsi
callq 0x21c0
testl %eax, %eax
jne 0x2a50
movq 0xb8(%rbp), %rdi
callq 0x2170
movq %rax, 0x28(%r14)
testq %rax, %rax
je 0x2a5c
callq 0x2030
testq %rax, %rax
je 0x2a68
movq %rax, %r15
movq 0x30(%rsp), %rdi
movq %rax, %rsi
pushq $0x2
popq %rdx
callq 0x2180
testl %eax, %eax
js 0x2a74
cmpb $0x0, 0xe965(%rip)      # 0x111d0
jne 0x2889
movq 0x30(%rsp), %rdi
movq 0xb8(%rbp), %rsi
pushq $0x2
popq %rdx
callq 0x2190
testl %eax, %eax
js 0x2a80
movq %r14, %rdi
callq 0x2406
cmpb $0x0, 0xe938(%rip)      # 0x111d0
jne 0x28b2
movq 0x30(%rsp), %rdi
movq %r15, %rsi
pushq $0x2
popq %rdx
callq 0x2190
testl %eax, %eax
js 0x2a80
movq %r15, %rdi
callq 0x2060
incq %r13
addq $0x30, %r14
addq $0x8, %r12
jmp 0x27b7
leaq 0x6c(%rsp), %rsi
movq 0x10(%rsp), %rax
movl %eax, (%rsi)
movl 0x5c(%rsp), %edi
pushq $0x1
popq %rdx
callq 0x20d0
cmpq $0x1, %rax
jne 0x2a8c
movq 0x30(%rsp), %rdi
callq 0x2280
leaq 0x2883(%rip), %rax      # 0x5180
movq (%rax), %rsi
leaq 0x800(%rip), %rdi       # 0x3107
xorl %eax, %eax
callq 0x20f0
leaq 0x87e(%rip), %rdi       # 0x3193
callq 0x20a0
movq 0x20(%rsp), %rax
addl $0x5, %eax
movl %eax, 0x2c(%rsp)
movq $0x0, 0x50(%rsp)
movq $0x0, 0x8(%rsp)
pushq $0x1
popq %rdi
callq 0x22d0
leaq 0xa889(%rip), %rbx      # 0xd1d0
xorl %r14d, %r14d
xorl %ecx, %ecx
pushq $-0x1
popq %rbp
xorl %r15d, %r15d
cmpq %r14, 0x10(%rsp)
je 0x29ab
movq (%rbx), %rax
movq (%rax), %r12
movq %r12, %r13
subq 0x70(%rsp,%r14,8), %r13
testb $0x1, 0x4(%rsp)
je 0x298c
leaq 0x79f(%rip), %rdi       # 0x3114
movl %r14d, %esi
movq %r13, %rdx
xorl %eax, %eax
movq %rcx, 0x18(%rsp)
callq 0x20f0
movq 0x18(%rsp), %rcx
cmpq %rbp, %r13
cmovbq %r13, %rbp
cmpq %r15, %r13
cmovaq %r13, %r15
addq %r13, %rcx
movq %r12, 0x70(%rsp,%r14,8)
incq %r14
addq $0x8, %rbx
jmp 0x2952
leaq 0x76f(%rip), %rdi       # 0x3121
movq %rbp, %rsi
movq %r15, %rdx
movq %rcx, %r14
xorl %eax, %eax
callq 0x20f0
movq 0x8(%rsp), %rbx
cmpl $0x5, %ebx
jne 0x29d8
leaq 0x7c7(%rip), %rdi       # 0x319a
callq 0x20a0
cmpl $0x0, 0x20(%rsp)
sete %al
cmpl $0x6, %ebx
movl $0x0, %ecx
cmovbq %rcx, %r14
addq %r14, 0x50(%rsp)
cmpl 0x2c(%rsp), %ebx
leal 0x1(%rbx), %ecx
setl %dl
orb %al, %dl
movl %ecx, %eax
movq %rax, 0x8(%rsp)
jne 0x2938
movslq 0x20(%rsp), %rcx
xorl %ebx, %ebx
movq 0x50(%rsp), %rax
xorl %edx, %edx
divq %rcx
leaq 0x71d(%rip), %rdi       # 0x313f
movq %rax, %rsi
xorl %eax, %eax
callq 0x20f0
leaq 0x278d(%rip), %r14      # 0x51c0
movslq 0xa786(%rip), %rax      # 0xd1c0
cmpq %rax, %rbx
jge 0x2a98
movq (%r14), %rdi
callq 0x22c0
incq %rbx
addq $0x8, %r14
jmp 0x2a33
leaq 0x64f(%rip), %rdi       # 0x30a6
jmp 0x2610
leaq 0x656(%rip), %rdi       # 0x30b9
jmp 0x2610
leaq 0x65b(%rip), %rdi       # 0x30ca
jmp 0x2610
leaq 0x662(%rip), %rdi       # 0x30dd
jmp 0x2610
leaq 0x668(%rip), %rdi       # 0x30ef
jmp 0x2610
leaq 0x66e(%rip), %rdi       # 0x3101
jmp 0x2610
leaq 0x2721(%rip), %rbx      # 0x51c0
xorl %r14d, %r14d
cltq
cmpq %rax, %r14
jge 0x2ac2
movq (%rbx), %rdi
xorl %esi, %esi
callq 0x22b0
incq %r14
movl 0xa704(%rip), %eax      # 0xd1c0
addq $0x8, %rbx
jmp 0x2aa2
imulq $0x30, 0x10(%rsp), %rbx
xorl %r14d, %r14d
movq 0x60(%rsp), %r15
cmpq %r14, %rbx
je 0x2aef
movq 0x28(%r15,%r14), %rdi
callq 0x2060
movq 0x20(%r15,%r14), %rdi
callq 0x2280
addq $0x30, %r14
jmp 0x2ad0
movq %r15, %rdi
callq 0x2050
callq 0x2c50
xorl %edi, %edi
callq 0x22a0