// File: /workspace/github_c_makefile_star2_O3/derhass[P]glx_hook/glx_hook.c
// Function: latency_init
static void
latency_init(GH_latency *lat, int latency, int manual_wait, unsigned gl_wait_timeout_usecs, unsigned int gl_wait_interval_usecs, unsigned int self_wait_interval_usecs)
{
	lat->latency=latency;
	lat->sync_object=NULL;
	lat->cur_pos=0;
	lat->flags=0;
	lat->gl_wait_timeout=(GLuint64)gl_wait_timeout_usecs * (GLuint64)1000;
	lat->gl_wait_interval=(GLuint64)gl_wait_interval_usecs * (GLuint64)1000;
	lat->self_wait_interval=(useconds_t)self_wait_interval_usecs;

	if (manual_wait > 0) {
		lat->flags |= GH_LATENCY_FLAG_MANUAL_WAIT;
	} else if (manual_wait < 0) {
		if ((gl_wait_interval_usecs > 0) || (self_wait_interval_usecs > 0))  {
			lat->flags |= GH_LATENCY_FLAG_MANUAL_WAIT;
		}
	}

	if (latency != GH_LATENCY_NOP) {
		GH_verbose(GH_MSG_INFO, "setting up latency limiter mode %d\n", latency);
	}

	if (latency > 0) {
		if (latency_gl_init()) {
			lat->latency=GH_LATENCY_FINISH_BEFORE;
			GH_verbose(GH_MSG_WARNING, "GPU sync not available, using latency mode %d\n",
					GH_LATENCY_FINISH_BEFORE);
		}
	}

	if (lat->latency > 0) {
		unsigned int cnt=(unsigned)lat->latency;
		lat->sync_object=malloc(sizeof(*lat->sync_object) * cnt);
		if (!lat->sync_object) {
			lat->latency=GH_LATENCY_FINISH_BEFORE;
			GH_verbose(GH_MSG_WARNING, "out of memory for sync objects, using latency mode %d\n");
		} else {
			unsigned int i;
			for (i=0; i<cnt; i++) {
				lat->sync_object[i]=NULL;
			}
			GH_verbose(GH_MSG_DEBUG, "enabling latency limiter: %d\n",lat->latency);
			if (lat->flags & GH_LATENCY_FLAG_MANUAL_WAIT) {
				GH_verbose(GH_MSG_INFO, "latency limiter with manual waits GL: %u usecs + self: %u usecs\n",
						gl_wait_interval_usecs, self_wait_interval_usecs);
			} else {
				GH_verbose(GH_MSG_INFO, "latency limiter with timeout: %u usecs\n",
						gl_wait_timeout_usecs);
			}
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %r9d, %ebx
movl %r8d, %ebp
movl %ecx, %r14d
movl %esi, %r12d
movq %rdi, %r15
movl %esi, (%rdi)
movl %ecx, %eax
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rdi)
movq %rax, 0x18(%rdi)
movl %r8d, %eax
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
movq %rax, 0x20(%rdi)
movl %r9d, 0x28(%rdi)
testl %edx, %edx
jg 0x63d0
sete %al
movl %ebx, %ecx
orl %ebp, %ecx
sete %cl
orb %al, %cl
jne 0x63d8
movl $0x1, 0x14(%r15)
cmpl $-0x2, %r12d
je 0x65b3
leaq 0x1afa(%rip), %rsi      # 0x7ee3
movl $0x3, %edi
movl %r12d, %edx
xorl %eax, %eax
callq 0x2cac
testl %r12d, %r12d
jle 0x65b3
leaq 0x4d68(%rip), %rdi      # 0xb170
callq 0x21a0
cmpq $0x0, 0x4ee3(%rip)      # 0xb2f8
jne 0x6461
leaq 0x4d52(%rip), %rdi      # 0xb170
callq 0x2120
leaq 0x1bca(%rip), %r13      # 0x7ff4
movq %r13, %rdi
callq 0x601b
movq %rax, %r12
leaq 0x115a(%rip), %rsi      # 0x7596
movl $0x4, %edi
movq %r13, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2cac
leaq 0x4d1b(%rip), %rdi      # 0xb170
callq 0x21a0
movq %r12, 0x4e97(%rip)      # 0xb2f8
leaq 0x4d08(%rip), %rdi      # 0xb170
callq 0x2120
cmpq $0x0, 0x4e83(%rip)      # 0xb2f8
je 0x6574
leaq 0x4cee(%rip), %rdi      # 0xb170
callq 0x21a0
cmpq $0x0, 0x4e71(%rip)      # 0xb300
jne 0x64db
leaq 0x4cd8(%rip), %rdi      # 0xb170
callq 0x2120
leaq 0x1b5c(%rip), %r13      # 0x8000
movq %r13, %rdi
callq 0x601b
movq %rax, %r12
leaq 0x10e0(%rip), %rsi      # 0x7596
movl $0x4, %edi
movq %r13, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2cac
leaq 0x4ca1(%rip), %rdi      # 0xb170
callq 0x21a0
movq %r12, 0x4e25(%rip)      # 0xb300
leaq 0x4c8e(%rip), %rdi      # 0xb170
callq 0x2120
cmpq $0x0, 0x4e11(%rip)      # 0xb300
je 0x657d
leaq 0x4c74(%rip), %rdi      # 0xb170
callq 0x21a0
cmpq $0x0, 0x4dff(%rip)      # 0xb308
jne 0x6555
leaq 0x4c5e(%rip), %rdi      # 0xb170
callq 0x2120
leaq 0x1aef(%rip), %r13      # 0x800d
movq %r13, %rdi
callq 0x601b
movq %rax, %r12
leaq 0x1066(%rip), %rsi      # 0x7596
movl $0x4, %edi
movq %r13, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2cac
leaq 0x4c27(%rip), %rdi      # 0xb170
callq 0x21a0
movq %r12, 0x4db3(%rip)      # 0xb308
leaq 0x4c14(%rip), %rdi      # 0xb170
callq 0x2120
cmpq $0x0, 0x4d9f(%rip)      # 0xb308
jne 0x65b3
leaq 0x1a9b(%rip), %rdx      # 0x800d
jmp 0x6584
leaq 0x1a79(%rip), %rdx      # 0x7ff4
jmp 0x6584
leaq 0x1a7c(%rip), %rdx      # 0x8000
leaq 0x1900(%rip), %rsi      # 0x7e8b
movl $0x2, %edi
xorl %eax, %eax
callq 0x2cac
movl $0x0, (%r15)
leaq 0x1962(%rip), %rsi      # 0x7f07
movl $0x2, %edi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2cac
movslq (%r15), %r12
testq %r12, %r12
jle 0x662c
leaq 0x8(%r15), %r13
leaq (,%r12,8), %rdi
callq 0x2130
movq %rax, (%r13)
testq %rax, %rax
je 0x663b
xorl %eax, %eax
movq (%r13), %rcx
movq $0x0, (%rcx,%rax,8)
incq %rax
cmpq %rax, %r12
jne 0x65d7
leaq 0x197b(%rip), %rsi      # 0x7f6d
movl $0x4, %edi
movl %r12d, %edx
xorl %eax, %eax
callq 0x2cac
testb $0x1, 0x14(%r15)
jne 0x6663
leaq 0x19bd(%rip), %rsi      # 0x7fcc
movl $0x3, %edi
movl %r14d, %edx
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2cac
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x0, (%r15)
leaq 0x18ed(%rip), %rsi      # 0x7f36
movl $0x2, %edi
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2cac
leaq 0x1921(%rip), %rsi      # 0x7f8b
movl $0x3, %edi
movl %ebp, %edx
movl %ebx, %ecx
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2cac