// File: /workspace/github_c_makefile_star2_O2/enzo1982[P]smooth/libraries/libcpuid/rdtsc.c
// Function: cpu_clock_measure
int cpu_clock_measure(int millis, int quad_check)
{
	struct cpu_mark_t begin[4], end[4], temp, temp2;
	int results[4], cycles, n, k, i, j, bi, bj, mdiff, diff, _zero = 0;
	uint64_t tl;

	if (millis < 1) return -1;
	tl = millis * (uint64_t) 1000;
	if (quad_check)
		tl /= 4;
	n = quad_check ? 4 : 1;
	cycles = 1;
	for (k = 0; k < n; k++) {
		cpu_tsc_mark(&begin[k]);
		end[k] = begin[k];
		do {
			/* Run busy loop, and fool the compiler that we USE the garbishy
			   value it calculates */
			_zero |= (1 & busy_loop(cycles));
			cpu_tsc_mark(&temp);
			mark_t_subtract(&temp, &end[k], &temp2);
			/* If busy loop is too short, increase it */
			if (temp2.sys_clock < tl / 8)
				cycles *= 2;
			end[k] = temp;
		} while (end[k].sys_clock - begin[k].sys_clock < tl);
		mark_t_subtract(&end[k], &begin[k], &temp);
		results[k] = cpu_clock_by_mark(&temp);
	}
	if (n == 1) return results[0];
	mdiff = 0x7fffffff;
	bi = bj = -1;
	for (i = 0; i < 4; i++) {
		for (j = i + 1; j < 4; j++) {
			diff = results[i] - results[j];
			if (diff < 0) diff = -diff;
			if (diff < mdiff) {
				mdiff = diff;
				bi = i;
				bj = j;
			}
		}
	}
	if (results[bi] == -1) return -1;
	return (results[bi] + results[bj] + _zero) / 2;
}

// Assembly:
testl %edi, %edi
jle 0xc4253
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movl %edi, %eax
xorl %ecx, %ecx
movl %esi, 0xc(%rsp)
testl %esi, %esi
setne %cl
leaq 0x1(%rcx,%rcx,2), %rcx
movq %rcx, 0x30(%rsp)
movl $0x3e8, %ecx            # imm = 0x3E8
movl $0xfa, %ebx
cmoveq %rcx, %rbx
imulq %rax, %rbx
movq %rbx, %r15
shrq $0x3, %r15
xorl %r14d, %r14d
pushq $0x1
popq %rbp
xorl %r12d, %r12d
cmpq 0x30(%rsp), %r14
je 0xc41e2
movq %r14, 0x38(%rsp)
shlq $0x4, %r14
leaq (%rsp,%r14), %r13
addq $0x80, %r13
movq %r13, %rdi
callq 0xc3f03
addq %rsp, %r14
addq $0x40, %r14
movaps (%r13), %xmm0
movaps %xmm0, (%r14)
movl %ebp, %edi
callq 0xc4099
andl $0x1, %eax
orl %eax, %r12d
leaq 0x20(%rsp), %rdi
callq 0xc3f03
movq 0x28(%rsp), %rax
subq 0x8(%r14), %rax
cmpq %r15, %rax
setb %cl
shll %cl, %ebp
movups 0x20(%rsp), %xmm0
movups %xmm0, (%r14)
movq 0x8(%r14), %rsi
subq 0x8(%r13), %rsi
cmpq %rbx, %rsi
jb 0xc415f
movq (%r14), %rax
subq (%r13), %rax
movq %rax, 0x20(%rsp)
movq %rsi, 0x28(%rsp)
pushq $-0x1
popq %rdi
movl %edi, %ecx
testq %rsi, %rsi
jle 0xc41d0
movl %edi, %ecx
testq %rax, %rax
js 0xc41d0
xorl %edx, %edx
divq %rsi
movq %rax, %rcx
cmpq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
cmoval %edi, %ecx
movq 0x38(%rsp), %r14
movl %ecx, 0x10(%rsp,%r14,4)
incq %r14
jmp 0xc4128
cmpl $0x0, 0xc(%rsp)
je 0xc4257
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
pushq $-0x1
popq %rax
pushq $0x1
popq %rsi
xorl %edi, %edi
movl %eax, %edx
cmpq $0x4, %rdi
je 0xc4236
movq %rsi, %r8
cmpq $0x4, %r8
je 0xc422e
movl 0x10(%rsp,%rdi,4), %r9d
subl 0x10(%rsp,%r8,4), %r9d
movl %r9d, %r10d
negl %r10d
cmovsl %r9d, %r10d
cmpl %ecx, %r10d
cmovll %edi, %eax
cmovll %r8d, %edx
cmovll %r10d, %ecx
incq %r8
jmp 0xc4201
incq %rdi
incq %rsi
jmp 0xc41f8
cltq
movl 0x10(%rsp,%rax,4), %eax
cmpl $-0x1, %eax
je 0xc425d
movslq %edx, %rcx
addl %r12d, %eax
addl 0x10(%rsp,%rcx,4), %eax
pushq $0x2
popq %rcx
cltd
idivl %ecx
jmp 0xc4260
pushq $-0x1
popq %rax
retq
movl 0x10(%rsp), %eax
jmp 0xc4260
pushq $-0x1
popq %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq