// File: /workspace/github_c_makefile_star2_O1/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 0xcb8ee
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movl %edi, %eax
testl %esi, %esi
movl $0xfa, %ecx
movl $0x3e8, %r13d           # imm = 0x3E8
cmovneq %rcx, %r13
imulq %rax, %r13
movq %r13, 0x48(%rsp)
shrq $0x3, %r13
movl $0x1, %r15d
xorl %eax, %eax
leaq 0xa3ba8(%rip), %r14     # 0x16f2c0
xorl %ebp, %ebp
movl %esi, 0xc(%rsp)
movq %rax, 0x20(%rsp)
movq %rax, %rbx
shlq $0x4, %rbx
leaq (%rsp,%rbx), %r12
addq $0x90, %r12
movq %r12, %rdi
callq 0xcd91f
leaq 0x38(%rsp), %rdi
xorl %esi, %esi
callq 0x567e0
imulq $0xf4240, 0x38(%rsp), %rax # imm = 0xF4240
addq 0x40(%rsp), %rax
movq %rax, 0x8(%r12)
addq %rsp, %rbx
addq $0x50, %rbx
movq (%r12), %rax
movq %rax, (%rbx)
movq 0x8(%r12), %rax
movq %rax, 0x8(%rbx)
xorl %eax, %eax
testl %r15d, %r15d
jle 0xcb7a5
xorl %ecx, %ecx
xorl %edx, %edx
xorl %esi, %esi
addl (%rsi,%r14), %eax
addq $0x4, %rsi
cmpq $0xa8, %rsi
jne 0xcb780
incl %edx
cmpl $0x10000, %edx          # imm = 0x10000
jne 0xcb77e
incl %ecx
cmpl %r15d, %ecx
jne 0xcb77c
andl $0x1, %eax
orl %eax, %ebp
leaq 0x28(%rsp), %rdi
callq 0xcd91f
leaq 0x38(%rsp), %rdi
xorl %esi, %esi
callq 0x567e0
imulq $0xf4240, 0x38(%rsp), %rax # imm = 0xF4240
addq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
subq 0x8(%rbx), %rax
cmpq %r13, %rax
setb %cl
shll %cl, %r15d
movq 0x28(%rsp), %rax
movq %rax, (%rbx)
movq 0x30(%rsp), %rax
movq %rax, 0x8(%rbx)
movq 0x8(%rbx), %rdi
subq 0x8(%r12), %rdi
cmpq 0x48(%rsp), %rdi
jb 0xcb773
movq (%rbx), %rcx
subq (%r12), %rcx
movq %rcx, 0x28(%rsp)
movq %rdi, 0x30(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testq %rdi, %rdi
jle 0xcb843
testq %rcx, %rcx
movl 0xc(%rsp), %esi
movq 0x20(%rsp), %r8
js 0xcb84c
movq %rcx, %rax
xorl %edx, %edx
divq %rdi
cmpq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmoval %ecx, %eax
jmp 0xcb84c
movl 0xc(%rsp), %esi
movq 0x20(%rsp), %r8
movl %eax, 0x10(%rsp,%r8,4)
testl %esi, %esi
je 0xcb863
leaq 0x1(%r8), %rax
cmpq $0x3, %r8
jb 0xcb71e
testl %esi, %esi
je 0xcb8f4
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl $0x1, %ecx
xorl %edx, %edx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
cmpq $0x2, %rdx
ja 0xcb8bd
movl 0x10(%rsp,%rdx,4), %r8d
movq %rcx, %r10
movl %r8d, %r11d
subl 0x10(%rsp,%r10,4), %r11d
movl %r11d, %r9d
negl %r9d
cmovsl %r11d, %r9d
cmpl %edi, %r9d
cmovll %edx, %esi
cmovll %r10d, %eax
cmovgel %edi, %r9d
incq %r10
movl %r9d, %edi
cmpq $0x4, %r10
jne 0xcb88f
jmp 0xcb8c0
movl %edi, %r9d
incq %rdx
incq %rcx
movl %r9d, %edi
cmpq $0x4, %rdx
jne 0xcb881
movslq %esi, %rcx
movl 0x10(%rsp,%rcx,4), %ecx
cmpl $-0x1, %ecx
je 0xcb8fa
cltq
addl %ebp, %ecx
addl 0x10(%rsp,%rax,4), %ecx
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
jmp 0xcb8ff
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
retq
movl 0x10(%rsp), %eax
jmp 0xcb8ff
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq