// File: /workspace/github_c_makefile_star2_O0/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:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl $0x0, -0xe8(%rbp)
cmpl $0x1, -0x8(%rbp)
jge 0x136d0d
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x137044
movslq -0x8(%rbp), %rax
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
movq %rax, -0xf0(%rbp)
cmpl $0x0, -0xc(%rbp)
je 0x136d37
movq -0xf0(%rbp), %rax
shrq $0x2, %rax
movq %rax, -0xf0(%rbp)
movl -0xc(%rbp), %edx
movl $0x1, %eax
movl $0x4, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0xc8(%rbp)
movl $0x1, -0xc4(%rbp)
movl $0x0, -0xcc(%rbp)
movl -0xcc(%rbp), %eax
cmpl -0xc8(%rbp), %eax
jge 0x136efe
movslq -0xcc(%rbp), %rax
leaq -0x50(%rbp), %rdi
shlq $0x4, %rax
addq %rax, %rdi
callq 0x136980
movslq -0xcc(%rbp), %rcx
leaq -0x90(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movslq -0xcc(%rbp), %rdx
leaq -0x50(%rbp), %rcx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movl -0xc4(%rbp), %edi
callq 0x136c60
andl $0x1, %eax
orl -0xe8(%rbp), %eax
movl %eax, -0xe8(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x136980
movslq -0xcc(%rbp), %rax
leaq -0x90(%rbp), %rsi
shlq $0x4, %rax
addq %rax, %rsi
leaq -0xa0(%rbp), %rdi
leaq -0xb0(%rbp), %rdx
callq 0x1369e0
movq -0xa8(%rbp), %rax
movq -0xf0(%rbp), %rcx
shrq $0x3, %rcx
cmpq %rcx, %rax
jae 0x136e35
movl -0xc4(%rbp), %eax
shll %eax
movl %eax, -0xc4(%rbp)
movslq -0xcc(%rbp), %rcx
leaq -0x90(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movq -0xa0(%rbp), %rcx
movq %rcx, (%rax)
movq -0x98(%rbp), %rcx
movq %rcx, 0x8(%rax)
movslq -0xcc(%rbp), %rcx
leaq -0x90(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movslq -0xcc(%rbp), %rdx
leaq -0x50(%rbp), %rcx
shlq $0x4, %rdx
addq %rdx, %rcx
subq 0x8(%rcx), %rax
cmpq -0xf0(%rbp), %rax
jb 0x136dc2
movslq -0xcc(%rbp), %rax
leaq -0x90(%rbp), %rdi
shlq $0x4, %rax
addq %rax, %rdi
movslq -0xcc(%rbp), %rax
leaq -0x50(%rbp), %rsi
shlq $0x4, %rax
addq %rax, %rsi
leaq -0xa0(%rbp), %rdx
callq 0x1369e0
leaq -0xa0(%rbp), %rdi
callq 0x136a20
movl %eax, %ecx
movslq -0xcc(%rbp), %rax
movl %ecx, -0xc0(%rbp,%rax,4)
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
jmp 0x136d64
cmpl $0x1, -0xc8(%rbp)
jne 0x136f15
movl -0xc0(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x137044
movl $0x7fffffff, -0xe0(%rbp) # imm = 0x7FFFFFFF
movl $0xffffffff, -0xdc(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0xd8(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0xd0(%rbp)
cmpl $0x4, -0xd0(%rbp)
jge 0x136ffd
movl -0xd0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd4(%rbp)
cmpl $0x4, -0xd4(%rbp)
jge 0x136fe7
movslq -0xd0(%rbp), %rax
movl -0xc0(%rbp,%rax,4), %eax
movslq -0xd4(%rbp), %rcx
subl -0xc0(%rbp,%rcx,4), %eax
movl %eax, -0xe4(%rbp)
cmpl $0x0, -0xe4(%rbp)
jge 0x136f9f
xorl %eax, %eax
subl -0xe4(%rbp), %eax
movl %eax, -0xe4(%rbp)
movl -0xe4(%rbp), %eax
cmpl -0xe0(%rbp), %eax
jge 0x136fd1
movl -0xe4(%rbp), %eax
movl %eax, -0xe0(%rbp)
movl -0xd0(%rbp), %eax
movl %eax, -0xd8(%rbp)
movl -0xd4(%rbp), %eax
movl %eax, -0xdc(%rbp)
jmp 0x136fd3
movl -0xd4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd4(%rbp)
jmp 0x136f59
jmp 0x136fe9
movl -0xd0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd0(%rbp)
jmp 0x136f3d
movslq -0xd8(%rbp), %rax
cmpl $-0x1, -0xc0(%rbp,%rax,4)
jne 0x137017
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x137044
movslq -0xd8(%rbp), %rax
movl -0xc0(%rbp,%rax,4), %eax
movslq -0xdc(%rbp), %rcx
addl -0xc0(%rbp,%rcx,4), %eax
addl -0xe8(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq