// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
testl %edi, %edi
setle %al
movl %esi, 0x24(%rsp)
testl %esi, %esi
setle %cl
orb %al, %cl
je 0xc429a
pushq $-0x2
popq %rax
jmp 0xc44a5
movl %edi, %ebp
callq 0xc3b52
movq %rax, %rbx
pushq $-0x1
popq %rax
testq %rbx, %rbx
je 0xc44a5
cmpb $0x0, 0x6f(%rbx)
je 0xc44a5
xorl %r15d, %r15d
cmpl $0x80, 0x190(%rbx)
setl %r14b
leaq 0x40675(%rip), %rax     # 0x104947
leaq 0x4068c(%rip), %rsi     # 0x104965
cmovlq %rax, %rsi
pushq $0x1
popq %r12
movl %r12d, %edi
xorl %eax, %eax
callq 0xc34c6
movl 0x54(%rbx), %eax
cmpl $0xa, %eax
je 0xc4307
movb %r14b, %r15b
incl %r15d
cmpl $0x1, %eax
jne 0xc4323
cmpl $0x15, 0xe4(%rbx)
jl 0xc4349
leaq 0x40676(%rip), %rsi     # 0x104984
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0xc34c6
movl 0x54(%rbx), %eax
pushq $0x7
popq %r12
pushq $0x5
popq %r15
testl %eax, %eax
jne 0xc434d
cmpl $0x5e, 0xe8(%rbx)
jl 0xc434d
pushq $0x5
popq %r15
leaq 0x40691(%rip), %rsi     # 0x1049cc
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0xc34c6
pushq $0x8
jmp 0xc434b
pushq $0x1
popq %r12
movl %r12d, 0x8(%rsp)
movl %r15d, 0xc(%rsp)
imull $0x7d, %ebp, %r15d
pushq $0x1
popq %rbp
movl $0x80, %r14d
leaq 0x18(%rsp), %rbx
leaq 0x10(%rsp), %r12
cmpl $0x3b9aca00, %r14d      # imm = 0x3B9ACA00
setge %al
setl %cl
shll %cl, %r14d
movl %eax, %ecx
shll %cl, %ebp
movq %rbx, %rdi
callq 0xc3ed4
testl %ebp, %ebp
movl $0x0, %r13d
cmovgl %ebp, %r13d
subl $0x1, %r13d
jb 0xc43a6
movl %r14d, %edi
callq 0xc5f44
jmp 0xc4396
movq %r12, %rdi
callq 0xc3ed4
movq 0x10(%rsp), %rax
subq 0x18(%rsp), %rax
cmpq %r15, %rax
jb 0xc436e
leaq 0x4055c(%rip), %rsi     # 0x104920
xorl %ebx, %ebx
pushq $0x2
popq %rdi
movl %r14d, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0xc34c6
shlq $0x3, %r15
movslq %r14d, %rax
shlq $0x8, %rax
orq $0xc, %rax
movslq %ebp, %rcx
imull $0xf4240, 0xc(%rsp), %edx # imm = 0xF4240
imulq %rcx, %rdx
imulq %rax, %rdx
movq %rdx, 0x30(%rsp)
movl 0x8(%rsp), %eax
movq %rax, 0x28(%rsp)
testl %ebp, %ebp
cmovlel %ebx, %ebp
xorl %ecx, %ecx
xorl %eax, %eax
cmpl 0x24(%rsp), %ecx
je 0xc44a5
movl %ecx, 0x8(%rsp)
movl %eax, 0xc(%rsp)
leaq 0x18(%rsp), %rdi
callq 0xc3ed4
xorl %r13d, %r13d
movq 0x18(%rsp), %r12
movl %ebp, %ebx
subl $0x1, %ebx
jb 0xc4443
movl %r14d, %edi
callq 0xc5f44
jmp 0xc4434
incl %r13d
leaq 0x10(%rsp), %rdi
callq 0xc3ed4
movq 0x10(%rsp), %rbx
subq %r12, %rbx
cmpq %r15, %rbx
jb 0xc4432
pushq $0x2
popq %rdi
leaq 0x404cf(%rip), %rsi     # 0x104936
movl %r13d, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0xc34c6
movl %r13d, %eax
imulq 0x30(%rsp), %rax
imulq 0x28(%rsp), %rbx
xorl %edx, %edx
divq %rbx
xorl %edx, %edx
movl $0xf4240, %ecx          # imm = 0xF4240
divq %rcx
movl 0xc(%rsp), %ecx
cmpl %eax, %ecx
cmovgl %ecx, %eax
movl 0x8(%rsp), %ecx
incl %ecx
jmp 0xc440e
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq