// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl $0x0, -0x10(%rbp)
movl $0x1, -0x54(%rbp)
movl $0x1, -0x58(%rbp)
cmpl $0x0, -0x8(%rbp)
jle 0x13707f
cmpl $0x0, -0xc(%rbp)
jg 0x13708b
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x137255
callq 0x136110
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x1370a5
movq -0x30(%rbp), %rax
cmpb $0x0, 0x6f(%rax)
jne 0x1370b1
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x137255
movq -0x30(%rbp), %rdi
leaq -0x54(%rbp), %rsi
leaq -0x58(%rbp), %rdx
callq 0x137260
imull $0x7d, -0x8(%rbp), %eax
cltq
movq %rax, -0x48(%rbp)
movl $0x80, -0x20(%rbp)
movl $0x1, -0x24(%rbp)
cmpl $0x3b9aca00, -0x20(%rbp) # imm = 0x3B9ACA00
jge 0x1370ed
movl -0x20(%rbp), %eax
shll %eax
movl %eax, -0x20(%rbp)
jmp 0x1370f5
movl -0x24(%rbp), %eax
shll %eax
movl %eax, -0x24(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x136940
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x137120
movl -0x20(%rbp), %edi
callq 0x13a3b0
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x137105
leaq -0x40(%rbp), %rdi
callq 0x136940
movq -0x40(%rbp), %rax
subq -0x38(%rbp), %rax
cmpq -0x48(%rbp), %rax
jb 0x1370da
movl -0x20(%rbp), %edx
movl -0x24(%rbp), %ecx
movl $0x2, %edi
leaq 0x711eb(%rip), %rsi     # 0x1a8334
movb $0x0, %al
callq 0x135780
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x13724f
leaq -0x38(%rbp), %rdi
callq 0x136940
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x13719e
movl -0x20(%rbp), %edi
callq 0x13a3b0
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x137183
leaq -0x40(%rbp), %rdi
callq 0x136940
movq -0x40(%rbp), %rax
subq -0x38(%rbp), %rax
movq -0x48(%rbp), %rcx
shlq $0x3, %rcx
cmpq %rcx, %rax
jb 0x137173
movl -0x28(%rbp), %edx
movq -0x40(%rbp), %rax
subq -0x38(%rbp), %rax
movl %eax, %ecx
movl $0x2, %edi
leaq 0x71175(%rip), %rsi     # 0x1a834a
movb $0x0, %al
callq 0x135780
movslq -0x20(%rbp), %rax
shlq $0x8, %rax
addq $0xc, %rax
movslq -0x24(%rbp), %rcx
imulq %rcx, %rax
movslq -0x54(%rbp), %rcx
imulq %rcx, %rax
movslq -0x28(%rbp), %rcx
imulq %rcx, %rax
imulq $0xf4240, %rax, %rax    # imm = 0xF4240
movq -0x40(%rbp), %rcx
subq -0x38(%rbp), %rcx
movslq -0x58(%rbp), %rdx
imulq %rdx, %rcx
xorl %edx, %edx
divq %rcx
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movl $0xf4240, %ecx          # imm = 0xF4240
xorl %edx, %edx
divq %rcx
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x10(%rbp), %eax
jle 0x13723f
movl -0x14(%rbp), %eax
movl %eax, -0x10(%rbp)
jmp 0x137241
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x137157
movl -0x10(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nop