#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <sched.h>

long mygetpid() {
	return syscall(SYS_getpid);
}

inline volatile long long RDTSC() {
	register long long TSC asm("eax");
	asm volatile (".byte 15, 49" : : : "eax", "edx");
	return TSC;
}

int main(int argc, char *argv[]) {
	cpu_set_t cpu;
	CPU_ZERO(&cpu);
	CPU_SET(0, &cpu);

	if (sched_setaffinity(0, sizeof(cpu), &cpu) < 0) {
		perror("sched_setaffinity");
		exit(1);
	}

	int i, qtd = 0;
	if (argc == 2) {
		qtd = atoi(argv[1]);
	}

	long long start = RDTSC();

	for (i = 0; i < qtd; i++) {
		mygetpid();
	}

	long long end = RDTSC();
	printf("%lld\n",  (end - start));

	return 0;
}
