// File: /workspace/github_c_makefile_star2_O3/enzo1982[P]smooth/libraries/libcpuid/rdtsc.c
// Function: cpu_clock_by_ic
int cpu_clock_by_ic(int millis, int runs)
{
	int max_value = 0, cur_value, i, ri, cycles_inner, cycles_outer, c;
	struct cpu_id_t* id;
	uint64_t t0, t1, tl, hz;
	int multiplier_numerator = 1, multiplier_denom = 1;
	if (millis <= 0 || runs <= 0) return -2;
	id = get_cached_cpuid();
	// if there aren't SSE instructions - we can't run the test at all
	if (!id || !id->flags[CPU_FEATURE_SSE]) return -1;
	//
	adjust_march_ic_multiplier(id, &multiplier_numerator, &multiplier_denom);
	//
	tl = millis * 125; // (*1000 / 8)
	cycles_inner = 128;
	cycles_outer = 1;
	do {
		if (cycles_inner < 1000000000) cycles_inner *= 2;
		else cycles_outer *= 2;
		sys_precise_clock(&t0);
		for (i = 0; i < cycles_outer; i++)
			busy_sse_loop(cycles_inner);
		sys_precise_clock(&t1);
	} while (t1 - t0 < tl);
	debugf(2, "inner: %d, outer: %d\n", cycles_inner, cycles_outer);
	for (ri = 0; ri < runs; ri++) {
		sys_precise_clock(&t0);
		c = 0;
		do {
			c++;
			for (i = 0; i < cycles_outer; i++)
				busy_sse_loop(cycles_inner);
			sys_precise_clock(&t1);
		} while (t1 - t0 < tl * (uint64_t) 8);
		// cpu_Hz = cycles_inner * cycles_outer * 256 / (t1 - t0) * 1000000
		debugf(2, "c = %d, td = %d\n", c, (int) (t1 - t0));
		hz = ((uint64_t) cycles_inner * (uint64_t) 256 + 12) *
		     (uint64_t) cycles_outer * (uint64_t) multiplier_numerator * (uint64_t) c * (uint64_t) 1000000
		     / ((t1 - t0) * (uint64_t) multiplier_denom);
		cur_value = (int) (hz / 1000000);
		if (cur_value > max_value) max_value = cur_value;
	}
	return max_value;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
testl %edi, %edi
setle %al
movl %esi, 0x34(%rsp)
testl %esi, %esi
setle %cl
orb %al, %cl
movl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
jne 0xcadd3
movl %edi, %ebp
callq 0xca2f7
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0xcadd3
movq %rax, %r14
cmpb $0x0, 0x6f(%rax)
je 0xcadd3
xorl %r15d, %r15d
cmpl $0x80, 0x190(%r14)
setl %bl
leaq 0x49f3b(%rip), %rax     # 0x114ab7
leaq 0x49f52(%rip), %rsi     # 0x114ad5
cmovlq %rax, %rsi
movl $0x1, %r12d
movl $0x1, %edi
xorl %eax, %eax
callq 0xc9c3a
movl 0x54(%r14), %eax
cmpl $0xa, %eax
je 0xcabbd
movb %bl, %r15b
incl %r15d
cmpl $0x1, %eax
jne 0xcabe0
movl $0x1, %r12d
cmpl $0x15, 0xe4(%r14)
jl 0xcac0d
leaq 0x49f30(%rip), %rsi     # 0x114af4
movl $0x1, %edi
xorl %eax, %eax
callq 0xc9c3a
movl 0x54(%r14), %eax
movl $0x7, %r12d
movl $0x5, %r15d
testl %eax, %eax
jne 0xcac0d
cmpl $0x5e, 0xe8(%r14)
jl 0xcac0d
leaq 0x49f47(%rip), %rsi     # 0x114b3c
movl $0x1, %edi
xorl %eax, %eax
callq 0xc9c3a
movl $0x8, %r12d
movl $0x5, %r15d
movl %r12d, 0x8(%rsp)
movl %r15d, 0xc(%rsp)
imull $0x7d, %ebp, %eax
movq %rax, 0x28(%rsp)
movl $0x1, %r12d
movl $0x80, %ebp
leaq 0x10(%rsp), %rdi
movl %r12d, %r13d
cmpl $0x3b9aca00, %ebp       # imm = 0x3B9ACA00
setge %al
setl %cl
shll %cl, %ebp
movl %eax, %ecx
shll %cl, %r12d
movq %rdi, %rbx
xorl %esi, %esi
callq 0x567e0
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %rax
movq %rax, 0x20(%rsp)
movl %r13d, %r15d
testl %r13d, %r13d
jle 0xcac80
cmpl $0x2, %r12d
movl $0x1, %r13d
cmovgel %r12d, %r13d
movl %ebp, %edi
callq 0xccb36
decl %r13d
jne 0xcac74
movq %rbx, %rdi
xorl %esi, %esi
callq 0x567e0
movq %rbx, %rdi
movq 0x10(%rsp), %rax
subq %r14, %rax
movq 0x18(%rsp), %rcx
subq 0x20(%rsp), %rcx
imulq $0xf4240, %rax, %rax    # imm = 0xF4240
addq %rcx, %rax
cmpq 0x28(%rsp), %rax
jb 0xcac2f
leaq 0x49dd5(%rip), %rsi     # 0x114a90
movl $0x2, %edi
movl %ebp, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0xc9c3a
shlq $0x3, 0x28(%rsp)
movslq %ebp, %rax
shlq $0x8, %rax
orq $0xc, %rax
movslq %r12d, %rcx
imull $0xf4240, 0xc(%rsp), %edx # imm = 0xF4240
imulq %rcx, %rdx
imulq %rax, %rdx
movq %rdx, 0x40(%rsp)
movl 0x8(%rsp), %eax
movq %rax, 0x38(%rsp)
cmpl $0x2, %r12d
movl $0x1, %eax
cmovgel %r12d, %eax
movl %eax, 0x20(%rsp)
xorl %ecx, %ecx
xorl %eax, %eax
movl %ecx, 0x8(%rsp)
movl %eax, 0xc(%rsp)
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x567e0
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r14
xorl %r12d, %r12d
testl %r15d, %r15d
jle 0xcad4a
movl 0x20(%rsp), %r13d
movl %ebp, %edi
callq 0xccb36
decl %r13d
jne 0xcad3e
incl %r12d
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x567e0
movq 0x10(%rsp), %rax
subq %rbx, %rax
movq 0x18(%rsp), %rcx
subq %r14, %rcx
imulq $0xf4240, %rax, %r13    # imm = 0xF4240
addq %rcx, %r13
cmpq 0x28(%rsp), %r13
jb 0xcad34
movl $0x2, %edi
leaq 0x49d20(%rip), %rsi     # 0x114aa6
movl %r12d, %edx
movl %r13d, %ecx
xorl %eax, %eax
callq 0xc9c3a
movl %r12d, %eax
imulq 0x40(%rsp), %rax
imulq 0x38(%rsp), %r13
xorl %edx, %edx
divq %r13
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
mulq %rcx
shrq $0x12, %rdx
movl 0x8(%rsp), %eax
cmpl %edx, %eax
cmovgl %eax, %edx
movl 0xc(%rsp), %eax
incl %eax
movl %edx, %ecx
cmpl 0x34(%rsp), %eax
jne 0xcad13
movl %edx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq