// File: /workspace/github_c_makefile_star2_O1/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, 0x14(%rsp)
testl %esi, %esi
setle %cl
orb %al, %cl
movl $0xfffffffe, %r12d      # imm = 0xFFFFFFFE
jne 0xcbbd5
movl %edi, %ebp
callq 0xcb0ed
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testq %rax, %rax
je 0xcbbd5
movq %rax, %r14
cmpb $0x0, 0x6f(%rax)
je 0xcbbd5
xorl %r15d, %r15d
cmpl $0x80, 0x190(%r14)
setl %bl
leaq 0x49f31(%rip), %rax     # 0x1158a7
leaq 0x49f48(%rip), %rsi     # 0x1158c5
cmovlq %rax, %rsi
movl $0x1, %r12d
movl $0x1, %edi
xorl %eax, %eax
callq 0xcaa16
movl 0x54(%r14), %eax
cmpl $0xa, %eax
je 0xcb9b6
movb %bl, %r15b
incl %r15d
cmpl $0x1, %eax
jne 0xcb9d5
cmpl $0xa, %eax
je 0xcb9b6
cmpl $0x15, 0xe4(%r14)
jl 0xcb9d5
leaq 0x49f27(%rip), %rsi     # 0x1158e4
movl $0x1, %edi
xorl %eax, %eax
callq 0xcaa16
movl $0x7, %r12d
movl $0x5, %r15d
cmpl $0x0, 0x54(%r14)
jne 0xcba05
cmpl $0x5e, 0xe8(%r14)
jl 0xcba05
leaq 0x49f3f(%rip), %rsi     # 0x11592c
movl $0x1, %edi
xorl %eax, %eax
callq 0xcaa16
movl $0x8, %r12d
movl $0x5, %r15d
movl %r12d, 0xc(%rsp)
movl %r15d, 0x10(%rsp)
imull $0x7d, %ebp, %eax
movq %rax, 0x30(%rsp)
movl $0x1, %ebx
movl $0x80, %ebp
leaq 0x18(%rsp), %r15
movl %ebx, %r14d
cmpl $0x3b9aca00, %ebp       # imm = 0x3B9ACA00
setge %al
setl %cl
shll %cl, %ebp
movl %eax, %ecx
shll %cl, %ebx
movq %r15, %r13
movq %r15, %rdi
xorl %esi, %esi
callq 0x567e0
movq 0x18(%rsp), %r12
movq 0x20(%rsp), %rax
movq %rax, 0x28(%rsp)
testl %r14d, %r14d
jle 0xcba75
cmpl $0x2, %ebx
movl $0x1, %r15d
cmovgel %ebx, %r15d
movl %ebp, %edi
callq 0xcd946
decl %r15d
jne 0xcba69
movq %r13, %r15
movq %r13, %rdi
xorl %esi, %esi
callq 0x567e0
movq 0x18(%rsp), %rax
subq %r12, %rax
movq 0x20(%rsp), %rcx
subq 0x28(%rsp), %rcx
imulq $0xf4240, %rax, %rax    # imm = 0xF4240
addq %rcx, %rax
cmpq 0x30(%rsp), %rax
jb 0xcba26
leaq 0x49dd4(%rip), %rsi     # 0x115880
xorl %r12d, %r12d
movl $0x2, %edi
movl %ebp, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0xcaa16
cmpl $0x0, 0x14(%rsp)
jle 0xcbbd5
shlq $0x3, 0x30(%rsp)
movslq %ebp, %rax
shlq $0x8, %rax
orq $0xc, %rax
movslq %ebx, %rcx
imull $0xf4240, 0x10(%rsp), %edx # imm = 0xF4240
imulq %rcx, %rdx
imulq %rax, %rdx
movq %rdx, 0x40(%rsp)
movl 0xc(%rsp), %eax
movq %rax, 0x38(%rsp)
cmpl $0x2, %ebx
movl $0x1, %eax
cmovgel %ebx, %eax
movl %eax, 0x28(%rsp)
xorl %ecx, %ecx
xorl %eax, %eax
movl %ecx, 0xc(%rsp)
movl %eax, 0x10(%rsp)
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x567e0
movq 0x18(%rsp), %rbx
movq 0x20(%rsp), %r15
xorl %r12d, %r12d
testl %r14d, %r14d
jle 0xcbb46
movl 0x28(%rsp), %r13d
movl %ebp, %edi
callq 0xcd946
decl %r13d
jne 0xcbb3a
incl %r12d
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x567e0
movq 0x18(%rsp), %rax
subq %rbx, %rax
movq 0x20(%rsp), %rcx
subq %r15, %rcx
imulq $0xf4240, %rax, %r13    # imm = 0xF4240
addq %rcx, %r13
cmpq 0x30(%rsp), %r13
jb 0xcbb30
movl $0x2, %edi
leaq 0x49d14(%rip), %rsi     # 0x115896
movl %r12d, %edx
movl %r13d, %ecx
xorl %eax, %eax
callq 0xcaa16
movl %r12d, %eax
imulq 0x40(%rsp), %rax
imulq 0x38(%rsp), %r13
xorl %edx, %edx
divq %r13
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
mulq %rcx
movq %rdx, %r12
shrq $0x12, %r12
movl 0xc(%rsp), %eax
cmpl %r12d, %eax
cmovgl %eax, %r12d
movl 0x10(%rsp), %eax
incl %eax
movl %r12d, %ecx
cmpl 0x14(%rsp), %eax
jne 0xcbb0f
movl %r12d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq