// File: /workspace/github_c_makefile_star2_O1/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, %r15
movl %edi, %ebp
leaq 0x70(%rsp), %rdi
movl $0x0, 0x1c(%rsp)
movl $0x4000, %edx           # imm = 0x4000
xorl %esi, %esi
callq 0x2130
movl $0x1, 0x4(%rsp)
leaq 0xaf3(%rip), %r12       # 0x302c
leaq 0xac4(%rip), %r14       # 0x3004
movq 0x2a99(%rip), %r13      # 0x4fe0
movl $0x0, 0x8(%rsp)
movq $0x0, 0x20(%rsp)
movl %ebp, %edi
movq %r15, %rsi
movq %r12, %rdx
callq 0x2230
movsbl %al, %ebx
testl %ebx, %ebx
js 0x25bd
leal -0x6d(%rbx), %eax
cmpl $0x9, %eax
ja 0x25cc
movslq (%r14,%rax,4), %rax
addq %r14, %rax
jmpq *%rax
movb $0x1, %al
movl %eax, 0x8(%rsp)
jmp 0x25bd
movb $0x1, %al
movl %eax, 0x1c(%rsp)
jmp 0x25bd
movq (%r13), %rdi
callq 0x2260
movq %rax, 0x20(%rsp)
jmp 0x25bd
movq (%r13), %rdi
callq 0x2260
movl %eax, 0x4(%rsp)
cmpl $0x801, %eax            # imm = 0x801
jl 0x25bd
jmp 0x265f
movb $0x1, 0xec13(%rip)      # 0x111d0
testl %ebx, %ebx
jns 0x2558
movq 0x2a10(%rip), %rax      # 0x4fd8
cmpl %ebp, (%rax)
jge 0x25d4
movq (%r15), %rdi
callq 0x2b46
testb $0x1, 0x8(%rsp)
je 0x25e4
cmpb $0x1, 0xebee(%rip)      # 0x111d0
je 0x25cc
movaps 0xc65(%rip), %xmm0      # 0x3250
leaq 0x30(%rsp), %r15
movaps %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 0x2627
leaq 0xb5f(%rip), %rdi       # 0x317c
callq 0x2220
jmp 0x26ac
movl %eax, %ebp
movl 0x4(%rsp), %r13d
movl %r13d, %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 0x2674
leaq 0xb27(%rip), %rdi       # 0x3184
jmp 0x269d
leaq 0x9cf(%rip), %rdi       # 0x3035
movl $0x800, %esi            # imm = 0x800
xorl %eax, %eax
callq 0x20f0
jmp 0x26ac
xorl %ebx, %ebx
xorl %edi, %edi
movq %r12, %rsi
movl $0x3, %edx
movl $0x1, %ecx
movl %ebp, %r8d
xorl %r9d, %r9d
callq 0x20e0
cmpq $-0x1, %rax
jne 0x26b6
leaq 0xaf1(%rip), %rdi       # 0x318e
callq 0x2220
leaq 0x30(%rsp), %rdi
callq 0x2080
movl $0x1, %edi
callq 0x22a0
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x2130
leaq 0x30(%rsp), %rdi
callq 0x2080
testl %r13d, %r13d
jle 0x26f7
movl %r13d, %eax
leaq 0xaaf1(%rip), %rcx      # 0xd1d0
xorl %ebx, %ebx
movq %r15, (%rcx)
incq %rbx
addq $0x100, %r15            # imm = 0x100
addq $0x8, %rcx
cmpq %rbx, %rax
jne 0x26e1
movl %ebx, (%rsp)
leaq 0x58(%rsp), %rdi
callq 0x2150
cmpl $-0x1, %eax
jne 0x2715
leaq 0x93d(%rip), %rdi       # 0x304d
jmp 0x261d
movslq %r13d, %r15
movq %r15, %rdi
callq 0x2bfc
leaq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x2090
movq (%r14), %rdi
callq 0x20b0
movq (%r14), %r14
movzbl 0x8(%rsp), %esi
andl $0x1, %esi
orl $0x2, %esi
movq %r14, %rdi
movl %esi, %ebx
callq 0x2100
cmpl $-0x1, %eax
je 0x275e
cmpl $-0x2, %eax
jne 0x2762
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x276e
xorl %ebp, %ebp
jmp 0x276e
movq %r14, %rdi
movl %eax, %esi
callq 0x21a0
movl %eax, %ebp
testl %ebp, %ebp
jne 0x279c
leaq 0x8fd(%rip), %rax       # 0x3076
leaq 0x8ff(%rip), %rsi       # 0x307f
testb $0x1, 0x8(%rsp)
cmovneq %rax, %rsi
leaq 0x8c2(%rip), %rdi       # 0x3052
xorl %eax, %eax
callq 0x20f0
jmp 0x26ac
jg 0x27aa
leaq 0x8e1(%rip), %rdi       # 0x3086
jmp 0x261d
shlq $0x4, %r15
leaq (%r15,%r15,2), %rdi
callq 0x21b0
testq %rax, %rax
jne 0x27c8
leaq 0x8dc(%rip), %rdi       # 0x309f
jmp 0x261d
movq %rax, %r15
movl %r13d, %eax
xorl %r13d, %r13d
testl %eax, %eax
movq %r15, 0x50(%rsp)
jle 0x2906
movl %ebp, 0x10(%rsp)
movl %eax, %eax
movq %rax, 0x8(%rsp)
leaq 0xa9e0(%rip), %r14      # 0xd1d0
xorl %r13d, %r13d
leaq 0x48f(%rip), %rax       # 0x2c89
movq %rax, (%r15)
movq (%r14), %rax
movq %rax, 0x8(%r15)
movq %r13, 0x10(%r15)
movl 0x58(%rsp), %eax
movl %eax, 0x18(%r15)
movq 0x30(%rsp), %r12
movq %r12, %rdi
movl %ebx, %esi
callq 0x2100
cmpl $-0x3, %eax
jbe 0x2828
xorl %ebp, %ebp
jmp 0x283e
movl %eax, %esi
movl %r13d, %eax
xorl %edx, %edx
divl 0x10(%rsp)
movq %r12, %rdi
callq 0x2040
movq %rax, %rbp
leaq 0x20(%r15), %rdi
movq %r12, %rsi
callq 0x21c0
testl %eax, %eax
jne 0x2af6
movq 0xb8(%rbp), %rdi
callq 0x2170
movq %rax, 0x28(%r15)
testq %rax, %rax
je 0x2b06
callq 0x2030
testq %rax, %rax
je 0x2b16
movq %rax, %r12
movq 0x30(%rsp), %rdi
movq %rax, %rsi
movl $0x2, %edx
callq 0x2180
testl %eax, %eax
js 0x2b26
cmpb $0x0, 0xe933(%rip)      # 0x111d0
jne 0x28bd
movq 0x30(%rsp), %rdi
movq 0xb8(%rbp), %rsi
movl $0x2, %edx
callq 0x2190
testl %eax, %eax
js 0x2b36
movq %r15, %rdi
callq 0x2426
cmpb $0x0, 0xe904(%rip)      # 0x111d0
jne 0x28e8
movq 0x30(%rsp), %rdi
movq %r12, %rsi
movl $0x2, %edx
callq 0x2190
testl %eax, %eax
js 0x2b36
movq %r12, %rdi
callq 0x2060
incq %r13
addq $0x30, %r15
addq $0x8, %r14
cmpq %r13, 0x8(%rsp)
jne 0x27f3
movq %rsp, %rsi
movl %r13d, (%rsi)
movl 0x5c(%rsp), %edi
movl $0x1, %edx
callq 0x20d0
cmpq $0x1, %rax
jne 0x2aea
movq 0x30(%rsp), %rdi
callq 0x2280
leaq 0x284b(%rip), %rax      # 0x5180
movq (%rax), %rsi
leaq 0x7c8(%rip), %rdi       # 0x3107
xorl %eax, %eax
callq 0x20f0
leaq 0x846(%rip), %rdi       # 0x3193
callq 0x20a0
movq 0x20(%rsp), %rax
addl $0x5, %eax
movl %eax, 0x2c(%rsp)
movl 0x4(%rsp), %r14d
movl %r14d, %eax
movq %rax, 0x10(%rsp)
xorl %r13d, %r13d
xorl %ebx, %ebx
movl $0x1, %edi
callq 0x22d0
testl %r14d, %r14d
jle 0x2a12
movq %rbx, 0x60(%rsp)
movq %r13, 0x68(%rsp)
movq $-0x1, %r12
leaq 0xa835(%rip), %rbx      # 0xd1d0
xorl %ebp, %ebp
xorl %r15d, %r15d
xorl %r8d, %r8d
movq 0x10(%rsp), %rcx
movq (%rbx), %rax
movq (%rax), %r13
movq %r13, %r14
subq 0x70(%rsp,%rbp,8), %r14
testb $0x1, 0x1c(%rsp)
je 0x29df
leaq 0x750(%rip), %rdi       # 0x3114
movl %ebp, %esi
movq %r14, %rdx
xorl %eax, %eax
movq %r8, 0x8(%rsp)
callq 0x20f0
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %rcx
cmpq %r12, %r14
cmovbq %r14, %r12
cmpq %r15, %r14
cmovaq %r14, %r15
addq %r14, %r8
movq %r13, 0x70(%rsp,%rbp,8)
incq %rbp
addq $0x8, %rbx
cmpq %rbp, %rcx
jne 0x29a8
movl 0x4(%rsp), %r14d
movq 0x68(%rsp), %r13
movq 0x60(%rsp), %rbx
jmp 0x2a21
movq $-0x1, %r12
xorl %ebp, %ebp
xorl %r8d, %r8d
xorl %r15d, %r15d
leaq 0x6f9(%rip), %rdi       # 0x3121
movq %r12, %rsi
movq %r15, %rdx
movq %r8, %r15
movq %r8, %rcx
xorl %eax, %eax
callq 0x20f0
cmpl $0x5, %ebx
jne 0x2a4c
leaq 0x753(%rip), %rdi       # 0x319a
callq 0x20a0
cmpl $0x0, 0x20(%rsp)
sete %al
cmpl $0x6, %ebx
movl $0x0, %ecx
cmovbq %rcx, %r15
addq %r15, %r13
cmpl 0x2c(%rsp), %ebx
leal 0x1(%rbx), %ecx
setl %dl
orb %al, %dl
movl %ecx, %ebx
jne 0x2970
movl %ebp, (%rsp)
movslq 0x20(%rsp), %rcx
xorl %ebx, %ebx
movq %r13, %rax
xorl %edx, %edx
divq %rcx
leaq 0x6af(%rip), %rdi       # 0x313f
movq %rax, %rsi
xorl %eax, %eax
callq 0x20f0
callq 0x2b92
testl %r14d, %r14d
jle 0x2ad1
movq 0x50(%rsp), %r14
addq $0x28, %r14
xorl %ebx, %ebx
movq 0x10(%rsp), %r15
movq (%r14), %rdi
callq 0x2060
movq -0x8(%r14), %rdi
callq 0x2280
incq %rbx
addq $0x30, %r14
cmpq %rbx, %r15
jne 0x2ab4
movl %ebx, (%rsp)
movq 0x50(%rsp), %rdi
callq 0x2050
callq 0x2d23
xorl %edi, %edi
callq 0x22a0
leaq 0x610(%rip), %rdi       # 0x3101
jmp 0x261d
movl %r13d, (%rsp)
leaq 0x5a5(%rip), %rdi       # 0x30a6
jmp 0x261d
movl %r13d, (%rsp)
leaq 0x5a8(%rip), %rdi       # 0x30b9
jmp 0x261d
movl %r13d, (%rsp)
leaq 0x5a9(%rip), %rdi       # 0x30ca
jmp 0x261d
movl %r13d, (%rsp)
leaq 0x5ac(%rip), %rdi       # 0x30dd
jmp 0x261d
movl %r13d, (%rsp)
leaq 0x5ae(%rip), %rdi       # 0x30ef
jmp 0x261d