// File: /workspace/github_c_makefile_star2_O3/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 $0x4088, %rsp           # imm = 0x4088
movq %rsi, %r15
movl %edi, %ebp
leaq 0x80(%rsp), %rdi
movq $0x0, 0x18(%rsp)
movl $0x4000, %edx           # imm = 0x4000
xorl %esi, %esi
callq 0x2130
leaq 0xb33(%rip), %rdx       # 0x3068
movl %ebp, %edi
movq %r15, %rsi
callq 0x2230
movsbl %al, %eax
testl %eax, %eax
jns 0x2573
movq 0x2a8b(%rip), %rax      # 0x4fd8
cmpl %ebp, (%rax)
jl 0x260c
movl $0x1, %ebx
movl $0x0, 0x14(%rsp)
xorl %r13d, %r13d
movq $0x0, 0x18(%rsp)
jmp 0x262c
movl $0x1, %ebx
leaq 0xac1(%rip), %r14       # 0x3040
leaq 0xae2(%rip), %r12       # 0x3068
movq 0x2a53(%rip), %r13      # 0x4fe0
movl $0x0, 0x8(%rsp)
movl $0x0, 0x14(%rsp)
addl $-0x6d, %eax
cmpl $0x9, %eax
ja 0x260c
movslq (%r14,%rax,4), %rax
addq %r14, %rax
jmpq *%rax
movb $0x1, %al
movl %eax, 0x8(%rsp)
jmp 0x25ed
movb $0x1, %al
movl %eax, 0x14(%rsp)
jmp 0x25ed
movq (%r13), %rdi
callq 0x2260
movq %rax, 0x18(%rsp)
jmp 0x25ed
movq (%r13), %rdi
callq 0x2260
movq %rax, %rbx
cmpl $0x801, %eax            # imm = 0x801
jl 0x25ed
jmp 0x26d8
movb $0x1, 0xebe3(%rip)      # 0x111d0
movl %ebp, %edi
movq %r15, %rsi
movq %r12, %rdx
callq 0x2230
movsbl %al, %eax
testl %eax, %eax
jns 0x259d
movq 0x29d0(%rip), %rax      # 0x4fd8
cmpl %ebp, (%rax)
jge 0x2614
movq (%r15), %rdi
callq 0x2be1
testb $0x1, 0x8(%rsp)
je 0x2629
movb $0x1, %r13b
cmpb $0x1, 0xebab(%rip)      # 0x111d0
jne 0x262c
jmp 0x260c
xorl %r13d, %r13d
movdqa 0xc5c(%rip), %xmm0      # 0x3290
leaq 0x40(%rsp), %r15
movdqa %xmm0, (%r15)
movabsq $0x5858585858585f, %rax # imm = 0x5858585858585F
movq %rax, 0x10(%r15)
callq 0x2290
movl %eax, %r14d
movq %r15, %rdi
callq 0x21d0
testl %eax, %eax
jns 0x266e
leaq 0xb51(%rip), %rdi       # 0x31b8
callq 0x2220
jmp 0x26eb
movl %eax, %ebp
movl %ebx, %eax
shll $0x8, %eax
cltq
movslq %r14d, %rcx
addq %rcx, %rax
decq %rax
negl %ecx
movslq %ecx, %r12
andq %rax, %r12
movl %ebp, %edi
movq %r12, %rsi
callq 0x2110
cmpl $-0x1, %eax
jne 0x26a0
leaq 0xb22(%rip), %rdi       # 0x31c0
jmp 0x26c7
xorl %edi, %edi
movq %r12, %rsi
movl $0x3, %edx
movl $0x1, %ecx
movl %ebp, %r8d
xorl %r9d, %r9d
callq 0x20e0
cmpq $-0x1, %rax
jne 0x26f5
leaq 0xb03(%rip), %rdi       # 0x31ca
callq 0x2220
leaq 0x40(%rsp), %rdi
callq 0x2080
jmp 0x26eb
leaq 0x992(%rip), %rdi       # 0x3071
movl $0x800, %esi            # imm = 0x800
xorl %eax, %eax
callq 0x20f0
movl $0x1, %edi
callq 0x22a0
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x2130
leaq 0x40(%rsp), %rdi
callq 0x2080
testl %ebx, %ebx
jle 0x27cd
movl %ebx, %ecx
leal 0x1(%rbx), %eax
decq %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
shrl %eax
shlq $0x4, %rax
movdqa 0x8d9(%rip), %xmm1      # 0x3010
xorl %ecx, %ecx
movdqa 0x8df(%rip), %xmm2      # 0x3020
pxor %xmm2, %xmm0
leaq 0xaa84(%rip), %rdx      # 0xd1d0
pcmpeqd %xmm3, %xmm3
movdqa 0x8d8(%rip), %xmm4      # 0x3030
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm5     # xmm5 = xmm5[1,1,3,3]
pand %xmm6, %xmm5
pshufd $0xf5, %xmm6, %xmm6     # xmm6 = xmm6[1,1,3,3]
por %xmm5, %xmm6
movdqa %xmm1, %xmm5
psllq $0x8, %xmm5
movd %xmm6, %esi
notl %esi
testb $0x1, %sil
je 0x279f
movq %xmm5, %rsi
addq %r15, %rsi
movq %rsi, (%rcx,%rdx)
pxor %xmm3, %xmm6
pextrw $0x4, %xmm6, %esi
testb $0x1, %sil
je 0x27c0
pshufd $0xee, %xmm5, %xmm5     # xmm5 = xmm5[2,3,2,3]
movq %xmm5, %rsi
addq %r15, %rsi
movq %rsi, 0x8(%rcx,%rdx)
paddq %xmm4, %xmm1
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x2758
leaq 0x60(%rsp), %rdi
callq 0x2150
cmpl $-0x1, %eax
jne 0x27e8
leaq 0x8a6(%rip), %rdi       # 0x3089
jmp 0x2667
movslq %ebx, %r15
movq %r15, %rdi
callq 0x2c94
leaq 0x40(%rsp), %r14
movq %r14, %rdi
callq 0x2090
movq (%r14), %rdi
callq 0x20b0
movq (%r14), %r14
movzbl %r13b, %esi
orl $0x2, %esi
movq %r14, %rdi
movl %esi, 0x8(%rsp)
callq 0x2100
cmpl $-0x2, %eax
je 0x2864
cmpl $-0x1, %eax
je 0x283a
movq %r14, %rdi
movl %eax, %esi
callq 0x21a0
movl %eax, 0x28(%rsp)
testl %eax, %eax
jne 0x2862
leaq 0x871(%rip), %rax       # 0x30b2
leaq 0x873(%rip), %rsi       # 0x30bb
testb %r13b, %r13b
cmovneq %rax, %rsi
leaq 0x838(%rip), %rdi       # 0x308e
xorl %eax, %eax
callq 0x20f0
jmp 0x26eb
jg 0x2870
leaq 0x857(%rip), %rdi       # 0x30c2
jmp 0x2667
shlq $0x4, %r15
leaq (%r15,%r15,2), %rdi
callq 0x21b0
testq %rax, %rax
jne 0x288e
leaq 0x852(%rip), %rdi       # 0x30db
jmp 0x2667
movq %rax, %r15
xorl %eax, %eax
testl %ebx, %ebx
jle 0x29cd
movl %ebx, %eax
movq %rax, 0x30(%rsp)
leaq 0xa927(%rip), %r14      # 0xd1d0
xorl %r13d, %r13d
movq %r15, 0x20(%rsp)
leaq 0x469(%rip), %rax       # 0x2d21
movq %rax, (%r15)
movq (%r14), %rax
movq %rax, 0x8(%r15)
movq %r13, 0x10(%r15)
movl 0x60(%rsp), %eax
movl %eax, 0x18(%r15)
movq 0x40(%rsp), %r12
movq %r12, %rdi
movl 0x8(%rsp), %esi
callq 0x2100
cmpl $-0x3, %eax
jbe 0x28e8
xorl %ebp, %ebp
jmp 0x28fe
movl %eax, %esi
movl %r13d, %eax
xorl %edx, %edx
divl 0x28(%rsp)
movq %r12, %rdi
callq 0x2040
movq %rax, %rbp
leaq 0x20(%r15), %rdi
movq %r12, %rsi
callq 0x21c0
testl %eax, %eax
jne 0x2ba5
movq 0xb8(%rbp), %rdi
callq 0x2170
movq %rax, 0x28(%r15)
testq %rax, %rax
je 0x2bb1
callq 0x2030
testq %rax, %rax
je 0x2bbd
movq %rax, %r12
movq 0x40(%rsp), %rdi
movq %rax, %rsi
movl $0x2, %edx
callq 0x2180
testl %eax, %eax
js 0x2bc9
cmpb $0x0, 0xe873(%rip)      # 0x111d0
jne 0x297d
movq 0x40(%rsp), %rdi
movq 0xb8(%rbp), %rsi
movl $0x2, %edx
callq 0x2190
testl %eax, %eax
js 0x2bd5
movq %r15, %rdi
callq 0x2426
cmpb $0x0, 0xe844(%rip)      # 0x111d0
jne 0x29a8
movq 0x40(%rsp), %rdi
movq %r12, %rsi
movl $0x2, %edx
callq 0x2190
testl %eax, %eax
js 0x2bd5
movq %r12, %rdi
callq 0x2060
incq %r13
addq $0x30, %r15
addq $0x8, %r14
cmpq %r13, 0x30(%rsp)
jne 0x28b1
movl %ebx, %eax
movq 0x20(%rsp), %r15
leaq 0x7c(%rsp), %rsi
movl %eax, (%rsi)
movl 0x64(%rsp), %edi
movl $0x1, %edx
callq 0x20d0
cmpq $0x1, %rax
jne 0x2b99
movq %r15, 0x20(%rsp)
movq 0x40(%rsp), %rdi
callq 0x2280
leaq 0x277e(%rip), %rax      # 0x5180
movq (%rax), %rsi
leaq 0x737(%rip), %rdi       # 0x3143
xorl %eax, %eax
callq 0x20f0
leaq 0x7b5(%rip), %rdi       # 0x31cf
callq 0x20a0
movq 0x18(%rsp), %rax
addl $0x5, %eax
movl %eax, 0x3c(%rsp)
movl %ebx, %eax
movq %rax, 0x8(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
movq %rbx, 0x68(%rsp)
movq %rcx, 0x70(%rsp)
movq %rax, 0x28(%rsp)
movl $0x1, %edi
callq 0x22d0
movq $-0x1, %rbp
testl %ebx, %ebx
jle 0x2ac8
leaq 0xa76f(%rip), %rbx      # 0xd1d0
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %ecx, %ecx
movq (%rbx), %rax
movq (%rax), %r12
movq %r12, %r14
subq 0x80(%rsp,%r15,8), %r14
testb $0x1, 0x14(%rsp)
je 0x2a9f
leaq 0x6c8(%rip), %rdi       # 0x3150
movl %r15d, %esi
movq %r14, %rdx
xorl %eax, %eax
movq %rcx, 0x30(%rsp)
callq 0x20f0
movq 0x30(%rsp), %rcx
cmpq %rbp, %r14
cmovbq %r14, %rbp
cmpq %r13, %r14
cmovaq %r14, %r13
addq %r14, %rcx
movq %r12, 0x80(%rsp,%r15,8)
incq %r15
addq $0x8, %rbx
cmpq %r15, 0x8(%rsp)
jne 0x2a69
jmp 0x2acd
xorl %ecx, %ecx
xorl %r13d, %r13d
leaq 0x689(%rip), %rdi       # 0x315d
movq %rbp, %rsi
movq %r13, %rdx
movq %rcx, %r14
xorl %eax, %eax
callq 0x20f0
movq 0x70(%rsp), %rbx
cmpl $0x5, %ebx
jne 0x2afa
leaq 0x6e1(%rip), %rdi       # 0x31d6
callq 0x20a0
cmpl $0x0, 0x18(%rsp)
sete %sil
cmpl $0x6, %ebx
movl $0x0, %ecx
cmovbq %rcx, %r14
movq 0x28(%rsp), %rax
addq %r14, %rax
cmpl 0x3c(%rsp), %ebx
leal 0x1(%rbx), %ecx
setl %dl
orb %sil, %dl
movq 0x68(%rsp), %rbx
jne 0x2a3b
movslq 0x18(%rsp), %rcx
xorl %edx, %edx
divq %rcx
leaq 0x63b(%rip), %rdi       # 0x317b
movq %rax, %rsi
xorl %eax, %eax
callq 0x20f0
callq 0x2c2d
testl %ebx, %ebx
movq 0x20(%rsp), %r15
jle 0x2b85
movq 0x8(%rsp), %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rbx
xorl %r14d, %r14d
movq 0x28(%r15,%r14), %rdi
callq 0x2060
movq 0x20(%r15,%r14), %rdi
callq 0x2280
addq $0x30, %r14
cmpq %r14, %rbx
jne 0x2b68
movq %r15, %rdi
callq 0x2050
callq 0x2dbb
xorl %edi, %edi
callq 0x22a0
leaq 0x59d(%rip), %rdi       # 0x313d
jmp 0x2667
leaq 0x536(%rip), %rdi       # 0x30e2
jmp 0x2667
leaq 0x53d(%rip), %rdi       # 0x30f5
jmp 0x2667
leaq 0x542(%rip), %rdi       # 0x3106
jmp 0x2667
leaq 0x549(%rip), %rdi       # 0x3119
jmp 0x2667
leaq 0x54f(%rip), %rdi       # 0x312b
jmp 0x2667